Skip to content

Commit 3e4927b

Browse files
Scotty Jacobsonmhadam
authored andcommitted
Fix Beacon tracking to flush to OutQueue (close #708)
1 parent 59d6143 commit 3e4927b

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

src/js/out_queue.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -270,16 +270,22 @@
270270
// Keep track of number of events to delete from queue
271271
var numberToSend = chooseHowManyToExecute(outQueue);
272272

273+
// The events (`numberToSend` of them), have been sent, so we remove them from the outQueue
274+
// We also call executeQueue() again, to let executeQueue() check if we should keep running through the queue
275+
function onPostSuccess(numberToSend) {
276+
for (var deleteCount = 0; deleteCount < numberToSend; deleteCount++) {
277+
outQueue.shift();
278+
}
279+
if (useLocalStorage) {
280+
helpers.attemptWriteLocalStorage(queueName, JSON.stringify(outQueue));
281+
}
282+
executeQueue();
283+
}
284+
273285
xhr.onreadystatechange = function () {
274286
if (xhr.readyState === 4 && xhr.status >= 200 && xhr.status < 400) {
275-
for (var deleteCount = 0; deleteCount < numberToSend; deleteCount++) {
276-
outQueue.shift();
277-
}
278-
if (useLocalStorage) {
279-
helpers.attemptWriteLocalStorage(queueName, JSON.stringify(outQueue));
280-
}
281287
clearTimeout(xhrTimeout);
282-
executeQueue();
288+
onPostSuccess(numberToSend);
283289
} else if (xhr.readyState === 4 && xhr.status >= 400) {
284290
clearTimeout(xhrTimeout);
285291
executingQueue = false;
@@ -304,6 +310,12 @@
304310
}
305311

306312
}
313+
// When beaconStatus is true, we can't _guarantee_ that it was successful (beacon queues asynchronously)
314+
// but the browser has taken it out of our hands, so we want to flush the queue assuming it will do its job
315+
if (beaconStatus === true) {
316+
onPostSuccess(numberToSend);
317+
}
318+
307319
if (!useBeacon || !beaconStatus) {
308320
xhr.send(encloseInPayloadDataEnvelope(attachStmToEvent(batch)));
309321
}

0 commit comments

Comments
 (0)