From 660cf851ff92888b17407ad075469de0528a7c74 Mon Sep 17 00:00:00 2001 From: Edd Sowden Date: Thu, 8 Jan 2015 10:12:03 +0000 Subject: [PATCH] Remove old analytics JavaScript This analytics was added for the performance platform to create an engagement score of pages. They have since stopped using these events and they are no longer needed. Remove them so they stop sending lots of events to Google Analytics. Tidy up the function in `tracking.js` which is still needed so that it is clearer as to what is going on. --- app/assets/javascripts/analytics.js | 2 - .../analytics/events-cookie-handler.js | 87 ---- .../analytics/tracking-strategies.js | 149 ------ app/assets/javascripts/analytics/tracking.js | 145 +----- .../analytics/events-cookie-handler-spec.js | 111 ----- spec/javascripts/analytics/tracking-spec.js | 460 ------------------ 6 files changed, 7 insertions(+), 947 deletions(-) delete mode 100644 app/assets/javascripts/analytics/events-cookie-handler.js delete mode 100644 app/assets/javascripts/analytics/tracking-strategies.js delete mode 100644 spec/javascripts/analytics/events-cookie-handler-spec.js delete mode 100644 spec/javascripts/analytics/tracking-spec.js diff --git a/app/assets/javascripts/analytics.js b/app/assets/javascripts/analytics.js index dbbbaf8f4..44078d507 100644 --- a/app/assets/javascripts/analytics.js +++ b/app/assets/javascripts/analytics.js @@ -1,5 +1,3 @@ -//= require analytics/events-cookie-handler -//= require analytics/tracking-strategies //= require analytics/tracking //= require analytics/print-tracking //= require analytics/print-intent diff --git a/app/assets/javascripts/analytics/events-cookie-handler.js b/app/assets/javascripts/analytics/events-cookie-handler.js deleted file mode 100644 index 4d171426a..000000000 --- a/app/assets/javascripts/analytics/events-cookie-handler.js +++ /dev/null @@ -1,87 +0,0 @@ -(function() { - "use strict"; - var root = this; - - root.GOVUK = root.GOVUK || {}; - var GOVUK = root.GOVUK; - - GOVUK.Analytics = root.GOVUK.Analytics || {}; - - GOVUK.Analytics.internalSiteEvents = function () { - - var COOKIE_NAME = "GDS_successEvents"; - var eventQueue = []; - - var loadCookie = function () { - var value = GOVUK.cookie(COOKIE_NAME); - if (value) { - value = jQuery.parseJSON(jQuery.base64Decode(value)); - } else { - value = []; - } - eventQueue = value; - }; - - var sendCookieEvents = function () { - loadCookie(); - $(eventQueue).each(function () { - GOVUK.sendToAnalytics(this); - }); - eventQueue = []; - GOVUK.cookie(COOKIE_NAME, null); - }; - - var pushCookieEvent = function (event) { - eventQueue.push(event); - GOVUK.cookie(COOKIE_NAME, jQuery.base64Encode(JSON.stringify(eventQueue)), { days: 4 * 30 }); - }; - - return { - push:pushCookieEvent, - sendAll:sendCookieEvents - }; - }(); - - GOVUK.Analytics.entryTokens = function () { - - var COOKIE_NAME = "GDS_analyticsTokens"; - - var valueIsInArray = function (value, arr) { - return $.inArray(value, arr) !== -1; - }; - - var uniqueIdentifierOfArtifact = function () { - return GOVUK.Analytics.getSlug(document.URL, GOVUK.Analytics.Trackers[GOVUK.Analytics.Format].slugLocation); - }; - - var assignToken = function () { - var tokens = JSON.parse(GOVUK.cookie(COOKIE_NAME)); - if (!tokens) tokens = []; - if (!valueIsInArray(uniqueIdentifierOfArtifact(), tokens)) - { - tokens.push(uniqueIdentifierOfArtifact()); - GOVUK.cookie(COOKIE_NAME, JSON.stringify(tokens), { days: 4 * 30 }); - } - }; - - var revokeToken = function () { - var tokens = JSON.parse(GOVUK.cookie(COOKIE_NAME)); - var positionOfToken = $.inArray(uniqueIdentifierOfArtifact(),tokens); - if (positionOfToken !== -1) { - tokens.splice(positionOfToken,1); - GOVUK.cookie(COOKIE_NAME, JSON.stringify(tokens), { days: 4 * 30 }); - } - }; - - var tokenExists = function () { - var tokens = JSON.parse(GOVUK.cookie(COOKIE_NAME)); - return valueIsInArray(uniqueIdentifierOfArtifact(), tokens); - }; - - return { - assignToken:assignToken, - revokeToken:revokeToken, - tokenExists:tokenExists - }; - }(); -}).call(this); diff --git a/app/assets/javascripts/analytics/tracking-strategies.js b/app/assets/javascripts/analytics/tracking-strategies.js deleted file mode 100644 index b88f20b8f..000000000 --- a/app/assets/javascripts/analytics/tracking-strategies.js +++ /dev/null @@ -1,149 +0,0 @@ -(function() { - "use strict"; - var root = this; - - root.GOVUK = root.GOVUK || {}; - var GOVUK = root.GOVUK; - - GOVUK.Analytics = GOVUK.Analytics || {}; - GOVUK.Analytics.Trackers = {}; - - /* - * Available methods on control: - * - trackTimeBasedSuccess(millisecondsUntilSuccess) - * - trackLinks(linkSelector) - * - trackSuccess(isNonInteraction) (see: https://developers.google.com/analytics/devguides/collection/gajs/eventTrackerGuide#non-interaction) - * - * Additional methods: - * Trackers can optionally add functions to override control over whether we fire - * entry or success events. - * - * - shouldTrackEntry() bool - * - shouldTrackSuccess() bool - */ - - GOVUK.Analytics.trackingPrefixes = { - MAINSTREAM: 'MS_', - INSIDE_GOV: 'IG_' - }; - - GOVUK.Analytics.Tracker = function (prefix, slugLocation, trackingSpecification) { - var tracker = trackingSpecification; - tracker.prefix = prefix; - tracker.slugLocation = slugLocation; - return tracker; - }; - - /* Guide */ - GOVUK.Analytics.Trackers.guide = new GOVUK.Analytics.Tracker( - GOVUK.Analytics.trackingPrefixes.MAINSTREAM, - 0, - function (trackingApi) { - trackingApi.trackTimeBasedSuccess(7000); - trackingApi.trackInternalLinks($("#content a")); - }); - - /* Transaction (services) */ - GOVUK.Analytics.Trackers.transaction = new GOVUK.Analytics.Tracker( - GOVUK.Analytics.trackingPrefixes.MAINSTREAM, - 0, - function (trackingApi) { - trackingApi.trackInternalLinks($("#content a")); - trackingApi.trackLinks($("#get-started a")); - }); - - /* Benefit */ - GOVUK.Analytics.Trackers.programme = new GOVUK.Analytics.Tracker( - GOVUK.Analytics.trackingPrefixes.MAINSTREAM, - 0, - function (trackingApi) { - trackingApi.trackTimeBasedSuccess(7000); - trackingApi.trackInternalLinks($("#content a")); - }); - - /* Quick answer */ - GOVUK.Analytics.Trackers.answer = new GOVUK.Analytics.Tracker( - GOVUK.Analytics.trackingPrefixes.MAINSTREAM, - 0, - function (trackingApi) { - trackingApi.trackTimeBasedSuccess(7000); - trackingApi.trackInternalLinks($("#content a")); - }); - - /* Smart Answer */ - /** - * The Entry event should only be fired on the first page of the smart answer (the one with the get started button). - * The Success event should only be fired if the user is coming from the first page and the 'smartanswerOutcome' custom - * event has been fired. - * - */ - GOVUK.Analytics.Trackers.smart_answer = new GOVUK.Analytics.Tracker( - GOVUK.Analytics.trackingPrefixes.MAINSTREAM, - 0, - function (trackingApi) { - if (GOVUK.Analytics.Trackers.smart_answer.isAjaxNavigation()) { - // For AJAX navigation we expect an event on success - $(document).bind("smartanswerOutcome", trackingApi.trackSuccessFunc(false)); - } else { - // For multi-page navigation, we need to check if this page has an outcome - $(function () { - if ($("article.outcome").length === 1) { - trackingApi.trackSuccess(true); - } - }); - } - }); - - var browserSupportsHtml5HistoryApi = browserSupportsHtml5HistoryApi || function () { - return !!(history && history.replaceState && history.pushState); - }; - - GOVUK.Analytics.Trackers.smart_answer.isAjaxNavigation = browserSupportsHtml5HistoryApi; - - GOVUK.Analytics.Trackers.smart_answer.shouldTrackEntry = function () { - return GOVUK.Analytics.isRootOfArtefact(document.URL, GOVUK.Analytics.Trackers.smart_answer.slugLocation); - }; - - GOVUK.Analytics.Trackers.smart_answer.shouldTrackSuccess = function () { - if (GOVUK.Analytics.Trackers.smart_answer.isAjaxNavigation()) { - // For AJAX navigation we should track success on the smart answers flow page (non-root page) - return GOVUK.Analytics.entryTokens.tokenExists() && !GOVUK.Analytics.isRootOfArtefact(document.URL, GOVUK.Analytics.Trackers.smart_answer.slugLocation); - } else { - // For multi-page navigation, we should track success if entry event has been fired (token exists) - return GOVUK.Analytics.entryTokens.tokenExists() && $("article.outcome").length === 1; - } - }; - - - GOVUK.Analytics.Trackers.policy = - new GOVUK.Analytics.Tracker( - GOVUK.Analytics.trackingPrefixes.INSIDE_GOV, - 2, - function (trackingApi) { - trackingApi.trackTimeBasedSuccess(30000); - - trackingApi.trackInternalLinks($("#page a").filter(function () { - return !GOVUK.Analytics.isLinkToFragmentInCurrentDocument(this); - })); - - } - ); - - GOVUK.Analytics.Trackers.detailed_guidance = - new GOVUK.Analytics.Tracker( - GOVUK.Analytics.trackingPrefixes.INSIDE_GOV, - 0, - function (trackingApi) { - trackingApi.trackTimeBasedSuccess(30000); - trackingApi.trackInternalLinks($("#page a")); - }); - - GOVUK.Analytics.Trackers.news = new GOVUK.Analytics.Tracker( - GOVUK.Analytics.trackingPrefixes.INSIDE_GOV, - 2, - function (trackingApi) { - trackingApi.trackInternalLinks($("#page a")); - trackingApi.trackTimeBasedSuccess(30000); - }); - -}).call(this); diff --git a/app/assets/javascripts/analytics/tracking.js b/app/assets/javascripts/analytics/tracking.js index 8fc0de646..6e34beb5f 100644 --- a/app/assets/javascripts/analytics/tracking.js +++ b/app/assets/javascripts/analytics/tracking.js @@ -1,141 +1,10 @@ (function() { - "use strict"; - var root = this; + "use strict"; - root.GOVUK = root.GOVUK || {}; - var GOVUK = root.GOVUK; + window.GOVUK = window.GOVUK || {}; + window._gaq = window._gaq || []; - GOVUK.Analytics = GOVUK.Analytics || {}; - - root._gaq = root._gaq || []; - - GOVUK.sendToAnalytics = function (analyticsData) { - root._gaq.push(analyticsData); - }; - - GOVUK.Analytics.isTheSameArtefact = function(currentUrl, previousUrl, slugLocation) { - var rootOfArtefact = function(url) { - return url.split("/").slice(0, 4 + slugLocation).join("/"); - }; - - var currentSlug = rootOfArtefact(currentUrl).replace(/#.*$/, ''); - var previousSlug = rootOfArtefact(previousUrl).replace(/#.*$/, ''); - return currentSlug === previousSlug; - }; - - GOVUK.Analytics.getSlug = function(url, slugLocation) { - return url.split('/')[3 + slugLocation].split("#")[0].split("?")[0]; - }; - - GOVUK.Analytics.isRootOfArtefact = function(url, slugLocation) { - return url.replace(/\/$/, "").split("/").slice(3 + slugLocation).length === 1; - }; - - GOVUK.Analytics.isLinkToFragmentInCurrentDocument = function(anchorElement) { - var linksToCurrentDocument = anchorElement.href.split("#")[0] === document.URL.split("#")[0]; - var hasFragment = anchorElement.hash !== ""; - return linksToCurrentDocument && hasFragment; - } - - GOVUK.Analytics.startAnalytics = function () { - var ENTER_KEYCODE = 13; - var success = false; - var prefix = 'none'; - var format = GOVUK.Analytics.Format, - trackingStrategy = GOVUK.Analytics.Trackers[format]; - - /** - * Decide whether we should track an event based on a condition function. - * If the condition function isn't defined then the default condition is used. - * - * @param condition, an optional function that returns a boolean - * @return bool - */ - var shouldTrackEvent = function(condition, defaultValue) { - if (typeof condition === "function") { - return condition(); - } else { - return defaultValue; - } - }; - - var createEvent = function(type, isNonInteraction) { - var slug = GOVUK.Analytics.getSlug(document.URL, trackingStrategy.slugLocation); - return ['_trackEvent', prefix + GOVUK.Analytics.Format, slug, type, 0, isNonInteraction]; - }; - - var handleInternalLink = function () { - if (success) return; - success = true; - var event = createEvent("Success", false); - if (GOVUK.Analytics.isLinkToFragmentInCurrentDocument(this)) { - GOVUK.sendToAnalytics(event); - } else { - GOVUK.Analytics.internalSiteEvents.push(event); - } - }; - - var trackLinks = function(selection, trackExternal) { - // TODO: refactor this to use jQuery("#content").on("click", "a", fireFunction) - selection.each(function () { - var linkToTrack = $(this); - - if (this.hostname === window.location.hostname) { - linkToTrack.click(handleInternalLink); - linkToTrack.keydown(function(e) { - if (e.which === ENTER_KEYCODE) { - handleInternalLink.call(this, e); - } - }); - } - }); - }; - - var trackingApi = { - trackSuccessFunc: function(isNonInteraction) { - if (isNonInteraction === undefined) { - isNonInteraction = false; - } - return function() { - trackingApi.trackSuccess(isNonInteraction); - }; - }, - trackSuccess: function (isNonInteraction) { - if (isNonInteraction === undefined) { - isNonInteraction = false; - } - if (success) return; - success = true; - GOVUK.sendToAnalytics(createEvent("Success", isNonInteraction)); - }, - trackInternalLinks: function(selection) { - trackLinks(selection, false); - }, - trackLinks:function (selection) { - trackLinks(selection, true); - }, - trackTimeBasedSuccess:function (time) { - setTimeout(trackingApi.trackSuccessFunc(true), time); - } - }; - - if (GOVUK.Analytics.Trackers[format] !== undefined) prefix = GOVUK.Analytics.Trackers[format].prefix; - if (typeof trackingStrategy === "function") { - var isTheSameArtefact = GOVUK.Analytics.isTheSameArtefact(document.URL, document.referrer, trackingStrategy.slugLocation); - if (shouldTrackEvent(trackingStrategy.shouldTrackEntry, !isTheSameArtefact)) { - GOVUK.sendToAnalytics(createEvent("Entry", true)); - GOVUK.Analytics.entryTokens.assignToken(); - } - if (shouldTrackEvent(trackingStrategy.shouldTrackSuccess, !isTheSameArtefact)) { - trackingStrategy(trackingApi); - GOVUK.Analytics.entryTokens.revokeToken(); - } - } - - GOVUK.Analytics.internalSiteEvents.sendAll(); - - return trackingApi; - }; - - $(GOVUK.Analytics.startAnalytics); -}).call(this); + GOVUK.sendToAnalytics = function (analyticsData) { + window._gaq.push(analyticsData); + }; +}()); diff --git a/spec/javascripts/analytics/events-cookie-handler-spec.js b/spec/javascripts/analytics/events-cookie-handler-spec.js deleted file mode 100644 index c2d2a5ca6..000000000 --- a/spec/javascripts/analytics/events-cookie-handler-spec.js +++ /dev/null @@ -1,111 +0,0 @@ -describe("analytics cookie tokens", function () { - describe("GOVUK.Analytics.entryTokens", function () { - var cookieName = "GDS_analyticsTokens"; - - beforeEach(function () { - GOVUK.cookie(cookieName, null); - spyOn(GOVUK.Analytics, "getSlug").and.callFake(function () { return getSlugResult; }); - GOVUK.Analytics.Format = "guide"; - }); - - it("should write a token into a cookie array", function () { - getSlugResult = 12; - GOVUK.Analytics.entryTokens.assignToken(); - - expect(GOVUK.cookie(cookieName)).toBe("[12]"); - }); - - it("should write multiple tokens to the array", function () { - getSlugResult = 12; - GOVUK.Analytics.entryTokens.assignToken(); - getSlugResult = 42; - GOVUK.Analytics.entryTokens.assignToken(); - - expect(GOVUK.cookie(cookieName)).toBe("[12,42]"); - }); - - it("should not write the same token multiple times", function () { - getSlugResult = 15; - GOVUK.Analytics.entryTokens.assignToken(); - GOVUK.Analytics.entryTokens.assignToken(); - - expect(GOVUK.cookie(cookieName)).toBe("[15]"); - }); - - it("should remove a token from the cookie array", function () { - getSlugResult = 20; - GOVUK.Analytics.entryTokens.assignToken(); - - GOVUK.Analytics.entryTokens.revokeToken(); - - expect(GOVUK.cookie(cookieName)).toBe("[]"); - }); - - it("should preserve the remaining tokens when one is removed", function () { - getSlugResult = 16; - GOVUK.Analytics.entryTokens.assignToken(); - getSlugResult = 19; - GOVUK.Analytics.entryTokens.assignToken(); - - GOVUK.Analytics.entryTokens.revokeToken(); - - expect(GOVUK.cookie(cookieName)).toBe("[16]"); - }); - - it("should not blow up if a token is revoked when the cookie array is empty", function () { - getSlugResult = 33; - GOVUK.Analytics.entryTokens.revokeToken(); - - // should not throw any error - }); - - it("should not blow up if a nonexistent token is revoked", function () { - getSlugResult = 21; - GOVUK.Analytics.entryTokens.assignToken(); - getSlugResult = 42; - - GOVUK.Analytics.entryTokens.revokeToken(); - - expect(GOVUK.cookie(cookieName)).toBe("[21]"); - }); - - it("should return true if token has been assigned", function () { - getSlugResult = 25; - GOVUK.Analytics.entryTokens.assignToken(); - - expect(GOVUK.Analytics.entryTokens.tokenExists()).toBeTruthy(); - }); - - it("should return false if token has not been assigned", function () { - getSlugResult = 35; - - expect(GOVUK.Analytics.entryTokens.tokenExists()).toBeFalsy(); - }); - }); - - describe("GOVUK.Analytics.internalSiteEvents", function () { - afterEach(function () { - GOVUK.Analytics.internalSiteEvents.sendAll(); - }); - - it("should store an event in the cookie on push", function () { - GOVUK.Analytics.internalSiteEvents.push("event"); - - expect(GOVUK.cookie("GDS_successEvents")).toEqual(jQuery.base64Encode(JSON.stringify(["event"]))); - }); - - it("should send the stored events to Google Analytics on sendAll", function () { - spyOn(GOVUK, 'sendToAnalytics'); - - GOVUK.Analytics.internalSiteEvents.push("event1"); - GOVUK.Analytics.internalSiteEvents.push("event2"); - GOVUK.Analytics.internalSiteEvents.sendAll(); - - var args = GOVUK.sendToAnalytics.calls.allArgs(); - expect(args.length).toBe(2); - expect(args[0][0]).toBeEqualAsJSON("event1"); - expect(args[1][0]).toBeEqualAsJSON("event2"); - expect(GOVUK.cookie("GDS_successEvents")).toBe(null); - }); - }); -}); diff --git a/spec/javascripts/analytics/tracking-spec.js b/spec/javascripts/analytics/tracking-spec.js deleted file mode 100644 index c3e14cdc5..000000000 --- a/spec/javascripts/analytics/tracking-spec.js +++ /dev/null @@ -1,460 +0,0 @@ -describe("success event tracking", function () { - - var guideMarkup = $("
" + - "link" + - "link" + - "
" + - "link" + - "
" + - "
"); - - var articleContainer = $("
link
"); - - beforeEach(function () { - $('a').unbind(); - articleContainer.clone().appendTo('body'); - guideMarkup.clone().appendTo('body'); - spyOn(GOVUK, 'sendToAnalytics'); - }); - - afterEach(function () { - GOVUK.cookie("successEvents", null); - $(".test-stub").remove(); - $.event.trigger("smartanswerOutcome"); - }); - - describe("isTheSameArtefact", function () { - it("should support basic case", function () { - var result = GOVUK.Analytics.isTheSameArtefact( - "http://www.gov.uk/claim-tax/first", - "http://www.gov.uk/claim-tax/second", - 0); - - expect(result).toBeTruthy(); - }); - - it("should support coming to very same url", function () { - var result = GOVUK.Analytics.isTheSameArtefact( - "http://www.gov.uk/claim-tax/first", - "http://www.gov.uk/claim-tax/first", - 0); - - expect(result).toBeTruthy(); - }); - - it("should support local anchor on previous url", function () { - var result = GOVUK.Analytics.isTheSameArtefact( - "http://www.gov.uk/claim-tax", - "http://www.gov.uk/claim-tax#foobar", - 0); - - expect(result).toBeTruthy(); - }); - - it("should support local anchor on current url", function () { - var result = GOVUK.Analytics.isTheSameArtefact( - "http://www.gov.uk/claim-tax#foobar", - "http://www.gov.uk/claim-tax", - 0); - - expect(result).toBeTruthy(); - }); - - it("should support different slug locations", function () { - var result = GOVUK.Analytics.isTheSameArtefact( - "https://www.gov.uk/government/policies/making-sure-council-tax-payers-get-good-value-for-money", - "https://www.gov.uk/government/policies/making-sure-council-tax-payers-get-good-value-for-money/foo", - 2); - - expect(result).toBeTruthy(); - }); - - it("should support different slug locations", function () { - var result = GOVUK.Analytics.isTheSameArtefact( - "https://www.gov.uk/government/policies/making-sure-council-tax-payers-get-good-value-for-money", - "https://www.gov.uk/government/policies/something-different", - 2); - - expect(result).toBeFalsy(); - }); - }); - - describe("isRootArtefact", function () { - it("should be true for standard artefact url", function () { - expect(GOVUK.Analytics.isRootOfArtefact("http://smartanswers.dev.gov.uk/student-finance-calculator", 0)).toBeTruthy(); - expect(GOVUK.Analytics.isRootOfArtefact("http://smartanswers.dev.gov.uk/government/policies/foo", 2)).toBeTruthy(); - }); - - it("should be true for standard artefact url ending with a slash", function () { - expect(GOVUK.Analytics.isRootOfArtefact("http://smartanswers.dev.gov.uk/student-finance-calculator/", 0)).toBeTruthy(); - expect(GOVUK.Analytics.isRootOfArtefact("http://smartanswers.dev.gov.uk/government/policies/foo/", 2)).toBeTruthy(); - }); - - it("should be false for non-root artefact url", function () { - expect(GOVUK.Analytics.isRootOfArtefact("http://smartanswers.dev.gov.uk/student-finance-calculator/y", 0)).toBeFalsy(); - expect(GOVUK.Analytics.isRootOfArtefact("http://smartanswers.dev.gov.uk/government/policies/foo/bar", 2)).toBeFalsy(); - }); - - }); - - describe("getSlug", function () { - it("should return the slug from a url", function () { - var mainstreamSlug = GOVUK.Analytics.getSlug("https://www.gov.uk/student-finance-calculator", 0); - var insideGovPolicySlug = GOVUK.Analytics.getSlug("https://www.gov.uk/government/policies/making-sure-council-tax-payers-get-good-value-for-money", 2); - - expect(mainstreamSlug).toEqual("student-finance-calculator"); - expect(insideGovPolicySlug).toEqual("making-sure-council-tax-payers-get-good-value-for-money"); - }); - - it("should return slug even if there is a fragment", function () { - var result = GOVUK.Analytics.getSlug("https://www.gov.uk/student-finance-calculator#foobar", 0); - - expect(result).toEqual("student-finance-calculator"); - }); - - it("should return slug even if there is more of the request path", function () { - var result = GOVUK.Analytics.getSlug("https://www.gov.uk/student-finance-calculator/foobar", 0); - - expect(result).toEqual("student-finance-calculator"); - }); - - it("should return slug even if there is a query string", function () { - var result = GOVUK.Analytics.getSlug("https://www.gov.uk/student-finance-calculator?foobar=barfoo", 0); - - expect(result).toEqual("student-finance-calculator"); - }); - }); - - describe("analytics integration", function () { - beforeEach(function() { - spyOn(GOVUK.Analytics, "getSlug").and.returnValue(""); - }); - - it("should register entry event", function () { - GOVUK.Analytics.Format = 'guide'; - GOVUK.Analytics.startAnalytics(); - - var arguments = GOVUK.sendToAnalytics.calls.allArgs(); - var expectedDataToSendToGoogle = ['_trackEvent', 'MS_guide', '', 'Entry', 0, true]; - expect(arguments.length).toBe(1); - // using JSONEquals because there is a bug in the .toHaveBeenCalledWith() method - // see: https://github.com/pivotal/jasmine/issues/45 - expect(arguments[0][0]).toBeEqualAsJSON(expectedDataToSendToGoogle); - }); - - it("should only call guide strategy when format is guide", function () { - GOVUK.Analytics.Format = 'guide'; - spyOn(GOVUK.Analytics.Trackers, 'guide'); - GOVUK.Analytics.Trackers.guide.slugLocation = 0; - spyOn(GOVUK.Analytics.Trackers, 'transaction'); - - GOVUK.Analytics.startAnalytics(); - - expect(GOVUK.Analytics.Trackers.transaction).not.toHaveBeenCalled(); - expect(GOVUK.Analytics.Trackers.guide).toHaveBeenCalled(); - }); - - it("should only call transaction strategy when format is transaction", function () { - GOVUK.Analytics.Format = 'transaction'; - spyOn(GOVUK.Analytics.Trackers, 'guide'); - spyOn(GOVUK.Analytics.Trackers, 'transaction'); - GOVUK.Analytics.Trackers.transaction.slugLocation = 0; - - GOVUK.Analytics.startAnalytics(); - - expect(GOVUK.Analytics.Trackers.transaction).toHaveBeenCalled(); - expect(GOVUK.Analytics.Trackers.guide).not.toHaveBeenCalled(); - }); - - it("should not error if format is not supported", function () { - GOVUK.Analytics.Format = 'blahblah'; - - GOVUK.Analytics.startAnalytics(); - }); - }); - - describe("user interactions", function () { - beforeEach(function() { - spyOn(GOVUK.Analytics, "getSlug").and.returnValue(""); - }); - - it("should register success event for guide format when an internal link inside #content receives a 'return' key press", function () { - GOVUK.Analytics.Format = 'guide'; - GOVUK.Analytics.startAnalytics(); - - var e = jQuery.Event("keydown"); - e.which = 13; - e.keyCode = 13; - $("#guide-internal-link").trigger(e); - - var arguments = GOVUK.sendToAnalytics.calls.allArgs(); - expect(arguments.length).toBe(2); - expect(arguments[1][0]).toBeEqualAsJSON(['_trackEvent', 'MS_guide', '', 'Success', 0, false]); - }); - - it("should register success event for guide format when an internal link inside #content is clicked", function () { - GOVUK.Analytics.Format = 'guide'; - GOVUK.Analytics.startAnalytics(); - - $('#guide-internal-link').click(); - - var arguments = GOVUK.sendToAnalytics.calls.allArgs(); - expect(arguments.length).toBe(2); - expect(arguments[1][0]).toBeEqualAsJSON(['_trackEvent', 'MS_guide', '', 'Success', 0, false]); - }); - - it("should not register multiple guide success events when navigating to items on the same page", function () { - GOVUK.Analytics.Format = 'guide'; - GOVUK.Analytics.startAnalytics(); - - $('#guide-internal-link').click(); - $('#guide-internal-link').click(); - $('#guide-internal-link').click(); - $('#guide-internal-link').click(); - - var arguments = GOVUK.sendToAnalytics.calls.allArgs(); - expect(arguments.length).toBe(2); - expect(arguments[1][0]).toBeEqualAsJSON(['_trackEvent', 'MS_guide', '', 'Success', 0, false]); - }); - - it("should not register external click if internal link has been clicked", function () { - GOVUK.Analytics.Format = 'guide'; - GOVUK.Analytics.startAnalytics(); - - $('#guide-internal-link').click(); - $('#guide-external-link').click(); - - var href = $("#guide-external-link").prop("href"); - expect(href).toEqual("http://www.google.com/"); - }); - - it("should not register internal click if external link has been clicked", function () { - GOVUK.Analytics.Format = 'guide'; - GOVUK.Analytics.startAnalytics(); - - $('#guide-external-link').click(); - $('#guide-internal-link').click(); - - expect(GOVUK.cookie("successEvents")).toBe(null); - }); - - it("should register a smart answer success if the smartanswerOutcome event is fired", function () { - GOVUK.Analytics.Format = 'smart_answer'; - spyOn(GOVUK.Analytics.Trackers.smart_answer, 'shouldTrackSuccess').and.returnValue(true); - GOVUK.Analytics.startAnalytics(); - - $.event.trigger("smartanswerOutcome"); - - var arguments = GOVUK.sendToAnalytics.calls.allArgs(); - expect(arguments.length).toBe(1); - expect(arguments[0][0]).toBeEqualAsJSON(['_trackEvent', 'MS_smart_answer', '', 'Success', 0, false]); - }); - - it("should not register a smart answer success if a smartanswerOutcome event has already been fired", function () { - GOVUK.Analytics.Format = 'smart_answer'; - spyOn(GOVUK.Analytics.Trackers.smart_answer, 'shouldTrackSuccess').and.returnValue(true); - GOVUK.Analytics.startAnalytics(); - - $.event.trigger("smartanswerOutcome"); - $.event.trigger("smartanswerOutcome"); - - var arguments = GOVUK.sendToAnalytics.calls.allArgs(); - expect(arguments.length).toBe(1); - expect(arguments[0][0]).toBeEqualAsJSON(['_trackEvent', 'MS_smart_answer', '', 'Success', 0, false]); - }); - - it("should register custom condition for entry and success tracking for smart answers", function () { - GOVUK.Analytics.Format = 'smart_answer'; - spyOn(GOVUK.Analytics.Trackers.smart_answer, 'shouldTrackEntry'); - spyOn(GOVUK.Analytics.Trackers.smart_answer, 'shouldTrackSuccess'); - - GOVUK.Analytics.startAnalytics(); - - expect(GOVUK.Analytics.Trackers.smart_answer.shouldTrackEntry).toHaveBeenCalled(); - expect(GOVUK.Analytics.Trackers.smart_answer.shouldTrackSuccess).toHaveBeenCalled(); - }); - }); - - describe("Success tracking for inside gov. policy format", function () { - var policyMarkup = $("
" + - "" + - "" + - "
"); - - beforeEach(function () { - spyOn(GOVUK.Analytics, "getSlug").and.returnValue(""); - policyMarkup.clone().appendTo('body'); - }); - - afterEach(function () { - $('.policy-stub').remove(); - }); - - it("should register a success event for internal (GOV.UK) links", function () { - GOVUK.Analytics.Format = 'policy'; - GOVUK.Analytics.startAnalytics(); - - spyOn(GOVUK.Analytics.internalSiteEvents, 'push'); - - $('#policy-internal-link').click(); - - expect(GOVUK.Analytics.internalSiteEvents.push).toHaveBeenCalled(); - }); - - it("should not register a success event for in-page links", function () { - GOVUK.Analytics.Format = 'policy'; - GOVUK.Analytics.startAnalytics(); - - $('#policy-in-page-link').click(); - var arguments = GOVUK.sendToAnalytics.calls.allArgs(); - - // should only get entry event, not success. - expect(arguments.length).toBe(1); - expect(arguments[0][0][3]).toBe('Entry'); - expect(arguments[0][0][1]).toBe('IG_policy'); - expect(arguments[0][0][5]).toBe(true); - }); - - it("should register a success timeout for thirty seconds", function () { - spyOn(window, 'setTimeout'); - GOVUK.Analytics.Format = 'policy'; - GOVUK.Analytics.startAnalytics(); - - expect(window.setTimeout.calls.allArgs()[0][1]).toBe(30000); - - // call the timeout function - window.setTimeout.calls.allArgs()[0][0](); - - var arguments = GOVUK.sendToAnalytics.calls.allArgs(); - expect(arguments.length).toBe(2); - expect(arguments[1][0]).toBeEqualAsJSON(['_trackEvent', 'IG_policy', '', 'Success', 0, true]); - }); - }); - - describe("Success tracking for inside gov. detailed-guidance format", function () { - var detailedGuidanceMarkup = $("
" + - "" + - "" + - "" + - "
"); - - beforeEach(function () { - detailedGuidanceMarkup.clone().appendTo('body'); - spyOn(GOVUK.Analytics, "getSlug").and.returnValue(""); - }); - - afterEach(function () { - $('.guidance-stub').remove(); - }); - - it("should register a success event for internal (GOV.UK) links", function () { - GOVUK.Analytics.Format = 'detailed_guidance'; - GOVUK.Analytics.startAnalytics(); - - spyOn(GOVUK.Analytics.internalSiteEvents, 'push'); - - $('#detailed-guide-internal-link').click(); - - expect(GOVUK.Analytics.internalSiteEvents.push).toHaveBeenCalled(); - }); - - it("should register a success event for in-page links", function () { - GOVUK.Analytics.Format = 'detailed_guidance'; - GOVUK.Analytics.startAnalytics(); - - $('#detailed-guide-in-page-link').click(); - var arguments = GOVUK.sendToAnalytics.calls.allArgs(); - - expect(arguments.length).toBe(2); - expect(arguments[0][0][3]).toBe('Entry'); - expect(arguments[0][0][1]).toBe('IG_detailed_guidance'); - expect(arguments[0][0][5]).toBe(true); - expect(arguments[1][0][3]).toBe('Success'); - expect(arguments[1][0][1]).toBe('IG_detailed_guidance'); - expect(arguments[1][0][5]).toBe(false); - }); - - it("should not attempt to rewrite the href for external links", function () { - GOVUK.Analytics.Format = 'detailed_guidance'; - GOVUK.Analytics.startAnalytics(); - - var currentHref = $('#detailed-guide-external-link').attr('href'); - - $('#detailed-guide-external-link').click(); - - expect($('#detailed-guide-external-link').attr('href')).toBe(currentHref); - }); - - it("should register a success timeout for thirty seconds", function () { - spyOn(window, 'setTimeout'); - GOVUK.Analytics.Format = 'detailed_guidance'; - GOVUK.Analytics.startAnalytics(); - - expect(window.setTimeout.calls.allArgs()[0][1]).toBe(30000); - }); - }); - - describe("success tracking for inside-gov news format", function () { - var newsMarkup = $("
" + - "" + - "" + - "" + - "
"); - - beforeEach(function () { - newsMarkup.clone().appendTo('body'); - spyOn(GOVUK.Analytics, "getSlug").and.returnValue(""); - }); - - afterEach(function () { - $('.page-stub').remove(); - }); - - it("should register a success event when an internal link is clicked inside the format content", function () { - GOVUK.Analytics.Format = 'news'; - GOVUK.Analytics.startAnalytics(); - - spyOn(GOVUK.Analytics.internalSiteEvents, 'push'); - - $('#news-internal-link').click(); - - expect(GOVUK.Analytics.internalSiteEvents.push).toHaveBeenCalled(); - }); - - it("should register a success event for in-page links", function () { - GOVUK.Analytics.Format = 'news'; - GOVUK.Analytics.startAnalytics(); - - $('#news-in-page-link').click(); - var arguments = GOVUK.sendToAnalytics.calls.allArgs(); - - expect(arguments.length).toBe(2); - expect(arguments[0][0][3]).toBe('Entry'); - expect(arguments[0][0][1]).toBe('IG_news'); - expect(arguments[0][0][5]).toBe(true); - expect(arguments[1][0][3]).toBe('Success'); - expect(arguments[1][0][1]).toBe('IG_news'); - expect(arguments[1][0][5]).toBe(false); - }); - - it("should not attempt to rewrite the href for external links", function () { - GOVUK.Analytics.Format = 'news'; - GOVUK.Analytics.startAnalytics(); - - var currentHref = $('#news-external-link').attr('href'); - - $('#news-external-link').click(); - - expect($('#news-external-link').attr('href')).toBe(currentHref); - }); - - it("should register a callback for success after 30 seconds", function () { - spyOn(window, "setTimeout"); - GOVUK.Analytics.Format = 'news'; - GOVUK.Analytics.startAnalytics(); - - var argumentsForSetTimeout = window.setTimeout.calls.allArgs(); - expect(argumentsForSetTimeout[0][1]).toBe(30000); - }); - }); -});