From 0caa8bf09b113f1672a50d04ca302d2c9c717390 Mon Sep 17 00:00:00 2001 From: Cris Ryan Tan Date: Tue, 24 Mar 2026 16:02:11 +1100 Subject: [PATCH 1/2] feat: add defensive queue to _sendEventStream to prevent event loss Buffer events in eventStreamQueue when window.Rokt.__event_stream__ is not yet available, and flush them in FIFO order once it is. Made-with: Cursor --- src/Rokt-Kit.js | 10 ++++++ test/src/tests.js | 79 ++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 77 insertions(+), 12 deletions(-) diff --git a/src/Rokt-Kit.js b/src/Rokt-Kit.js index a3b7fa8..2e7a703 100644 --- a/src/Rokt-Kit.js +++ b/src/Rokt-Kit.js @@ -39,6 +39,7 @@ var constructor = function () { self.placementEventMappingLookup = {}; self.placementEventAttributeMappingLookup = {}; self.eventQueue = []; + self.eventStreamQueue = []; self.integrationName = null; function getEventAttributeValue(event, eventAttributeKey) { @@ -553,7 +554,16 @@ var constructor = function () { function _sendEventStream(event) { if (window.Rokt && typeof window.Rokt.__event_stream__ === 'function') { + if (self.eventStreamQueue.length) { + var queuedEvents = self.eventStreamQueue; + self.eventStreamQueue = []; + for (var i = 0; i < queuedEvents.length; i++) { + window.Rokt.__event_stream__(queuedEvents[i]); + } + } window.Rokt.__event_stream__(event); + } else { + self.eventStreamQueue.push(event); } } diff --git a/test/src/tests.js b/test/src/tests.js index 80a3db2..f9a4a55 100644 --- a/test/src/tests.js +++ b/test/src/tests.js @@ -4571,6 +4571,7 @@ describe('Rokt Forwarder', () => { describe('#_sendEventStream', () => { beforeEach(() => { + window.mParticle.forwarder.eventStreamQueue = []; window.Rokt = new MockRoktForwarder(); window.Rokt.createLauncher = async function () { return Promise.resolve({ @@ -4618,6 +4619,7 @@ describe('Rokt Forwarder', () => { afterEach(() => { delete window.Rokt.__event_stream__; window.mParticle.forwarder.eventQueue = []; + window.mParticle.forwarder.eventStreamQueue = []; window.mParticle.forwarder.isInitialized = false; window.mParticle.Rokt.attachKitCalled = false; }); @@ -4650,7 +4652,7 @@ describe('Rokt Forwarder', () => { receivedEvents[0].should.deepEqual(testEvent); }); - it('should not throw when window.Rokt.__event_stream__ is not defined', async () => { + it('should queue event when window.Rokt.__event_stream__ is not defined', async () => { await window.mParticle.forwarder.init( { accountId: '123456' }, reportService.cb, @@ -4661,16 +4663,21 @@ describe('Rokt Forwarder', () => { await waitForCondition(() => window.mParticle.Rokt.attachKitCalled); + var testEvent = { + EventName: 'Test Event', + EventCategory: EventType.Other, + EventDataType: MessageType.PageEvent, + }; + (function () { - window.mParticle.forwarder.process({ - EventName: 'Test Event', - EventCategory: EventType.Other, - EventDataType: MessageType.PageEvent, - }); + window.mParticle.forwarder.process(testEvent); }).should.not.throw(); + + window.mParticle.forwarder.eventStreamQueue.length.should.equal(1); + window.mParticle.forwarder.eventStreamQueue[0].should.deepEqual(testEvent); }); - it('should not throw when window.Rokt is undefined', async () => { + it('should queue event when window.Rokt is undefined', async () => { await window.mParticle.forwarder.init( { accountId: '123456' }, reportService.cb, @@ -4684,14 +4691,19 @@ describe('Rokt Forwarder', () => { var savedRokt = window.Rokt; window.Rokt = undefined; + var testEvent = { + EventName: 'Test Event', + EventCategory: EventType.Other, + EventDataType: MessageType.PageEvent, + }; + (function () { - window.mParticle.forwarder.process({ - EventName: 'Test Event', - EventCategory: EventType.Other, - EventDataType: MessageType.PageEvent, - }); + window.mParticle.forwarder.process(testEvent); }).should.not.throw(); + window.mParticle.forwarder.eventStreamQueue.length.should.equal(1); + window.mParticle.forwarder.eventStreamQueue[0].should.deepEqual(testEvent); + window.Rokt = savedRokt; }); @@ -4833,6 +4845,49 @@ describe('Rokt Forwarder', () => { 'foo-mapped-flag': true, }); }); + + it('should flush queued events in FIFO order when __event_stream__ becomes available', async () => { + await window.mParticle.forwarder.init( + { accountId: '123456' }, + reportService.cb, + true, + null, + {} + ); + + await waitForCondition(() => window.mParticle.Rokt.attachKitCalled); + + window.mParticle.forwarder.process({ + EventName: 'Event A', + EventCategory: EventType.Other, + EventDataType: MessageType.PageEvent, + }); + window.mParticle.forwarder.process({ + EventName: 'Event B', + EventCategory: EventType.Other, + EventDataType: MessageType.PageEvent, + }); + + window.mParticle.forwarder.eventStreamQueue.length.should.equal(2); + + var receivedEvents = []; + window.Rokt.__event_stream__ = function (event) { + receivedEvents.push(event); + }; + + window.mParticle.forwarder.process({ + EventName: 'Event C', + EventCategory: EventType.Other, + EventDataType: MessageType.PageEvent, + }); + + receivedEvents.length.should.equal(3); + receivedEvents[0].EventName.should.equal('Event A'); + receivedEvents[1].EventName.should.equal('Event B'); + receivedEvents[2].EventName.should.equal('Event C'); + window.mParticle.forwarder.eventStreamQueue.length.should.equal(0); + }); + }); describe('#_setRoktSessionId', () => { From 1c0c392db10769abe9a64e4f97c04b03f94e5466 Mon Sep 17 00:00:00 2001 From: Alexander Sapountzis Date: Wed, 25 Mar 2026 09:58:22 -0400 Subject: [PATCH 2/2] fix: resolve prettier lint errors in test file --- test/src/tests.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/test/src/tests.js b/test/src/tests.js index f9a4a55..157c277 100644 --- a/test/src/tests.js +++ b/test/src/tests.js @@ -4674,7 +4674,9 @@ describe('Rokt Forwarder', () => { }).should.not.throw(); window.mParticle.forwarder.eventStreamQueue.length.should.equal(1); - window.mParticle.forwarder.eventStreamQueue[0].should.deepEqual(testEvent); + window.mParticle.forwarder.eventStreamQueue[0].should.deepEqual( + testEvent + ); }); it('should queue event when window.Rokt is undefined', async () => { @@ -4702,7 +4704,9 @@ describe('Rokt Forwarder', () => { }).should.not.throw(); window.mParticle.forwarder.eventStreamQueue.length.should.equal(1); - window.mParticle.forwarder.eventStreamQueue[0].should.deepEqual(testEvent); + window.mParticle.forwarder.eventStreamQueue[0].should.deepEqual( + testEvent + ); window.Rokt = savedRokt; }); @@ -4887,7 +4891,6 @@ describe('Rokt Forwarder', () => { receivedEvents[2].EventName.should.equal('Event C'); window.mParticle.forwarder.eventStreamQueue.length.should.equal(0); }); - }); describe('#_setRoktSessionId', () => {