diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index fc6dece59..83ae86502 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -10,10 +10,3 @@ //= require govuk_publishing_components/components/step-by-step-nav //= require_tree ./modules -//= require set-ga-client-id-on-form - -var form = document.querySelector('.js-service-sign-in-form') - -if (form) { - new GOVUK.SetGaClientIdOnForm(form) // eslint-disable-line no-new -} diff --git a/app/assets/javascripts/modules/set-ga-client-id-on-form.js b/app/assets/javascripts/modules/set-ga-client-id-on-form.js new file mode 100644 index 000000000..250777bb7 --- /dev/null +++ b/app/assets/javascripts/modules/set-ga-client-id-on-form.js @@ -0,0 +1,19 @@ +window.GOVUK = window.GOVUK || {} +window.GOVUK.Modules = window.GOVUK.Modules || {}; + +(function (Modules) { + function SetGaClientIdOnForm ($module) { + this.$module = $module + } + + SetGaClientIdOnForm.prototype.init = function () { + var form = this.$module + window.ga(function (tracker) { + var clientId = tracker.get('clientId') + var action = form.getAttribute('action') + form.setAttribute('action', action + '?_ga=' + clientId) + }) + } + + Modules.SetGaClientIdOnForm = SetGaClientIdOnForm +})(window.GOVUK.Modules) 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 dd61d789b..000000000 --- a/app/assets/javascripts/set-ga-client-id-on-form.js +++ /dev/null @@ -1,18 +0,0 @@ -(function () { - 'use strict' - - window.GOVUK = window.GOVUK || {} - var GOVUK = window.GOVUK - - function SetGaClientIdOnForm (form) { - if (!form || !window.ga) { return } - - window.ga(function (tracker) { - var clientId = tracker.get('clientId') - var action = form.getAttribute('action') - form.setAttribute('action', action + '?_ga=' + clientId) - }) - } - - GOVUK.SetGaClientIdOnForm = SetGaClientIdOnForm -})(window, window.GOVUK) diff --git a/app/views/content_items/service_sign_in/_choose_sign_in.html.erb b/app/views/content_items/service_sign_in/_choose_sign_in.html.erb index af9ddc701..58245b984 100644 --- a/app/views/content_items/service_sign_in/_choose_sign_in.html.erb +++ b/app/views/content_items/service_sign_in/_choose_sign_in.html.erb @@ -14,15 +14,14 @@ <% end %> <% - data_attrs = { module: "track-radio-group" } + data_attrs = { module: "track-radio-group set-ga-client-id-on-form" } data_attrs["tracking-code"] = @content_item.tracking_code if @content_item.tracking_code data_attrs["tracking-domain"] = @content_item.tracking_domain if @content_item.tracking_domain data_attrs["tracking-name"] = @content_item.tracking_name if @content_item.tracking_name %> <%= form_tag({controller: 'content_items', action: 'service_sign_in_options'}, method: "post", - data: data_attrs, - class: 'js-service-sign-in-form') do %> + data: data_attrs) do %> <% legend_text = render 'govuk_publishing_components/components/title', title: @content_item.title %> <%= render "govuk_publishing_components/components/fieldset", legend_text: legend_text do %>
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 b6c6c8718..69272fefa 100644 --- a/spec/javascripts/set-ga-client-id-on-form.spec.js +++ b/spec/javascripts/set-ga-client-id-on-form.spec.js @@ -8,10 +8,9 @@ describe('SetGaClientIdOnForm', function () { var form beforeEach(function () { - form = $( - '
' - ) - new GOVUK.SetGaClientIdOnForm(form[0]) // eslint-disable-line no-new + form = $('
') + var setId = new GOVUK.Modules.SetGaClientIdOnForm(form[0]) + setId.init() }) it('sets the _ga client id as a query param on the form action', function () { diff --git a/test/integration/service_sign_in/choose_sign_in_test.rb b/test/integration/service_sign_in/choose_sign_in_test.rb index ea6561dca..e09b24bbc 100644 --- a/test/integration/service_sign_in/choose_sign_in_test.rb +++ b/test/integration/service_sign_in/choose_sign_in_test.rb @@ -20,7 +20,7 @@ class ChooseSignInTest < ActionDispatch::IntegrationTest assert_not page.has_css?("#proposition-menu") assert page.has_css?('.gem-c-back-link[href="/log-in-file-self-assessment-tax-return"]', text: "Back") - assert page.has_css?('form[data-module="track-radio-group"]') + assert page.has_css?('form[data-module="track-radio-group set-ga-client-id-on-form"]') assert page.has_css?("form[data-tracking-code='UA-xxxxxx']") assert page.has_css?("form[data-tracking-domain='tax.service.gov.uk']") assert page.has_css?("form[data-tracking-name='somethingClicky']")