diff --git a/Gemfile b/Gemfile index 75b4d0f80..bb2f04d1a 100644 --- a/Gemfile +++ b/Gemfile @@ -32,7 +32,7 @@ end gem 'plek', '1.7.0' -gem 'govuk_frontend_toolkit', '3.5.0' +gem 'govuk_frontend_toolkit', '4.0.0' if ENV['GOVUK_TEMPLATE_DEV'] gem 'govuk_template', :path => "../govuk_template" else diff --git a/Gemfile.lock b/Gemfile.lock index f669ee22f..c400cbdc9 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -55,7 +55,7 @@ GEM null_logger plek rest-client (~> 1.6.3) - govuk_frontend_toolkit (3.5.0) + govuk_frontend_toolkit (4.0.0) rails (>= 3.1.0) sass (>= 3.2.0) govuk_template (0.13.0) @@ -175,7 +175,7 @@ DEPENDENCIES capybara (= 2.1.0) exception_notification gds-api-adapters (= 7.18.0) - govuk_frontend_toolkit (= 3.5.0) + govuk_frontend_toolkit (= 4.0.0) govuk_template (= 0.13.0) image_optim (= 0.17.1) jasmine-rails (~> 0.10.6) diff --git a/app/assets/javascripts/analytics.js b/app/assets/javascripts/analytics.js index abbf4a8d8..0b5b16057 100644 --- a/app/assets/javascripts/analytics.js +++ b/app/assets/javascripts/analytics.js @@ -1,9 +1,8 @@ //= require govuk/analytics/google-analytics-universal-tracker -//= require govuk/analytics/google-analytics-classic-tracker -//= require govuk/analytics/tracker +//= require govuk/analytics/analytics //= require govuk/analytics/print-intent //= require govuk/analytics/error-tracking -//= require analytics/static-tracker +//= require analytics/static-analytics //= require analytics/init //= require analytics/scroll-tracker diff --git a/app/assets/javascripts/analytics/init.js b/app/assets/javascripts/analytics/init.js index 16f9ba687..14b3f1a14 100644 --- a/app/assets/javascripts/analytics/init.js +++ b/app/assets/javascripts/analytics/init.js @@ -2,16 +2,15 @@ "use strict"; // Load Google Analytics libraries - GOVUK.StaticTracker.load(); + 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; // Configure profiles, setup custom vars, track initial pageview - var analytics = new GOVUK.StaticTracker({ - universalId: 'UA-26179049-7', - classicId: 'UA-26179049-1', + var analytics = new GOVUK.StaticAnalytics({ + universalId: 'UA-26179049-1', cookieDomain: cookieDomain }); diff --git a/app/assets/javascripts/analytics/static-analytics.js b/app/assets/javascripts/analytics/static-analytics.js new file mode 100644 index 000000000..41d52b368 --- /dev/null +++ b/app/assets/javascripts/analytics/static-analytics.js @@ -0,0 +1,160 @@ +(function() { + "use strict"; + window.GOVUK = window.GOVUK || {}; + + var StaticAnalytics = function(config) { + + // 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 + var analytics = new GOVUK.Analytics(config); + this.analytics = analytics; + + setPixelDensityDimension(); + setHTTPStatusCodeDimension(); + this.setDimensionsFromMetaTags(); + this.callMethodRequestedByPreviousPage(); + + // Track initial pageview + analytics.trackPageview(); + + // Begin error and print tracking + GOVUK.analyticsPlugins.error(); + GOVUK.analyticsPlugins.printIntent(); + + function setPixelDensityDimension() { + if (window.devicePixelRatio) { + analytics.setDimension(11, window.devicePixelRatio); + } + } + + function setHTTPStatusCodeDimension() { + analytics.setDimension(15, window.httpStatusCode || 200); + } + + }; + + StaticAnalytics.prototype.callOnNextPage = function(method, params) { + params = params || []; + + if (!$.isArray(params)) { + params = [params]; + } + + if (GOVUK.cookie && typeof this[method] === "function") { + params.unshift(method); + GOVUK.cookie('analytics_next_page_call', JSON.stringify(params)); + } + }; + + StaticAnalytics.prototype.callMethodRequestedByPreviousPage = function() { + if (GOVUK.cookie && GOVUK.cookie('analytics_next_page_call') !== null) { + var params, method; + + try { + params = JSON.parse(GOVUK.cookie('analytics_next_page_call')); + method = params.shift(); + } catch(e) {} + + if (method && typeof this[method] === "function") { + this[method].apply(this, params); + } + + // Delete cookie + GOVUK.cookie('analytics_next_page_call', null); + } + }; + + StaticAnalytics.load = function() { + GOVUK.Analytics.load(); + }; + + StaticAnalytics.prototype.setDimensionsFromMetaTags = function() { + var $metas = $('meta[name^="govuk:"]'), + dimensions = {}; + + $metas.each(function() { + var $meta = $(this), + key = $meta.attr('name').split('govuk:')[1], + value = $meta.attr('content'); + + dimensions[key] = value; + }); + + this.setSectionDimension(dimensions['section']); + this.setFormatDimension(dimensions['format']); + this.setNeedIDsDimension(dimensions['need-ids']); + this.setResultCountDimension(dimensions['search-result-count']); + this.setPublishingGovernmentDimension(dimensions['publishing-government']); + this.setPoliticalStatusDimension(dimensions['political-status']); + this.setOrganisationsDimension(dimensions['analytics:organisations']); + this.setWorldLocationsDimension(dimensions['analytics:world-locations']); + this.setRenderingApplicationDimension(dimensions['rendering-application']); + }; + + StaticAnalytics.prototype.trackPageview = function(path, title) { + this.analytics.trackPageview(path, title); + }; + + StaticAnalytics.prototype.trackEvent = function(category, action, options) { + this.analytics.trackEvent(category, action, options); + }; + + StaticAnalytics.prototype.setDimension = function(index, value, name, scope) { + if (typeof value === "undefined") { + return; + } + + this.analytics.setDimension(index, value, name, scope); + }; + + StaticAnalytics.prototype.trackShare = function(network) { + this.analytics.trackShare(network); + }; + + StaticAnalytics.prototype.addLinkedTrackerDomain = function(trackerId, name, domain) { + this.analytics.addLinkedTrackerDomain(trackerId, name, domain); + }; + + StaticAnalytics.prototype.setSectionDimension = function(section) { + this.setDimension(1, section); + }; + + StaticAnalytics.prototype.setFormatDimension = function(format) { + this.setDimension(2, format); + }; + + StaticAnalytics.prototype.setNeedIDsDimension = function(ids) { + this.setDimension(3, ids); + }; + + StaticAnalytics.prototype.setResultCountDimension = function(count) { + this.setDimension(5, count); + }; + + StaticAnalytics.prototype.setPublishingGovernmentDimension = function(government) { + this.setDimension(6, government); + }; + + StaticAnalytics.prototype.setPoliticalStatusDimension = function(status) { + this.setDimension(7, status); + }; + + StaticAnalytics.prototype.setOrganisationsDimension = function(orgs) { + this.setDimension(9, orgs); + }; + + StaticAnalytics.prototype.setWorldLocationsDimension = function(locations) { + this.setDimension(10, locations); + }; + + StaticAnalytics.prototype.setRenderingApplicationDimension = function(app) { + this.setDimension(20, app); + }; + + StaticAnalytics.prototype.setSearchPositionDimension = function(position) { + this.setDimension(21, position); + }; + + GOVUK.StaticAnalytics = StaticAnalytics; +})(); diff --git a/app/assets/javascripts/analytics/static-tracker.js b/app/assets/javascripts/analytics/static-tracker.js deleted file mode 100644 index 8b951c779..000000000 --- a/app/assets/javascripts/analytics/static-tracker.js +++ /dev/null @@ -1,160 +0,0 @@ -(function() { - "use strict"; - window.GOVUK = window.GOVUK || {}; - - var StaticTracker = function(config) { - - // Create universal and classic analytics 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/tracker.js - var tracker = new GOVUK.Tracker(config); - this.tracker = tracker; - - setPixelDensityDimension(); - setHTTPStatusCodeDimension(); - this.setDimensionsFromMetaTags(); - this.callMethodRequestedByPreviousPage(); - - // Track initial pageview - tracker.trackPageview(); - - // Begin error and print tracking - GOVUK.analyticsPlugins.error(); - GOVUK.analyticsPlugins.printIntent(); - - function setPixelDensityDimension() { - if (window.devicePixelRatio) { - tracker.setDimension(11, window.devicePixelRatio, 'Pixel Ratio', 2); - } - } - - function setHTTPStatusCodeDimension() { - tracker.setDimension(15, window.httpStatusCode || 200, 'httpStatusCode'); - } - - }; - - StaticTracker.prototype.callOnNextPage = function(method, params) { - params = params || []; - - if (!$.isArray(params)) { - params = [params]; - } - - if (GOVUK.cookie && typeof this[method] === "function") { - params.unshift(method); - GOVUK.cookie('analytics_next_page_call', JSON.stringify(params)); - } - }; - - StaticTracker.prototype.callMethodRequestedByPreviousPage = function() { - if (GOVUK.cookie && GOVUK.cookie('analytics_next_page_call') !== null) { - var params, method; - - try { - params = JSON.parse(GOVUK.cookie('analytics_next_page_call')); - method = params.shift(); - } catch(e) {} - - if (method && typeof this[method] === "function") { - this[method].apply(this, params); - } - - // Delete cookie - GOVUK.cookie('analytics_next_page_call', null); - } - }; - - StaticTracker.load = function() { - GOVUK.Tracker.load(); - }; - - StaticTracker.prototype.setDimensionsFromMetaTags = function() { - var $metas = $('meta[name^="govuk:"]'), - dimensions = {}; - - $metas.each(function() { - var $meta = $(this), - key = $meta.attr('name').split('govuk:')[1], - value = $meta.attr('content'); - - dimensions[key] = value; - }); - - this.setSectionDimension(dimensions['section']); - this.setFormatDimension(dimensions['format']); - this.setNeedIDsDimension(dimensions['need-ids']); - this.setResultCountDimension(dimensions['search-result-count']); - this.setPublishingGovernmentDimension(dimensions['publishing-government']); - this.setPoliticalStatusDimension(dimensions['political-status']); - this.setOrganisationsDimension(dimensions['analytics:organisations']); - this.setWorldLocationsDimension(dimensions['analytics:world-locations']); - this.setRenderingApplicationDimension(dimensions['rendering-application']); - }; - - StaticTracker.prototype.trackPageview = function(path, title) { - this.tracker.trackPageview(path, title); - }; - - StaticTracker.prototype.trackEvent = function(category, action, options) { - this.tracker.trackEvent(category, action, options); - }; - - StaticTracker.prototype.setDimension = function(index, value, name, scope) { - if (typeof value === "undefined") { - return; - } - - this.tracker.setDimension(index, value, name, scope); - }; - - StaticTracker.prototype.trackShare = function(network) { - this.tracker.trackShare(network); - }; - - StaticTracker.prototype.addLinkedTrackerDomain = function(trackerId, name, domain) { - this.tracker.addLinkedTrackerDomain(trackerId, name, domain); - }; - - StaticTracker.prototype.setSectionDimension = function(section) { - this.setDimension(1, section, 'Section'); - }; - - StaticTracker.prototype.setFormatDimension = function(format) { - this.setDimension(2, format, 'Format'); - }; - - StaticTracker.prototype.setNeedIDsDimension = function(ids) { - this.setDimension(3, ids, 'NeedID'); - }; - - StaticTracker.prototype.setResultCountDimension = function(count) { - this.setDimension(5, count, 'ResultCount'); - }; - - StaticTracker.prototype.setPublishingGovernmentDimension = function(government) { - this.setDimension(6, government, 'PublishingGovernment'); - }; - - StaticTracker.prototype.setPoliticalStatusDimension = function(status) { - this.setDimension(7, status, 'PoliticalStatus'); - }; - - StaticTracker.prototype.setOrganisationsDimension = function(orgs) { - this.setDimension(9, orgs, 'Organisations'); - }; - - StaticTracker.prototype.setWorldLocationsDimension = function(locations) { - this.setDimension(10, locations, 'WorldLocations'); - }; - - StaticTracker.prototype.setRenderingApplicationDimension = function(app) { - this.setDimension(20, app, 'RenderingApps'); - }; - - StaticTracker.prototype.setSearchPositionDimension = function(position) { - this.setDimension(21, position, 'searchPosition'); - }; - - GOVUK.StaticTracker = StaticTracker; -})(); diff --git a/spec/javascripts/analytics/static-tracker-spec.js b/spec/javascripts/analytics/static-analytics-spec.js similarity index 63% rename from spec/javascripts/analytics/static-tracker-spec.js rename to spec/javascripts/analytics/static-analytics-spec.js index 8f07af57b..2c020757c 100644 --- a/spec/javascripts/analytics/static-tracker-spec.js +++ b/spec/javascripts/analytics/static-analytics-spec.js @@ -1,15 +1,13 @@ -describe("GOVUK.StaticTracker", function() { - var tracker; +describe("GOVUK.StaticAnalytics", function() { + var analytics; beforeEach(function() { - window._gaq = []; window.ga = function() {}; spyOn(window, 'ga'); spyOn(GOVUK.analyticsPlugins, 'printIntent'); spyOn(GOVUK.analyticsPlugins, 'error'); - tracker = new GOVUK.StaticTracker({ + analytics = new GOVUK.StaticAnalytics({ universalId: 'universal-id', - classicId: 'classic-id', cookieDomain: '.www.gov.uk' }); }); @@ -21,24 +19,19 @@ describe("GOVUK.StaticTracker", function() { universalSetupArguments = window.ga.calls.allArgs(); }); - it('configures classic and universal trackers', function() { - expect(window._gaq[0]).toEqual(['_setAccount', 'classic-id']); - expect(window._gaq[1]).toEqual(['_setDomainName', '.www.gov.uk']); + it('configures a universal tracker', function() { expect(universalSetupArguments[0]).toEqual(['create', 'universal-id', {'cookieDomain': '.www.gov.uk'}]); }); it('sets the device pixel ratio', function() { - expect(window._gaq[4][2]).toEqual('Pixel Ratio'); expect(universalSetupArguments[2][1]).toEqual('dimension11'); }); it('sets the HTTP status code', function() { - expect(window._gaq[5][2]).toEqual('httpStatusCode'); expect(universalSetupArguments[3][1]).toEqual('dimension15'); }); - it('tracks a pageview in both classic and universal', function() { - expect(window._gaq[6]).toEqual(['_trackPageview']); + it('tracks a pageview in universal', function() { expect(universalSetupArguments[4]).toEqual(['send', 'pageview']); }); @@ -53,7 +46,6 @@ describe("GOVUK.StaticTracker", function() { describe('when there are govuk: meta tags', function() { beforeEach(function() { window.ga.calls.reset(); - window._gaq = []; }); afterEach(function() { @@ -73,31 +65,16 @@ describe("GOVUK.StaticTracker", function() { \ '); - tracker = new GOVUK.StaticTracker({universalId: 'universal-id', classicId: 'classic-id'}); + analytics = new GOVUK.StaticAnalytics({universalId: 'universal-id'}); universalSetupArguments = window.ga.calls.allArgs(); - expect(window._gaq[6]).toEqual(['_setCustomVar', 1, 'Section', 'section', 3]); expect(universalSetupArguments[4]).toEqual(['set', 'dimension1', 'section']); - - expect(window._gaq[7]).toEqual(['_setCustomVar', 2, 'Format', 'format', 3]); expect(universalSetupArguments[5]).toEqual(['set', 'dimension2', 'format']); - - expect(window._gaq[8]).toEqual(['_setCustomVar', 3, 'NeedID', '1,2,3', 3]); expect(universalSetupArguments[6]).toEqual(['set', 'dimension3', '1,2,3']); - - expect(window._gaq[9]).toEqual(['_setCustomVar', 5, 'ResultCount', '1000', 3]); expect(universalSetupArguments[7]).toEqual(['set', 'dimension5', '1000']); - - expect(window._gaq[10]).toEqual(['_setCustomVar', 6, 'PublishingGovernment', '2005-to-2010-labour-government', 3]); expect(universalSetupArguments[8]).toEqual(['set', 'dimension6', '2005-to-2010-labour-government']); - - expect(window._gaq[11]).toEqual(['_setCustomVar', 7, 'PoliticalStatus', 'historic', 3]); expect(universalSetupArguments[9]).toEqual(['set', 'dimension7', 'historic']); - - expect(window._gaq[12]).toEqual(['_setCustomVar', 9, 'Organisations', '', 3]); expect(universalSetupArguments[10]).toEqual(['set', 'dimension9', '']); - - expect(window._gaq[13]).toEqual(['_setCustomVar', 10, 'WorldLocations', '', 3]); expect(universalSetupArguments[11]).toEqual(['set', 'dimension10', '']); }); @@ -105,43 +82,33 @@ describe("GOVUK.StaticTracker", function() { $('head').append(''); - tracker = new GOVUK.StaticTracker({universalId: 'universal-id', classicId: 'classic-id'}); + analytics = new GOVUK.StaticAnalytics({universalId: 'universal-id'}); universalSetupArguments = window.ga.calls.allArgs(); - expect(window._gaq[6]).toEqual(['_setCustomVar', 1, 'Section', 'section', 3]); expect(universalSetupArguments[4]).toEqual(['set', 'dimension1', 'section']); - - expect(window._gaq[7]).toEqual(['_trackPageview']); expect(universalSetupArguments[5]).toEqual(['send', 'pageview']); }); }); }); describe('when tracking pageviews, events and custom dimensions', function() { - it('tracks in both classic and universal', function() { - window._gaq = []; - tracker.trackPageview('/path', 'Title'); - expect(window._gaq[0]).toEqual(['_trackPageview', '/path']); + it('tracks them in universal', function() { + + analytics.trackPageview('/path', 'Title'); expect(window.ga.calls.mostRecent().args).toEqual(['send', 'pageview', {page: '/path', title: 'Title'}]); - window._gaq = []; - tracker.trackEvent('category', 'action'); - expect(window._gaq[0]).toEqual(['_trackEvent', 'category', 'action']); + analytics.trackEvent('category', 'action'); expect(window.ga.calls.mostRecent().args).toEqual(['send', {hitType: 'event', eventCategory: 'category', eventAction: 'action'}]); - window._gaq = []; - tracker.setSectionDimension('value'); - expect(window._gaq[0]).toEqual(['_setCustomVar', 1, 'Section', 'value', 3]); + analytics.setSectionDimension('value'); expect(window.ga.calls.mostRecent().args).toEqual(['set', 'dimension1', 'value']); }); }); describe('when tracking social media shares', function() { - it('tracks in both classic and universal', function() { - window._gaq = []; - tracker.trackShare('network'); + it('tracks them in universal', function() { + analytics.trackShare('network'); - expect(window._gaq[0]).toEqual(['_trackSocial', 'network', 'share', jasmine.any(String)]); expect(window.ga.calls.mostRecent().args).toEqual(['send', { hitType: 'social', socialNetwork: 'network', @@ -158,24 +125,24 @@ describe("GOVUK.StaticTracker", function() { describe('and the method exists', function() { it('sets a cookie with the method name', function() { - tracker.callOnNextPage('trackPageview'); + analytics.callOnNextPage('trackPageview'); expect(GOVUK.cookie).toHaveBeenCalledWith('analytics_next_page_call', '["trackPageview"]'); }); it('sets a cookie with the parameters to call', function() { - tracker.callOnNextPage('trackPageview', ['/path', 'Custom Title']); + analytics.callOnNextPage('trackPageview', ['/path', 'Custom Title']); expect(GOVUK.cookie).toHaveBeenCalledWith('analytics_next_page_call', '["trackPageview","/path","Custom Title"]'); }); it('sets a cookie with the single parameter to call', function() { - tracker.callOnNextPage('trackPageview', '/path'); + analytics.callOnNextPage('trackPageview', '/path'); expect(GOVUK.cookie).toHaveBeenCalledWith('analytics_next_page_call', '["trackPageview","/path"]'); }); }); describe('and the method doesn’t exist', function() { it('no cookie is set', function() { - tracker.callOnNextPage('trackPageviewToNowhere'); + analytics.callOnNextPage('trackPageviewToNowhere'); expect(GOVUK.cookie).not.toHaveBeenCalled(); }); }); @@ -183,19 +150,19 @@ describe("GOVUK.StaticTracker", function() { describe('when there is a cookie indicating a method to call', function() { beforeEach(function() { - spyOn(tracker, 'trackPageview'); + spyOn(analytics, 'trackPageview'); }); it('calls the method', function() { spyOn(GOVUK, 'cookie').and.returnValue('["trackPageview"]'); - tracker.callMethodRequestedByPreviousPage(); - expect(tracker.trackPageview).toHaveBeenCalledWith(); + analytics.callMethodRequestedByPreviousPage(); + expect(analytics.trackPageview).toHaveBeenCalledWith(); }); it('calls the method with given parameters', function() { spyOn(GOVUK, 'cookie').and.returnValue('["trackPageview","/path","Title"]'); - tracker.callMethodRequestedByPreviousPage(); - expect(tracker.trackPageview).toHaveBeenCalledWith('/path', 'Title'); + analytics.callMethodRequestedByPreviousPage(); + expect(analytics.trackPageview).toHaveBeenCalledWith('/path', 'Title'); }); }); });