diff --git a/app/assets/javascripts/analytics/init.js.erb b/app/assets/javascripts/analytics/init.js.erb index 070209827..871c53db9 100644 --- a/app/assets/javascripts/analytics/init.js.erb +++ b/app/assets/javascripts/analytics/init.js.erb @@ -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; + } })(); diff --git a/app/assets/javascripts/analytics/static-analytics.js b/app/assets/javascripts/analytics/static-analytics.js index 05aabef53..93b42b43a 100644 --- a/app/assets/javascripts/analytics/static-analytics.js +++ b/app/assets/javascripts/analytics/static-analytics.js @@ -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(); diff --git a/spec/javascripts/analytics/static-analytics-spec.js b/spec/javascripts/analytics/static-analytics-spec.js index 83c32e71e..cebfce4ab 100644 --- a/spec/javascripts/analytics/static-analytics-spec.js +++ b/spec/javascripts/analytics/static-analytics-spec.js @@ -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'); @@ -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;