Skip to content

Commit

Permalink
Reconfigure how internal domains works
Browse files Browse the repository at this point in the history
- move appendDomainsWithoutWWW into core functions
- change how internalDomains are stored from being part of the specialist link tracker to being one of the window.GOVUK.analyticsGa4.vars
- initialise www.gov.uk on analytics load in .vars and call appendDomainsWithoutWWW then
- update references and tests relying on internalDomains
  • Loading branch information
andysellick committed Nov 24, 2022
1 parent f78e1a8 commit d5350dd
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ window.GOVUK.analyticsGa4 = window.GOVUK.analyticsGa4 || {};
}
},

isInternalLink: function (href, internalDomains) {
isInternalLink: function (href) {
var internalDomains = window.GOVUK.analyticsGa4.vars.internalDomains
if (this.hrefIsRelative(href) || this.hrefIsAnchor(href)) {
return true
}
Expand All @@ -134,8 +135,8 @@ window.GOVUK.analyticsGa4 = window.GOVUK.analyticsGa4 || {};
return result
},

isExternalLink: function (href, internalDomains) {
return !this.isInternalLink(href, internalDomains)
isExternalLink: function (href) {
return !this.isInternalLink(href)
},

hrefPointsToDomain: function (href, domain) {
Expand Down Expand Up @@ -179,6 +180,10 @@ window.GOVUK.analyticsGa4 = window.GOVUK.analyticsGa4 || {};
return href
},

stringStartsWith: function (string, stringToFind) {
return string.substring(0, stringToFind.length) === stringToFind
},

stringEndsWith: function (string, stringToFind) {
return string.substring(string.length - stringToFind.length, string.length) === stringToFind
},
Expand All @@ -205,6 +210,17 @@ window.GOVUK.analyticsGa4 = window.GOVUK.analyticsGa4 || {};

getHostname: function () {
return window.location.hostname
},

appendDomainsWithoutWWW: function (domainsArrays) {
// Add domains with www. removed, in case site hrefs are marked up without www. included.
for (var i = 0; i < domainsArrays.length; i++) {
var domain = domainsArrays[i]
if (this.stringStartsWith(domain, 'www.')) {
var domainWithoutWww = domain.replace('www.', '')
domainsArrays.push(domainWithoutWww)
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
})

ecommerceObject.event_data = {
external: GOVUK.analyticsGa4.core.trackFunctions.isExternalLink(searchResult.getAttribute('data-ecommerce-path', [])) ? 'true' : 'false'
external: GOVUK.analyticsGa4.core.trackFunctions.isExternalLink(searchResult.getAttribute('data-ecommerce-path')) ? 'true' : 'false'
}
} else {
for (var i = 0; i < ecommerceRows.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ window.GOVUK.Modules = window.GOVUK.Modules || {};
data.link_domain = window.GOVUK.analyticsGa4.core.trackFunctions.populateLinkDomain(data.url)
data.link_path_parts = window.GOVUK.analyticsGa4.core.trackFunctions.populateLinkPathParts(data.url)
data.method = window.GOVUK.analyticsGa4.core.trackFunctions.getClickType(event)
data.external = window.GOVUK.analyticsGa4.core.trackFunctions.isExternalLink(data.url, []) ? 'true' : 'false'
data.external = window.GOVUK.analyticsGa4.core.trackFunctions.isExternalLink(data.url) ? 'true' : 'false'

// get attributes from the data attribute to send to GA
// only allow it if it already exists in the schema
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@ window.GOVUK.analyticsGa4.analyticsModules = window.GOVUK.analyticsGa4.analytics
init: function (config) {
if (window.dataLayer) {
config = config || {}
this.internalDomains = config.internalDomains || []
this.internalDomains.push(window.GOVUK.analyticsGa4.core.trackFunctions.getHostname())
this.appendDomainsWithoutWWW(this.internalDomains)
this.internalDownloadPaths = config.internalDownloadPaths || ['/government/uploads/']
this.dedicatedDownloadDomains = config.dedicatedDownloadDomains || ['assets.publishing.service.gov.uk']
this.appendDomainsWithoutWWW(this.dedicatedDownloadDomains)
window.GOVUK.analyticsGa4.core.trackFunctions.appendDomainsWithoutWWW(this.dedicatedDownloadDomains)
this.handleClick = this.handleClick.bind(this)
this.handleMousedown = this.handleMousedown.bind(this)

Expand Down Expand Up @@ -57,17 +54,14 @@ window.GOVUK.analyticsGa4.analyticsModules = window.GOVUK.analyticsGa4.analytics
clickData.event_name = 'navigation'
clickData.type = 'email'
clickData.external = 'true'
clickData.url = href
} else if (this.isDownloadLink(href)) {
clickData.event_name = 'file_download'
clickData.type = this.isPreviewLink(href) ? 'preview' : 'generic download'
clickData.external = window.GOVUK.analyticsGa4.core.trackFunctions.isExternalLink(href, this.internalDomains) ? 'true' : 'false'
clickData.url = href
} else if (window.GOVUK.analyticsGa4.core.trackFunctions.isExternalLink(href, this.internalDomains)) {
clickData.external = window.GOVUK.analyticsGa4.core.trackFunctions.isExternalLink(href) ? 'true' : 'false'
} else if (window.GOVUK.analyticsGa4.core.trackFunctions.isExternalLink(href)) {
clickData.event_name = 'navigation'
clickData.type = 'generic link'
clickData.external = 'true'
clickData.url = href
}

if (Object.keys(clickData).length > 0) {
Expand Down Expand Up @@ -96,17 +90,6 @@ window.GOVUK.analyticsGa4.analyticsModules = window.GOVUK.analyticsGa4.analytics
}
},

appendDomainsWithoutWWW: function (domainsArrays) {
// Add domains with www. removed, in case site hrefs are marked up without www. included.
for (var i = 0; i < domainsArrays.length; i++) {
var domain = domainsArrays[i]
if (this.stringStartsWith(domain, 'www.')) {
var domainWithoutWww = domain.replace('www.', '')
domainsArrays.push(domainWithoutWww)
}
}
},

handleMousedown: function (event) {
// 1 = middle mouse button
if (event.button === 1) {
Expand All @@ -115,7 +98,7 @@ window.GOVUK.analyticsGa4.analyticsModules = window.GOVUK.analyticsGa4.analytics
},

isDownloadLink: function (href) {
if (window.GOVUK.analyticsGa4.core.trackFunctions.isInternalLink(href, this.internalDomains) && this.hrefPointsToDownloadPath(href)) {
if (window.GOVUK.analyticsGa4.core.trackFunctions.isInternalLink(href) && this.hrefPointsToDownloadPath(href)) {
return true
}

Expand Down Expand Up @@ -150,10 +133,6 @@ window.GOVUK.analyticsGa4.analyticsModules = window.GOVUK.analyticsGa4.analytics
}
}
return result
},

stringStartsWith: function (string, stringToFind) {
return string.substring(0, stringToFind.length) === stringToFind
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ var initFunction = function () {
var consentCookie = window.GOVUK.getConsentCookie()

if (consentCookie && consentCookie.usage) {
window.GOVUK.analyticsGa4.vars.internalDomains = []
window.GOVUK.analyticsGa4.vars.internalDomains.push(window.GOVUK.analyticsGa4.core.trackFunctions.getHostname())
window.GOVUK.analyticsGa4.core.trackFunctions.appendDomainsWithoutWWW(window.GOVUK.analyticsGa4.vars.internalDomains)
window.GOVUK.analyticsGa4.core.load()

var analyticsModules = window.GOVUK.analyticsGa4.analyticsModules
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,30 @@

describe('GA4 core', function () {
var GOVUK = window.GOVUK
var id
var auth
var preview

beforeEach(function () {
window.dataLayer = []
window.GOVUK.analyticsGa4.vars = {}
id = window.GOVUK.analyticsGa4.vars.id
auth = window.GOVUK.analyticsGa4.vars.auth
preview = window.GOVUK.analyticsGa4.vars.preview

window.GOVUK.analyticsGa4.vars.id = undefined
window.GOVUK.analyticsGa4.vars.auth = undefined
window.GOVUK.analyticsGa4.vars.preview = undefined
})

afterEach(function () {
window.dataLayer = []
window.GOVUK.analyticsGa4.vars.id = 'test-id'
window.GOVUK.analyticsGa4.vars.auth = 'test-auth'
window.GOVUK.analyticsGa4.vars.preview = 'test-preview'
window.GOVUK.analyticsGa4.vars.gem_version = 'gem-version'
window.GOVUK.analyticsGa4.vars.id = id
window.GOVUK.analyticsGa4.vars.auth = auth
window.GOVUK.analyticsGa4.vars.preview = preview
window.GOVUK.analyticsGa4.vars.gtag_id = null

window.GOVUK.analyticsGa4.vars.internalDomains = ['www.gov.uk']
window.GOVUK.analyticsGa4.core.trackFunctions.appendDomainsWithoutWWW(window.GOVUK.analyticsGa4.vars.internalDomains)
})

it('loads the GTM snippet', function () {
Expand Down Expand Up @@ -187,8 +198,11 @@ describe('GA4 core', function () {
})

it('correctly identifies an absolute link as internal when on the same domain', function () {
window.GOVUK.analyticsGa4.vars.internalDomains.push('www.notasite.com')
window.GOVUK.analyticsGa4.core.trackFunctions.appendDomainsWithoutWWW(window.GOVUK.analyticsGa4.vars.internalDomains)

var href = 'https://notasite.com/something'
expect(GOVUK.analyticsGa4.core.trackFunctions.isInternalLink(href, ['notasite.com'])).toEqual(true)
expect(GOVUK.analyticsGa4.core.trackFunctions.isInternalLink(href)).toEqual(true)
})
})

Expand Down Expand Up @@ -236,5 +250,12 @@ describe('GA4 core', function () {
expect(GOVUK.analyticsGa4.core.trackFunctions.populateLinkDomain(href)).toEqual('//www.something.com')
})
})

it('accepts an array of domains and increases it to include variants without www at the start', function () {
var domains = ['www.gov.uk']
GOVUK.analyticsGa4.core.trackFunctions.appendDomainsWithoutWWW(domains)
expect(domains).toContain('www.gov.uk')
expect(domains).toContain('gov.uk')
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ describe('A specialist link tracker', function () {
var defaultLinkPathParts

beforeAll(function () {
window.GOVUK.analyticsGa4.vars.internalDomains = ['www.gov.uk']
window.GOVUK.analyticsGa4.core.trackFunctions.appendDomainsWithoutWWW(window.GOVUK.analyticsGa4.vars.internalDomains)
spyOn(GOVUK.analyticsGa4.core, 'getGemVersion').and.returnValue('aVersion')
spyOn(GOVUK.analyticsGa4.core.trackFunctions, 'getHostname').and.returnValue('www.gov.uk')
spyOn(GOVUK.analyticsGa4.core.trackFunctions, 'getProtocol').and.returnValue('https:')
Expand Down Expand Up @@ -84,8 +86,7 @@ describe('A specialist link tracker', function () {
body.addEventListener('click', preventDefault)

linkTracker = GOVUK.analyticsGa4.analyticsModules.Ga4SpecialistLinkTracker
// Add gov.uk as an internal domain, as our tests are running from localhost
linkTracker.init({ internalDomains: ['www.gov.uk'] })
linkTracker.init()
})

afterEach(function () {
Expand Down Expand Up @@ -339,8 +340,7 @@ describe('A specialist link tracker', function () {
body.addEventListener('click', preventDefault)

linkTracker = GOVUK.analyticsGa4.analyticsModules.Ga4SpecialistLinkTracker
// Add gov.uk as an internal domain, as our tests are running from localhost
linkTracker.init({ internalDomains: ['www.gov.uk'] })
linkTracker.init()
})

afterEach(function () {
Expand Down Expand Up @@ -533,8 +533,7 @@ describe('A specialist link tracker', function () {
body.addEventListener('click', preventDefault)

linkTracker = GOVUK.analyticsGa4.analyticsModules.Ga4SpecialistLinkTracker
// Add gov.uk as an internal domain, as our tests are running from localhost
linkTracker.init({ internalDomains: ['www.gov.uk'] })
linkTracker.init()
})

afterEach(function () {
Expand Down Expand Up @@ -584,17 +583,9 @@ describe('A specialist link tracker', function () {
linkTracker.stopTracking()
})

it('adds "gov.uk" to the internal domain list after "www.gov.uk" was added as an internal domain', function () {
linkTracker = GOVUK.analyticsGa4.analyticsModules.Ga4SpecialistLinkTracker
// Add gov.uk as an internal domain, as our tests are running from localhost
linkTracker.init({ internalDomains: ['www.gov.uk'] })
expect(linkTracker.internalDomains).toContain('www.gov.uk')
expect(linkTracker.internalDomains).toContain('gov.uk')
})

it('removes _ga and _gl from href query parameters', function () {
linkTracker = GOVUK.analyticsGa4.analyticsModules.Ga4SpecialistLinkTracker
linkTracker.init({ internalDomains: ['www.gov.uk'] })
linkTracker.init()

links.innerHTML = '<div class="query-param-links">' +
'<a href="https://nationalarchives.gov.uk/test?_ga=2.179870689.471678113.1662373341-1606126050.1639392506">_ga only link</a>' +
Expand Down Expand Up @@ -625,7 +616,7 @@ describe('A specialist link tracker', function () {

it('cleans up link text with multiple lines and spaces', function () {
linkTracker = GOVUK.analyticsGa4.analyticsModules.Ga4SpecialistLinkTracker
linkTracker.init({ internalDomains: ['www.gov.uk'] })
linkTracker.init()

links.innerHTML = '<div class="messy-text">' +
'<a href="https://example.com">\nI \nam \non \nmultiple \n\n\n lines</a>' +
Expand All @@ -650,7 +641,7 @@ describe('A specialist link tracker', function () {

it('splits hrefs longer than 100 characters into an object of parts', function () {
linkTracker = GOVUK.analyticsGa4.analyticsModules.Ga4SpecialistLinkTracker
linkTracker.init({ internalDomains: ['www.gov.uk'] })
linkTracker.init()

links.innerHTML = '<div class="long-links">' +
'<a href="https://example.com/supercalifragilisticexpialidocious-even-though-the-sound-of-it-is-something-quite-atrocious-if-you-">100 char path</a>' +
Expand Down
2 changes: 2 additions & 0 deletions spec/javascripts/helpers/SpecHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ beforeAll(function () {
window.GOVUK.analyticsGa4.vars.auth = 'test-auth'
window.GOVUK.analyticsGa4.vars.preview = 'test-preview'
window.GOVUK.analyticsGa4.vars.gem_version = 'gem-version'
window.GOVUK.analyticsGa4.vars.internalDomains = ['www.gov.uk']
window.GOVUK.analyticsGa4.core.trackFunctions.appendDomainsWithoutWWW(window.GOVUK.analyticsGa4.vars.internalDomains)

delete ga
window.GOVUK.analyticsInit()
Expand Down

0 comments on commit d5350dd

Please sign in to comment.