Skip to content

Commit

Permalink
Merge pull request #1125 from alphagov/track-pageviews-and-events
Browse files Browse the repository at this point in the history
Track events and pageviews
  • Loading branch information
steventux authored Oct 8, 2018
2 parents 87ca42c + 0b5e3bc commit 6c1c3a9
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 17 deletions.
37 changes: 22 additions & 15 deletions app/assets/javascripts/modules/track-radio-group.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,25 @@ window.GOVUK.Modules = window.GOVUK.Modules || {};
}

function track (element, withHint) {
element.on('submit', function (event) {
element.on('submit', function (event) {

var options = { transport: 'beacon' }

var $submittedForm = $(event.target)

var $checkedOption = $submittedForm.find('input:checked')

var checkedValue = $checkedOption.val();
GOVUK.analytics.trackEvent('Radio button chosen', eventTrackingValue($checkedOption, withHint), options)

if (typeof checkedValue === 'undefined') {
checkedValue = 'submitted-without-choosing'
}

GOVUK.analytics.trackEvent('Radio button chosen', checkedValue + (withHint ? '-with-hint' : ''), options)

if (typeof element.attr('data-tracking-code') !== 'undefined') {
addCrossDomainTracking(element, $checkedOption, options)
if (typeof $submittedForm.attr('data-tracking-code') !== 'undefined') {
addCrossDomainTracking($submittedForm, $checkedOption, options, withHint)
}
})
}

function checkVerifyUser (element) {
$.ajax({
url: 'https://www.signin.service.gov.uk/hint',
url: 'https://www.signin.service.gov.uk/hint',
cache: false,
dataType: 'jsonp',
timeout: 3000
Expand All @@ -58,15 +52,28 @@ window.GOVUK.Modules = window.GOVUK.Modules || {};
}
}

function addCrossDomainTracking(element, $checkedOption, options) {
function eventTrackingValue(element, withHint) {
var value = element.val()

if (typeof value === 'undefined') {
value = 'submitted-without-choosing'
}

if (withHint) {
value += '-with-hint'
}
return value
}

function addCrossDomainTracking(element, $checkedOption, options, withHint) {
var code = element.attr('data-tracking-code')
var name = element.attr('data-tracking-name')
var url = $checkedOption.attr('data-tracking-url')
var hostname = $('<a>').prop('href', url).prop('hostname')
var eventOptions = $.extend({ 'trackerName': name }, options)

GOVUK.analytics.addLinkedTrackerDomain(code, name, hostname, false)
options['trackerName'] = name
GOVUK.analytics.trackEvent('Radio button chosen', $checkedOption.val(), options)
GOVUK.analytics.addLinkedTrackerDomain(code, name, hostname)
GOVUK.analytics.trackEvent('Radio button chosen', eventTrackingValue($checkedOption, withHint), eventOptions)
}
}
})(window, window.GOVUK);
36 changes: 34 additions & 2 deletions spec/javascripts/track-radio-group.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
var $ = window.jQuery

describe('A radio group tracker', function () {


var GOVUK = window.GOVUK
var tracker
var element

GOVUK.analytics = GOVUK.analytics || {}
GOVUK.analytics.trackEvent = function () {}
GOVUK.analytics.addLinkedTrackerDomain = function () {}

beforeEach(function () {
spyOn(GOVUK.analytics, 'trackEvent')
Expand Down Expand Up @@ -80,7 +80,7 @@ describe('A radio group tracker', function () {
)
element.find('input[value="govuk-verify"]').trigger('click')
element.find('form').trigger('submit')

expect(GOVUK.analytics.trackEvent).toHaveBeenCalledWith(
'Radio button chosen', 'govuk-verify', { transport: 'beacon' }
)
Expand Down Expand Up @@ -150,4 +150,36 @@ describe('A radio group tracker', function () {
'Radio button chosen', 'submitted-without-choosing', { transport: 'beacon' }
)
})

describe('cross domain tracking enabled', function () {
var $form, $radioInput

beforeEach(function () {
tracker.trackVerifyUser(element, { status: 'OK', value: true })

spyOn(GOVUK.analytics, 'addLinkedTrackerDomain')

var $form = element.find('form')
$form.attr('data-tracking-code', 'UA-xxxxxx')
$form.attr('data-tracking-name', 'testTracker')

var $radioInput = element.find('input[value="govuk-verify"]')
$radioInput.attr('data-tracking-url', 'https://test.service.gov.uk')

$radioInput.trigger('click')
$form.trigger('submit')
})

it('adds a linked tracker', function () {
expect(GOVUK.analytics.addLinkedTrackerDomain).toHaveBeenCalledWith(
'UA-xxxxxx', 'testTracker', 'test.service.gov.uk'
)
})

it('sends an event to the linked tracker', function() {
expect(GOVUK.analytics.trackEvent).toHaveBeenCalledWith(
'Radio button chosen', 'govuk-verify-with-hint', { trackerName: 'testTracker', transport: 'beacon' }
)
})
})
})

0 comments on commit 6c1c3a9

Please sign in to comment.