Skip to content

Commit 85ebdad

Browse files
committed
Modularised tracker.js using mutSnowplowState
Corrected helpers.expireDateTime to mutSnowplowState.expireDateTime
1 parent 3faa903 commit 85ebdad

4 files changed

Lines changed: 1382 additions & 1364 deletions

File tree

Gruntfile.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ module.exports = function(grunt) {
6868
'src/js/init.js',
6969
'src/js/lib/cookie.js',
7070
'src/js/lib/context.js',
71-
'src/js/tracker.js',
7271
'src/js/snowplow.js',
7372
'src/js/constructor.js'],
7473

@@ -86,7 +85,8 @@ module.exports = function(grunt) {
8685
'src/js/lib/cookie.js:cookie',
8786
'src/js/lib/context.js:detectors',
8887
'src/js/lib/jstz.js:jstz',
89-
'src/js/lib/sha1.js:sha1'
88+
'src/js/lib/sha1.js:sha1',
89+
'src/js/tracker.js:tracker'
9090
]
9191
},
9292
files: {

src/js/init.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,14 @@ var SnowPlow = SnowPlow || function() {
4747
/* Tracker identifier with version */
4848
version: 'js-0.14.0', // Update banner.js too
4949

50-
expireDateTime: null,
50+
/* Contains three variables that are shared with tracker.js and must be passed by reference */
51+
mutSnowplowState: {
52+
expireDateTime: null,
5153

52-
/* DOM Ready */
53-
hasLoaded: false,
54-
registeredOnLoadHandlers: [],
54+
/* DOM Ready */
55+
hasLoaded: false,
56+
registeredOnLoadHandlers: []
57+
},
5558

5659
/* Alias frequently used globals for added minification */
5760
documentAlias: document,
@@ -80,3 +83,4 @@ var payload = require('payload');
8083
var json2 = require('JSON');
8184
var cookie = require('cookie');
8285
var detectors = require('detectors');
86+
var tracker = require('tracker');

src/js/snowplow.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,13 @@ SnowPlow.build = function () {
116116
/*
117117
* Delay/pause (blocks UI)
118118
*/
119-
if (helpers.expireDateTime) {
119+
if (SnowPlow.mutSnowplowState.expireDate) {
120120
// the things we do for backwards compatibility...
121121
// in ECMA-262 5th ed., we could simply use:
122-
// while (Date.now() < helpers.expireDateTime) { }
122+
// while (Date.now() < SnowPlow.mutSnowplowState.expireDate) { }
123123
do {
124124
now = new Date();
125-
} while (now.getTimeAlias() < helpers.expireDateTime);
125+
} while (now.getTimeAlias() < SnowPlow.mutSnowplowState.expireDate);
126126
}
127127
}
128128

@@ -132,10 +132,10 @@ SnowPlow.build = function () {
132132
function loadHandler() {
133133
var i;
134134

135-
if (!SnowPlow.hasLoaded) {
136-
SnowPlow.hasLoaded = true;
137-
for (i = 0; i < SnowPlow.registeredOnLoadHandlers.length; i++) {
138-
SnowPlow.registeredOnLoadHandlers[i]();
135+
if (!SnowPlow.mutSnowplowState.hasLoaded) {
136+
SnowPlow.mutSnowplowState.hasLoaded = true;
137+
for (i = 0; i < SnowPlow.mutSnowplowState.registeredOnLoadHandlers.length; i++) {
138+
SnowPlow.mutSnowplowState.registeredOnLoadHandlers[i]();
139139
}
140140
}
141141
return true;
@@ -162,7 +162,7 @@ SnowPlow.build = function () {
162162

163163
if (SnowPlow.documentAlias.documentElement.doScroll && SnowPlow.windowAlias === SnowPlow.windowAlias.top) {
164164
(function ready() {
165-
if (!SnowPlow.hasLoaded) {
165+
if (!SnowPlow.mutSnowplowState.hasLoaded) {
166166
try {
167167
SnowPlow.documentAlias.documentElement.doScroll('left');
168168
} catch (error) {
@@ -178,7 +178,7 @@ SnowPlow.build = function () {
178178
// sniff for older WebKit versions
179179
if ((new RegExp('WebKit')).test(SnowPlow.navigatorAlias.userAgent)) {
180180
_timer = setInterval(function () {
181-
if (SnowPlow.hasLoaded || /loaded|complete/.test(SnowPlow.documentAlias.readyState)) {
181+
if (SnowPlow.mutSnowplowState.hasLoaded || /loaded|complete/.test(SnowPlow.documentAlias.readyState)) {
182182
clearInterval(_timer);
183183
loadHandler();
184184
}
@@ -212,7 +212,7 @@ SnowPlow.build = function () {
212212

213213
Date.prototype.getTimeAlias = Date.prototype.getTime;
214214

215-
SnowPlow.asyncTracker = new SnowPlow.Tracker();
215+
SnowPlow.asyncTracker = new tracker.Tracker(SnowPlow.version, SnowPlow.mutSnowplowState);
216216

217217
for (var i = 0; i < SnowPlow.windowAlias._snaq.length; i++) {
218218
apply(SnowPlow.windowAlias._snaq[i]);
@@ -235,7 +235,7 @@ SnowPlow.build = function () {
235235
* @param string distSubdomain The subdomain on your CloudFront collector's distribution
236236
*/
237237
getTrackerCf: function (distSubdomain) {
238-
return new SnowPlow.Tracker({cf: distSubdomain});
238+
return new tracker.Tracker(SnowPlow.version, SnowPlow.mutSnowplowState, {cf: distSubdomain});
239239
},
240240

241241
/**
@@ -245,7 +245,7 @@ SnowPlow.build = function () {
245245
* @param string rawUrl The collector URL minus protocol and /i
246246
*/
247247
getTrackerUrl: function (rawUrl) {
248-
return new SnowPlow.Tracker({url: rawUrl});
248+
return new tracker.Tracker(SnowPlow.version, SnowPlow.mutSnowplowState, {url: rawUrl});
249249
},
250250

251251
/**

0 commit comments

Comments
 (0)