Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop redacting dates in GA4 tracking on GOVUK search pages #4223

Merged
merged 1 commit into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* Update metatags for GA4 ([PR #4222](https://github.com/alphagov/govuk_publishing_components/pull/4222))
* Set aria-label text in govuk_logo.html to "GOV.UK" ([PR #4217](https://github.com/alphagov/govuk_publishing_components/pull/4217))
* Fix cookie expiration date potentially relying on user's timezone ([PR #4219](https://github.com/alphagov/govuk_publishing_components/pull/4219))
* Stop redacting dates in GA4 tracking on GOVUK search pages ([PR #4223](https://github.com/alphagov/govuk_publishing_components/pull/4223))
* Remove 100 character limit on search results ([PR #4230](https://github.com/alphagov/govuk_publishing_components/pull/4230))

## 43.1.1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ window.GOVUK.analyticsGa4 = window.GOVUK.analyticsGa4 || {};
var PIIRemover = new window.GOVUK.analyticsGa4.PIIRemover()
searchTerm = searchTerm.replace(/\++|(%2B)+/gm, ' ') // Turn + characters or unicode + characters (%2B) into a space.
searchTerm = window.GOVUK.analyticsGa4.core.trackFunctions.removeLinesAndExtraSpaces(searchTerm)
searchTerm = PIIRemover.stripPIIWithOverride(searchTerm, true, true)
searchTerm = PIIRemover.stripPIIWithOverride(searchTerm, false, true)
searchTerm = searchTerm.toLowerCase()
return searchTerm
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ window.GOVUK.analyticsGa4.analyticsModules = window.GOVUK.analyticsGa4.analytics

init: function (referrer) {
if (window.dataLayer) {
this.stripDates = !this.getSearchTerm()
var data = {
event: 'page_view',
page_view: {
Expand Down Expand Up @@ -78,7 +79,8 @@ window.GOVUK.analyticsGa4.analyticsModules = window.GOVUK.analyticsGa4.analytics
},

getLocation: function () {
return this.PIIRemover.stripPIIWithOverride(this.stripGaParam(document.location.href), true, true)
// We don't want to remove dates on search pages.
return this.PIIRemover.stripPIIWithOverride(this.stripGaParam(document.location.href), this.stripDates, true)
},

getSearchTerm: function () {
Expand Down Expand Up @@ -110,8 +112,9 @@ window.GOVUK.analyticsGa4.analyticsModules = window.GOVUK.analyticsGa4.analytics
getQueryString: function () {
var queryString = window.GOVUK.analyticsGa4.core.trackFunctions.getSearch()
if (queryString) {
// We don't want to remove dates on search pages.
queryString = this.stripGaParam(queryString)
queryString = this.PIIRemover.stripPIIWithOverride(queryString, true, true)
queryString = this.PIIRemover.stripPIIWithOverride(queryString, this.stripDates, true)
queryString = queryString.substring(1) // removes the '?' character from the start.
return queryString
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ describe('GA4 core', function () {

it('standardises search terms for consistency across trackers', function () {
var searchTerm = 'NO UPPERCASE, NO %2B plus + signs, NO PII [email protected] SW1A 2AA 1st Jan 1990 and NO extra spaces \n \r '
var expected = 'no uppercase, no plus signs, no pii [email] [postcode] [date] and no extra spaces'
var expected = 'no uppercase, no plus signs, no pii [email] [postcode] 1st jan 1990 and no extra spaces'

searchTerm = GOVUK.analyticsGa4.core.trackFunctions.standardiseSearchTerm(searchTerm)
expect(searchTerm).toEqual(expected)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,33 @@ describe('Google Tag Manager page view tracking', function () {
expect(window.dataLayer[0]).toEqual(expected)
})

it('doesn\'t remove date pii from the location when on a search page', function () {
spyOn(GOVUK.analyticsGa4.core.trackFunctions, 'getSearch').and.returnValue('?keywords=2020-01-01')

// We can't spy on location, so instead we use an anchor link to change the URL temporarily.
// Reset the URL so we can build the expected link string.
var linkForURLMock = document.createElement('a')
linkForURLMock.href = '#'
linkForURLMock.click()
var location = document.location.href

expected.page_view.location = location + '[email]/[postcode]?keywords=2020-01-01'
expected.page_view.search_term = '2020-01-01'
expected.page_view.query_string = 'keywords=2020-01-01'

// Add personally identifiable information to the current page location
linkForURLMock.href = '#[email protected]/SW12AA?keywords=2020-01-01'
linkForURLMock.click()

GOVUK.analyticsGa4.analyticsModules.PageViewTracker.init()

// Reset the page location for other tests
linkForURLMock.href = '#'
linkForURLMock.click()

expect(window.dataLayer[0]).toEqual(expected)
})

it('can redact ga parameters as expected', function () {
var url = 'https://www.gov.uk/welfare?'
var param1 = '_ga=2.237932419.220854294.1692605006-1618308030.1687790776'
Expand Down Expand Up @@ -558,20 +585,28 @@ describe('Google Tag Manager page view tracking', function () {
expect(window.dataLayer[0]).toEqual(expected)
})

it('correctly sets the query_string parameter without PII date redaction when a search term exists', function () {
spyOn(GOVUK.analyticsGa4.core.trackFunctions, 'getSearch').and.returnValue('?keywords=hello&query2=world&[email protected]&postcode=SW12AA&birthday=1990-01-01&_ga=19900101.567&_gl=19900101.567')
expected.page_view.search_term = 'hello'
expected.page_view.query_string = 'keywords=hello&query2=world&email=[email]&postcode=[postcode]&birthday=1990-01-01&_ga=[id]&_gl=[id]'
GOVUK.analyticsGa4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
})

describe('search_term parameter', function () {
describe('using the query parameter "keywords"', function () {
it('correctly sets the parameter using ?keywords= with PII values redacted', function () {
spyOn(GOVUK.analyticsGa4.core.trackFunctions, 'getSearch').and.returnValue('[email protected]+SW12AA+1990-01-01&another=one')
expected.page_view.query_string = 'keywords=hello+world+[email]+[postcode]+[date]&another=one'
expected.page_view.search_term = 'hello world [email] [postcode] [date]'
expected.page_view.query_string = 'keywords=hello+world+[email]+[postcode]+1990-01-01&another=one'
expected.page_view.search_term = 'hello world [email] [postcode] 1990-01-01'
GOVUK.analyticsGa4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
})

it('correctly sets the parameter using &keywords= with PII values redacted', function () {
it('correctly sets the parameter using &keywords= with PII values redacted (except for dates)', function () {
spyOn(GOVUK.analyticsGa4.core.trackFunctions, 'getSearch').and.returnValue('?test=true&[email protected]+SW12AA+1990-01-01&another=one')
expected.page_view.query_string = 'test=true&keywords=hello+world+[email]+[postcode]+[date]&another=one'
expected.page_view.search_term = 'hello world [email] [postcode] [date]'
expected.page_view.query_string = 'test=true&keywords=hello+world+[email]+[postcode]+1990-01-01&another=one'
expected.page_view.search_term = 'hello world [email] [postcode] 1990-01-01'
GOVUK.analyticsGa4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
})
Expand Down Expand Up @@ -620,9 +655,9 @@ describe('Google Tag Manager page view tracking', function () {
document.body.removeChild(div)
})

it('correctly sets the parameter with PII values redacted', function () {
it('correctly sets the parameter with PII values redacted (except for dates)', function () {
div.setAttribute('data-ga4-search-query', 'hello world [email protected] SW12AA 1990-01-01')
expected.page_view.search_term = 'hello world [email] [postcode] [date]'
expected.page_view.search_term = 'hello world [email] [postcode] 1990-01-01'
GOVUK.analyticsGa4.analyticsModules.PageViewTracker.init()
expect(window.dataLayer[0]).toEqual(expected)
})
Expand Down
Loading