forked from snowplow/snowplow-javascript-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathout_queue.js
More file actions
244 lines (205 loc) · 7.91 KB
/
Copy pathout_queue.js
File metadata and controls
244 lines (205 loc) · 7.91 KB
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
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
* JavaScript tracker for Snowplow: out_queue.js
*
* Significant portions copyright 2010 Anthon Pang. Remainder copyright
* 2012-2014 Snowplow Analytics Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of Anthon Pang nor Snowplow Analytics Ltd nor the
* names of their contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
;(function() {
var
json2 = require('JSON'),
lodash = require('./lib_managed/lodash'),
localStorageAccessible = require('./lib/detectors').localStorageAccessible,
object = typeof exports !== 'undefined' ? exports : this; // For eventual node.js environment support
/**
* Object handling sending events to a collector.
* Instantiated once per tracker instance.
*
* @param string functionName The Snowplow function name (used to generate the localStorage key)
* @param string namespace The tracker instance's namespace (used to generate the localStorage key)
* @param object mutSnowplowState Gives the pageUnloadGuard a reference to the outbound queue
* so it can unload the page when all queues are empty
* @param boolean useLocalStorage Whether to use localStorage at all
* @param boolean usePost Whether to send events by POST or GET
* @param int bufferSize How many events to batch in localStorage before sending them all.
* Only applies when sending POST requests and when localStorage is available.
* @return object OutQueueManager instance
*/
object.OutQueueManager = function (functionName, namespace, mutSnowplowState, useLocalStorage, usePost, bufferSize) {
var queueName,
executingQueue = false,
configCollectorUrl,
outQueue;
// Fall back to GET for browsers which don't support CORS XMLHttpRequests (e.g. IE <= 9)
usePost = usePost && window.XMLHttpRequest && ('withCredentials' in new XMLHttpRequest());
var path = usePost ? '/com.snowplowanalytics.snowplow/tp2' : '/i';
bufferSize = (localStorageAccessible() && useLocalStorage && usePost && bufferSize) || 1;
// Different queue names for GET and POST since they are stored differently
queueName = ['snowplowOutQueue', functionName, namespace, usePost ? 'post' : 'get'].join('_');
if (localStorageAccessible() && useLocalStorage) {
// Catch any JSON parse errors that might be thrown
try {
outQueue = json2.parse(localStorage.getItem(queueName));
}
catch(e) {}
}
// Initialize to and empty array if we didn't get anything out of localStorage
if (!lodash.isArray(outQueue)) {
outQueue = [];
}
// Used by pageUnloadGuard
mutSnowplowState.outQueues.push(outQueue);
if (usePost && bufferSize > 1) {
mutSnowplowState.bufferFlushers.push(function () {
if (!executingQueue) {
executeQueue();
}
});
}
/*
* Convert a dictionary to a querystring
* The context field is the last in the querystring
*/
function getQuerystring(request) {
var querystring = '?',
lowPriorityKeys = {'co': true, 'cx': true},
firstPair = true;
for (var key in request) {
if (request.hasOwnProperty(key) && !(lowPriorityKeys.hasOwnProperty(key))) {
if (!firstPair) {
querystring += '&';
} else {
firstPair = false;
}
querystring += encodeURIComponent(key) + '=' + encodeURIComponent(request[key]);
}
}
for (var contextKey in lowPriorityKeys) {
if (request.hasOwnProperty(contextKey) && lowPriorityKeys.hasOwnProperty(contextKey)) {
querystring += '&' + contextKey + '=' + encodeURIComponent(request[contextKey]);
}
}
return querystring;
}
/*
* Convert numeric fields to strings to match payload_data schema
*/
function getBody(request) {
return lodash.mapValues(request, function (v) {
return v.toString();
});
}
/*
* Queue an image beacon for submission to the collector.
* If we're not processing the queue, we'll start.
*/
function enqueueRequest (request, url) {
outQueue.push(usePost ? getBody(request) : getQuerystring(request));
configCollectorUrl = url + path;
if (localStorageAccessible() && useLocalStorage) {
localStorage.setItem(queueName, json2.stringify(outQueue));
}
if (!executingQueue && outQueue.length >= bufferSize) {
executeQueue();
}
}
/*
* Run through the queue of image beacons, sending them one at a time.
* Stops processing when we run out of queued requests, or we get an error.
*/
function executeQueue () {
// Failsafe in case there is some way for a bad value like "null" to end up in the outQueue
while (outQueue.length && typeof outQueue[0] !== 'string' && typeof outQueue[0] !== 'object') {
outQueue.shift();
}
if (outQueue.length < 1) {
executingQueue = false;
return;
}
// Let's check that we have a Url to ping
if (!lodash.isString(configCollectorUrl)) {
throw "No Snowplow collector configured, cannot track";
}
executingQueue = true;
var nextRequest = outQueue[0];
if (usePost) {
var xhr = new XMLHttpRequest();
xhr.open('POST', configCollectorUrl, true);
xhr.withCredentials = true;
// Time out POST requests after 5 seconds
var xhrTimeout = setTimeout(function () {
xhr.abort();
executingQueue = false;
}, 5000);
// Keep track of number of events to delete from queue
var eventCount = outQueue.length;
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status >= 200 && xhr.status < 400) {
for (var deleteCount = 0; deleteCount < eventCount; deleteCount++) {
outQueue.shift();
}
if (localStorageAccessible() && useLocalStorage) {
localStorage.setItem(queueName, json2.stringify(outQueue));
}
clearTimeout(xhrTimeout);
executeQueue();
} else if (xhr.readyState === 4 && xhr.status >= 400) {
clearTimeout(xhrTimeout);
executingQueue = false;
}
};
xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
var batchRequest = {
schema: 'iglu:com.snowplowanalytics.snowplow/payload_data/jsonschema/1-0-2',
data: outQueue
};
if (outQueue.length > 0) {
xhr.send(json2.stringify(batchRequest));
}
} else {
var image = new Image(1, 1);
image.onload = function () {
outQueue.shift();
if (localStorageAccessible() && useLocalStorage) {
localStorage.setItem(queueName, json2.stringify(outQueue));
}
executeQueue();
};
image.onerror = function () {
executingQueue = false;
};
image.src = configCollectorUrl + nextRequest;
}
}
return {
enqueueRequest: enqueueRequest,
executeQueue: executeQueue
};
};
}());