Sha256: 119a7432f26d5f169572bfa0de7305c2891da1e2580c4721807e2050113a67a6

Contents?: true

Size: 1.3 KB

Versions: 12

Compression:

Stored size: 1.3 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.settings) {
      this.startModule()
    } else {
      this.startModule = this.startModule.bind(this)
      window.addEventListener('cookie-consent', this.startModule)
    }
  }

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

  Ga4AutoTracker.prototype.sendEvent = function () {
    if (window.dataLayer) {
      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

12 entries across 12 versions & 1 rubygems

Version Path
govuk_publishing_components-37.2.3 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-auto-tracker.js
govuk_publishing_components-37.2.2 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-auto-tracker.js
govuk_publishing_components-37.2.1 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-auto-tracker.js
govuk_publishing_components-37.2.0 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-auto-tracker.js
govuk_publishing_components-37.1.1 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-auto-tracker.js
govuk_publishing_components-37.1.0 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-auto-tracker.js
govuk_publishing_components-37.0.0 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-auto-tracker.js
govuk_publishing_components-36.1.0 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-auto-tracker.js
govuk_publishing_components-36.0.3 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-auto-tracker.js
govuk_publishing_components-36.0.2 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-auto-tracker.js
govuk_publishing_components-36.0.1 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-auto-tracker.js
govuk_publishing_components-36.0.0 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-auto-tracker.js