Skip to content

Commit d8a972c

Browse files
fix: SDKE-319 - Add Events to Event Queue when Kit is not ready (#46)
1 parent 22ec8a9 commit d8a972c

2 files changed

Lines changed: 76 additions & 1 deletion

File tree

src/Rokt-Kit.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ var constructor = function () {
3030
self.userAttributes = {};
3131
self.testHelpers = null;
3232
self.placementEventMappingLookup = {};
33+
self.eventQueue = [];
3334

3435
/**
3536
* Generates the Rokt launcher script URL with optional domain override and extensions
@@ -276,9 +277,20 @@ var constructor = function () {
276277
window.Rokt.setExtensionData(partnerExtensionData);
277278
}
278279

280+
function processEventQueue() {
281+
self.eventQueue.forEach(function (event) {
282+
processEvent(event);
283+
});
284+
self.eventQueue = [];
285+
}
286+
279287
function processEvent(event) {
288+
if (!isKitReady()) {
289+
self.eventQueue.push(event);
290+
return;
291+
}
292+
280293
if (
281-
!isKitReady() ||
282294
isEmpty(self.placementEventMappingLookup) ||
283295
typeof window.mParticle.Rokt.setLocalSessionAttribute !== 'function'
284296
) {
@@ -342,6 +354,7 @@ var constructor = function () {
342354
self.isInitialized = true;
343355
// Attaches the kit to the Rokt manager
344356
window.mParticle.Rokt.attachKit(self);
357+
processEventQueue();
345358
})
346359
.catch(function (err) {
347360
console.error('Error creating Rokt launcher:', err);

test/src/tests.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1995,6 +1995,12 @@ describe('Rokt Forwarder', () => {
19951995
};
19961996
});
19971997

1998+
afterEach(() => {
1999+
window.mParticle.forwarder.eventQueue = [];
2000+
window.mParticle.forwarder.isInitialized = false;
2001+
window.mParticle.Rokt.attachKitCalled = false;
2002+
});
2003+
19982004
it('set a local session selection attribute if the event is a mapped placement event', async () => {
19992005
// Mocks hashed values for testing
20002006
const placementEventMapping = JSON.stringify([
@@ -2035,6 +2041,62 @@ describe('Rokt Forwarder', () => {
20352041
'foo-mapped-flag': true,
20362042
});
20372043
});
2044+
2045+
it('should add the event to the event queue if the kit is not initialized', async () => {
2046+
await window.mParticle.forwarder.init(
2047+
{
2048+
accountId: '123456',
2049+
},
2050+
reportService.cb,
2051+
true,
2052+
null,
2053+
{}
2054+
);
2055+
2056+
window.mParticle.forwarder.process({
2057+
EventName: 'Video Watched A',
2058+
EventCategory: EventType.Other,
2059+
EventDataType: MessageType.PageEvent,
2060+
});
2061+
2062+
window.mParticle.forwarder.eventQueue.should.deepEqual([
2063+
{
2064+
EventName: 'Video Watched A',
2065+
EventCategory: EventType.Other,
2066+
EventDataType: MessageType.PageEvent,
2067+
},
2068+
]);
2069+
});
2070+
2071+
it('should process queued events once the kit is ready', async () => {
2072+
await window.mParticle.forwarder.init(
2073+
{
2074+
accountId: '123456',
2075+
},
2076+
reportService.cb,
2077+
true,
2078+
null,
2079+
{}
2080+
);
2081+
2082+
window.mParticle.forwarder.process({
2083+
EventName: 'Video Watched B',
2084+
EventCategory: EventType.Other,
2085+
EventDataType: MessageType.PageEvent,
2086+
});
2087+
2088+
window.mParticle.forwarder.eventQueue.should.deepEqual([
2089+
{
2090+
EventName: 'Video Watched B',
2091+
EventCategory: EventType.Other,
2092+
EventDataType: MessageType.PageEvent,
2093+
},
2094+
]);
2095+
2096+
await waitForCondition(() => window.mParticle.Rokt.attachKitCalled);
2097+
2098+
window.mParticle.forwarder.eventQueue.should.deepEqual([]);
2099+
});
20382100
});
20392101

20402102
describe('#parseSettingsString', () => {

0 commit comments

Comments
 (0)