From 7d808b43aada2724953fec73d6c7684d9c369196 Mon Sep 17 00:00:00 2001 From: AshGDS <8880610+AshGDS@users.noreply.github.com> Date: Fri, 13 Sep 2024 12:19:13 +0100 Subject: [PATCH] Stop redacting dates in GA4 tracking on GOVUK search pages --- CHANGELOG.md | 1 + .../analytics-ga4/ga4-core.js | 2 +- .../analytics-ga4/ga4-page-views.js | 8 ++- .../analytics-ga4/ga4-core.spec.js | 2 +- .../analytics-ga4/ga4-page-views.spec.js | 50 ++++++++++++++++--- 5 files changed, 52 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 36496c9e24..f097ad1203 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-core.js b/app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-core.js index 92ee812ab6..76a985df26 100644 --- a/app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-core.js +++ b/app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-core.js @@ -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 } diff --git a/app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-page-views.js b/app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-page-views.js index 4ea7441a4d..f48f0f093f 100644 --- a/app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-page-views.js +++ b/app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-page-views.js @@ -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 () { @@ -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 } diff --git a/spec/javascripts/govuk_publishing_components/analytics-ga4/ga4-core.spec.js b/spec/javascripts/govuk_publishing_components/analytics-ga4/ga4-core.spec.js index 9534aa597c..e4eabd609a 100644 --- a/spec/javascripts/govuk_publishing_components/analytics-ga4/ga4-core.spec.js +++ b/spec/javascripts/govuk_publishing_components/analytics-ga4/ga4-core.spec.js @@ -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@example.com 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) diff --git a/spec/javascripts/govuk_publishing_components/analytics-ga4/ga4-page-views.spec.js b/spec/javascripts/govuk_publishing_components/analytics-ga4/ga4-page-views.spec.js index 932bca047f..fb996e457b 100644 --- a/spec/javascripts/govuk_publishing_components/analytics-ga4/ga4-page-views.spec.js +++ b/spec/javascripts/govuk_publishing_components/analytics-ga4/ga4-page-views.spec.js @@ -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 = '#example@gov.uk/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' @@ -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=email@example.com&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('?keywords=hello+world+email@example.com+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&keywords=hello+world+email@example.com+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) }) @@ -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@example.com 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) })