Skip to content

Commit

Permalink
Reuse populateLinkDomain
Browse files Browse the repository at this point in the history
- move the code for including the domain of a link into the core functions, so both the specialist link checker and the link checker can both use it
- add tests
  • Loading branch information
andysellick committed Nov 24, 2022
1 parent d3f480b commit a6c710e
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 47 deletions.
Expand Up @@ -181,6 +181,30 @@ window.GOVUK.analyticsGa4 = window.GOVUK.analyticsGa4 || {};

stringEndsWith: function (string, stringToFind) {
return string.substring(string.length - stringToFind.length, string.length) === stringToFind
},

populateLinkDomain: function (href) {
// We always want mailto links to have an undefined link_domain
if (this.isMailToLink(href)) {
return undefined
}

if (this.hrefIsRelative(href) || this.hrefIsAnchor(href)) {
return this.getProtocol() + '//' + this.getHostname()
} else {
// This regex matches a protocol and domain name at the start of a string such as https://www.gov.uk, http://gov.uk, //gov.uk
var domainRegex = /^(http:||https:)?(\/\/)([^\/]*)/ // eslint-disable-line no-useless-escape
var domain = domainRegex.exec(href)[0]
return domain
}
},

getProtocol: function () {
return window.location.protocol
},

getHostname: function () {
return window.location.hostname
}
}
}
Expand Down
Expand Up @@ -72,6 +72,7 @@ window.GOVUK.Modules = window.GOVUK.Modules || {};
schema.event = 'event_data'
data.text = window.GOVUK.analyticsGa4.core.trackFunctions.removeLinesAndExtraSpaces(event.target.textContent)
data.url = window.GOVUK.analyticsGa4.core.trackFunctions.removeCrossDomainParams(this.findLink(event.target).getAttribute('href'))
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'
Expand Down
Expand Up @@ -11,7 +11,7 @@ window.GOVUK.analyticsGa4.analyticsModules = window.GOVUK.analyticsGa4.analytics
if (window.dataLayer) {
config = config || {}
this.internalDomains = config.internalDomains || []
this.internalDomains.push(this.getHostname())
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']
Expand Down Expand Up @@ -74,7 +74,7 @@ window.GOVUK.analyticsGa4.analyticsModules = window.GOVUK.analyticsGa4.analytics
clickData.url = href
if (clickData.url) {
clickData.url = window.GOVUK.analyticsGa4.core.trackFunctions.removeCrossDomainParams(clickData.url)
clickData.link_domain = this.populateLinkDomain(clickData.url)
clickData.link_domain = window.GOVUK.analyticsGa4.core.trackFunctions.populateLinkDomain(clickData.url)
clickData.link_path_parts = window.GOVUK.analyticsGa4.core.trackFunctions.populateLinkPathParts(clickData.url)
}

Expand All @@ -96,22 +96,6 @@ window.GOVUK.analyticsGa4.analyticsModules = window.GOVUK.analyticsGa4.analytics
}
},

populateLinkDomain: function (href) {
// We always want mailto links to have an undefined link_domain
if (window.GOVUK.analyticsGa4.core.trackFunctions.isMailToLink(href)) {
return undefined
}

if (window.GOVUK.analyticsGa4.core.trackFunctions.hrefIsRelative(href)) {
return this.getProtocol() + '//' + this.getHostname()
} else {
// This regex matches a protocol and domain name at the start of a string such as https://www.gov.uk, http://gov.uk, //gov.uk
var domainRegex = /^(http:||https:)?(\/\/)([^\/]*)/ // eslint-disable-line no-useless-escape
var domain = domainRegex.exec(href)[0]
return domain
}
},

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++) {
Expand Down Expand Up @@ -170,14 +154,6 @@ window.GOVUK.analyticsGa4.analyticsModules = window.GOVUK.analyticsGa4.analytics

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

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

getProtocol: function () {
return window.location.protocol
}
}

Expand Down
Expand Up @@ -205,5 +205,36 @@ describe('GA4 core', function () {
expect(GOVUK.analyticsGa4.core.trackFunctions.hrefPointsToDomain(href, domain)).toEqual(false)
})
})

describe('when populating the link domain', function () {
beforeEach(function () {
spyOn(GOVUK.analyticsGa4.core.trackFunctions, 'getHostname').and.returnValue('www.gov.uk')
spyOn(GOVUK.analyticsGa4.core.trackFunctions, 'getProtocol').and.returnValue('https:')
})

it('ignores mailto links', function () {
var href = 'mailto:meunlessitsspam'
expect(GOVUK.analyticsGa4.core.trackFunctions.populateLinkDomain(href)).toEqual(undefined)
})

it('handles relative links and anchor links correctly', function () {
var href = '/something'
expect(GOVUK.analyticsGa4.core.trackFunctions.populateLinkDomain(href)).toEqual('https://www.gov.uk')

href = '#something'
expect(GOVUK.analyticsGa4.core.trackFunctions.populateLinkDomain(href)).toEqual('https://www.gov.uk')
})

it('handles absolute links correctly', function () {
var href = 'https://www.something.com/something'
expect(GOVUK.analyticsGa4.core.trackFunctions.populateLinkDomain(href)).toEqual('https://www.something.com')

href = 'http://www.something.com/something'
expect(GOVUK.analyticsGa4.core.trackFunctions.populateLinkDomain(href)).toEqual('http://www.something.com')

href = '//www.something.com/something'
expect(GOVUK.analyticsGa4.core.trackFunctions.populateLinkDomain(href)).toEqual('//www.something.com')
})
})
})
})
Expand Up @@ -5,13 +5,6 @@ describe('GA4 click tracker', function () {
var element
var expected
var attributes
var linkParts = {
1: undefined,
2: undefined,
3: undefined,
4: undefined,
5: undefined
}

function initModule (element, click) {
new GOVUK.Modules.Ga4LinkTracker(element).init()
Expand All @@ -30,10 +23,23 @@ describe('GA4 click tracker', function () {

beforeAll(function () {
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:')
})

beforeEach(function () {
window.dataLayer = []
expected = new GOVUK.analyticsGa4.Schemas().eventSchema()
expected.event_data.event_name = 'navigation'
expected.govuk_gem_version = 'aVersion'
expected.event_data.link_domain = 'https://www.gov.uk'
expected.event_data.link_path_parts = {
1: undefined,
2: undefined,
3: undefined,
4: undefined,
5: undefined
}
agreeToCookies()
})

Expand Down Expand Up @@ -92,12 +98,8 @@ describe('GA4 click tracker', function () {
event_name: 'navigation',
type: 'a link'
}
expected = new GOVUK.analyticsGa4.Schemas().eventSchema()
expected.event = 'event_data'
expected.event_data.event_name = 'navigation'
expected.event_data.type = 'a link'
expected.govuk_gem_version = 'aVersion'
expected.event_data.link_path_parts = linkParts
expected.event_data.method = 'primary click'
expected.event_data.external = 'false'
})
Expand Down Expand Up @@ -168,12 +170,8 @@ describe('GA4 click tracker', function () {
event_name: 'navigation',
type: 'a link'
}
expected = new GOVUK.analyticsGa4.Schemas().eventSchema()
expected.event = 'event_data'
expected.event_data.event_name = 'navigation'
expected.event_data.type = 'a link'
expected.govuk_gem_version = 'aVersion'
expected.event_data.link_path_parts = linkParts
expected.event_data.method = 'primary click'
expected.event_data.external = 'false'
})
Expand Down Expand Up @@ -219,12 +217,8 @@ describe('GA4 click tracker', function () {
event_name: 'navigation',
type: 'a link'
}
expected = new GOVUK.analyticsGa4.Schemas().eventSchema()
expected.event = 'event_data'
expected.event_data.event_name = 'navigation'
expected.event_data.type = 'a link'
expected.govuk_gem_version = 'aVersion'
expected.event_data.link_path_parts = linkParts
expected.event_data.method = 'primary click'
expected.event_data.external = 'false'
})
Expand Down
Expand Up @@ -14,8 +14,8 @@ describe('A specialist link tracker', function () {

beforeAll(function () {
spyOn(GOVUK.analyticsGa4.core, 'getGemVersion').and.returnValue('aVersion')
spyOn(GOVUK.analyticsGa4.analyticsModules.Ga4SpecialistLinkTracker, 'getHostname').and.returnValue('www.gov.uk')
spyOn(GOVUK.analyticsGa4.analyticsModules.Ga4SpecialistLinkTracker, 'getProtocol').and.returnValue('https:')
spyOn(GOVUK.analyticsGa4.core.trackFunctions, 'getHostname').and.returnValue('www.gov.uk')
spyOn(GOVUK.analyticsGa4.core.trackFunctions, 'getProtocol').and.returnValue('https:')
})

afterAll(function () {
Expand Down

0 comments on commit a6c710e

Please sign in to comment.