From 5b4730d3079e693ebbdb118e8894b4be6b7f6804 Mon Sep 17 00:00:00 2001 From: Kelvin Gan Date: Thu, 23 Apr 2020 14:25:46 +0100 Subject: [PATCH 1/5] Add GA params to /ask survey link --- app/assets/javascripts/application.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 617a32a54..e76e57078 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -8,4 +8,12 @@ jQuery(function ($) { if ($form.length) { new GOVUK.SetGaClientIdOnForm({ $form: $form }) } + + if ($(location).attr('pathname') === '/ask') { + var $link = $('[href="https://www.smartsurvey.co.uk/ss/govuk-coronavirus-ask/"]') + if ($link) { + var href = $link.attr('href') + $link.attr('href', GOVUK.userSurveys.addParamsToURL(href)) + } + } }) From 0ee42d30476ab2df21a65912344ed3b371bd3b14 Mon Sep 17 00:00:00 2001 From: Kelvin Gan Date: Thu, 23 Apr 2020 16:33:54 +0100 Subject: [PATCH 2/5] Abstract adding GA params to links --- app/assets/javascripts/application.js | 7 ++----- .../javascripts/set-ga-client-id-on-link.js | 20 +++++++++++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 app/assets/javascripts/set-ga-client-id-on-link.js diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index e76e57078..5d02a1248 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -1,19 +1,16 @@ //= require_tree ./modules //= require govuk_publishing_components/all_components //= require set-ga-client-id-on-form +//= require set-ga-client-id-on-link jQuery(function ($) { var $form = $('.js-service-sign-in-form') - if ($form.length) { new GOVUK.SetGaClientIdOnForm({ $form: $form }) } if ($(location).attr('pathname') === '/ask') { var $link = $('[href="https://www.smartsurvey.co.uk/ss/govuk-coronavirus-ask/"]') - if ($link) { - var href = $link.attr('href') - $link.attr('href', GOVUK.userSurveys.addParamsToURL(href)) - } + new GOVUK.SetGaClientIdOnLink({ $link: $link }) } }) diff --git a/app/assets/javascripts/set-ga-client-id-on-link.js b/app/assets/javascripts/set-ga-client-id-on-link.js new file mode 100644 index 000000000..16274e9a3 --- /dev/null +++ b/app/assets/javascripts/set-ga-client-id-on-link.js @@ -0,0 +1,20 @@ +(function() { + 'use strict' + + window.GOVUK = window.GOVUK || {} + var GOVUK = window.GOVUK + + function SetGaClientIdOnLink (options) { + if (!options.$link || !window.ga) { return } + + var link = options.$link + + window.ga(function(tracker) { + var clientId = tracker.get('clientId') + var href = link.attr('href') + link.attr('href', href + "?_ga=" + clientId) + }) + } + + GOVUK.SetGaClientIdOnLink = SetGaClientIdOnLink +})(window, window.GOVUK); From b0daccb60692d4b6d6395f7505f2a240679bcd03 Mon Sep 17 00:00:00 2001 From: Kelvin Gan Date: Fri, 24 Apr 2020 12:43:39 +0100 Subject: [PATCH 3/5] Add test for SetGaClientIdOnLink This clearly can be de-duplicated between SetGaClientIdOnForm, which will come in future commit. --- .../set-ga-client-id-on-link.spec.js | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 spec/javascripts/set-ga-client-id-on-link.spec.js diff --git a/spec/javascripts/set-ga-client-id-on-link.spec.js b/spec/javascripts/set-ga-client-id-on-link.spec.js new file mode 100644 index 000000000..f03fad286 --- /dev/null +++ b/spec/javascripts/set-ga-client-id-on-link.spec.js @@ -0,0 +1,25 @@ +/* global describe beforeEach it expect */ + +var $ = window.jQuery + +describe('SetGaClientIdOnLink', function () { + + var GOVUK = window.GOVUK + var tracker = { clientId: 'clientId' } + tracker.get = function(arg) { return this[arg] } + window.ga = function(callback) { callback(tracker) } + var link + + beforeEach(function () { + link = $( + 'Start now' + ) + setter = new GOVUK.SetGaClientIdOnLink({ + $link: link + }) + }) + + it('sets the _ga client id as a query param on the link action', function () { + expect(link.attr('href')).toBe('https://some-service.batman.co.uk?_ga=clientId') + }) +}) From 316dad63ae757fceba9ae67ba3f38c64a7207fa4 Mon Sep 17 00:00:00 2001 From: Kelvin Gan Date: Fri, 24 Apr 2020 14:34:01 +0100 Subject: [PATCH 4/5] Refactor duplicated code for adding GA params --- app/assets/javascripts/application.js | 13 +++-- .../javascripts/set-ga-client-id-on-form.js | 20 -------- .../javascripts/set-ga-client-id-on-link.js | 20 -------- .../set-ga-client-id-on-url-in-element.js | 23 +++++++++ .../set-ga-client-id-on-form.spec.js | 23 --------- .../set-ga-client-id-on-link.spec.js | 25 ---------- ...set-ga-client-id-on-url-in-element.spec.js | 50 +++++++++++++++++++ 7 files changed, 82 insertions(+), 92 deletions(-) delete mode 100644 app/assets/javascripts/set-ga-client-id-on-form.js delete mode 100644 app/assets/javascripts/set-ga-client-id-on-link.js create mode 100644 app/assets/javascripts/set-ga-client-id-on-url-in-element.js delete mode 100644 spec/javascripts/set-ga-client-id-on-form.spec.js delete mode 100644 spec/javascripts/set-ga-client-id-on-link.spec.js create mode 100644 spec/javascripts/set-ga-client-id-on-url-in-element.spec.js diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 5d02a1248..922845865 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -1,16 +1,21 @@ //= require_tree ./modules //= require govuk_publishing_components/all_components -//= require set-ga-client-id-on-form -//= require set-ga-client-id-on-link +//= 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.SetGaClientIdOnLink({ $link: $link }) + 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-link.js b/app/assets/javascripts/set-ga-client-id-on-link.js deleted file mode 100644 index 16274e9a3..000000000 --- a/app/assets/javascripts/set-ga-client-id-on-link.js +++ /dev/null @@ -1,20 +0,0 @@ -(function() { - 'use strict' - - window.GOVUK = window.GOVUK || {} - var GOVUK = window.GOVUK - - function SetGaClientIdOnLink (options) { - if (!options.$link || !window.ga) { return } - - var link = options.$link - - window.ga(function(tracker) { - var clientId = tracker.get('clientId') - var href = link.attr('href') - link.attr('href', href + "?_ga=" + clientId) - }) - } - - GOVUK.SetGaClientIdOnLink = SetGaClientIdOnLink -})(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..7b0d92f6e --- /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 href = linkedElement.attr(attribute) + linkedElement.attr(attribute, href + "?_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-link.spec.js b/spec/javascripts/set-ga-client-id-on-link.spec.js deleted file mode 100644 index f03fad286..000000000 --- a/spec/javascripts/set-ga-client-id-on-link.spec.js +++ /dev/null @@ -1,25 +0,0 @@ -/* global describe beforeEach it expect */ - -var $ = window.jQuery - -describe('SetGaClientIdOnLink', function () { - - var GOVUK = window.GOVUK - var tracker = { clientId: 'clientId' } - tracker.get = function(arg) { return this[arg] } - window.ga = function(callback) { callback(tracker) } - var link - - beforeEach(function () { - link = $( - 'Start now' - ) - setter = new GOVUK.SetGaClientIdOnLink({ - $link: link - }) - }) - - it('sets the _ga client id as a query param on the link action', function () { - expect(link.attr('href')).toBe('https://some-service.batman.co.uk?_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') + }) + }) +}) From 2286ccabb85d83bfaaf5e5cefabc4f2873f7832e Mon Sep 17 00:00:00 2001 From: Kelvin Gan Date: Fri, 24 Apr 2020 15:29:48 +0100 Subject: [PATCH 5/5] Rename var to be more general --- app/assets/javascripts/set-ga-client-id-on-url-in-element.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 index 7b0d92f6e..3f0516193 100644 --- 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 @@ -14,8 +14,8 @@ window.ga(function (tracker) { var clientId = tracker.get('clientId') - var href = linkedElement.attr(attribute) - linkedElement.attr(attribute, href + "?_ga=" + clientId) + var attributeUrl = linkedElement.attr(attribute) + linkedElement.attr(attribute, attributeUrl + "?_ga=" + clientId) }) }