Skip to content

Commit

Permalink
Add cross domain linking to main GA property
Browse files Browse the repository at this point in the history
- we already have cross domain linking configured for a second GA property. The way this works is that the analytics code is passed the property and the list of domains, and this is created and linking set up all in one go, using the addLinkedTrackerDomain function
- however, we now want to add cross domain tracking into the main property as well, for a different set of domains (well, one - for the new DI accounts system). We could call the same function, but by that point we've already created the property, so instead the code has been modified to check for the presence of linked domains for the primary property and initialise the linker in the function for creating the main property
- all of this is a bit of a spaghetti - ideally we'd have one clear function that creates a property and optionally configures cross domain linking, and we'd call that as many times as there are properties
- also tried to clarify some of the variable names in init.js, but as some of this is currently being called from accounts I don't want to introduce breaking changes
  • Loading branch information
andysellick committed Oct 21, 2021
1 parent 31e89c4 commit 78a0a63
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 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 cross domain linking to main GA property ([PR #2378](https://github.com/alphagov/govuk_publishing_components/pull/2378))
* Fix organisation logo size when printing ([PR #2371](https://github.com/alphagov/govuk_publishing_components/pull/2371)) PATCH

## 27.8.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,20 @@
fieldsObject = { cookieDomain: fieldsObject }
}

function setLinkedDomains () {
var domains = window.GOVUK.analyticsVars.primaryLinkedDomains
if (domains && domains.length > 0) {
sendToGa('require', 'linker')
sendToGa('linker:autoLink', domains)
}
}

configureProfile()
anonymizeIp()
disableAdFeatures()
stripTitlePII()
stripLocationPII()
setLinkedDomains()
}

GoogleAnalyticsUniversalTracker.load = function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ var analyticsInit = function () {

var analyticsVars = window.GOVUK.analyticsVars || false
if (analyticsVars) {
var gaProperty = window.GOVUK.analyticsVars.gaProperty || false
var gaPropertyCrossDomain = window.GOVUK.analyticsVars.gaPropertyCrossDomain || false
var linkedDomains = window.GOVUK.analyticsVars.linkedDomains || false
// the property naming convention here isn't consistent, but used in static and
// govuk-account-manager-prototype, so hard to change
var primaryGaProperty = window.GOVUK.analyticsVars.gaProperty || false

var crossDomainGaProperty = window.GOVUK.analyticsVars.gaPropertyCrossDomain || false
var crossDomainLinkedDomains = window.GOVUK.analyticsVars.linkedDomains || false
}

window.GOVUK.Analytics.checkDigitalIdentityConsent = function (location) {
Expand Down Expand Up @@ -39,7 +42,7 @@ var analyticsInit = function () {

// Disable analytics by default
// This will be reversed below, if the consent cookie says usage cookies are allowed
var disabler = 'ga-disable-' + gaProperty
var disabler = 'ga-disable-' + primaryGaProperty
window[disabler] = true

if (consentCookie && consentCookie.usage) {
Expand All @@ -48,23 +51,24 @@ var analyticsInit = function () {
// Load Google Analytics libraries
window.GOVUK.StaticAnalytics.load()

if (gaProperty) {
if (primaryGaProperty) {
// Use document.domain in dev, preview and staging so that tracking works
// Otherwise explicitly set the domain as www.gov.uk (and not gov.uk).
var cookieDomain = (document.domain === 'www.gov.uk') ? '.www.gov.uk' : document.domain

// Configure profiles, setup custom vars, track initial pageview
var analytics = new window.GOVUK.StaticAnalytics({
universalId: gaProperty,
universalId: primaryGaProperty,
cookieDomain: cookieDomain,
allowLinker: true
})

// Make interface public for virtual pageviews and events
window.GOVUK.analytics = analytics

if (linkedDomains && linkedDomains.length > 0) {
window.GOVUK.analytics.addLinkedTrackerDomain(gaPropertyCrossDomain, 'govuk', linkedDomains)
// set up linking of domains for cross domain ga property
if (crossDomainLinkedDomains && crossDomainLinkedDomains.length > 0) {
window.GOVUK.analytics.addLinkedTrackerDomain(crossDomainGaProperty, 'govuk', crossDomainLinkedDomains)
}
}
} else {
Expand Down

0 comments on commit 78a0a63

Please sign in to comment.