diff --git a/CHANGELOG.md b/CHANGELOG.md index 3421819aa2..de7339eab5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ ## Unreleased * Remove option select GA4 attributes ([PR #3625](https://github.com/alphagov/govuk_publishing_components/pull/3625)) +* Fix various bugs with the GA4 pageview tracker ([PR #3626](https://github.com/alphagov/govuk_publishing_components/pull/3626)) ## 35.16.0 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 ae019e66ac..b9017311f7 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 @@ -68,7 +68,7 @@ window.GOVUK.analyticsGa4.analyticsModules = window.GOVUK.analyticsGa4.analytics }, getLocation: function () { - return this.PIIRemover.stripPII(this.stripGaParam(document.location.href)) + return this.PIIRemover.stripPIIWithOverride(this.stripGaParam(document.location.href), true, true) }, getSearch: function () { @@ -95,8 +95,8 @@ window.GOVUK.analyticsGa4.analyticsModules = window.GOVUK.analyticsGa4.analytics getQueryString: function () { var queryString = this.getSearch() if (queryString) { - queryString = this.PIIRemover.stripPIIWithOverride(queryString, true, true) queryString = this.stripGaParam(queryString) + queryString = this.PIIRemover.stripPIIWithOverride(queryString, true, true) queryString = queryString.substring(1) // removes the '?' character from the start. return queryString } @@ -115,7 +115,7 @@ window.GOVUK.analyticsGa4.analyticsModules = window.GOVUK.analyticsGa4.analytics }, getTitle: function () { - return this.PIIRemover.stripPII(document.title) + return this.PIIRemover.stripPIIWithOverride(document.title, true, true) }, // window.httpStatusCode is set in the source of the error page in static @@ -148,7 +148,10 @@ window.GOVUK.analyticsGa4.analyticsModules = window.GOVUK.analyticsGa4.analytics var content = document.getElementById('content') var html = document.querySelector('html') if (content) { - return content.getAttribute('lang') || this.nullValue + var contentLanguage = content.getAttribute('lang') + if (contentLanguage) { + return contentLanguage + } } // html.getAttribute('lang') is untested - Jasmine would not allow lang to be set on . return html.getAttribute('lang') || this.nullValue 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 ba4cae300f..6be5653535 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 @@ -345,12 +345,12 @@ describe('Google Tag Manager page view tracking', function () { expect(window.dataLayer[0]).toEqual(expected) }) - it('removes email pii from the title, location and referrer', function () { - document.title = 'example@gov.uk' - expected.page_view.title = '[email]' + it('removes email, postcode, and date pii from the title, location and referrer', function () { + document.title = 'example@gov.uk - SW12AA - 2020-01-01' + expected.page_view.title = '[email] - [postcode] - [date]' - spyOnProperty(document, 'referrer', 'get').and.returnValue('https://gov.uk/example@gov.uk') - expected.page_view.referrer = 'https://gov.uk/[email]' + spyOnProperty(document, 'referrer', 'get').and.returnValue('https://gov.uk/example@gov.uk/SW12AA/2020-01-01') + expected.page_view.referrer = 'https://gov.uk/[email]/[postcode]/[date]' // We can't spy on location, so instead we use an anchor link to change the URL temporarily @@ -360,10 +360,10 @@ describe('Google Tag Manager page view tracking', function () { linkForURLMock.click() var location = document.location.href - expected.page_view.location = location + '[email]' + expected.page_view.location = location + '[email]/[postcode]/[date]' - // Add email address to the current page location - linkForURLMock.href = '#example@gov.uk' + // Add personally identifiable information to the current page location + linkForURLMock.href = '#example@gov.uk/SW12AA/2020-01-01' linkForURLMock.click() GOVUK.analyticsGa4.analyticsModules.PageViewTracker.init() @@ -531,7 +531,7 @@ describe('Google Tag Manager page view tracking', function () { }) it('correctly sets the query_string parameter with PII and _ga/_gl values redacted', function () { - spyOn(GOVUK.analyticsGa4.analyticsModules.PageViewTracker, 'getSearch').and.returnValue('?query1=hello&query2=world&email=email@example.com&postcode=SW12AA&birthday=1990-01-01&_ga=1234.567&_gl=1234.567') + spyOn(GOVUK.analyticsGa4.analyticsModules.PageViewTracker, 'getSearch').and.returnValue('?query1=hello&query2=world&email=email@example.com&postcode=SW12AA&birthday=1990-01-01&_ga=19900101.567&_gl=19900101.567') expected.page_view.query_string = 'query1=hello&query2=world&email=[email]&postcode=[postcode]&birthday=[date]&_ga=[id]&_gl=[id]' GOVUK.analyticsGa4.analyticsModules.PageViewTracker.init() expect(window.dataLayer[0]).toEqual(expected)