diff --git a/app/assets/javascripts/analytics.js b/app/assets/javascripts/analytics.js index 3a51f7349..a156d6992 100644 --- a/app/assets/javascripts/analytics.js +++ b/app/assets/javascripts/analytics.js @@ -1,7 +1,6 @@ //= require govuk_publishing_components/lib/cookie-functions //= require analytics/pii //= require analytics_toolkit/google-analytics-universal-tracker -//= require analytics_toolkit/govuk-tracker //= require analytics_toolkit/analytics //= require analytics_toolkit/print-intent //= require analytics_toolkit/error-tracking diff --git a/app/assets/javascripts/analytics/init.js.erb b/app/assets/javascripts/analytics/init.js.erb index 06ed1e1f7..988a13487 100644 --- a/app/assets/javascripts/analytics/init.js.erb +++ b/app/assets/javascripts/analytics/init.js.erb @@ -33,9 +33,7 @@ var analyticsInit = function() { 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" %>' + allowLinker: true }); // Make interface public for virtual pageviews and events diff --git a/app/assets/javascripts/analytics_toolkit/analytics.js b/app/assets/javascripts/analytics_toolkit/analytics.js index b06dc4958..0bf9f79f3 100644 --- a/app/assets/javascripts/analytics_toolkit/analytics.js +++ b/app/assets/javascripts/analytics_toolkit/analytics.js @@ -13,11 +13,6 @@ delete config.universalId this.trackers.push(new GOVUK.GoogleAnalyticsUniversalTracker(universalId, config)) } - if (typeof config.govukTrackerUrl !== 'undefined') { - var govukTrackerUrl = config.govukTrackerUrl - delete config.govukTrackerUrl - this.trackers.push(new GOVUK.GOVUKTracker(govukTrackerUrl)) - } } var PIISafe = function (value) { @@ -38,7 +33,6 @@ Analytics.load = function () { GOVUK.GoogleAnalyticsUniversalTracker.load() - GOVUK.GOVUKTracker.load() } Analytics.prototype.defaultPathForTrackPageview = function (location) { diff --git a/app/assets/javascripts/analytics_toolkit/govuk-tracker.js b/app/assets/javascripts/analytics_toolkit/govuk-tracker.js deleted file mode 100644 index 13e37a06b..000000000 --- a/app/assets/javascripts/analytics_toolkit/govuk-tracker.js +++ /dev/null @@ -1,134 +0,0 @@ -;(function (global) { - 'use strict' - - var $ = global.jQuery - var GOVUK = global.GOVUK || {} - - var GOVUKTracker = function (gifUrl) { - this.gifUrl = gifUrl - this.dimensions = [] - } - - GOVUKTracker.load = function () {} - - GOVUKTracker.prototype.trackPageview = function (path, title, options) { - var pageviewObject - - if (typeof path === 'string') { - pageviewObject = { page: path } - } - - if (typeof title === 'string') { - pageviewObject = pageviewObject || {} - pageviewObject.title = title - } - - // Set an options object for the pageview (e.g. transport, sessionControl) - // https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#transport - if (typeof options === 'object') { - pageviewObject = $.extend(pageviewObject || {}, options) - } - - if (!$.isEmptyObject(pageviewObject)) { - this.sendToTracker('pageview', pageviewObject) - } else { - this.sendToTracker('pageview') - } - } - - // https://developers.google.com/analytics/devguides/collection/analyticsjs/events - GOVUKTracker.prototype.trackEvent = function (category, action, options) { - options = options || {} - var evt = { - eventCategory: category, - eventAction: action - } - - if (options.label) { - evt.eventLabel = options.label - delete options.label - } - - if (options.value) { - evt.eventValue = options.value.toString() - delete options.value - } - - if (typeof options === 'object') { - $.extend(evt, options) - } - - this.sendToTracker('event', evt) - } - - GOVUKTracker.prototype.trackSocial = function (network, action, target, options) { - var trackingOptions = { - 'socialNetwork': network, - 'socialAction': action, - 'socialTarget': target - } - - $.extend(trackingOptions, options) - - this.sendToTracker('social', trackingOptions) - } - - GOVUKTracker.prototype.addLinkedTrackerDomain = function () { /* noop */ } - - GOVUKTracker.prototype.setDimension = function (index, value) { - this.dimensions['dimension' + index] = value - } - - GOVUKTracker.prototype.payloadParams = function (type, payload) { - var data = $.extend({}, - payload, - this.dimensions, - { - eventType: type, - referrer: global.document.referrer, - gaClientId: this.gaClientId, - windowWidth: global.innerWidth, - windowHeight: global.innerHeight, - screenWidth: global.screen.width, - screenHeight: global.screen.height, - colorDepth: global.screen.colorDepth - } - ) - - if (global.performance) { - data.navigationType = global.performance.navigation.type.toString() - data.redirectCount = global.performance.navigation.redirectCount.toString() - - for (var k in global.performance.timing) { - var v = global.performance.timing[k] - if (typeof v === 'string' || typeof v === 'number') { - data['timing_' + k] = v.toString() - } - } - } - - return data - } - - GOVUKTracker.prototype.sendData = function (params) { - var url = this.gifUrl + '?' + $.param(params) - $.get(url) - } - - GOVUKTracker.prototype.sendToTracker = function (type, payload) { - $(global.document).ready(function () { - if (global.ga) { - global.ga(function (tracker) { - this.gaClientId = tracker.get('clientId') - this.sendData(this.payloadParams(type, payload)) - }.bind(this)) - } else { - this.sendData(this.payloadParams(type, payload)) - } - }.bind(this)) - } - - GOVUK.GOVUKTracker = GOVUKTracker - - global.GOVUK = GOVUK -})(window) diff --git a/spec/javascripts/analytics_toolkit/govuk-tracker.spec.js b/spec/javascripts/analytics_toolkit/govuk-tracker.spec.js deleted file mode 100644 index b1f8ef994..000000000 --- a/spec/javascripts/analytics_toolkit/govuk-tracker.spec.js +++ /dev/null @@ -1,182 +0,0 @@ -/* global describe it expect beforeEach spyOn jasmine */ - -var $ = window.jQuery - -describe('GOVUK.GOVUKTracker', function () { - 'use strict' - var GOVUK = window.GOVUK - - var tracker - - function setupFakeGa (clientId) { - window.ga = function (cb) { - cb({ - get: function () { return clientId } - }) - } - } - - beforeEach(function () { - tracker = new GOVUK.GOVUKTracker('http://www.example.com/a.gif') - }) - - describe('sendData', function () { - it('sends the data using AJAX', function () { - spyOn($, 'get') - - tracker.sendData({foo: 'bar'}) - expect($.get).toHaveBeenCalledWith('http://www.example.com/a.gif?foo=bar') - }) - }) - - describe('payloadParams', function () { - it('adds the event type', function () { - var params = tracker.payloadParams('foo', {bar: 'qux'}) - - expect(params.eventType).toEqual('foo') - expect(params.bar).toEqual('qux') - }) - - it('adds the GA Client ID', function () { - tracker.gaClientId = '123456.789012' - var params = tracker.payloadParams('foo') - - expect(params.gaClientId).toEqual('123456.789012') - }) - - it('adds the referrer', function () { - var params = tracker.payloadParams('foo') - - // Can't stub window.referrer so just test that we got a string, not undefined - expect(typeof params.referrer).toEqual('string') - }) - - it('adds performance data', function () { - if (!window.performance) { - window.performance = { 'navigation': {}, 'timing': {} } - window.performance.navigation.type = '0' - window.performance.navigation.redirectCount = '0' - window.performance.timing.domComplete = '123456' - } - - var params = tracker.payloadParams('foo') - - expect(params.navigationType).toEqual('0') - expect(params.redirectCount).toEqual('0') - - if (!window.performance) { - expect(params.timing_domComplete).toEqual('123456') - } else { - expect(params.timing_domComplete).toEqual(window.performance.timing.domComplete.toString()) - } - }) - - it('adds custom dimensions', function () { - tracker.setDimension(1, 'foo') - tracker.setDimension(10, 'bar') - var params = tracker.payloadParams('foo') - - expect(params.dimension1).toEqual('foo') - expect(params.dimension10).toEqual('bar') - }) - - it('adds screen and window measurements', function () { - var params = tracker.payloadParams('foo') - - expect(params.screenWidth).toEqual(window.screen.width) - expect(params.screenHeight).toEqual(window.screen.height) - expect(params.windowWidth).toEqual(window.innerWidth) - expect(params.windowHeight).toEqual(window.innerHeight) - expect(params.colorDepth).toEqual(window.screen.colorDepth) - }) - }) - - describe('sendToTracker', function () { - it('sends when the DOM is complete', function () { - setupFakeGa('123456.789012') - - spyOn(tracker, 'sendData') - tracker.sendToTracker('foo') - - expect(tracker.sendData).toHaveBeenCalledWith(jasmine.any(Object)) - }) - - it('sets the ga Client ID', function () { - setupFakeGa('123456.789012') - - spyOn(tracker, 'sendData') - tracker.sendToTracker('foo') - - expect(tracker.gaClientId).toEqual('123456.789012') - }) - }) - - describe('tracking', function () { - beforeEach(function () { - spyOn(tracker, 'sendToTracker') - }) - - describe('when pageviews are tracked', function () { - it('sends them to the tracker', function () { - tracker.trackPageview() - expect(tracker.sendToTracker.calls.mostRecent().args).toEqual(['pageview']) - }) - }) - - describe('when events are tracked', function () { - it('sends them to the tracker', function () { - tracker.trackEvent('category', 'action', {label: 'label'}) - expect(tracker.sendToTracker.calls.mostRecent().args).toEqual( - ['event', {eventCategory: 'category', eventAction: 'action', eventLabel: 'label'}] - ) - }) - - it('tracks custom dimensions', function () { - tracker.trackEvent('category', 'action', {dimension29: 'Home'}) - expect(tracker.sendToTracker.calls.mostRecent().args).toEqual( - ['event', {eventCategory: 'category', eventAction: 'action', dimension29: 'Home'}] - ) - }) - - it('the label is optional', function () { - tracker.trackEvent('category', 'action') - expect(tracker.sendToTracker.calls.mostRecent().args).toEqual( - ['event', {eventCategory: 'category', eventAction: 'action'}] - ) - }) - - it('sends the page if supplied', function () { - tracker.trackEvent('category', 'action', {page: '/path/to/page'}) - expect(tracker.sendToTracker.calls.mostRecent().args).toEqual( - ['event', {eventCategory: 'category', eventAction: 'action', page: '/path/to/page'}] - ) - }) - - it('tracks multiple events', function () { - tracker.trackEvent('category', 'action', {label: 'foo'}) - tracker.trackEvent('category', 'action', {label: 'bar'}) - - expect(tracker.sendToTracker).toHaveBeenCalledWith( - 'event', {eventCategory: 'category', eventAction: 'action', eventLabel: 'foo'} - ) - expect(tracker.sendToTracker).toHaveBeenCalledWith( - 'event', {eventCategory: 'category', eventAction: 'action', eventLabel: 'bar'} - ) - }) - }) - - describe('when social events are tracked', function () { - it('sends them to Google Analytics', function () { - tracker.trackSocial('network', 'action', 'target') - expect(tracker.sendToTracker.calls.mostRecent().args).toEqual([ - 'social', - { - 'socialNetwork': 'network', - 'socialAction': 'action', - 'socialTarget': 'target' - } - ]) - }) - }) - }) -})