Skip to content

Commit

Permalink
Add /media/ path to GA4 download link tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
AshGDS committed Dec 11, 2024
1 parent c22b23e commit a0ac015
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
useful summary for people upgrading their application, not a replication
of the commit log.

## Unreleased

* Add /media/ path to GA4 download link tracking ([PR #4491](https://github.com/alphagov/govuk_publishing_components/pull/4491/))

## 46.3.0

* Remove min-width from share links flexbox variant list-items ([PR #4488](https://github.com/alphagov/govuk_publishing_components/pull/4488))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ window.GOVUK.analyticsGa4.analyticsModules = window.GOVUK.analyticsGa4.analytics
init: function (config) {
if (window.dataLayer) {
config = config || {}
this.internalDownloadPaths = config.internalDownloadPaths || ['/government/uploads/']
this.internalDownloadPaths = config.internalDownloadPaths || ['/government/uploads/', '/media/']
this.dedicatedDownloadDomains = config.dedicatedDownloadDomains || ['assets.publishing.service.gov.uk']
window.GOVUK.analyticsGa4.core.trackFunctions.appendDomainsWithoutWWW(this.dedicatedDownloadDomains)
this.handleClick = this.handleClick.bind(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,10 +371,12 @@ describe('A specialist link tracker', function () {
'<div class="preview-download-links">' +
'<a href="https://assets.publishing.service.gov.uk/government/uploads/system/uploads/attachment_data/file/444468/moj-hq.csv/preview" path="/government/uploads/system/uploads/attachment_data/file/444468/moj-hq.csv/preview" link_domain="https://assets.publishing.service.gov.uk">Preview link</a>' +
'<a href="http://assets.publishing.service.gov.uk/government/uploads/system/uploads/attachment_data/file/444468/moj-hq.csv/preview" path="/government/uploads/system/uploads/attachment_data/file/444468/moj-hq.csv/preview" link_domain="http://assets.publishing.service.gov.uk">Relative Spreadsheet link</a>' +
'<a href="https://www.gov.uk/media/66866f8d4a94d44125d9ccc1/Management_information_-_initial_teacher_education___in_year_inspection_outcomes_1_September_2023_to_30_April_2024.csv/preview" path="/media/66866f8d4a94d44125d9ccc1/Management_information_-_initial_teacher_education___in_year_inspection_outcomes_1_September_2023_to_30_April_2024.csv/preview" link_domain="https://www.gov.uk">Relative csv link</a>' +
'</div>' +
'<div class="not-a-preview-link">' +
'<a href="https://assets.publishing.service.gov.uk/government/uploads/system/uploads/attachment_data/file/444468/preview.mp4" path="/government/uploads/system/uploads/attachment_data/file/444468/preview.mp4" link_domain="https://assets.publishing.service.gov.uk">Preview link</a>' +
'<a href="http://assets.publishing.service.gov.uk/government/uploads/system/uploads/attachment_data/file/444468/preview.jpg&preview=false" path="/government/uploads/system/uploads/attachment_data/file/444468/preview.jpg&preview=false" link_domain="http://assets.publishing.service.gov.uk">Relative Spreadsheet link</a>' +
'<a href="https://www.gov.uk/media/66866f8d4a94d44125d9ccc1/preview.csv" link_domain="https://www.gov.uk">Relative csv link</a>' +
'</div>'

body.appendChild(links)
Expand Down Expand Up @@ -509,7 +511,7 @@ describe('A specialist link tracker', function () {

it('detects preview clicks on gov.uk download preview links', function () {
var linksToTest = document.querySelectorAll('.preview-download-links a')

var external = ['true', 'true', 'false']
for (var i = 0; i < linksToTest.length; i++) {
window.dataLayer = []
var link = linksToTest[i]
Expand All @@ -518,15 +520,15 @@ describe('A specialist link tracker', function () {
expected.event_data.url = link.getAttribute('href')
expected.event_data.type = 'preview'
expected.event_data.text = link.innerText.trim()
expected.event_data.external = 'true'
expected.event_data.external = external[i]

expect(window.dataLayer[0]).toEqual(expected)
}
})

it('detects files with preview in their filename as download links instead of preview links', function () {
var linksToTest = document.querySelectorAll('.not-a-preview-link a')

var external = ['true', 'true', 'false']
for (var i = 0; i < linksToTest.length; i++) {
window.dataLayer = []
var link = linksToTest[i]
Expand All @@ -535,7 +537,7 @@ describe('A specialist link tracker', function () {
expected.event_data.url = link.getAttribute('href')
expected.event_data.type = 'generic download'
expected.event_data.text = link.innerText.trim()
expected.event_data.external = 'true'
expected.event_data.external = external[i]

expect(window.dataLayer[0]).toEqual(expected)
}
Expand Down

0 comments on commit a0ac015

Please sign in to comment.