diff --git a/Gemfile b/Gemfile index 992cdb4cb..1ecc4909b 100644 --- a/Gemfile +++ b/Gemfile @@ -32,7 +32,7 @@ end gem 'plek', '1.7.0' -gem 'govuk_frontend_toolkit', '3.1.0' +gem 'govuk_frontend_toolkit', '3.2.1' if ENV['GOVUK_TEMPLATE_DEV'] gem 'govuk_template', :path => "../govuk_template" else diff --git a/Gemfile.lock b/Gemfile.lock index d8300c850..5d72c6efc 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -55,7 +55,7 @@ GEM null_logger plek rest-client (~> 1.6.3) - govuk_frontend_toolkit (3.1.0) + govuk_frontend_toolkit (3.2.1) rails (>= 3.1.0) sass (>= 3.2.0) govuk_template (0.10.1) @@ -175,7 +175,7 @@ DEPENDENCIES capybara (= 2.1.0) exception_notification gds-api-adapters (= 7.18.0) - govuk_frontend_toolkit (= 3.1.0) + govuk_frontend_toolkit (= 3.2.1) govuk_template (= 0.10.1) 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 d45f3c8f8..c9522cd67 100644 --- a/app/assets/javascripts/analytics.js +++ b/app/assets/javascripts/analytics.js @@ -1,9 +1,10 @@ -//= require analytics/google-analytics-universal-tracker -//= require analytics/google-analytics-classic-tracker -//= require analytics/tracker -//= require analytics/init +//= require govuk/analytics/google-analytics-universal-tracker +//= require govuk/analytics/google-analytics-classic-tracker +//= require govuk/analytics/tracker +//= require govuk/analytics/print-intent +//= require govuk/analytics/error-tracking +//= require analytics/static-tracker +//= require analytics/init //= require analytics/print-tracking -//= require analytics/print-intent //= require analytics/scroll-tracker -//= require analytics/error-tracking diff --git a/app/assets/javascripts/analytics/error-tracking.js b/app/assets/javascripts/analytics/error-tracking.js deleted file mode 100644 index d06bf04dc..000000000 --- a/app/assets/javascripts/analytics/error-tracking.js +++ /dev/null @@ -1,22 +0,0 @@ -// Extension to track errors using google analytics as a data store. -(function() { - - "use strict"; - var trackJavaScriptError = function (e) { - var errorSource = e.filename + ': ' + e.lineno; - GOVUK.analytics.trackEvent('JavaScript Error', e.message, { - label: errorSource, - value: 1, - nonInteraction: true - }); - }; - - if (window.addEventListener) { - window.addEventListener('error', trackJavaScriptError, false); - } else if (window.attachEvent) { - window.attachEvent('onerror', trackJavaScriptError); - } else { - window.onerror = trackJavaScriptError; - } - -}()); diff --git a/app/assets/javascripts/analytics/google-analytics-classic-tracker.js b/app/assets/javascripts/analytics/google-analytics-classic-tracker.js deleted file mode 100644 index 3d5f2e0f7..000000000 --- a/app/assets/javascripts/analytics/google-analytics-classic-tracker.js +++ /dev/null @@ -1,111 +0,0 @@ -(function() { - "use strict"; - window.GOVUK = window.GOVUK || {}; - - var GoogleAnalyticsClassicTracker = function(id, cookieDomain) { - window._gaq = window._gaq || []; - configureProfile(id, cookieDomain); - allowCrossDomainTracking(); - anonymizeIp(); - - function configureProfile(id, cookieDomain) { - _gaq.push(['_setAccount', id]); - _gaq.push(['_setDomainName', cookieDomain]); - } - - function allowCrossDomainTracking() { - _gaq.push(['_setAllowLinker', true]); - } - - // https://developers.google.com/analytics/devguides/collection/gajs/methods/gaJSApi_gat#_gat._anonymizeIp - function anonymizeIp() { - _gaq.push(['_gat._anonymizeIp']); - } - }; - - GoogleAnalyticsClassicTracker.load = function() { - var ga = document.createElement('script'), - s = document.getElementsByTagName('script')[0]; - - ga.async = true; - ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; - s.parentNode.insertBefore(ga, s); - }; - - // https://developers.google.com/analytics/devguides/collection/gajs/asyncMigrationExamples#VirtualPageviews - GoogleAnalyticsClassicTracker.prototype.trackPageview = function(path) { - var pageview = ['_trackPageview']; - - if (typeof path === "string") { - pageview.push(path); - } - - _gaq.push(pageview); - }; - - // https://developers.google.com/analytics/devguides/collection/gajs/eventTrackerGuide - GoogleAnalyticsClassicTracker.prototype.trackEvent = function(category, action, options) { - var value, - options = options || {}, - hasLabel = false, - hasValue = false, - evt = ["_trackEvent", category, action]; - - // Label is optional - if (typeof options.label === "string") { - hasLabel = true; - evt.push(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)) { - hasValue = true; - - // Push an empty label if not set for correct final argument order - if (!hasLabel) { - evt.push(''); - } - - evt.push(value); - } - } - - // Prevents an event from affecting bounce rate - // https://developers.google.com/analytics/devguides/collection/gajs/eventTrackerGuide#non-interaction - if (options.nonInteraction) { - - // Push empty label/value if not already set, for correct final argument order - if (!hasValue) { - if (!hasLabel) { - evt.push(''); - } - evt.push(0); - } - - evt.push(true); - } - - _gaq.push(evt); - }; - - /* - https://developers.google.com/analytics/devguides/collection/gajs/methods/gaJSApiSocialTracking - network – The network on which the action occurs (e.g. Facebook, Twitter) - action – The type of action that happens (e.g. Like, Send, Tweet) - target – The text value that indicates the subject of the action - */ - GoogleAnalyticsClassicTracker.prototype.trackSocial = function(network, action, target) { - _gaq.push(['_trackSocial', network, action, target]); - }; - - // https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingCustomVariables - GoogleAnalyticsClassicTracker.prototype.setCustomVariable = function(index, value, name, scope) { - _gaq.push(['_setCustomVar', index, name, String(value), scope]); - }; - - GOVUK.GoogleAnalyticsClassicTracker = GoogleAnalyticsClassicTracker; -})(); diff --git a/app/assets/javascripts/analytics/google-analytics-universal-tracker.js b/app/assets/javascripts/analytics/google-analytics-universal-tracker.js deleted file mode 100644 index cdc86a351..000000000 --- a/app/assets/javascripts/analytics/google-analytics-universal-tracker.js +++ /dev/null @@ -1,104 +0,0 @@ -(function() { - "use strict"; - window.GOVUK = window.GOVUK || {}; - - var GoogleAnalyticsUniversalTracker = function(id, cookieDomain) { - configureProfile(id, cookieDomain); - anonymizeIp(); - - function configureProfile(id, cookieDomain) { - sendToGa('create', id, {'cookieDomain': cookieDomain}); - } - - function anonymizeIp() { - // https://developers.google.com/analytics/devguides/collection/analyticsjs/advanced#anonymizeip - sendToGa('set', 'anonymizeIp', true); - } - }; - - GoogleAnalyticsUniversalTracker.load = function() { - (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','//www.google-analytics.com/analytics.js','ga'); - }; - - // https://developers.google.com/analytics/devguides/collection/analyticsjs/pages - GoogleAnalyticsUniversalTracker.prototype.trackPageview = function(path, title) { - if (typeof path === "string") { - var pageviewObject = { - page: path - }; - - if (typeof title === "string") { - pageviewObject.title = title; - } - sendToGa('send', 'pageview', pageviewObject); - } else { - sendToGa('send', 'pageview'); - } - }; - - // https://developers.google.com/analytics/devguides/collection/analyticsjs/events - GoogleAnalyticsUniversalTracker.prototype.trackEvent = function(category, action, options) { - var value, - options = options || {}, - evt = { - hitType: 'event', - eventCategory: category, - eventAction: action - }; - - // Label is optional - if (typeof options.label === "string") { - evt.eventLabel = 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)) { - evt.eventValue = value; - } - } - - // Prevents an event from affecting bounce rate - // https://developers.google.com/analytics/devguides/collection/analyticsjs/events#implementation - if (options.nonInteraction) { - evt.nonInteraction = 1; - } - - sendToGa('send', evt); - }; - - /* - https://developers.google.com/analytics/devguides/collection/analyticsjs/social-interactions - network – The network on which the action occurs (e.g. Facebook, Twitter) - action – The type of action that happens (e.g. Like, Send, Tweet) - target – Specifies the target of a social interaction. - This value is typically a URL but can be any text. - */ - GoogleAnalyticsUniversalTracker.prototype.trackSocial = function(network, action, target) { - sendToGa('send', { - 'hitType': 'social', - 'socialNetwork': network, - 'socialAction': action, - 'socialTarget': target - }); - }; - - // https://developers.google.com/analytics/devguides/collection/analyticsjs/custom-dims-mets - GoogleAnalyticsUniversalTracker.prototype.setDimension = function(index, value) { - sendToGa('set', 'dimension' + index, String(value)); - }; - - function sendToGa() { - if (typeof window.ga === "function") { - ga.apply(window, arguments); - } - } - - GOVUK.GoogleAnalyticsUniversalTracker = GoogleAnalyticsUniversalTracker; -})(); diff --git a/app/assets/javascripts/analytics/init.js b/app/assets/javascripts/analytics/init.js index 855cdf83b..16f9ba687 100644 --- a/app/assets/javascripts/analytics/init.js +++ b/app/assets/javascripts/analytics/init.js @@ -2,14 +2,14 @@ "use strict"; // Load Google Analytics libraries - GOVUK.Tracker.load(); + GOVUK.StaticTracker.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.Tracker({ + var analytics = new GOVUK.StaticTracker({ universalId: 'UA-26179049-7', classicId: 'UA-26179049-1', cookieDomain: cookieDomain diff --git a/app/assets/javascripts/analytics/print-intent.js b/app/assets/javascripts/analytics/print-intent.js deleted file mode 100644 index 54f13f76d..000000000 --- a/app/assets/javascripts/analytics/print-intent.js +++ /dev/null @@ -1,31 +0,0 @@ -// Extension to monitor attempts to print pages. -(function() { - - "use strict"; - var printAttempt = (function() { - GOVUK.analytics.trackEvent('Print Intent', document.location.pathname); - }); - - // Most browsers - if (window.matchMedia) { - var mediaQueryList = window.matchMedia('print'), - mqlListenerCount = 0; - mediaQueryList.addListener(function(mql) { - if (!mql.matches && mqlListenerCount === 0) { - printAttempt(); - mqlListenerCount++; - // If we try and print again in 3 seconds, don't log it - window.setTimeout(function(){ - mqlListenerCount = 0; - // printing will be tracked again now - },1e3); - } - }); - } - - // IE < 10 - if(window.onafterprint){ - window.onafterprint = printAttempt; - } - -}()); diff --git a/app/assets/javascripts/analytics/static-tracker.js b/app/assets/javascripts/analytics/static-tracker.js new file mode 100644 index 000000000..d9118b604 --- /dev/null +++ b/app/assets/javascripts/analytics/static-tracker.js @@ -0,0 +1,116 @@ +(function() { + "use strict"; + window.GOVUK = window.GOVUK || {}; + + var StaticTracker = function(config) { + var classicQueue, + tracker; + + classicQueue = getClassicAnalyticsQueue(); + resetClassicAnalyticsQueue(); + + // 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 + tracker = new GOVUK.Tracker(config); + this.tracker = tracker; + + setPixelDensityDimension(); + setHTTPStatusCodeDimension(); + shimNextPageParams(); + shimClassicAnalyticsQueue(classicQueue); + + // Track initial pageview + tracker.trackPageview(); + + // Begin error and print tracking + GOVUK.analyticsPlugins.error(); + GOVUK.analyticsPlugins.printIntent(); + + function getClassicAnalyticsQueue() { + // Slimmer inserts custom variables into the ga-params script tag + // https://github.com/alphagov/slimmer/blob/master/lib/slimmer/processors/google_analytics_configurator.rb + // Pickout these variables before continuing + return (window._gaq && window._gaq.length) > 0 ? window._gaq.slice() : []; + } + + function resetClassicAnalyticsQueue() { + window._gaq = []; + } + + function setPixelDensityDimension() { + if (window.devicePixelRatio) { + tracker.setDimension(11, window.devicePixelRatio, 'Pixel Ratio', 2); + } + } + + function setHTTPStatusCodeDimension() { + tracker.setDimension(15, window.httpStatusCode || 200, 'httpStatusCode'); + } + + function shimNextPageParams() { + // A cookie is sometimes set by apps to declare the GA parameters that + // should run on the subsequent page. These declare the actual methods + // to call in classic analytics. This is a temporary shim to ensure these + // are applied to both classic and universal before updating apps. + if (GOVUK.cookie && GOVUK.cookie('ga_nextpage_params') !== null){ + var classicParams = GOVUK.cookie('ga_nextpage_params').split(','); + + if (classicParams[0] == "_setCustomVar") { + setDimensionFromCustomVariable(classicParams); + } + + // Delete cookie + GOVUK.cookie('ga_nextpage_params', null); + } + } + + function shimClassicAnalyticsQueue(queue) { + $.each(queue, function(index, classicParams) { + if (classicParams[0] == "_setCustomVar") { + setDimensionFromCustomVariable(classicParams); + } + }); + } + + function setDimensionFromCustomVariable(customVar) { + // index, value, name, scope + tracker.setDimension(customVar[1], customVar[3], customVar[2], customVar[4]); + } + + }; + + StaticTracker.load = function() { + GOVUK.Tracker.load(); + }; + + 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) { + this.tracker.setDimension(index, value, name, scope); + }; + + StaticTracker.prototype.trackShare = function(network) { + this.tracker.trackShare(network); + }; + + StaticTracker.prototype.setSearchPositionDimension = function(position) { + this.tracker.setDimension(21, position, 'searchPosition'); + }; + + StaticTracker.prototype.setResultCountDimension = function(count) { + this.tracker.setDimension(5, count, 'ResultCount'); + }; + + StaticTracker.prototype.setSectionDimension = function(section) { + this.tracker.setDimension(1, section, 'Section'); + }; + + GOVUK.StaticTracker = StaticTracker; +})(); diff --git a/app/assets/javascripts/analytics/tracker.js b/app/assets/javascripts/analytics/tracker.js deleted file mode 100644 index d3418f6d5..000000000 --- a/app/assets/javascripts/analytics/tracker.js +++ /dev/null @@ -1,136 +0,0 @@ -(function() { - "use strict"; - window.GOVUK = window.GOVUK || {}; - - var Tracker = function(args) { - var classicQueue, - setDimension = this.setDimension.bind(this); - - classicQueue = getClassicAnalyticsQueue(); - resetClassicAnalyticsQueue(); - - this.universal = new GOVUK.GoogleAnalyticsUniversalTracker(args.universalId, args.cookieDomain); - this.classic = new GOVUK.GoogleAnalyticsClassicTracker(args.classicId, args.cookieDomain); - - setPixelDensityDimension(); - setHTTPStatusCodeDimension(); - shimNextPageParams(); - shimClassicAnalyticsQueue(classicQueue); - - this.trackPageview(); - - function getClassicAnalyticsQueue() { - // Slimmer inserts custom variables into the ga-params script tag - // https://github.com/alphagov/slimmer/blob/master/lib/slimmer/processors/google_analytics_configurator.rb - // Pickout these variables before continuing - return (window._gaq && window._gaq.length) > 0 ? window._gaq.slice() : []; - } - - function resetClassicAnalyticsQueue() { - window._gaq = []; - } - - function setPixelDensityDimension() { - var pixelRatioDimensionIndex = 11; - - if (window.devicePixelRatio) { - setDimension(pixelRatioDimensionIndex, window.devicePixelRatio, 'Pixel Ratio', 2); - } - } - - function setHTTPStatusCodeDimension() { - var statusCode = window.httpStatusCode || 200, - statusCodeDimensionIndex = 15; - - setDimension(statusCodeDimensionIndex, statusCode, 'httpStatusCode'); - } - - function shimNextPageParams() { - // A cookie is sometimes set by apps to declare the GA parameters that - // should run on the subsequent page. These declare the actual methods - // to call in classic analytics. This is a temporary shim to ensure these - // are applied to both classic and universal before updating apps. - if (GOVUK.cookie && GOVUK.cookie('ga_nextpage_params') !== null){ - var classicParams = GOVUK.cookie('ga_nextpage_params').split(','); - - if (classicParams[0] == "_setCustomVar") { - setDimensionFromCustomVariable(classicParams); - } - - // Delete cookie - GOVUK.cookie('ga_nextpage_params', null); - } - } - - function shimClassicAnalyticsQueue(queue) { - $.each(queue, function(index, classicParams) { - if (classicParams[0] == "_setCustomVar") { - setDimensionFromCustomVariable(classicParams); - } - }); - } - - function setDimensionFromCustomVariable(customVar) { - // index, value, name, scope - setDimension(customVar[1], customVar[3], customVar[2], customVar[4]); - } - - }; - - Tracker.load = function() { - GOVUK.GoogleAnalyticsClassicTracker.load(); - GOVUK.GoogleAnalyticsUniversalTracker.load(); - }; - - Tracker.prototype.trackPageview = function(path, title) { - this.classic.trackPageview(path); - this.universal.trackPageview(path, title); - } - - /* - https://developers.google.com/analytics/devguides/collection/analyticsjs/events - options.label – Useful for categorizing events (eg nav buttons) - options.value – Values must be non-negative. Useful to pass counts - options.nonInteraction – Prevent event from impacting bounce rate - */ - Tracker.prototype.trackEvent = function(category, action, options) { - this.classic.trackEvent(category, action, options); - this.universal.trackEvent(category, action, options); - }; - - Tracker.prototype.trackShare = function(network) { - var target = location.pathname; - this.classic.trackSocial(network, 'share', target); - this.universal.trackSocial(network, 'share', target); - }; - - Tracker.prototype.setDimension = function(index, value, name, scope) { - var PAGE_LEVEL_SCOPE = 3; - scope = scope || PAGE_LEVEL_SCOPE; - - if (typeof index !== "number") { - index = parseInt(index, 10); - } - - if (typeof scope !== "number") { - scope = parseInt(scope, 10); - } - - this.universal.setDimension(index, value); - this.classic.setCustomVariable(index, value, name, scope); - }; - - Tracker.prototype.setSearchPositionDimension = function(position) { - this.setDimension(21, position, 'searchPosition'); - }; - - Tracker.prototype.setResultCountDimension = function(count) { - this.setDimension(5, count, 'ResultCount'); - }; - - Tracker.prototype.setSectionDimension = function(section) { - this.setDimension(1, section, 'Section'); - }; - - GOVUK.Tracker = Tracker; -})(); diff --git a/spec/javascripts/analytics/google-analytics-classic-tracker-spec.js b/spec/javascripts/analytics/google-analytics-classic-tracker-spec.js deleted file mode 100644 index 74e79e0f8..000000000 --- a/spec/javascripts/analytics/google-analytics-classic-tracker-spec.js +++ /dev/null @@ -1,123 +0,0 @@ -describe("GOVUK.GoogleAnalyticsClassicTracker", function() { - var classic; - - beforeEach(function() { - window._gaq = []; - classic = new GOVUK.GoogleAnalyticsClassicTracker('id', 'cookie-domain.com'); - }); - - it('can load the libraries needed to run classic Google Analytics', function() { - delete window._gaq; - $('[src*="google-analytics.com/ga.js"]').remove(); - GOVUK.GoogleAnalyticsClassicTracker.load(); - expect($('head script[async][src*="google-analytics.com/ga.js"]').length).toBe(1); - }); - - describe('when created', function() { - it('configures a Google tracker using the provided profile ID and cookie domain', function() { - expect(window._gaq[0]).toEqual(['_setAccount', 'id']); - expect(window._gaq[1]).toEqual(['_setDomainName', 'cookie-domain.com']); - }); - - it('allows cross site linking', function() { - expect(window._gaq[2]).toEqual(['_setAllowLinker', true]); - }); - - it('anonymises the IP', function() { - expect(window._gaq[3]).toEqual(['_gat._anonymizeIp']); - }); - }); - - describe('when pageviews are tracked', function() { - beforeEach(function() { - // reset queue after setup - window._gaq = []; - }); - - it('sends them to Google Analytics', function() { - classic.trackPageview(); - expect(window._gaq[0]).toEqual(['_trackPageview']); - }); - - it('can track a virtual pageview', function() { - classic.trackPageview('/nicholas-page'); - expect(window._gaq[0]).toEqual(['_trackPageview', '/nicholas-page']); - }); - }); - - describe('when events are tracked', function() { - beforeEach(function() { - // reset queue after setup - window._gaq = []; - }); - - it('sends them to Google Analytics', function() { - classic.trackEvent('category', 'action', {label: 'label'}); - expect(window._gaq[0]).toEqual(['_trackEvent', 'category', 'action', 'label']); - }); - - it('the label is optional', function() { - classic.trackEvent('category', 'action'); - expect(window._gaq[0]).toEqual(['_trackEvent', 'category', 'action']); - }); - - it('a value can be tracked if the label is omitted', function() { - classic.trackEvent('category', 'action', {value: 10}); - expect(window._gaq[0]).toEqual(['_trackEvent', 'category', 'action', '', 10]); - }); - - it('only sends values if they are parseable as numbers', function() { - classic.trackEvent('category', 'action', {label: 'label', value: '10'}); - expect(window._gaq[0]).toEqual(['_trackEvent', 'category', 'action', 'label', 10]); - - classic.trackEvent('category', 'action', {label: 'label', value: 10}); - expect(window._gaq[1]).toEqual(['_trackEvent', 'category', 'action', 'label', 10]); - - classic.trackEvent('category', 'action', {label: 'label', value: 'not a number'}); - expect(window._gaq[2]).toEqual(['_trackEvent', 'category', 'action', 'label']); - }); - - it('can mark an event as non interactive', function() { - classic.trackEvent('category', 'action', {label: 'label', value: 1, nonInteraction: true}); - expect(window._gaq[0]).toEqual(['_trackEvent', 'category', 'action', 'label', 1, true]); - }); - - it('can mark an event as non interactive without a value', function() { - classic.trackEvent('category', 'action', {label: 'label', nonInteraction: true}); - expect(window._gaq[0]).toEqual(['_trackEvent', 'category', 'action', 'label', 0, true]); - }); - - it('can mark an event as non interactive without a label or value', function() { - classic.trackEvent('category', 'action', {nonInteraction: true}); - expect(window._gaq[0]).toEqual(['_trackEvent', 'category', 'action', '', 0, true]); - }); - }); - - describe('when social events are tracked', function() { - beforeEach(function() { - window._gaq = []; - }); - - it('sends them to Google Analytics', function() { - classic.trackSocial('network', 'action', 'target'); - expect(window._gaq[0]).toEqual(['_trackSocial', 'network', 'action', 'target']); - }); - }); - - describe('when setting a custom variable', function() { - beforeEach(function() { - // reset queue after setup - window._gaq = []; - }); - - it('sends the variable to Google Analytics', function() { - classic.setCustomVariable(1, 'value', 'name', 10); - expect(window._gaq[0]).toEqual(['_setCustomVar', 1, 'name', 'value', 10]); - }); - - it('coerces the value to a string', function() { - classic.setCustomVariable(1, 100, 'name', 10); - expect(window._gaq[0]).toEqual(['_setCustomVar', 1, 'name', '100', 10]); - }); - }); -}); diff --git a/spec/javascripts/analytics/google-analytics-universal-tracker-spec.js b/spec/javascripts/analytics/google-analytics-universal-tracker-spec.js deleted file mode 100644 index 2c076857f..000000000 --- a/spec/javascripts/analytics/google-analytics-universal-tracker-spec.js +++ /dev/null @@ -1,122 +0,0 @@ -describe("GOVUK.GoogleAnalyticsUniversalTracker", function() { - var universal; - - beforeEach(function() { - window.ga = function() {}; - spyOn(window, 'ga'); - universal = new GOVUK.GoogleAnalyticsUniversalTracker('id', 'cookie-domain.com'); - }); - - it('can load the libraries needed to run universal Google Analytics', function() { - delete window.ga; - $('[src="//www.google-analytics.com/analytics.js"]').remove(); - GOVUK.GoogleAnalyticsUniversalTracker.load(); - expect($('head script[async][src="//www.google-analytics.com/analytics.js"]').length).toBe(1); - expect(typeof window.ga).toBe('function'); - - window.ga('send message'); - expect(window.ga.q[0]).toEqual(jasmine.any(Object)); - }); - - describe('when created', function() { - var setupArguments; - - beforeEach(function() { - setupArguments = window.ga.calls.allArgs(); - }); - - it('configures a Google tracker using the provided profile ID and cookie domain', function() { - expect(setupArguments[0]).toEqual(['create', 'id', {'cookieDomain': 'cookie-domain.com'}]); - }); - - it('anonymises the IP', function() { - expect(setupArguments[1]).toEqual(['set', 'anonymizeIp', true]); - }); - }); - - describe('when pageviews are tracked', function() { - it('sends them to Google Analytics', function() { - universal.trackPageview(); - expect(window.ga.calls.mostRecent().args).toEqual(['send', 'pageview']); - }); - - it('can track a virtual pageview', function() { - universal.trackPageview('/nicholas-page'); - expect(window.ga.calls.mostRecent().args).toEqual(['send', 'pageview', {page: '/nicholas-page'}]); - }); - - it('can track a virtual pageview with a custom title', function() { - universal.trackPageview('/nicholas-page', 'Nicholas Page'); - expect(window.ga.calls.mostRecent().args).toEqual(['send', 'pageview', {page: '/nicholas-page', title: 'Nicholas Page'}]); - }); - }); - - describe('when events are tracked', function() { - function eventObjectFromSpy() { - return window.ga.calls.mostRecent().args[1]; - } - - it('sends them to Google Analytics', function() { - universal.trackEvent('category', 'action', {label: 'label'}); - expect(window.ga.calls.mostRecent().args).toEqual( - ['send', {hitType: 'event', eventCategory: 'category', eventAction: 'action', eventLabel: 'label'}] - ); - }); - - it('the label is optional', function() { - universal.trackEvent('category', 'action'); - expect(window.ga.calls.mostRecent().args).toEqual( - ['send', {hitType: 'event', eventCategory: 'category', eventAction: 'action'}] - ); - }); - - it('only sends values if they are parseable as numbers', function() { - universal.trackEvent('category', 'action', {label: 'label', value: '10'}); - expect(eventObjectFromSpy()['eventValue']).toEqual(10); - - universal.trackEvent('category', 'action', {label: 'label', value: 10}); - expect(eventObjectFromSpy()['eventValue']).toEqual(10); - - universal.trackEvent('category', 'action', {label: 'label', value: 'not a number'}); - expect(eventObjectFromSpy()['eventValue']).toEqual(undefined); - }); - - it('can mark an event as non interactive', function() { - universal.trackEvent('category', 'action', {label: 'label', value: 0, nonInteraction: true}); - expect(window.ga.calls.mostRecent().args).toEqual( - ['send', { - hitType: 'event', - eventCategory: 'category', - eventAction: 'action', - eventLabel: 'label', - eventValue: 0, - nonInteraction: 1 - }] - ); - }); - }); - - describe('when social events are tracked', function() { - it('sends them to Google Analytics', function() { - universal.trackSocial('network', 'action', 'target'); - expect(window.ga.calls.mostRecent().args).toEqual(['send', { - 'hitType': 'social', - 'socialNetwork': 'network', - 'socialAction': 'action', - 'socialTarget': 'target' - }]); - }); - }); - - describe('when setting a custom dimension', function() { - it('sends the dimension to Google Analytics with the specified index and value', function() { - universal.setDimension(1, 'value'); - expect(window.ga.calls.mostRecent().args).toEqual(['set', 'dimension1', 'value']); - }); - - it('coerces the value to a string', function() { - universal.setDimension(1, 10); - expect(window.ga.calls.mostRecent().args).toEqual(['set', 'dimension1', '10']); - }); - }); -}); diff --git a/spec/javascripts/analytics/tracker-spec.js b/spec/javascripts/analytics/static-tracker-spec.js similarity index 84% rename from spec/javascripts/analytics/tracker-spec.js rename to spec/javascripts/analytics/static-tracker-spec.js index ea52cae9c..163863db5 100644 --- a/spec/javascripts/analytics/tracker-spec.js +++ b/spec/javascripts/analytics/static-tracker-spec.js @@ -1,11 +1,13 @@ -describe("GOVUK.Tracker", function() { +describe("GOVUK.StaticTracker", function() { var tracker; beforeEach(function() { window._gaq = []; window.ga = function() {}; spyOn(window, 'ga'); - tracker = new GOVUK.Tracker({ + spyOn(GOVUK.analyticsPlugins, 'printIntent'); + spyOn(GOVUK.analyticsPlugins, 'error'); + tracker = new GOVUK.StaticTracker({ universalId: 'universal-id', classicId: 'classic-id', cookieDomain: '.www.gov.uk' @@ -40,12 +42,20 @@ describe("GOVUK.Tracker", function() { expect(universalSetupArguments[4]).toEqual(['send', 'pageview']); }); + it('begins print tracking', function() { + expect(GOVUK.analyticsPlugins.printIntent).toHaveBeenCalled(); + }); + + it('begins error tracking', function() { + expect(GOVUK.analyticsPlugins.error).toHaveBeenCalled(); + }); + describe('when there is a cookie with next page parameters set', function() { it('sets them as a dimension', function() { window.ga.calls.reset(); window._gaq = []; spyOn(GOVUK, 'cookie').and.returnValue("_setCustomVar,21,name,value,3"); - tracker = new GOVUK.Tracker({universalId: 'universal-id', classicId: 'classic-id'}); + tracker = new GOVUK.StaticTracker({universalId: 'universal-id', classicId: 'classic-id'}); universalSetupArguments = window.ga.calls.allArgs(); expect(window._gaq[6]).toEqual(['_setCustomVar', 21, 'name', 'value', 3]); @@ -60,7 +70,7 @@ describe("GOVUK.Tracker", function() { ['_setCustomVar', 21, 'name', 'value', 3], ['_setCustomVar', 10, 'name-2', 'value-2', 2] ]; - tracker = new GOVUK.Tracker({universalId: 'universal-id', classicId: 'classic-id'}); + tracker = new GOVUK.StaticTracker({universalId: 'universal-id', classicId: 'classic-id'}); universalSetupArguments = window.ga.calls.allArgs(); }); @@ -91,8 +101,8 @@ describe("GOVUK.Tracker", function() { expect(window.ga.calls.mostRecent().args).toEqual(['send', {hitType: 'event', eventCategory: 'category', eventAction: 'action'}]); window._gaq = []; - tracker.setDimension(1, 'value', 'name'); - expect(window._gaq[0]).toEqual(['_setCustomVar', 1, 'name', 'value', 3]); + tracker.setSectionDimension('value'); + expect(window._gaq[0]).toEqual(['_setCustomVar', 1, 'Section', 'value', 3]); expect(window.ga.calls.mostRecent().args).toEqual(['set', 'dimension1', 'value']); }); });