-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit adds the cookieless A/A test to government-frontend, which fires on every request. The purpose of the cookieless A/A test is to test an approach with our CDN which does not use cookies for A/B testing, and is something that we intend to use to A/B test the cookie consent banner should the A/A test be successful; as the cookie consent banner is presented up until users either consent to or decline cookies, we need to be able to understand which variant a user falls in before they have consented to cookies. The data involved in this is the variant of the test only - no other data is being sent to analytics, and data that is generally sent by default has been masked by hardcoding the various properties. This test has been approved by IA on the basis set out above, for collecting only variant data. In terms of the changes in this commit, there is _some_ duplication with the cookieless-tracker.js file, which has been largely copied from _Static_ but scoped only to send the data that we need to send. This approach has been verified with @DilwoarH.
- Loading branch information
Karl Baker
committed
Jan 15, 2021
1 parent
79bdd29
commit 6ba279d
Showing
7 changed files
with
267 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
window.GOVUK = window.GOVUK || {} | ||
window.GOVUK.Modules = window.GOVUK.Modules || {}; | ||
|
||
(function (Modules) { | ||
var CookielessTracker = function (trackingId, fieldsObject) { | ||
var trackerName = fieldsObject.name + '.' | ||
|
||
function configureProfile () { | ||
// https://developers.google.com/analytics/devguides/collection/analyticsjs/command-queue-reference#create | ||
sendToGa('create', trackingId, fieldsObject) | ||
} | ||
|
||
function anonymizeIp () { | ||
// https://developers.google.com/analytics/devguides/collection/analyticsjs/advanced#anonymizeip | ||
sendToGa(trackerName + 'set', 'anonymizeIp', true) | ||
} | ||
|
||
function disableAdFeatures () { | ||
// https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#allowAdFeatures | ||
sendToGa(trackerName + 'set', 'allowAdFeatures', false) | ||
} | ||
|
||
function stripTitlePII () { | ||
sendToGa(trackerName + 'set', 'title', '') | ||
} | ||
|
||
function stripLocationPII () { | ||
sendToGa(trackerName + 'set', 'location', '') | ||
} | ||
|
||
function load () { | ||
/* eslint-disable */ | ||
(function (i, s, o, g, r, a, m) { i['GoogleAnalyticsObject'] = r; i[r] = i[r] || function () { | ||
(i[r].q = i[r].q || []).push(arguments) }, i[r].l = 1 * new Date(); a = s.createElement(o), | ||
m = s.getElementsByTagName(o)[0]; a.async = 1; a.src = g; m.parentNode.insertBefore(a, m) | ||
})(window, document, 'script', 'https://www.google-analytics.com/analytics.js', 'ga') | ||
/* eslint-enable */ | ||
} | ||
|
||
// Support legacy cookieDomain param | ||
if (typeof fieldsObject === 'string') { | ||
fieldsObject = { cookieDomain: fieldsObject } | ||
} | ||
|
||
load() | ||
configureProfile() | ||
anonymizeIp() | ||
disableAdFeatures() | ||
stripTitlePII() | ||
stripLocationPII() | ||
} | ||
|
||
CookielessTracker.load = function () { | ||
/* eslint-disable */ | ||
(function (i, s, o, g, r, a, m) { i['GoogleAnalyticsObject'] = r; i[r] = i[r] || function () { | ||
(i[r].q = i[r].q || []).push(arguments) }, i[r].l = 1 * new Date(); a = s.createElement(o), | ||
m = s.getElementsByTagName(o)[0]; a.async = 1; a.src = g; m.parentNode.insertBefore(a, m) | ||
})(window, document, 'script', 'https://www.google-analytics.com/analytics.js', 'ga') | ||
/* eslint-enable */ | ||
} | ||
|
||
// https://developers.google.com/analytics/devguides/collection/analyticsjs/events | ||
CookielessTracker.prototype.trackEvent = function (category, action, options) { | ||
options = options || {} | ||
var value | ||
var trackerName = '' | ||
var evt = { | ||
hitType: 'event', | ||
eventCategory: category, | ||
eventAction: action | ||
} | ||
|
||
// Label is optional | ||
if (typeof options.label === 'string') { | ||
evt.eventLabel = options.label | ||
delete options.label | ||
} | ||
|
||
// Value is optional, but when used must be an | ||
// integer, otherwise the event will be invalid | ||
// and not logged | ||
if (options.value || options.value === 0) { | ||
value = parseInt(options.value, 10) | ||
if (typeof value === 'number' && !isNaN(value)) { | ||
options.eventValue = value | ||
} | ||
delete options.value | ||
} | ||
|
||
// trackerName is optional | ||
if (typeof options.trackerName === 'string') { | ||
trackerName = options.trackerName + '.' | ||
delete options.trackerName | ||
} | ||
|
||
// Prevents an event from affecting bounce rate | ||
// https://developers.google.com/analytics/devguides/collection/analyticsjs/events#implementation | ||
if (options.nonInteraction) { | ||
options.nonInteraction = 1 | ||
} | ||
|
||
if (typeof options === 'object') { | ||
$.extend(evt, options) | ||
} | ||
|
||
sendToGa(trackerName + 'send', evt) | ||
} | ||
|
||
function sendToGa () { | ||
if (typeof window.ga === 'function') { | ||
window.ga.apply(window, arguments) | ||
} | ||
} | ||
|
||
Modules.CookielessTracker = CookielessTracker | ||
})(window.GOVUK.Modules) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
window.GOVUK.Modules = window.GOVUK.Modules || {}; | ||
|
||
(function (Modules) { | ||
'use strict' | ||
|
||
Modules.TrackVariant = function () { | ||
this.start = function ($element) { | ||
var element = $element[0] | ||
|
||
if (window.GOVUK.cookie('cookies_preferences_set') !== 'true') { | ||
var variant = element.getAttribute('content') | ||
|
||
if (variant === undefined) { | ||
return | ||
} | ||
|
||
var cookielessTracker = new GOVUK.Modules.CookielessTracker('UA-26179049-29', { | ||
name: 'CookielessTracker', | ||
storage: 'none', | ||
clientId: '0' | ||
}) | ||
|
||
cookielessTracker.trackEvent('cookieless', 'hit', { | ||
trackerName: 'CookielessTracker', | ||
label: variant, | ||
javaEnabled: false, | ||
language: '' | ||
}) | ||
} | ||
} | ||
} | ||
})(window.GOVUK.Modules) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
module CookielessTestable | ||
extend ActiveSupport::Concern | ||
|
||
CUSTOM_DIMENSION = 49 | ||
|
||
def self.included(base) | ||
base.helper_method( | ||
:cookieless_variant, | ||
) | ||
base.after_action :set_test_response_header | ||
end | ||
|
||
def cookieless_variant | ||
@cookieless_variant ||= cookieless_test.requested_variant(request.headers) | ||
end | ||
|
||
private | ||
|
||
def cookieless_test | ||
@cookieless_test ||= GovukAbTesting::AbTest.new( | ||
"CookielessAATest", | ||
dimension: CUSTOM_DIMENSION, | ||
allowed_variants: %w[A B Z], | ||
control_variant: "Z", | ||
) | ||
end | ||
|
||
def set_test_response_header | ||
cookieless_variant.configure_response(response) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
describe('Test variant tracker', function () { | ||
'use strict' | ||
|
||
var tracker, | ||
element, | ||
FakeCookielessTracker, | ||
gaSpy | ||
|
||
beforeEach(function () { | ||
GOVUK.cookie('cookies_preferences_set', null) | ||
gaSpy = jasmine.createSpyObj('initGa', ['send']) | ||
|
||
FakeCookielessTracker = function (trackingId, fieldsObject) {} | ||
FakeCookielessTracker.prototype.trackEvent = function (category, action, options) { | ||
gaSpy.send(category, action, options) | ||
} | ||
|
||
GOVUK.Modules.CookielessTracker = FakeCookielessTracker | ||
|
||
tracker = new GOVUK.Modules.TrackVariant() | ||
}) | ||
|
||
afterEach(function () { | ||
GOVUK.Modules.CookielessTracker = null | ||
}) | ||
|
||
it('tracks A variant', function () { | ||
element = $('<meta name="Cookieless-Variant" content="A" data-module="track-variant">') | ||
|
||
tracker.start(element) | ||
|
||
expect(gaSpy.send).toHaveBeenCalledWith('cookieless', 'hit', { | ||
trackerName: 'CookielessTracker', | ||
label: 'A', | ||
javaEnabled: false, | ||
language: '' | ||
}) | ||
}) | ||
|
||
it('tracks B variant', function () { | ||
element = $('<meta name="Cookieless-Variant" content="B" data-module="track-variant">') | ||
|
||
tracker.start(element) | ||
|
||
expect(gaSpy.send).toHaveBeenCalledWith('cookieless', 'hit', { | ||
trackerName: 'CookielessTracker', | ||
label: 'B', | ||
javaEnabled: false, | ||
language: '' | ||
}) | ||
}) | ||
|
||
it('does not track variant if cookie is set', function () { | ||
GOVUK.cookie('cookies_preferences_set', true) | ||
element = $('<meta name="Cookieless-Variant" content="Z" data-module="track-variant">') | ||
|
||
tracker.start(element) | ||
|
||
expect(gaSpy.send).not.toHaveBeenCalled() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters