From e517dbee69d731fd2b068b10a6da321e3e98f9a8 Mon Sep 17 00:00:00 2001 From: Steve Laing Date: Wed, 26 Sep 2018 14:30:11 +0100 Subject: [PATCH 1/2] Add cross domain tracking to service sign in form We need to track radio button selection events across domains so that other departments can monitor the efficacy of changes and options on the service sign in pages. This commit modifies the track radio group module to include cross domain tracking via a tracking code and name. Support is being added for radio buttons to pass the relevant url so that the hostname can be identified as part of the tracking. --- .../javascripts/modules/track-radio-group.js | 14 +++++++++++++- .../service_sign_in/choose_sign_in_presenter.rb | 11 ++++++++++- .../service_sign_in/_choose_sign_in.html.erb | 10 ++++++++-- .../service_sign_in/choose_sign_in_test.rb | 2 ++ 4 files changed, 33 insertions(+), 4 deletions(-) diff --git a/app/assets/javascripts/modules/track-radio-group.js b/app/assets/javascripts/modules/track-radio-group.js index a34dfe68d..38f75cf10 100644 --- a/app/assets/javascripts/modules/track-radio-group.js +++ b/app/assets/javascripts/modules/track-radio-group.js @@ -15,7 +15,7 @@ window.GOVUK.Modules = window.GOVUK.Modules || {}; function track (element, withHint) { element.on('submit', function (event) { - + var options = { transport: 'beacon' } var $submittedForm = $(event.target) @@ -27,6 +27,9 @@ window.GOVUK.Modules = window.GOVUK.Modules || {}; if (typeof checkedValue === 'undefined') { checkedValue = 'submitted-without-choosing' } + if (typeof element.attr('data-tracking-code') !== 'undefined') { + addCrossDomainTracking(element, $checkedOption) + } GOVUK.analytics.trackEvent('Radio button chosen', checkedValue + (withHint ? '-with-hint' : ''), options) }) } @@ -52,5 +55,14 @@ window.GOVUK.Modules = window.GOVUK.Modules || {}; track(element, data.value); } } + + function addCrossDomainTracking(element, $checkedOption) { + var code = element.attr('data-tracking-code') + var name = element.attr('data-tracking-name') + var url = $checkedOption.attr('data-tracking-url') + var hostname = $('').prop('href', url).prop('hostname') + + GOVUK.analytics.addLinkedTrackerDomain(code, name, hostname) + } } })(window, window.GOVUK); diff --git a/app/presenters/service_sign_in/choose_sign_in_presenter.rb b/app/presenters/service_sign_in/choose_sign_in_presenter.rb index cead02ba1..6b23886df 100644 --- a/app/presenters/service_sign_in/choose_sign_in_presenter.rb +++ b/app/presenters/service_sign_in/choose_sign_in_presenter.rb @@ -14,13 +14,22 @@ def description choose_sign_in["description"] end + def tracking_code + choose_sign_in["tracking_code"] + end + + def tracking_name + choose_sign_in["tracking_name"] + end + def options radio_options = mapped_options.map do |option| { text: option[:text], value: option[:value], hint_text: option[:hint_text], - bold: true + url: option[:url], + bold: true, } end # TODO: Move to decision of when or should be applied to schema 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 b69a846ad..8e7bb6758 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 @@ -13,8 +13,14 @@ <% end %> - -<%= form_tag({controller: 'content_items', action: 'service_sign_in_options'}, method: "post", data: { module: 'track-radio-group' }) do %> +<% + data_attrs = { module: "track-radio-group" } + data_attrs["tracking-code"] = @content_item.tracking_code if @content_item.tracking_code + 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) 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/test/integration/service_sign_in/choose_sign_in_test.rb b/test/integration/service_sign_in/choose_sign_in_test.rb index f5ed25ee8..1431ded02 100644 --- a/test/integration/service_sign_in/choose_sign_in_test.rb +++ b/test/integration/service_sign_in/choose_sign_in_test.rb @@ -21,6 +21,8 @@ class ChooseSignInTest < ActionDispatch::IntegrationTest 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-tracking-code='UA-xxxxxx']") + assert page.has_css?("form[data-tracking-name='somethingClicky']") within "#content form" do within ".gem-c-fieldset" do From cc046ed57223f57dad9cff062ae70cda324ad949 Mon Sep 17 00:00:00 2001 From: Steve Laing Date: Fri, 28 Sep 2018 12:03:45 +0100 Subject: [PATCH 2/2] Update govuk_publishing_components gem This provides support for the tracking url to enable cross domain tracking in the track radio group module. --- Gemfile | 2 +- Gemfile.lock | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Gemfile b/Gemfile index 50a340f06..a7e98b0aa 100644 --- a/Gemfile +++ b/Gemfile @@ -17,7 +17,7 @@ gem 'gds-api-adapters', '~> 53.1' gem 'govuk_ab_testing', '~> 2.4' gem 'govuk_app_config', '~> 1.9' gem 'govuk_frontend_toolkit', '~> 8.0' -gem 'govuk_publishing_components', '~> 10.1.0' +gem 'govuk_publishing_components', '~> 10.2.0' gem 'plek', '~> 2.1' gem 'slimmer', '~> 13.0' diff --git a/Gemfile.lock b/Gemfile.lock index 5c78d98b8..c18ff9b34 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -127,7 +127,7 @@ GEM govuk_frontend_toolkit (8.0.0) railties (>= 3.1.0) sass (>= 3.2.0) - govuk_publishing_components (10.1.0) + govuk_publishing_components (10.2.0) govspeak (>= 5.0.3) govuk_app_config govuk_frontend_toolkit @@ -367,7 +367,7 @@ DEPENDENCIES govuk_ab_testing (~> 2.4) govuk_app_config (~> 1.9) govuk_frontend_toolkit (~> 8.0) - govuk_publishing_components (~> 10.1.0) + govuk_publishing_components (~> 10.2.0) govuk_schemas (~> 3.2) htmlentities (~> 4.3) jasmine-rails