diff --git a/app/assets/javascripts/analytics/scroll-tracker.js b/app/assets/javascripts/analytics/scroll-tracker.js index 723af000b..2295a808f 100644 --- a/app/assets/javascripts/analytics/scroll-tracker.js +++ b/app/assets/javascripts/analytics/scroll-tracker.js @@ -97,21 +97,6 @@ } }; - ScrollTracker.PercentNode = function (percentage) { - this.percentage = percentage; - this.eventData = {action: "Percent", label: String(percentage)}; - }; - - ScrollTracker.PercentNode.prototype.isVisible = function () { - return this.currentScrollPercent() >= this.percentage; - }; - - ScrollTracker.PercentNode.prototype.currentScrollPercent = function () { - var $document = $(document); - var $window = $(window); - return( ($window.scrollTop() / ($document.height() - $window.height())) * 100.0 ); - }; - ScrollTracker.HeadingNode = function (headingText) { this.$element = getHeadingElement(headingText); this.eventData = {action: "Heading", label: headingText}; diff --git a/spec/javascripts/analytics/scroll-tracker-spec.js b/spec/javascripts/analytics/scroll-tracker-spec.js index e8b4e80ce..4aa860b87 100644 --- a/spec/javascripts/analytics/scroll-tracker-spec.js +++ b/spec/javascripts/analytics/scroll-tracker-spec.js @@ -10,9 +10,16 @@ describe("GOVUK.ScrollTracker", function() { }); describe("enabling on correct pages", function() { + var FIXTURE = "

A heading

"; + + beforeEach(function() { + setFixtures(FIXTURE); + spyOn(GOVUK.ScrollTracker.HeadingNode.prototype, 'elementIsVisible'); + }); + it("should be enabled on a tracked page", function() { var config = {} - config[window.location.pathname] = [ ['Percent', 50] ]; + config[window.location.pathname] = [ ['Heading', 'A heading'] ]; expect( (new GOVUK.ScrollTracker(config)).enabled ).toBeTruthy(); }); @@ -20,34 +27,13 @@ describe("GOVUK.ScrollTracker", function() { it("should not be enabled on an untracked page", function() { var config = { '/some/other/path': [ - ['Percent', 50] + ['Heading', 'A heading'] ] }; expect( (new GOVUK.ScrollTracker(config)).enabled ).toBeFalsy(); }); }); - describe("tracking by scrolled percentage", function() { - beforeEach(function() { - spyOn(GOVUK.ScrollTracker.PercentNode.prototype, "currentScrollPercent"); - }); - - it("should send an event when the page scrolls to >= the percentage specified", function() { - var config = buildConfigForThisPath([ - ['Percent', 25], - ['Percent', 50], - ['Percent', 75] - ]); - new GOVUK.ScrollTracker(config); - - scrollToPercent(60); - - expect(GOVUK.analytics.trackEvent.calls.count()).toBe(2); - expect(GOVUK.analytics.trackEvent.calls.argsFor(0)).toEqual(["ScrollTo", "Percent", {label: "25", nonInteraction: true}]); - expect(GOVUK.analytics.trackEvent.calls.argsFor(1)).toEqual(["ScrollTo", "Percent", {label: "50", nonInteraction: true}]); - }); - }); - describe("tracking by headings", function() { var FIXTURE = "\

This is the first heading

\ @@ -59,15 +45,14 @@ describe("GOVUK.ScrollTracker", function() { beforeEach(function() { setFixtures(FIXTURE); spyOn(GOVUK.ScrollTracker.HeadingNode.prototype, 'elementIsVisible'); - }); - - it("should send an event when the user scrolls so the heading is visible", function() { var config = buildConfigForThisPath([ ['Heading', "This is the first heading"], ['Heading', "This is the third heading"] ]); new GOVUK.ScrollTracker(config); + }); + it("should send an event when the user scrolls so the heading is visible", function() { scrollToShowHeadingNumber(1); expect(GOVUK.analytics.trackEvent.calls.count()).toBe(1); @@ -82,34 +67,22 @@ describe("GOVUK.ScrollTracker", function() { expect(GOVUK.analytics.trackEvent.calls.count()).toBe(2); expect(GOVUK.analytics.trackEvent.calls.argsFor(1)).toEqual(["ScrollTo", "Heading", {label: "This is the third heading", nonInteraction: true}]); }); - }); - it("should not send duplicate events", function() { - spyOn(GOVUK.ScrollTracker.PercentNode.prototype, "currentScrollPercent"); - - var config = buildConfigForThisPath([ - ['Percent', 25] - ]); - new GOVUK.ScrollTracker(config); + it("should not send duplicate events", function() { + scrollToShowHeadingNumber(1); + scrollToShowHeadingNumber(3); + scrollToShowHeadingNumber(1); - scrollToPercent(30); - scrollToPercent(35); - expect(GOVUK.analytics.trackEvent.calls.count()).toBe(1); + expect(GOVUK.analytics.trackEvent.calls.count()).toBe(2); + }); }); - function buildConfigForThisPath(thisPathData) { var config = {}; config[window.location.pathname] = thisPathData; return config; } - function scrollToPercent(percent) { - GOVUK.ScrollTracker.PercentNode.prototype.currentScrollPercent.and.returnValue(percent); - $(window).scroll(); - jasmine.clock().tick(510); - }; - function scrollToShowHeadingNumber(headingNumber) { var elementScrolledTo = $('h1, h2, h3, h4, h5, h6')[headingNumber-1]; GOVUK.ScrollTracker.HeadingNode.prototype.elementIsVisible.and.callFake(function($element) {