Sha256: 0f9ad15f8601b8209221dfeea528541eca1f3879c93680bd87fc22ada99ee353

Contents?: true

Size: 1.3 KB

Versions: 1

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.usage) {
      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

1 entries across 1 versions & 1 rubygems

Version Path
govuk_publishing_components-37.2.4 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-auto-tracker.js