diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 9cf647db4..fc6dece59 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -12,10 +12,8 @@ //= require_tree ./modules //= require set-ga-client-id-on-form -jQuery(function ($) { - var $form = $('.js-service-sign-in-form') +var form = document.querySelector('.js-service-sign-in-form') - if ($form.length) { - new GOVUK.SetGaClientIdOnForm({ $form: $form }) // eslint-disable-line no-new - } -}) +if (form) { + new GOVUK.SetGaClientIdOnForm(form) // eslint-disable-line no-new +} diff --git a/app/assets/javascripts/set-ga-client-id-on-form.js b/app/assets/javascripts/set-ga-client-id-on-form.js index 7998804eb..dd61d789b 100644 --- a/app/assets/javascripts/set-ga-client-id-on-form.js +++ b/app/assets/javascripts/set-ga-client-id-on-form.js @@ -4,15 +4,13 @@ window.GOVUK = window.GOVUK || {} var GOVUK = window.GOVUK - function SetGaClientIdOnForm (options) { - if (!options.$form || !window.ga) { return } - - var form = options.$form + function SetGaClientIdOnForm (form) { + if (!form || !window.ga) { return } window.ga(function (tracker) { var clientId = tracker.get('clientId') - var action = form.attr('action') - form.attr('action', action + '?_ga=' + clientId) + var action = form.getAttribute('action') + form.setAttribute('action', action + '?_ga=' + clientId) }) } diff --git a/spec/javascripts/set-ga-client-id-on-form.spec.js b/spec/javascripts/set-ga-client-id-on-form.spec.js index c4a605463..b6c6c8718 100644 --- a/spec/javascripts/set-ga-client-id-on-form.spec.js +++ b/spec/javascripts/set-ga-client-id-on-form.spec.js @@ -2,16 +2,16 @@ 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 mockTracker = { clientId: 'clientId' } + mockTracker.get = function (arg) { return this[arg] } + window.ga = function (callback) { callback(mockTracker) } var form beforeEach(function () { form = $( '
' ) - new GOVUK.SetGaClientIdOnForm({ $form: form }) // eslint-disable-line no-new + new GOVUK.SetGaClientIdOnForm(form[0]) // eslint-disable-line no-new }) it('sets the _ga client id as a query param on the form action', function () {