Sha256: 36b7e69926c44d8fb494805f44b812dfef08db50538747f30c0703f8fe36bde2

Contents?: true

Size: 1.86 KB

Versions: 143

Compression:

Stored size: 1.86 KB

Contents

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

(function (Modules) {
  function TrackSelectChange () { }

  TrackSelectChange.prototype.start = function ($module) {
    this.$module = $module[0]
    this.$module.trackChange = this.trackChange.bind(this)
    this.$module.fireTrackingChange = this.fireTrackingChange.bind(this)
    this.$module.addEventListener('change', this.trackChange)
  }

  TrackSelectChange.prototype.trackChange = function () {
    var selectedOption = this.options[this.selectedIndex]

    if (selectedOption.hasAttribute('data-track-category') && selectedOption.hasAttribute('data-track-action')) {
      this.fireTrackingChange(selectedOption)
    }
  }

  TrackSelectChange.prototype.fireTrackingChange = function (selectedOption) {
    var options = { transport: 'beacon' }
    var category = selectedOption.getAttribute('data-track-category')
    var action = selectedOption.getAttribute('data-track-action')
    var label = selectedOption.getAttribute('data-track-label')
    var value = selectedOption.getAttribute('data-track-value')
    var dimension = selectedOption.getAttribute('data-track-dimension')
    var dimensionIndex = selectedOption.getAttribute('data-track-dimension-index')
    var extraOptions = selectedOption.getAttribute('data-track-options')

    if (label) {
      options.label = label
    }

    if (value) {
      options.value = value
    }

    if (dimension && dimensionIndex) {
      options['dimension' + dimensionIndex] = dimension
    }

    if (extraOptions) {
      extraOptions = JSON.parse(extraOptions)
      for (var k in extraOptions) options[k] = extraOptions[k]
    }

    if (window.GOVUK.analytics && window.GOVUK.analytics.trackEvent) {
      window.GOVUK.analytics.trackEvent(category, action, options)
    }
  }

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

Version data entries

143 entries across 143 versions & 1 rubygems

Version Path
govuk_publishing_components-24.1.1 app/assets/javascripts/govuk_publishing_components/lib/select.js
govuk_publishing_components-24.1.0 app/assets/javascripts/govuk_publishing_components/lib/select.js
govuk_publishing_components-24.0.0 app/assets/javascripts/govuk_publishing_components/lib/select.js
govuk_publishing_components-23.15.0 app/assets/javascripts/govuk_publishing_components/lib/select.js
govuk_publishing_components-23.14.0 app/assets/javascripts/govuk_publishing_components/lib/select.js
govuk_publishing_components-23.13.1 app/assets/javascripts/govuk_publishing_components/lib/select.js
govuk_publishing_components-23.13.0 app/assets/javascripts/govuk_publishing_components/lib/select.js
govuk_publishing_components-23.12.3 app/assets/javascripts/govuk_publishing_components/lib/select.js
govuk_publishing_components-23.12.2 app/assets/javascripts/govuk_publishing_components/lib/select.js
govuk_publishing_components-23.12.1 app/assets/javascripts/govuk_publishing_components/lib/select.js
govuk_publishing_components-23.12.0 app/assets/javascripts/govuk_publishing_components/lib/select.js
govuk_publishing_components-23.11.1 app/assets/javascripts/govuk_publishing_components/lib/select.js
govuk_publishing_components-23.11.0 app/assets/javascripts/govuk_publishing_components/lib/select.js
govuk_publishing_components-23.10.2 app/assets/javascripts/govuk_publishing_components/lib/select.js
govuk_publishing_components-23.10.1 app/assets/javascripts/govuk_publishing_components/lib/select.js
govuk_publishing_components-23.10.0 app/assets/javascripts/govuk_publishing_components/lib/select.js
govuk_publishing_components-23.9.2 app/assets/javascripts/govuk_publishing_components/lib/select.js
govuk_publishing_components-23.9.1 app/assets/javascripts/govuk_publishing_components/lib/select.js
govuk_publishing_components-23.9.0 app/assets/javascripts/govuk_publishing_components/lib/select.js
govuk_publishing_components-23.8.0 app/assets/javascripts/govuk_publishing_components/lib/select.js