From c05ccf072221a5ea029e3dea08fc6e9983e260b0 Mon Sep 17 00:00:00 2001 From: AshGDS <8880610+AshGDS@users.noreply.github.com> Date: Fri, 13 Dec 2024 12:30:14 +0000 Subject: [PATCH] Add canonical_url value to GA4 page view tracking --- CHANGELOG.md | 1 + .../analytics-ga4/ga4-page-views.js | 11 ++++++++++- .../analytics-ga4/ga4-page-views.spec.js | 19 ++++++++++++++++++- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c144f4c936..342365034d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ ## Unreleased +* Add canonical_url value to GA4 page view tracking ([PR #4500](https://github.com/alphagov/govuk_publishing_components/pull/4500)) * Add margin_bottom option to component wrapper helper ([PR #4494](https://github.com/alphagov/govuk_publishing_components/pull/4494)) * Limit GA4 search term tracking to 500 characters ([PR #4496](https://github.com/alphagov/govuk_publishing_components/pull/4496)) 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 7ad676f1b5..359c00f8fb 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 @@ -66,7 +66,8 @@ window.GOVUK.analyticsGa4.analyticsModules = window.GOVUK.analyticsGa4.analytics search_term: this.getSearchTerm(), tool_name: this.getToolName(), spelling_suggestion: this.getMetaContent('spelling-suggestion'), - discovery_engine_attribution_token: this.getMetaContent('discovery-engine-attribution-token') + discovery_engine_attribution_token: this.getMetaContent('discovery-engine-attribution-token'), + canonical_url: this.getCanonicalHref() } } window.GOVUK.analyticsGa4.core.sendData(data) @@ -79,6 +80,14 @@ window.GOVUK.analyticsGa4.analyticsModules = window.GOVUK.analyticsGa4.analytics } }, + getCanonicalHref: function () { + var link = document.querySelector('link[rel=canonical]') + + if (link) { + return link.href + } + }, + getLocation: function () { // We don't want to remove dates on search pages. return this.PIIRemover.stripPIIWithOverride(this.stripGaParam(document.location.href), this.stripDates, true) 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 4ee6f28dcc..5c9b92d057 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 @@ -63,7 +63,8 @@ describe('Google Tag Manager page view tracking', function () { search_term: undefined, tool_name: undefined, spelling_suggestion: undefined, - discovery_engine_attribution_token: undefined + discovery_engine_attribution_token: undefined, + canonical_url: undefined } } spyOn(GOVUK.analyticsGa4.core, 'getTimestamp').and.returnValue('123456') @@ -717,4 +718,20 @@ describe('Google Tag Manager page view tracking', function () { expect(window.dataLayer[0].page_view.tool_name).toEqual('autocomplete') }) }) + + it('returns a canonical_url', function () { + var link = document.createElement('link') + var head = document.getElementsByTagName('head')[0] + + link.setAttribute('rel', 'canonical') + link.setAttribute('href', 'https://www.gov.uk/benefits-calculators/') + + head.appendChild(link) + + expected.page_view.canonical_url = 'https://www.gov.uk/benefits-calculators/' + GOVUK.analyticsGa4.analyticsModules.PageViewTracker.init() + expect(window.dataLayer[0]).toEqual(expected) + + head.removeChild(link) + }) })