Skip to content

Commit bb2e550

Browse files
committed
Adding pixel depth tracking. Moving GA type detection out of Send Event function. robflaherty#21
1 parent e1810a4 commit bb2e550

File tree

2 files changed

+75
-29
lines changed

2 files changed

+75
-29
lines changed

jquery.scrolldepth.js

Lines changed: 72 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,30 @@
55
* Licensed under the MIT and GPL licenses.
66
*/
77
;(function ( $, window, document, undefined ) {
8-
8+
99
"use strict";
1010

1111
var defaults = {
12-
elements: [],
1312
minHeight: 0,
13+
elements: [],
1414
percentage: true,
15-
userTiming: true
16-
},
15+
userTiming: true,
16+
pixelDepth: true
17+
};
1718

18-
$window = $(window),
19-
cache = [];
19+
var $window = $(window),
20+
cache = [],
21+
lastPixelDepth = 0,
22+
universalGA,
23+
classicGA,
24+
googleTagManager;
2025

2126
/*
2227
* Plugin
2328
*/
2429

2530
$.scrollDepth = function(options) {
26-
31+
2732
var startTime = +new Date;
2833

2934
options = $.extend({}, defaults, options);
@@ -33,39 +38,79 @@
3338
return;
3439
}
3540

41+
/*
42+
* Determine which version of GA is being used
43+
* "ga", "_gaq", and "dataLayer" are the possible globals
44+
*/
45+
46+
if (typeof ga === "function") {
47+
universalGA = true;
48+
}
49+
50+
if (typeof _gaq !== "undefined" && typeof _gaq.push === "function") {
51+
classicGA = true;
52+
}
53+
54+
if (typeof dataLayer !== "undefined" && typeof dataLayer.push === "function") {
55+
googleTagManager = true;
56+
}
57+
3658
// Establish baseline (0% scroll)
3759
sendEvent('Percentage', 'Baseline');
3860

3961
/*
4062
* Functions
4163
*/
4264

43-
function sendEvent(action, label, timing) {
65+
function sendEvent(action, label, scrollDistance, timing) {
4466

45-
if (typeof dataLayer !== "undefined" && typeof dataLayer.push === "function") {
46-
dataLayer.push({'event':'ScrollDistance', 'eventCategory':'Scroll Depth', 'eventAction': action, 'eventLabel': label, 'eventValue': 1, 'eventNonInteraction': true});
67+
if (googleTagManager) {
4768

48-
if (options.userTiming && arguments.length > 2) {
49-
dataLayer.push({'event':'ScrollTiming', 'eventCategory':'Scroll Depth', 'eventAction': action, 'eventLabel': label, 'eventTiming': timing});
50-
}
51-
} else {
69+
dataLayer.push({'event': 'ScrollDistance', 'eventCategory': 'Scroll Depth', 'eventAction': action, 'eventLabel': label, 'eventValue': 1, 'eventNonInteraction': true});
70+
71+
if (options.pixelDepth && arguments.length > 2 && scrollDistance > lastPixelDepth) {
72+
lastPixelDepth = scrollDistance;
73+
dataLayer.push({'event': 'ScrollDistance', 'eventCategory': 'Scroll Depth', 'eventAction': 'Pixel Depth', 'eventLabel': scrollDistance.toString(), 'eventValue': 1, 'eventNonInteraction': true});
74+
}
75+
76+
if (options.userTiming && arguments.length > 3) {
77+
dataLayer.push({'event': 'ScrollTiming', 'eventCategory': 'Scroll Depth', 'eventAction': action, 'eventLabel': label, 'eventTiming': timing});
78+
}
79+
80+
} else {
5281

53-
if (typeof ga === "function") {
54-
ga('send', 'event', 'Scroll Depth', action, label, 1, {'nonInteraction': 1});
82+
if (universalGA) {
5583

56-
if (options.userTiming && arguments.length > 2) {
57-
ga('send', 'timing', 'Scroll Depth', action, timing, label);
58-
}
84+
ga('send', 'event', 'Scroll Depth', action, label, 1, {'nonInteraction': 1});
85+
86+
if (options.pixelDepth && arguments.length > 2 && scrollDistance > lastPixelDepth) {
87+
lastPixelDepth = scrollDistance;
88+
ga('send', 'event', 'Scroll Depth', 'Pixel Depth', scrollDistance.toString(), 1, {'nonInteraction': 1});
89+
}
90+
91+
if (options.userTiming && arguments.length > 3) {
92+
ga('send', 'timing', 'Scroll Depth', action, timing, label);
5993
}
6094

61-
if (typeof _gaq !== "undefined" && typeof _gaq.push === "function") {
62-
_gaq.push(['_trackEvent', 'Scroll Depth', action, label, 1, true]);
95+
}
96+
97+
if (classicGA) {
98+
99+
_gaq.push(['_trackEvent', 'Scroll Depth', action, label, 1, true]);
100+
101+
if (options.pixelDepth && arguments.length > 2 && scrollDistance > lastPixelDepth) {
102+
lastPixelDepth = scrollDistance;
103+
_gaq.push(['_trackEvent', 'Scroll Depth', 'Pixel Depth', scrollDistance.toString(), 1, true]);
104+
}
63105

64-
if (options.userTiming && arguments.length > 2) {
65-
_gaq.push(['_trackTiming', 'Scroll Depth', action, timing, label, 100]);
66-
}
106+
if (options.userTiming && arguments.length > 3) {
107+
_gaq.push(['_trackTiming', 'Scroll Depth', action, timing, label, 100]);
67108
}
109+
68110
}
111+
112+
}
113+
69114
}
70115

71116
function calculateMarks(docHeight) {
@@ -82,7 +127,7 @@
82127
// Check each active mark
83128
$.each(marks, function(key, val) {
84129
if ( $.inArray(key, cache) === -1 && scrollDistance >= val ) {
85-
sendEvent('Percentage', key, timing);
130+
sendEvent('Percentage', key, scrollDistance, timing);
86131
cache.push(key);
87132
}
88133
});
@@ -92,7 +137,7 @@
92137
$.each(elements, function(index, elem) {
93138
if ( $.inArray(elem, cache) === -1 && $(elem).length ) {
94139
if ( scrollDistance >= $(elem).offset().top ) {
95-
sendEvent('Elements', elem, timing);
140+
sendEvent('Elements', elem, scrollDistance, timing);
96141
cache.push(elem);
97142
}
98143
}
@@ -166,7 +211,7 @@
166211
}
167212

168213
// Check standard marks
169-
if (options.percentage) {
214+
if (options.percentage) {
170215
checkMarks(marks, scrollDistance, timing);
171216
}
172217
}, 500));

test/index.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ <h1>jQuery Scroll Depth Test Page</h1>
1616
<script>
1717
var _gaq = {};
1818
_gaq.push = function(data) {
19-
console.log("_gaq.push(" + data + ");");
19+
console.log("_gaq.push(" + JSON.stringify(data) + ");");
2020
};
2121

2222
var dataLayer = {};
@@ -34,7 +34,8 @@ <h1>jQuery Scroll Depth Test Page</h1>
3434
//ga = undefined;
3535

3636
$.scrollDepth({
37-
elements: ['#main', 'footer']
37+
elements: ['#main', 'footer'],
38+
userTiming: false
3839
});
3940
</script>
4041
</body>

0 commit comments

Comments
 (0)