Skip to content

Commit cca9b2b

Browse files
committed
Moving rounding into function
1 parent 4b99c79 commit cca9b2b

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

jquery.scrolldepth.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,13 @@
6464

6565
function sendEvent(action, label, scrollDistance, timing) {
6666

67-
scrollDistance = Math.floor(scrollDistance/250) * 250;
68-
6967
if (googleTagManager) {
7068

7169
dataLayer.push({'event': 'ScrollDistance', 'eventCategory': 'Scroll Depth', 'eventAction': action, 'eventLabel': label, 'eventValue': 1, 'eventNonInteraction': true});
7270

7371
if (options.pixelDepth && arguments.length > 2 && scrollDistance > lastPixelDepth) {
7472
lastPixelDepth = scrollDistance;
75-
dataLayer.push({'event': 'ScrollDistance', 'eventCategory': 'Scroll Depth', 'eventAction': 'Pixel Depth', 'eventLabel': scrollDistance.toString(), 'eventValue': 1, 'eventNonInteraction': true});
73+
dataLayer.push({'event': 'ScrollDistance', 'eventCategory': 'Scroll Depth', 'eventAction': 'Pixel Depth', 'eventLabel': rounded(scrollDistance), 'eventValue': 1, 'eventNonInteraction': true});
7674
}
7775

7876
if (options.userTiming && arguments.length > 3) {
@@ -87,7 +85,7 @@
8785

8886
if (options.pixelDepth && arguments.length > 2 && scrollDistance > lastPixelDepth) {
8987
lastPixelDepth = scrollDistance;
90-
ga('send', 'event', 'Scroll Depth', 'Pixel Depth', scrollDistance.toString(), 1, {'nonInteraction': 1});
88+
ga('send', 'event', 'Scroll Depth', 'Pixel Depth', rounded(scrollDistance), 1, {'nonInteraction': 1});
9189
}
9290

9391
if (options.userTiming && arguments.length > 3) {
@@ -102,7 +100,7 @@
102100

103101
if (options.pixelDepth && arguments.length > 2 && scrollDistance > lastPixelDepth) {
104102
lastPixelDepth = scrollDistance;
105-
_gaq.push(['_trackEvent', 'Scroll Depth', 'Pixel Depth', scrollDistance.toString(), 1, true]);
103+
_gaq.push(['_trackEvent', 'Scroll Depth', 'Pixel Depth', rounded(scrollDistance), 1, true]);
106104
}
107105

108106
if (options.userTiming && arguments.length > 3) {
@@ -146,6 +144,11 @@
146144
});
147145
}
148146

147+
function rounded(scrollDistance) {
148+
// Returns String
149+
return (Math.floor(scrollDistance/250) * 250).toString();
150+
}
151+
149152
/*
150153
* Throttle function borrowed from:
151154
* Underscore.js 1.5.2

0 commit comments

Comments
 (0)