Skip to content

Commit

Permalink
Handle GA cookies when usage cookies are denied (#1755)
Browse files Browse the repository at this point in the history
Handle GA cookies when usage cookies are denied
  • Loading branch information
chao-xian authored Jun 17, 2019
2 parents 477b4bd + ae13166 commit b2532b9
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 21 deletions.
50 changes: 30 additions & 20 deletions app/assets/javascripts/analytics/init.js.erb
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
(function() {
"use strict";

// Load Google Analytics libraries
GOVUK.StaticAnalytics.load();

// Use document.domain in dev, preview and staging so that tracking works
// Otherwise explicitly set the domain as www.gov.uk (and not gov.uk).
var cookieDomain = (document.domain == 'www.gov.uk') ? '.www.gov.uk' : document.domain;

var universalId = '<%= Rails.application.config.ga_universal_id %>';

// Configure profiles, setup custom vars, track initial pageview
var analytics = new GOVUK.StaticAnalytics({
universalId: universalId,
cookieDomain: cookieDomain,
allowLinker: true,
// This is served by Fastly in production, and returns an empty 200 response
govukTrackerUrl: '<%= asset_path "/static/a" %>'
});

// Make interface public for virtual pageviews and events
GOVUK.analytics = analytics;
var consentCookie = window.GOVUK.getConsentCookie();

// Disable analytics by default
// This will be reversed below, if the consent cookie says usage cookies are allowed
window['ga-disable-UA-26179049-1'] = true;

if (!consentCookie || consentCookie["usage"]) {
window['ga-disable-UA-26179049-1'] = false;

// Load Google Analytics libraries
GOVUK.StaticAnalytics.load();

// Use document.domain in dev, preview and staging so that tracking works
// Otherwise explicitly set the domain as www.gov.uk (and not gov.uk).
var cookieDomain = (document.domain == 'www.gov.uk') ? '.www.gov.uk' : document.domain;

var universalId = '<%= Rails.application.config.ga_universal_id %>';

// Configure profiles, setup custom vars, track initial pageview
var analytics = new GOVUK.StaticAnalytics({
universalId: universalId,
cookieDomain: cookieDomain,
allowLinker: true,
// This is served by Fastly in production, and returns an empty 200 response
govukTrackerUrl: '<%= asset_path "/static/a" %>'
});

// Make interface public for virtual pageviews and events
GOVUK.analytics = analytics;
}
})();
6 changes: 5 additions & 1 deletion app/assets/javascripts/analytics/static-analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
// Create universal tracker
// https://github.com/alphagov/govuk_frontend_toolkit/blob/master/docs/analytics.md
// https://github.com/alphagov/govuk_frontend_toolkit/blob/master/javascripts/govuk/analytics/analytics.js
this.analytics = new GOVUK.Analytics(config);
var consentCookie = window.GOVUK.getConsentCookie();

if (!consentCookie || consentCookie['usage']) {
this.analytics = new GOVUK.Analytics(config);
}

var trackingOptions = getOptionsFromCookie();

Expand Down
23 changes: 23 additions & 0 deletions spec/javascripts/analytics/static-analytics-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ describe("GOVUK.StaticAnalytics", function() {
var analytics;

beforeEach(function() {
window.GOVUK.setConsentCookie({'usage': true});
window.ga = function() {};
spyOn(window, 'ga');
spyOn(GOVUK.analyticsPlugins, 'printIntent');
Expand Down Expand Up @@ -1058,6 +1059,28 @@ describe("GOVUK.StaticAnalytics", function() {
});
});

describe("when the consent cookie has been set", function() {
beforeEach(function() {
window.ga.calls.reset();
});

it('does not set analytics cookies as normal when usage cookies are allowed', function() {
window.GOVUK.setConsentCookie({'usage': false});
analytics = new GOVUK.StaticAnalytics({universalId: 'universal-id'});

expect(Object.keys(analytics).length).toBe(0)
});

it('does set analytics cookies as normal when usage cookies are allowed', function() {
window.GOVUK.setConsentCookie({'usage': true});
analytics = new GOVUK.StaticAnalytics({universalId: 'universal-id'});

expect(Object.keys(analytics).length).toBe(1)
expect(analytics.analytics.stripDatePII).toBe(false)
expect(analytics.analytics.stripPostcodePII).toBe(false)
});
});

describe('when the seen_cookie_message cookie does not exist', function() {
var pageViewObject;

Expand Down

0 comments on commit b2532b9

Please sign in to comment.