|
35 | 35 | ;(function() { |
36 | 36 |
|
37 | 37 | var |
38 | | - lodash = require('./lib/lodash'), |
| 38 | + lodash = require('./lib_managed/lodash'), |
| 39 | + tracker = require('./tracker'), |
| 40 | + |
39 | 41 | object = typeof exports !== 'undefined' ? exports : this; // For eventual node.js environment support |
40 | 42 |
|
41 | 43 | /************************************************************ |
|
44 | 46 | * after the Tracker has been initialized and loaded |
45 | 47 | ************************************************************/ |
46 | 48 |
|
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 | + } |
48 | 124 |
|
49 | 125 | /* |
50 | 126 | * apply wrapper |
|
60 | 136 | for (i = 0; i < arguments.length; i += 1) { |
61 | 137 | parameterArray = arguments[i]; |
62 | 138 | 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); |
63 | 156 |
|
64 | 157 | if (lodash.isString(f)) { |
65 | 158 | asyncTracker[f].apply(asyncTracker, parameterArray); |
|
0 commit comments