Skip to content

Commit

Permalink
Merge pull request #4500 from alphagov/ga4-canonical-url
Browse files Browse the repository at this point in the history
Add canonical_url value to GA4 page view tracking
  • Loading branch information
AshGDS authored Dec 13, 2024
2 parents 9f5c3cb + c05ccf0 commit dd36848
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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)
})
})

0 comments on commit dd36848

Please sign in to comment.