Skip to content

Commit a7f69c5

Browse files
author
William Franklin
committed
Pass _ga client id to external signon services
On pages such as https://www.gov.uk/log-in-file-self-assessment-tax-return/sign-in/prove-identity we redirect users to services such as tax.service.gov.uk to complete their sign in. This will pass the client id as a _ga param as part of cross-domain tracking, so that we can measure user journeys across domains. This will only pass the _ga param to other .gov.uk services. Currently these are: tax.service.gov.uk www.signin.service.gov.uk www.ruralpayments.service.gov.uk Further work is required on the signin.service.gov.uk to make use of the _ga param and ensure it is included in redirects. Trello: https://trello.com/c/ORdXWurC/101
1 parent 38dc142 commit a7f69c5

File tree

6 files changed

+84
-2
lines changed

6 files changed

+84
-2
lines changed

app/assets/javascripts/application.js

+9
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,11 @@
11
//= require_tree ./modules
22
//= require govuk_publishing_components/all_components
3+
//= require set-ga-client-id-on-form
4+
5+
jQuery(function ($) {
6+
var $form = $('.js-service-sign-in-form')
7+
8+
if ($form.length) {
9+
new GOVUK.SetGaClientIdOnForm({ $form: $form })
10+
}
11+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
(function () {
2+
'use strict'
3+
4+
window.GOVUK = window.GOVUK || {}
5+
var GOVUK = window.GOVUK
6+
7+
function SetGaClientIdOnForm (options) {
8+
if (!options.$form || !window.ga) { return }
9+
10+
var form = options.$form
11+
12+
window.ga(function(tracker) {
13+
var clientId = tracker.get('clientId')
14+
var action = form.attr('action')
15+
form.attr('action', action + "?_ga=" + clientId)
16+
});
17+
}
18+
19+
GOVUK.SetGaClientIdOnForm = SetGaClientIdOnForm
20+
})(window, window.GOVUK);

app/controllers/content_items_controller.rb

+11-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def service_sign_in_options
3434
return
3535
end
3636

37-
redirect_to selected[:url]
37+
redirect_to service_url(selected[:url])
3838
end
3939
end
4040

@@ -126,6 +126,16 @@ def set_expiry
126126
public: @content_item.cache_control_public?)
127127
end
128128

129+
def service_url(original_url)
130+
ga_param = params[:_ga]
131+
return original_url if ga_param.nil?
132+
133+
url = URI.parse(original_url)
134+
new_query_ar = URI.decode_www_form(url.query || '') << ["_ga", ga_param]
135+
url.query = URI.encode_www_form(new_query_ar)
136+
url.to_s
137+
end
138+
129139
def with_locale
130140
I18n.with_locale(@content_item.locale || I18n.default_locale) { yield }
131141
end

app/views/content_items/service_sign_in/_choose_sign_in.html.erb

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
%>
2222
<%= form_tag({controller: 'content_items', action: 'service_sign_in_options'},
2323
method: "post",
24-
data: data_attrs) do %>
24+
data: data_attrs,
25+
class: 'js-service-sign-in-form') do %>
2526
<% legend_text = render 'govuk_publishing_components/components/title', title: @content_item.title %>
2627
<%= render "govuk_publishing_components/components/fieldset", legend_text: legend_text do %>
2728
<div class="govuk-grid-row">
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* global describe beforeEach it expect */
2+
3+
var $ = window.jQuery
4+
5+
describe('SetGaClientIdOnForm', function () {
6+
7+
var GOVUK = window.GOVUK
8+
var tracker = { clientId: 'clientId' }
9+
tracker.get = function(arg) { return this[arg] }
10+
window.ga = function(callback) { callback(tracker) }
11+
var form
12+
13+
beforeEach(function () {
14+
form = $(
15+
'<form class="js-service-sign-in-form" action="/endpoint"></form>'
16+
)
17+
setter = new GOVUK.SetGaClientIdOnForm({ $form: form })
18+
})
19+
20+
it('sets the _ga client id as a query param on the form action', function () {
21+
expect(form.attr('action')).toBe('/endpoint?_ga=clientId')
22+
})
23+
})

test/controllers/service_sign_in_content_item_controller_test.rb

+19
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,25 @@ class ContentItemsControllerTest < ActionController::TestCase
100100
assert_template :service_sign_in
101101
end
102102

103+
test "includes _ga as a query param when redirecting if set" do
104+
content_item = govuk_content_schema_example("service_sign_in", "service_sign_in")
105+
link = 'https://www.horse.service.gov.uk/account?horse=brown'
106+
content_item['details']['choose_sign_in']['options'][0]['url'] = link
107+
content_store_has_item(content_item['base_path'], content_item)
108+
109+
path = "#{path_for(content_item)}/#{content_item['details']['choose_sign_in']['slug']}"
110+
111+
option = content_item['details']['choose_sign_in']['options'][0]
112+
value = option['text'].parameterize
113+
114+
stub_request(:get, %r{#{path}}).to_return(status: 200, body: content_item.to_json, headers: {})
115+
116+
post :service_sign_in_options, params: { path: path, _ga: "1.1111111.1111111.111111111", option: value }
117+
118+
assert_response :redirect
119+
assert_redirected_to "https://www.horse.service.gov.uk/account?horse=brown&_ga=1.1111111.1111111.111111111"
120+
end
121+
103122
def path_for(content_item, locale = nil)
104123
base_path = content_item['base_path'].sub(/^\//, '')
105124
base_path.gsub!(/\.#{locale}$/, '') if locale

0 commit comments

Comments
 (0)