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 %>