diff --git a/CHANGELOG.md b/CHANGELOG.md index f1731cbe90..b41c012924 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/app/assets/javascripts/govuk_publishing_components/analytics/google-analytics-universal-tracker.js b/app/assets/javascripts/govuk_publishing_components/analytics/google-analytics-universal-tracker.js index 6107e366c9..cf9d9fd543 100644 --- a/app/assets/javascripts/govuk_publishing_components/analytics/google-analytics-universal-tracker.js +++ b/app/assets/javascripts/govuk_publishing_components/analytics/google-analytics-universal-tracker.js @@ -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 () { diff --git a/app/assets/javascripts/govuk_publishing_components/analytics/init.js b/app/assets/javascripts/govuk_publishing_components/analytics/init.js index a89a7324be..45b48ba045 100644 --- a/app/assets/javascripts/govuk_publishing_components/analytics/init.js +++ b/app/assets/javascripts/govuk_publishing_components/analytics/init.js @@ -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) { @@ -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) { @@ -48,14 +51,14 @@ 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 }) @@ -63,8 +66,9 @@ var analyticsInit = function () { // 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 {