From 9900c65292acba4644e035d5f1ebf68ade2790b3 Mon Sep 17 00:00:00 2001 From: Andy Sellick Date: Wed, 10 Nov 2021 12:08:03 +0000 Subject: [PATCH 1/3] Convert set ga code to our module format - uses our standard module pattern for this code - means we can remove the manual initialisation in application.js - and add the module name to the form where the code is used --- app/assets/javascripts/application.js | 6 ------ .../javascripts/set-ga-client-id-on-form.js | 19 ++++++++++--------- .../service_sign_in/_choose_sign_in.html.erb | 2 +- .../set-ga-client-id-on-form.spec.js | 3 ++- 4 files changed, 13 insertions(+), 17 deletions(-) diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index fc6dece59..a4d574fd8 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -11,9 +11,3 @@ //= 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/set-ga-client-id-on-form.js b/app/assets/javascripts/set-ga-client-id-on-form.js index dd61d789b..250777bb7 100644 --- a/app/assets/javascripts/set-ga-client-id-on-form.js +++ b/app/assets/javascripts/set-ga-client-id-on-form.js @@ -1,12 +1,13 @@ -(function () { - 'use strict' +window.GOVUK = window.GOVUK || {} +window.GOVUK.Modules = window.GOVUK.Modules || {}; - window.GOVUK = window.GOVUK || {} - var GOVUK = window.GOVUK - - function SetGaClientIdOnForm (form) { - if (!form || !window.ga) { return } +(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') @@ -14,5 +15,5 @@ }) } - GOVUK.SetGaClientIdOnForm = SetGaClientIdOnForm -})(window, window.GOVUK) + Modules.SetGaClientIdOnForm = SetGaClientIdOnForm +})(window.GOVUK.Modules) 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..bcd39d269 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,7 +14,7 @@ <% 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 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..f74cf06d2 100644 --- a/spec/javascripts/set-ga-client-id-on-form.spec.js +++ b/spec/javascripts/set-ga-client-id-on-form.spec.js @@ -11,7 +11,8 @@ describe('SetGaClientIdOnForm', function () { form = $( '
' ) - new GOVUK.SetGaClientIdOnForm(form[0]) // eslint-disable-line no-new + var setId = new GOVUK.Modules.SetGaClientIdOnForm(form[0]) // eslint-disable-line no-new + setId.init() }) it('sets the _ga client id as a query param on the form action', function () { From e0851a924cb89c96612ac5709d74c7e2f3583708 Mon Sep 17 00:00:00 2001 From: Andy Sellick Date: Wed, 10 Nov 2021 12:13:34 +0000 Subject: [PATCH 2/3] Move set ga JS into modules now its a module --- app/assets/javascripts/application.js | 1 - app/assets/javascripts/{ => modules}/set-ga-client-id-on-form.js | 0 2 files changed, 1 deletion(-) rename app/assets/javascripts/{ => modules}/set-ga-client-id-on-form.js (100%) diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index a4d574fd8..83ae86502 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -10,4 +10,3 @@ //= require govuk_publishing_components/components/step-by-step-nav //= require_tree ./modules -//= require set-ga-client-id-on-form diff --git a/app/assets/javascripts/set-ga-client-id-on-form.js b/app/assets/javascripts/modules/set-ga-client-id-on-form.js similarity index 100% rename from app/assets/javascripts/set-ga-client-id-on-form.js rename to app/assets/javascripts/modules/set-ga-client-id-on-form.js From e028b8d8da03d3f74a958abdff87171b63b64bcb Mon Sep 17 00:00:00 2001 From: Andy Sellick Date: Wed, 10 Nov 2021 12:34:59 +0000 Subject: [PATCH 3/3] Update tests and last tweaks - no need for linting rule anymore - class for initialisation isn't used anymore either --- .../content_items/service_sign_in/_choose_sign_in.html.erb | 3 +-- spec/javascripts/set-ga-client-id-on-form.spec.js | 6 ++---- test/integration/service_sign_in/choose_sign_in_test.rb | 2 +- 3 files changed, 4 insertions(+), 7 deletions(-) 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 bcd39d269..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 @@ -21,8 +21,7 @@ %> <%= 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 f74cf06d2..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,8 @@ describe('SetGaClientIdOnForm', function () { var form beforeEach(function () { - form = $( - '' - ) - var setId = new GOVUK.Modules.SetGaClientIdOnForm(form[0]) // eslint-disable-line no-new + form = $('
') + var setId = new GOVUK.Modules.SetGaClientIdOnForm(form[0]) setId.init() }) 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']")