Sha256: 06783148eca84ec001851832b7fd7e0a9b9c4cb07a7e2e1d5fe80b13cb8c1a63

Contents?: true

Size: 1.4 KB

Versions: 4

Compression:

Stored size: 1.4 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)
      var schemas = new window.GOVUK.analyticsGa4.Schemas()
      var schema = schemas.mergeProperties(data, 'event_data')

      window.GOVUK.analyticsGa4.core.sendData(schema)
    }
  }

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
govuk_publishing_components-35.23.0 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-auto-tracker.js
govuk_publishing_components-35.22.0 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-auto-tracker.js
govuk_publishing_components-35.21.4 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-auto-tracker.js
govuk_publishing_components-35.21.3 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-auto-tracker.js