Skip to content

Commit

Permalink
Remove code for percent nodes
Browse files Browse the repository at this point in the history
The previous commit removed all existing percent nodes
as tracking scrolling by percent did not prove to be very helpful
in general according to Vinith (@vp2015).
This removes all code related to percent scroll tracking
as it's highly unlikely to be used again.
  • Loading branch information
selfthinker committed Aug 4, 2016
1 parent 44b7a3e commit 6289f90
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 59 deletions.
15 changes: 0 additions & 15 deletions app/assets/javascripts/analytics/scroll-tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
61 changes: 17 additions & 44 deletions spec/javascripts/analytics/scroll-tracker-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,30 @@ describe("GOVUK.ScrollTracker", function() {
});

describe("enabling on correct pages", function() {
var FIXTURE = "<h1>A heading</h1>";

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();
});

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 = "\
<h1>This is the first <span>heading</span></h1>\
Expand All @@ -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);
Expand All @@ -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) {
Expand Down

0 comments on commit 6289f90

Please sign in to comment.