Sha256: e5d841acfdd5b78db25ebf6e37a6b0fa6f4a5b3138d4c7526681f0825dfdf5a4

Contents?: true

Size: 1.55 KB

Versions: 5

Compression:

Stored size: 1.55 KB

Contents

window.GOVUK = window.GOVUK || {}
window.GOVUK.analyticsGa4 = window.GOVUK.analyticsGa4 || {}

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)
    var attachmentLinkData = [
      { key: 'data-module', value: 'ga4-link-tracker' },
      { key: 'data-ga4-track-links-only', value: '' },
      { key: 'data-ga4-link', value: JSON.stringify({ event_name: 'navigation', type: 'html attachment' }) }]
    window.GOVUK.analyticsGa4.core.trackFunctions.addAttributesToElements('[data-ga4-attachment-link]', attachmentLinkData)
    window.GOVUK.analyticsGa4.core.load()

    var analyticsModules = window.GOVUK.analyticsGa4.analyticsModules
    for (var property in analyticsModules) {
      var module = analyticsModules[property]
      if (typeof module.init === 'function') {
        try {
          module.init()
        } catch (e) {
          // if there's a problem with the module, catch the error to allow other modules to start
          console.warn('Error starting analytics module ' + property + ': ' + e.message, window.location)
        }
      }
    }
  } else {
    window.addEventListener('cookie-consent', window.GOVUK.analyticsGa4.init)
  }
}

window.GOVUK.analyticsGa4.init = initFunction

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
govuk_publishing_components-35.3.4 app/assets/javascripts/govuk_publishing_components/analytics-ga4/init-ga4.js
govuk_publishing_components-35.3.3 app/assets/javascripts/govuk_publishing_components/analytics-ga4/init-ga4.js
govuk_publishing_components-35.3.2 app/assets/javascripts/govuk_publishing_components/analytics-ga4/init-ga4.js
govuk_publishing_components-35.3.1 app/assets/javascripts/govuk_publishing_components/analytics-ga4/init-ga4.js
govuk_publishing_components-35.3.0 app/assets/javascripts/govuk_publishing_components/analytics-ga4/init-ga4.js