Skip to content

Commit

Permalink
Merge pull request #1483 from alphagov/include-ga-param-in-signins
Browse files Browse the repository at this point in the history
Pass _ga client id to external signon services
  • Loading branch information
bilbof authored Sep 16, 2019
2 parents 15c6032 + a7f69c5 commit 7cef9fb
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 2 deletions.
9 changes: 9 additions & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
//= require_tree ./modules
//= require govuk_publishing_components/all_components
//= require set-ga-client-id-on-form

jQuery(function ($) {
var $form = $('.js-service-sign-in-form')

if ($form.length) {
new GOVUK.SetGaClientIdOnForm({ $form: $form })
}
})
20 changes: 20 additions & 0 deletions app/assets/javascripts/set-ga-client-id-on-form.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
(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);
12 changes: 11 additions & 1 deletion app/controllers/content_items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def service_sign_in_options
return
end

redirect_to selected[:url]
redirect_to service_url(selected[:url])
end
end

Expand Down Expand Up @@ -126,6 +126,16 @@ def set_expiry
public: @content_item.cache_control_public?)
end

def service_url(original_url)
ga_param = params[:_ga]
return original_url if ga_param.nil?

url = URI.parse(original_url)
new_query_ar = URI.decode_www_form(url.query || '') << ["_ga", ga_param]
url.query = URI.encode_www_form(new_query_ar)
url.to_s
end

def with_locale
I18n.with_locale(@content_item.locale || I18n.default_locale) { yield }
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
%>
<%= form_tag({controller: 'content_items', action: 'service_sign_in_options'},
method: "post",
data: data_attrs) do %>
data: data_attrs,
class: 'js-service-sign-in-form') do %>
<% legend_text = render 'govuk_publishing_components/components/title', title: @content_item.title %>
<%= render "govuk_publishing_components/components/fieldset", legend_text: legend_text do %>
<div class="govuk-grid-row">
Expand Down
23 changes: 23 additions & 0 deletions spec/javascripts/set-ga-client-id-on-form.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* 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 = $(
'<form class="js-service-sign-in-form" action="/endpoint"></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')
})
})
19 changes: 19 additions & 0 deletions test/controllers/service_sign_in_content_item_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,25 @@ class ContentItemsControllerTest < ActionController::TestCase
assert_template :service_sign_in
end

test "includes _ga as a query param when redirecting if set" do
content_item = govuk_content_schema_example("service_sign_in", "service_sign_in")
link = 'https://www.horse.service.gov.uk/account?horse=brown'
content_item['details']['choose_sign_in']['options'][0]['url'] = link
content_store_has_item(content_item['base_path'], content_item)

path = "#{path_for(content_item)}/#{content_item['details']['choose_sign_in']['slug']}"

option = content_item['details']['choose_sign_in']['options'][0]
value = option['text'].parameterize

stub_request(:get, %r{#{path}}).to_return(status: 200, body: content_item.to_json, headers: {})

post :service_sign_in_options, params: { path: path, _ga: "1.1111111.1111111.111111111", option: value }

assert_response :redirect
assert_redirected_to "https://www.horse.service.gov.uk/account?horse=brown&_ga=1.1111111.1111111.111111111"
end

def path_for(content_item, locale = nil)
base_path = content_item['base_path'].sub(/^\//, '')
base_path.gsub!(/\.#{locale}$/, '') if locale
Expand Down

0 comments on commit 7cef9fb

Please sign in to comment.