-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- select.js is used to trigger analytics events on select elements - it seems to be only used by finder-frontend - it has an option for extra options to be added to the tracking via a data attribute on the select option elements - this option does not appear to be in use by finder-frontend, and there was no test for it, so I've added one
- Loading branch information
1 parent
439a992
commit 219030e
Showing
2 changed files
with
108 additions
and
69 deletions.
There are no files selected for viewing
93 changes: 48 additions & 45 deletions
93
app/assets/javascripts/govuk_publishing_components/lib/select.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,55 @@ | ||
/* eslint-env jquery */ | ||
|
||
window.GOVUK = window.GOVUK || {} | ||
window.GOVUK.Modules = window.GOVUK.Modules || {}; | ||
|
||
(function (Modules) { | ||
'use strict' | ||
|
||
Modules.TrackSelectChange = function () { | ||
this.start = function (element) { | ||
element.change(function (e) { | ||
var selectedOption = $(this).find(':selected') | ||
var trackable = '[data-track-category][data-track-action]' | ||
|
||
if (selectedOption.is(trackable)) { | ||
fireTrackingChange(selectedOption) | ||
} | ||
}) | ||
|
||
function fireTrackingChange (element) { | ||
var options = { transport: 'beacon' } | ||
var category = element.attr('data-track-category') | ||
var action = element.attr('data-track-action') | ||
var label = element.attr('data-track-label') | ||
var value = element.attr('data-track-value') | ||
var dimension = element.attr('data-track-dimension') | ||
var dimensionIndex = element.attr('data-track-dimension-index') | ||
var extraOptions = element.attr('data-track-options') | ||
|
||
if (label) { | ||
options.label = label | ||
} | ||
|
||
if (value) { | ||
options.value = value | ||
} | ||
|
||
if (dimension && dimensionIndex) { | ||
options['dimension' + dimensionIndex] = dimension | ||
} | ||
|
||
if (extraOptions) { | ||
$.extend(options, JSON.parse(extraOptions)) | ||
} | ||
|
||
if (window.GOVUK.analytics && window.GOVUK.analytics.trackEvent) { | ||
window.GOVUK.analytics.trackEvent(category, action, options) | ||
} | ||
}; | ||
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters