forked from robflaherty/riveted
-
Notifications
You must be signed in to change notification settings - Fork 1
/
riveted.js
231 lines (179 loc) · 5.49 KB
/
riveted.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*!
* @preserve
* riveted.js | v0.3.1
* Copyright (c) 2014 Rob Flaherty (@robflaherty)
* Licensed under the MIT license
*/
var riveted = (function() {
var started = false,
stopped = false,
clockTime = 0,
startTime = new Date(),
clockTimer = null,
idleTimer = null,
reportInterval,
idleTimeout,
nonInteraction,
universalGA,
classicGA,
googleTagManager;
/*
* Determine which version of GA is being used
* "ga", "_gaq", and "dataLayer" are the possible globals
*/
if (typeof ga === "function") {
universalGA = true;
}
if (typeof _gaq !== "undefined" && typeof _gaq.push === "function") {
classicGA = true;
}
if (typeof dataLayer !== "undefined" && typeof dataLayer.push === "function") {
googleTagManager = true;
}
function init(options) {
// Set up options and defaults
options = options || {};
reportInterval = parseInt(options.reportInterval, 10) || 5;
idleTimeout = parseInt(options.idleTimeout, 10) || 30;
if ('nonInteraction' in options && (options.nonInteraction === false || options.nonInteraction === 'false')) {
nonInteraction = false;
} else {
nonInteraction = true;
}
// Basic activity event listeners
addListener(document, 'keydown', trigger);
addListener(document, 'click', trigger);
addListener(window, 'mousemove', throttle(trigger, 500));
addListener(window, 'scroll', throttle(trigger, 500));
// Page visibility listeners
addListener(document, 'visibilitychange', visibilityChange);
addListener(document, 'webkitvisibilitychange', visibilityChange);
}
/*
* Throttle function borrowed from:
* Underscore.js 1.5.2
* http://underscorejs.org
* (c) 2009-2013 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
* Underscore may be freely distributed under the MIT license.
*/
function throttle(func, wait) {
var context, args, result;
var timeout = null;
var previous = 0;
var later = function() {
previous = new Date;
timeout = null;
result = func.apply(context, args);
};
return function() {
var now = new Date;
if (!previous) previous = now;
var remaining = wait - (now - previous);
context = this;
args = arguments;
if (remaining <= 0) {
clearTimeout(timeout);
timeout = null;
previous = now;
result = func.apply(context, args);
} else if (!timeout) {
timeout = setTimeout(later, remaining);
}
return result;
};
}
/*
* Cross-browser event listening
*/
function addListener(element, eventName, handler) {
if (element.addEventListener) {
element.addEventListener(eventName, handler, false);
}
else if (element.attachEvent) {
element.attachEvent('on' + eventName, handler);
}
else {
element['on' + eventName] = handler;
}
}
/*
* Function for logging User Timing event on initial interaction
*/
function sendUserTiming(timingValue) {
if (googleTagManager) {
dataLayer.push({'event':'RivetedTiming', 'eventCategory':'Riveted', 'timingVar': 'First Interaction', 'timingValue': timingValue});
} else {
if (universalGA) {
ga('send', 'timing', 'Riveted', 'First Interaction', timingValue);
}
if (classicGA) {
_gaq.push(['_trackTiming', 'Riveted', 'First Interaction', timingValue, null, 100]);
}
}
}
/*
* Function for logging ping events
*/
function sendEvent(time) {
if (googleTagManager) {
dataLayer.push({'event':'Riveted', 'eventCategory':'Riveted', 'eventAction': 'Time Spent', 'eventLabel': 'Seconds', 'eventValue': time, 'eventNonInteraction': nonInteraction});
} else {
if (universalGA) {
ga('send', 'event', 'Riveted', 'Time Spent', time.toString(), reportInterval, {'nonInteraction': nonInteraction});
}
if (classicGA) {
_gaq.push(['_trackEvent', 'Riveted', 'Time Spent', time.toString(), reportInterval, nonInteraction]);
}
}
}
function setIdle() {
clearTimeout(idleTimer);
stopClock();
}
function visibilityChange() {
if (document.hidden || document.webkitHidden) {
setIdle();
}
}
function clock() {
clockTime += 1;
if (clockTime > 0 && (clockTime % reportInterval === 0)) {
sendEvent(clockTime);
}
}
function stopClock() {
stopped = true;
clearTimeout(clockTimer);
}
function restartClock() {
stopped = false;
clearTimeout(clockTimer);
clockTimer = setInterval(clock, 1000);
}
function startRiveted() {
// Calculate seconds from start to first interaction
var currentTime = new Date();
var diff = currentTime - startTime;
// Set global
started = true;
// Send User Timing Event
sendUserTiming(diff);
// Start clock
clockTimer = setInterval(clock, 1000);
}
function trigger() {
if (!started) {
startRiveted();
}
if (stopped) {
restartClock();
}
clearTimeout(idleTimer);
idleTimer = setTimeout(setIdle, idleTimeout * 1000 + 100);
}
return {
init: init,
trigger: trigger,
setIdle: setIdle
};
})();