Sha256: c0b35751c221915cb4082ae568de5fb375a66eb7b3586433ead89a172149e04b

Contents?: true

Size: 1.35 KB

Versions: 71

Compression:

Stored size: 1.35 KB

Contents

window.GOVUK = window.GOVUK || {}
window.GOVUK.Modules = window.GOVUK.Modules || {};

(function (Modules) {
  'use strict'

  function Ga4AutoTracker (module) {
    this.module = module
    this.trackingTrigger = 'data-ga4-auto'
    this.PIIRemover = new window.GOVUK.analyticsGa4.PIIRemover()
  }

  Ga4AutoTracker.prototype.init = function () {
    var consentCookie = window.GOVUK.getConsentCookie()

    if (consentCookie && consentCookie.usage) {
      this.startModule()
    } else {
      this.start = this.startModule.bind(this)
      window.addEventListener('cookie-consent', this.start)
    }
  }

  Ga4AutoTracker.prototype.startModule = function () {
    this.sendEvent()
  }

  Ga4AutoTracker.prototype.sendEvent = function () {
    if (window.dataLayer) {
      window.removeEventListener('cookie-consent', this.start)
      try {
        var data = this.module.getAttribute(this.trackingTrigger)
        data = JSON.parse(data) || {}
      } catch (e) {
        // if there's a problem with the config, don't start the tracker
        console.error('GA4 configuration error: ' + e.message, window.location)
        return
      }
      data.text = this.PIIRemover.stripPIIWithOverride(data.text, true, true)
      window.GOVUK.analyticsGa4.core.applySchemaAndSendData(data, 'event_data')
    }
  }

  Modules.Ga4AutoTracker = Ga4AutoTracker
})(window.GOVUK.Modules)

Version data entries

71 entries across 71 versions & 1 rubygems

Version Path
govuk_publishing_components-45.7.0 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-auto-tracker.js
govuk_publishing_components-45.6.1 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-auto-tracker.js
govuk_publishing_components-45.6.0 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-auto-tracker.js
govuk_publishing_components-45.5.0 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-auto-tracker.js
govuk_publishing_components-45.4.1 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-auto-tracker.js
govuk_publishing_components-45.4.0 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-auto-tracker.js
govuk_publishing_components-45.3.0 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-auto-tracker.js
govuk_publishing_components-45.2.0 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-auto-tracker.js
govuk_publishing_components-45.1.0 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-auto-tracker.js
govuk_publishing_components-45.0.0 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-auto-tracker.js
govuk_publishing_components-44.11.0 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-auto-tracker.js
govuk_publishing_components-44.10.0 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-auto-tracker.js
govuk_publishing_components-44.9.1 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-auto-tracker.js
govuk_publishing_components-44.9.0 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-auto-tracker.js
govuk_publishing_components-44.8.0 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-auto-tracker.js
govuk_publishing_components-44.7.1 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-auto-tracker.js
govuk_publishing_components-44.7.0 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-auto-tracker.js
govuk_publishing_components-44.6.0 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-auto-tracker.js
govuk_publishing_components-44.5.0 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-auto-tracker.js
govuk_publishing_components-44.4.2 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-auto-tracker.js