diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 617a32a54..922845865 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -1,11 +1,21 @@ //= require_tree ./modules //= require govuk_publishing_components/all_components -//= require set-ga-client-id-on-form +//= require set-ga-client-id-on-url-in-element jQuery(function ($) { var $form = $('.js-service-sign-in-form') - if ($form.length) { - new GOVUK.SetGaClientIdOnForm({ $form: $form }) + new GOVUK.SetGaClientIdOnUrlInElement({ + $linkedElement: $form, + attribute: 'action' + }) + } + + if ($(location).attr('pathname') === '/ask') { + var $link = $('[href="https://www.smartsurvey.co.uk/ss/govuk-coronavirus-ask/"]') + new GOVUK.SetGaClientIdOnUrlInElement({ + $linkedElement: $link, + attribute: 'href' + }) } }) diff --git a/app/assets/javascripts/set-ga-client-id-on-form.js b/app/assets/javascripts/set-ga-client-id-on-form.js deleted file mode 100644 index 9dce3697d..000000000 --- a/app/assets/javascripts/set-ga-client-id-on-form.js +++ /dev/null @@ -1,20 +0,0 @@ -(function () { - 'use strict' - - window.GOVUK = window.GOVUK || {} - var GOVUK = window.GOVUK - - function SetGaClientIdOnForm (options) { - if (!options.$form || !window.ga) { return } - - var form = options.$form - - window.ga(function(tracker) { - var clientId = tracker.get('clientId') - var action = form.attr('action') - form.attr('action', action + "?_ga=" + clientId) - }); - } - - GOVUK.SetGaClientIdOnForm = SetGaClientIdOnForm -})(window, window.GOVUK); diff --git a/app/assets/javascripts/set-ga-client-id-on-url-in-element.js b/app/assets/javascripts/set-ga-client-id-on-url-in-element.js new file mode 100644 index 000000000..3f0516193 --- /dev/null +++ b/app/assets/javascripts/set-ga-client-id-on-url-in-element.js @@ -0,0 +1,23 @@ +(function () { + 'use strict' + + window.GOVUK = window.GOVUK || {} + var GOVUK = window.GOVUK + + function SetGaClientIdOnUrlInElement(options) { + if (!options.$linkedElement || !options.attribute || !window.ga) { + return + } + + var linkedElement = options.$linkedElement + var attribute = options.attribute + + window.ga(function (tracker) { + var clientId = tracker.get('clientId') + var attributeUrl = linkedElement.attr(attribute) + linkedElement.attr(attribute, attributeUrl + "?_ga=" + clientId) + }) + } + + GOVUK.SetGaClientIdOnUrlInElement = SetGaClientIdOnUrlInElement +})(window, window.GOVUK); diff --git a/spec/javascripts/set-ga-client-id-on-form.spec.js b/spec/javascripts/set-ga-client-id-on-form.spec.js deleted file mode 100644 index 773e25953..000000000 --- a/spec/javascripts/set-ga-client-id-on-form.spec.js +++ /dev/null @@ -1,23 +0,0 @@ -/* global describe beforeEach it expect */ - -var $ = window.jQuery - -describe('SetGaClientIdOnForm', function () { - - var GOVUK = window.GOVUK - var tracker = { clientId: 'clientId' } - tracker.get = function(arg) { return this[arg] } - window.ga = function(callback) { callback(tracker) } - var form - - beforeEach(function () { - form = $( - '
' - ) - setter = new GOVUK.SetGaClientIdOnForm({ $form: form }) - }) - - it('sets the _ga client id as a query param on the form action', function () { - expect(form.attr('action')).toBe('/endpoint?_ga=clientId') - }) -}) diff --git a/spec/javascripts/set-ga-client-id-on-url-in-element.spec.js b/spec/javascripts/set-ga-client-id-on-url-in-element.spec.js new file mode 100644 index 000000000..5cc2ebbe9 --- /dev/null +++ b/spec/javascripts/set-ga-client-id-on-url-in-element.spec.js @@ -0,0 +1,50 @@ +/* global describe beforeEach it expect */ + +var $ = window.jQuery + +describe('SetGaClientIdOnUrlInElement', function () { + var GOVUK = window.GOVUK + var tracker = { clientId: 'clientId' } + tracker.get = function(arg) { return this[arg] } + window.ga = function(callback) { callback(tracker) } + + describe('for a Start Button link', function() { + var linkedElement + var attribute = 'href' + + beforeEach(function () { + linkedElement = $( + 'Start now' + ) + + setter = new GOVUK.SetGaClientIdOnUrlInElement({ + $linkedElement: linkedElement, + attribute: attribute + }) + }) + + it('sets the _ga client id as a query param on the linked element action', function () { + expect(linkedElement.attr(attribute)).toBe('https://some-service.batman.co.uk?_ga=clientId') + }) + }) + + describe('for a form action', function() { + var form + var attribute = 'action' + + beforeEach(function () { + form = $( + '' + ) + + setter = new GOVUK.SetGaClientIdOnUrlInElement({ + $linkedElement: form, + attribute: attribute + }) + }) + + it('sets the _ga client id as a query param on the form action', function () { + expect(form.attr('action')).toBe('/endpoint?_ga=clientId') + }) + }) +})