Skip to content

Commit 6f7a96b

Browse files
committed
More work on the new InQueueManager. Relocated custom lodash library to src/js/lib_managed.
1 parent 66b0103 commit 6f7a96b

8 files changed

Lines changed: 117 additions & 22 deletions

File tree

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ Implement enableLinkTracking support (#51) (TODO)
2525
Replaced hard-coded version with template value (#120) (TODO)
2626
Added Sauce Labs small button at top of README (#123) (TODO)
2727
Added Sauce full test summary widget (long bar) at bottom of README (#124) (TODO)
28+
Added support for namespacing (#4) (TODO)
29+
Passed tracker namespace through to collector in Tracker Protocol (#126) (TODO)
2830

2931
Version 1.0.1 (2014-04-09)
3032
--------------------------

Gruntfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ module.exports = function(grunt) {
7979

8080
lodash: {
8181
build: {
82-
dest: 'src/js/lib/lodash.js',
82+
dest: 'src/js/lib_managed/lodash.js',
8383
options: {
8484
exports: 'node',
8585
include: 'isArray, isFunction, isString, isObject, isDate, isUndefined, isNull, map',

src/js/in_queue.js

Lines changed: 95 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@
3535
;(function() {
3636

3737
var
38-
lodash = require('./lib/lodash'),
38+
lodash = require('./lib_managed/lodash'),
39+
tracker = require('./tracker'),
40+
3941
object = typeof exports !== 'undefined' ? exports : this; // For eventual node.js environment support
4042

4143
/************************************************************
@@ -44,7 +46,81 @@
4446
* after the Tracker has been initialized and loaded
4547
************************************************************/
4648

47-
object.AsyncQueueProxy = function(asyncTracker, asyncQueue) {
49+
object.InQueueManager = function(version, mutSnowplowState, asyncQueue) {
50+
51+
var trackerDictionary = {};
52+
53+
/*
54+
* Get an array of trackers to which a function should be applied.
55+
*
56+
* @param names array List of namespaces to use. If empty, use all namespaces.
57+
*/
58+
function getNamedTrackers(names) {
59+
var namedTrackers = [];
60+
if (!names || names.length === 0) {
61+
namedTrackers = lodash.values(trackerDictionary);
62+
} else {
63+
for (var i = 0; i < names.length; i++) {
64+
if (trackerDictionary.hasOwnProperty(names[i])) {
65+
namedTrackers.push(trackerDictionary[names[i]]);
66+
} else if (!lodash.isUndefined(console)) {
67+
console.log('Warning: Tracker namespace "' + names[i] + '" not configured');
68+
}
69+
}
70+
}
71+
72+
return namedTrackers;
73+
}
74+
75+
/*
76+
* Legacy support for input of the form _snaq.push(['setCollectorCf', 'd34uzc5hjrimh8'])
77+
*
78+
* @param f string Either 'setCollectorCf' or 'setCollectorUrl'
79+
* @param endpoint string
80+
* @param namespace string Optional tracker name
81+
*
82+
* TODO: remove this in 1.2.0
83+
*/
84+
function legacyCreateNewNamespace(f, endpoint, namespace) {
85+
if (!lodash.isUndefined(console)) {
86+
console.log(f, 'is deprecated.'); //TODO: more instructions for switching
87+
}
88+
89+
var name;
90+
91+
if (lodash.isUndefined(namespace)) {
92+
name = 'default' // TODO: make default names work properly
93+
} else {
94+
name = namespace;
95+
}
96+
97+
createNewNamespace(name);
98+
trackerDictionary[name][f](endpoint);
99+
}
100+
101+
/*
102+
* Initiate a new tracker namespace
103+
*
104+
* @param namespace string
105+
* @param endpoint string Of the form d3rkrsqld9gmqf.cloudfront.net
106+
*/
107+
function createNewNamespace(namespace, endpoint) {
108+
trackerDictionary[namespace] = new tracker.Tracker(version, mutSnowplowState) // TODO: what of argmap?
109+
trackerDictionary[namespace].setCollectorUrl(endpoint);
110+
}
111+
112+
/*
113+
* Output an array of the form ['functionName', [trackerName1, trackerName2, ...]]
114+
*
115+
* @param inputString String
116+
*/
117+
function parseInputString(inputString) {
118+
var separatedString = inputString.split(':'),
119+
extractedFunction = separatedString[0],
120+
extractedNames = (separatedString.length > 1) ? separatedString[1].split(';') : [];
121+
122+
return [extractedFunction, extractedNames];
123+
}
48124

49125
/*
50126
* apply wrapper
@@ -60,6 +136,23 @@
60136
for (i = 0; i < arguments.length; i += 1) {
61137
parameterArray = arguments[i];
62138
f = parameterArray.shift();
139+
inputString = parameterArray.shift();
140+
parsedString = parseInputString(inputString);
141+
f = parsedString[0];
142+
names = parsedString[1];
143+
144+
if (f === 'newTracker') {
145+
createNewNamespace(parameterArray[0], parameterArray[1]);
146+
continue;
147+
}
148+
149+
if ((f === 'setCollectorCf' || f === 'setCollectorUrl') && (!names || names.length === 0)) {
150+
legacyCreateNewNamespace(f, parameterArray[0], parameterArray[1]);
151+
152+
continue;
153+
}
154+
155+
namedTrackers = getNamedTrackers(names);
63156

64157
if (lodash.isString(f)) {
65158
asyncTracker[f].apply(asyncTracker, parameterArray);

src/js/lib/detectors.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
;(function() {
3636

3737
var
38-
lodash = require('./lodash'),
38+
lodash = require('../lib_managed/lodash'),
3939
helpers = require('./helpers'),
4040
cookie = require('./cookie'),
4141
murmurhash3_32_gc = require('murmurhash').v3,

src/js/lib/helpers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
;(function () {
3535

3636
var
37-
lodash = require('./lodash'),
37+
lodash = require('../lib_managed/lodash'),
3838

3939
object = typeof exports !== 'undefined' ? exports : this; // For eventual node.js environment support
4040

src/js/out_queue.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,33 +36,34 @@
3636

3737
var
3838
json2 = require('JSON'),
39+
lodash = require('./lib_managed/lodash'),
3940
localStorageAccessible = require('./lib/detectors').localStorageAccessible(),
4041
object = typeof exports !== 'undefined' ? exports : this, // For eventual node.js environment support
4142

4243
executingQueue = false,
43-
imageQueue;
44+
outQueue;
4445

4546
if (localStorageAccessible) {
4647
// Catch any JSON parse errors that might be thrown
4748
try {
48-
imageQueue = json2.parse(localStorage.getItem('snaqImageQueue'));
49+
outQueue = json2.parse(localStorage.getItem('snaqoutQueue'));
4950
}
5051
catch(e) {}
5152
}
5253

5354
// Initialize to and empty array if we didn't get anything out of localStorage
54-
if (typeof imageQueue === 'undefined' || imageQueue == null) {
55-
imageQueue = [];
55+
if (typeof outQueue === 'undefined' || outQueue == null) {
56+
outQueue = [];
5657
}
5758

5859
/*
5960
* Queue an image beacon for submission to the collector.
6061
* If we're not processing the queue, we'll start.
6162
*/
62-
object.queueImage = function(request, configCollectorUrl) {
63-
imageQueue.push(request);
63+
object.enqueueImage = function(request, configCollectorUrl) {
64+
outQueue.push([request, configCollectorUrl]);
6465
if (localStorageAccessible) {
65-
localStorage.setItem('snaqImageQueue', json2.stringify(imageQueue));
66+
localStorage.setItem('snaqoutQueue', json2.stringify(outQueue));
6667
}
6768

6869
if (!executingQueue) {
@@ -74,14 +75,15 @@
7475
* Run through the queue of image beacons, sending them one at a time.
7576
* Stops processing when we run out of queued requests, or we get an error.
7677
*/
77-
function executeQueue(configCollectorUrl) {
78-
if (imageQueue.length < 1) {
78+
function executeQueue() {
79+
if (outQueue.length < 1) {
7980
executingQueue = false;
8081
return;
8182
}
8283

8384
executingQueue = true;
84-
var nextRequest = imageQueue[0];
85+
var nextRequest = outQueue[0][0],
86+
collectorUrl = outQueue[0][1];
8587

8688
/*
8789
* Send image request to the Snowplow Collector using GET.
@@ -97,9 +99,9 @@
9799
// Okay? Let's proceed.
98100
image.onload = function() {
99101
// We succeeded, let's remove this request from the queue
100-
imageQueue.shift();
102+
outQueue.shift();
101103
if (localStorageAccessible) {
102-
localStorage.setItem('snaqImageQueue', json2.stringify(imageQueue));
104+
localStorage.setItem('snaqoutQueue', json2.stringify(outQueue));
103105
}
104106
executeQueue(configCollectorUrl);
105107
}

src/js/payload.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
;(function() {
3636

3737
var
38-
lodash = require('./lib/lodash'),
38+
lodash = require('./lib_managed/lodash'),
3939
json2 = require('JSON'),
4040
base64 = require('./lib/base64'),
4141

src/js/snowplow.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,8 @@
200200
helpers.addEventListener(windowAlias, 'beforeunload', beforeUnloadHandler, false);
201201
addReadyListener();
202202

203-
asyncTracker = new tracker.Tracker(version, mutSnowplowState); // No argmap
204-
205-
// Now replace initialization array with proxy object
206-
windowAlias._snaq = new queue.AsyncQueueProxy(asyncTracker, windowAlias._snaq);
203+
// Now replace initialization array with queue manager object
204+
windowAlias._snaq = new queue.InQueueManager(version, mutSnowplowState, windowAlias._snaq);
207205

208206
/************************************************************
209207
* Public data and methods

0 commit comments

Comments
 (0)