Skip to content

Commit 82b8dfc

Browse files
authored
feat: add defensive queue to _sendEventStream to prevent event loss (#78)
1 parent d4b0072 commit 82b8dfc

2 files changed

Lines changed: 80 additions & 12 deletions

File tree

src/Rokt-Kit.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ var constructor = function () {
3939
self.placementEventMappingLookup = {};
4040
self.placementEventAttributeMappingLookup = {};
4141
self.eventQueue = [];
42+
self.eventStreamQueue = [];
4243
self.integrationName = null;
4344

4445
function getEventAttributeValue(event, eventAttributeKey) {
@@ -553,7 +554,16 @@ var constructor = function () {
553554

554555
function _sendEventStream(event) {
555556
if (window.Rokt && typeof window.Rokt.__event_stream__ === 'function') {
557+
if (self.eventStreamQueue.length) {
558+
var queuedEvents = self.eventStreamQueue;
559+
self.eventStreamQueue = [];
560+
for (var i = 0; i < queuedEvents.length; i++) {
561+
window.Rokt.__event_stream__(queuedEvents[i]);
562+
}
563+
}
556564
window.Rokt.__event_stream__(event);
565+
} else {
566+
self.eventStreamQueue.push(event);
557567
}
558568
}
559569

test/src/tests.js

Lines changed: 70 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4571,6 +4571,7 @@ describe('Rokt Forwarder', () => {
45714571

45724572
describe('#_sendEventStream', () => {
45734573
beforeEach(() => {
4574+
window.mParticle.forwarder.eventStreamQueue = [];
45744575
window.Rokt = new MockRoktForwarder();
45754576
window.Rokt.createLauncher = async function () {
45764577
return Promise.resolve({
@@ -4618,6 +4619,7 @@ describe('Rokt Forwarder', () => {
46184619
afterEach(() => {
46194620
delete window.Rokt.__event_stream__;
46204621
window.mParticle.forwarder.eventQueue = [];
4622+
window.mParticle.forwarder.eventStreamQueue = [];
46214623
window.mParticle.forwarder.isInitialized = false;
46224624
window.mParticle.Rokt.attachKitCalled = false;
46234625
});
@@ -4650,7 +4652,7 @@ describe('Rokt Forwarder', () => {
46504652
receivedEvents[0].should.deepEqual(testEvent);
46514653
});
46524654

4653-
it('should not throw when window.Rokt.__event_stream__ is not defined', async () => {
4655+
it('should queue event when window.Rokt.__event_stream__ is not defined', async () => {
46544656
await window.mParticle.forwarder.init(
46554657
{ accountId: '123456' },
46564658
reportService.cb,
@@ -4661,16 +4663,23 @@ describe('Rokt Forwarder', () => {
46614663

46624664
await waitForCondition(() => window.mParticle.Rokt.attachKitCalled);
46634665

4666+
var testEvent = {
4667+
EventName: 'Test Event',
4668+
EventCategory: EventType.Other,
4669+
EventDataType: MessageType.PageEvent,
4670+
};
4671+
46644672
(function () {
4665-
window.mParticle.forwarder.process({
4666-
EventName: 'Test Event',
4667-
EventCategory: EventType.Other,
4668-
EventDataType: MessageType.PageEvent,
4669-
});
4673+
window.mParticle.forwarder.process(testEvent);
46704674
}).should.not.throw();
4675+
4676+
window.mParticle.forwarder.eventStreamQueue.length.should.equal(1);
4677+
window.mParticle.forwarder.eventStreamQueue[0].should.deepEqual(
4678+
testEvent
4679+
);
46714680
});
46724681

4673-
it('should not throw when window.Rokt is undefined', async () => {
4682+
it('should queue event when window.Rokt is undefined', async () => {
46744683
await window.mParticle.forwarder.init(
46754684
{ accountId: '123456' },
46764685
reportService.cb,
@@ -4684,14 +4693,21 @@ describe('Rokt Forwarder', () => {
46844693
var savedRokt = window.Rokt;
46854694
window.Rokt = undefined;
46864695

4696+
var testEvent = {
4697+
EventName: 'Test Event',
4698+
EventCategory: EventType.Other,
4699+
EventDataType: MessageType.PageEvent,
4700+
};
4701+
46874702
(function () {
4688-
window.mParticle.forwarder.process({
4689-
EventName: 'Test Event',
4690-
EventCategory: EventType.Other,
4691-
EventDataType: MessageType.PageEvent,
4692-
});
4703+
window.mParticle.forwarder.process(testEvent);
46934704
}).should.not.throw();
46944705

4706+
window.mParticle.forwarder.eventStreamQueue.length.should.equal(1);
4707+
window.mParticle.forwarder.eventStreamQueue[0].should.deepEqual(
4708+
testEvent
4709+
);
4710+
46954711
window.Rokt = savedRokt;
46964712
});
46974713

@@ -4833,6 +4849,48 @@ describe('Rokt Forwarder', () => {
48334849
'foo-mapped-flag': true,
48344850
});
48354851
});
4852+
4853+
it('should flush queued events in FIFO order when __event_stream__ becomes available', async () => {
4854+
await window.mParticle.forwarder.init(
4855+
{ accountId: '123456' },
4856+
reportService.cb,
4857+
true,
4858+
null,
4859+
{}
4860+
);
4861+
4862+
await waitForCondition(() => window.mParticle.Rokt.attachKitCalled);
4863+
4864+
window.mParticle.forwarder.process({
4865+
EventName: 'Event A',
4866+
EventCategory: EventType.Other,
4867+
EventDataType: MessageType.PageEvent,
4868+
});
4869+
window.mParticle.forwarder.process({
4870+
EventName: 'Event B',
4871+
EventCategory: EventType.Other,
4872+
EventDataType: MessageType.PageEvent,
4873+
});
4874+
4875+
window.mParticle.forwarder.eventStreamQueue.length.should.equal(2);
4876+
4877+
var receivedEvents = [];
4878+
window.Rokt.__event_stream__ = function (event) {
4879+
receivedEvents.push(event);
4880+
};
4881+
4882+
window.mParticle.forwarder.process({
4883+
EventName: 'Event C',
4884+
EventCategory: EventType.Other,
4885+
EventDataType: MessageType.PageEvent,
4886+
});
4887+
4888+
receivedEvents.length.should.equal(3);
4889+
receivedEvents[0].EventName.should.equal('Event A');
4890+
receivedEvents[1].EventName.should.equal('Event B');
4891+
receivedEvents[2].EventName.should.equal('Event C');
4892+
window.mParticle.forwarder.eventStreamQueue.length.should.equal(0);
4893+
});
48364894
});
48374895

48384896
describe('#_setRoktSessionId', () => {

0 commit comments

Comments
 (0)