Skip to content

Commit

Permalink
Refactor domain configuration for load-analytics
Browse files Browse the repository at this point in the history
- now that the domain config contains information about more than just analytics, it feels like it should be separated out for clarity
- updated docs and tests accordingly
  • Loading branch information
andysellick committed Mar 5, 2024
1 parent f4b21b0 commit c6d89cb
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 98 deletions.
@@ -0,0 +1,73 @@
window.GOVUK = window.GOVUK || {}
window.GOVUK.vars = window.GOVUK.vars || {}
window.GOVUK.vars.domains = [
{
// need to have this one at the start, see loadGa4 function
name: 'development',
domains: [
'localhost',
'127.0.0.1',
'0.0.0.0',
'dev.gov.uk'
],
initialiseGA4: true,
id: 'GTM-MG7HG5W',
auth: 'bRiZ-jiEHtw6hHpGd6dF9w',
preview: 'env-3',
gaProperty: 'UA-UNSET',
gaPropertyCrossDomain: 'UA-UNSET',
consentApiUrl: 'staging'
},
{
name: 'production',
domains: [
'www.gov.uk',
'www-origin.publishing.service.gov.uk',
'assets.publishing.service.gov.uk'
],
initialiseGA4: true,
id: 'GTM-MG7HG5W',
gaProperty: 'UA-26179049-1',
gaPropertyCrossDomain: 'UA-145652997-1',
consentApiUrl: 'production'
},
{
name: 'staging',
domains: [
'www.staging.publishing.service.gov.uk',
'www-origin.staging.publishing.service.gov.uk',
'assets.staging.publishing.service.gov.uk'
],
initialiseGA4: true,
id: 'GTM-MG7HG5W',
auth: 'oJWs562CxSIjZKn_GlB5Bw',
preview: 'env-5',
gaProperty: 'UA-26179049-20',
gaPropertyCrossDomain: 'UA-145652997-1',
consentApiUrl: 'staging'
},
{
name: 'integration',
domains: [
'www.integration.publishing.service.gov.uk',
'www-origin.integration.publishing.service.gov.uk',
'assets.integration.publishing.service.gov.uk'
],
initialiseGA4: true,
id: 'GTM-MG7HG5W',
auth: 'C7iYdcsOlYgGmiUJjZKrHQ',
preview: 'env-4',
gaProperty: 'UA-26179049-22',
gaPropertyCrossDomain: 'UA-145652997-1',
consentApiUrl: 'staging'
},
{
name: 'devdocs',
domains: [
'docs.publishing.service.gov.uk'
],
initialiseGA4: true,
id: 'GTM-TNKCK97',
consentApiUrl: 'production'
}
]
@@ -1,83 +1,12 @@
//= require govuk_publishing_components/analytics
//= require govuk_publishing_components/analytics-ga4
//= require govuk_publishing_components/analytics/linked-domains
//= require govuk_publishing_components/domain-config

window.GOVUK.loadAnalytics = {
domains: [
{
// need to have this one at the start, see loadGa4 function
name: 'development',
domains: [
'localhost',
'127.0.0.1',
'0.0.0.0',
'dev.gov.uk'
],
initialiseGA4: true,
id: 'GTM-MG7HG5W',
auth: 'bRiZ-jiEHtw6hHpGd6dF9w',
preview: 'env-3',
gaProperty: 'UA-UNSET',
gaPropertyCrossDomain: 'UA-UNSET',
consentApiUrl: 'staging'
},
{
name: 'production',
domains: [
'www.gov.uk',
'www-origin.publishing.service.gov.uk',
'assets.publishing.service.gov.uk'
],
initialiseGA4: true,
id: 'GTM-MG7HG5W',
gaProperty: 'UA-26179049-1',
gaPropertyCrossDomain: 'UA-145652997-1',
consentApiUrl: 'production'
},
{
name: 'staging',
domains: [
'www.staging.publishing.service.gov.uk',
'www-origin.staging.publishing.service.gov.uk',
'assets.staging.publishing.service.gov.uk'
],
initialiseGA4: true,
id: 'GTM-MG7HG5W',
auth: 'oJWs562CxSIjZKn_GlB5Bw',
preview: 'env-5',
gaProperty: 'UA-26179049-20',
gaPropertyCrossDomain: 'UA-145652997-1',
consentApiUrl: 'staging'
},
{
name: 'integration',
domains: [
'www.integration.publishing.service.gov.uk',
'www-origin.integration.publishing.service.gov.uk',
'assets.integration.publishing.service.gov.uk'
],
initialiseGA4: true,
id: 'GTM-MG7HG5W',
auth: 'C7iYdcsOlYgGmiUJjZKrHQ',
preview: 'env-4',
gaProperty: 'UA-26179049-22',
gaPropertyCrossDomain: 'UA-145652997-1',
consentApiUrl: 'staging'
},
{
name: 'devdocs',
domains: [
'docs.publishing.service.gov.uk'
],
initialiseGA4: true,
id: 'GTM-TNKCK97',
consentApiUrl: 'production'
}
],

loadExtraDomains: function () {
if (Array.isArray(window.GOVUK.analyticsGa4Domains)) {
this.domains = this.domains.concat(window.GOVUK.analyticsGa4Domains)
if (Array.isArray(window.GOVUK.vars.extraDomains)) {
window.GOVUK.vars.domains = window.GOVUK.vars.domains.concat(window.GOVUK.vars.extraDomains)
}
},

Expand All @@ -94,8 +23,8 @@ window.GOVUK.loadAnalytics = {
window.GOVUK.analyticsVars.gaProperty = 'UA-UNSET'
window.GOVUK.analyticsVars.gaPropertyCrossDomain = 'UA-UNSET'

for (var i = 0; i < this.domains.length; i++) {
var current = this.domains[i]
for (var i = 0; i < window.GOVUK.vars.domains.length; i++) {
var current = window.GOVUK.vars.domains[i]
if (this.arrayContains(currentDomain, current.domains)) {
window.GOVUK.analyticsVars.gaProperty = current.gaProperty
window.GOVUK.analyticsVars.gaPropertyCrossDomain = current.gaPropertyCrossDomain
Expand Down Expand Up @@ -140,11 +69,11 @@ window.GOVUK.loadAnalytics = {
getEnvironment: function (currentDomain) {
// lots of dev domains, so simplify the matching process
if (currentDomain.match(/[a-zA-Z0-9.-]+dev\.gov\.uk/)) {
return this.domains[0]
return window.GOVUK.vars.domains[0]
} else {
for (var i = 0; i < this.domains.length; i++) {
if (this.arrayContains(currentDomain, this.domains[i].domains)) {
return this.domains[i]
for (var i = 0; i < window.GOVUK.vars.domains.length; i++) {
if (this.arrayContains(currentDomain, window.GOVUK.vars.domains[i].domains)) {
return window.GOVUK.vars.domains[i]
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions docs/load-analytics.md
Expand Up @@ -18,11 +18,12 @@ The Google Analytics 4 environment variables are:

## Passing extra options

If you wish to initialise the GA4 code on a new domain with different attributes, the code has been written to accept an array of additional values. Extra domains can be added before loading `dependencies.js` as below.
If you wish to initialise the GA4 code on a new domain with different attributes, the code has been written to accept an array of additional values. Extra domains can be added at the end of your code as shown.

```JavaScript
window.GOVUK = window.GOVUK || {}
window.GOVUK.analyticsGa4Domains = [
window.GOVUK.vars = window.GOVUK.vars || {}
window.GOVUK.vars.extraDomains = [
{
name: 'my-domain',
domains: ['not-a-real-domain.co.org.uk'],
Expand All @@ -34,6 +35,5 @@ window.GOVUK.analyticsGa4Domains = [
gaProperty: 'gaProperty', // for UA
gaPropertyCrossDomain: 'gaPropertyCrossDomain' // for UA (optional)
}
// add further into the array as required
]
```
35 changes: 20 additions & 15 deletions spec/javascripts/govuk_publishing_components/load-analytics.spec.js
Expand Up @@ -133,41 +133,46 @@ describe('Analytics loading', function () {
})

it('doesnt load GA4 variables if initialiseGA4 is set to false', function () {
window.GOVUK.loadAnalytics.domains[0].initialiseGA4 = false
window.GOVUK.vars.domains[0].initialiseGA4 = false
window.GOVUK.loadAnalytics.loadGa4('localhost')
expect(window.GOVUK.analyticsGa4.vars).toEqual(null)
window.GOVUK.loadAnalytics.domains[0].initialiseGA4 = true
window.GOVUK.vars.domains[0].initialiseGA4 = true
})

describe('when additional domain details are needed', function () {
var saveDomains

beforeEach(function () {
// use slice to clone the array, otherwise this 'saving' doesn't work
saveDomains = window.GOVUK.vars.domains.slice()
})

afterEach(function () {
delete window.GOVUK.analyticsGa4Domains
delete window.GOVUK.vars.extraDomains
window.GOVUK.vars.domains = saveDomains.slice()
})

it('defaults to the normal list when no extras are passed', function () {
var domains = window.GOVUK.loadAnalytics.domains
var expected = window.GOVUK.vars.domains.slice()
window.GOVUK.loadAnalytics.loadExtraDomains()
window.GOVUK.loadAnalytics.loadGa4()
expect(window.GOVUK.loadAnalytics.domains).toEqual(domains)
expect(window.GOVUK.vars.domains).toEqual(expected)
})

it('allows extra domains to be passed and appended to the existing list', function () {
var newDomain = {
var extra = {
name: 'test-domain',
domains: ['not-a-real-domain'],
initialiseGA4: true,
id: 'GTM-001'
}
window.GOVUK.analyticsGa4Domains = [newDomain]
window.GOVUK.vars.extraDomains = [extra]
expected = window.GOVUK.vars.domains.slice()
expected.push(extra)

window.GOVUK.loadAnalytics.loadExtraDomains()
expected = {
name: 'test-domain',
domains: ['not-a-real-domain'],
initialiseGA4: true,
id: 'GTM-001'
}
// the new domain gets appended to the end of the array
expect(window.GOVUK.loadAnalytics.domains[5]).toEqual(expected)
// the new domain should be appended to the end of the array
expect(window.GOVUK.vars.domains).toEqual(expected)

window.GOVUK.loadAnalytics.loadGa4('not-a-real-domain')
expect(window.GOVUK.analyticsGa4.vars.id).toEqual('GTM-001')
Expand Down

0 comments on commit c6d89cb

Please sign in to comment.