Sha256: cbe7373a9346c98279fad8487aca0094abe1e0e0e415fd3596e59eb104d76936

Contents?: true

Size: 1.25 KB

Versions: 5

Compression:

Stored size: 1.25 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'
  }

  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
      }

      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

5 entries across 5 versions & 1 rubygems

Version Path
govuk_publishing_components-34.13.0 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-auto-tracker.js
govuk_publishing_components-34.12.0 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-auto-tracker.js
govuk_publishing_components-34.11.0 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-auto-tracker.js
govuk_publishing_components-34.10.1 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-auto-tracker.js
govuk_publishing_components-34.10.0 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-auto-tracker.js