Skip to content

Commit

Permalink
Stop redacting dates in GA4 tracking on GOVUK search pages
Browse files Browse the repository at this point in the history
  • Loading branch information
AshGDS committed Sep 13, 2024
1 parent 8245c95 commit 7d808b4
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
## Unreleased

* Set aria-label text in govuk_logo.html to "GOV.UK" ([PR #4217](https://github.com/alphagov/govuk_publishing_components/pull/4217))
* Stop redacting dates in GA4 tracking on GOVUK search pages ([PR #4223](https://github.com/alphagov/govuk_publishing_components/pull/4223))

## 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 @@ -78,7 +78,9 @@ 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.
var stripDates = !this.getSearchTerm()
return this.PIIRemover.stripPIIWithOverride(this.stripGaParam(document.location.href), stripDates, true)
},

getSearchTerm: function () {
Expand Down Expand Up @@ -110,8 +112,10 @@ 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.
var stripDates = !this.getSearchTerm()
queryString = this.stripGaParam(queryString)
queryString = this.PIIRemover.stripPIIWithOverride(queryString, true, true)
queryString = this.PIIRemover.stripPIIWithOverride(queryString, 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,34 @@ describe('Google Tag Manager page view tracking', function () {
expect(window.dataLayer[0]).toEqual(expected)
})

it('doesn\'t remove date pii from the location if 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 +586,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 if 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 +656,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 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

0 comments on commit 7d808b4

Please sign in to comment.