Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/Rokt-Kit.js
Original file line number Diff line number Diff line change
Expand Up @@ -552,16 +552,22 @@ var constructor = function () {
}
}

function _enrichEvent(event) {
return mergeObjects({}, event, {
UserAttributes: self.userAttributes,
});
}

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__(_enrichEvent(queuedEvents[i]));
}
}
window.Rokt.__event_stream__(event);
window.Rokt.__event_stream__(_enrichEvent(event));
} else {
self.eventStreamQueue.push(event);
}
Expand Down
137 changes: 136 additions & 1 deletion test/src/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -4649,7 +4649,10 @@ describe('Rokt Forwarder', () => {
window.mParticle.forwarder.process(testEvent);

receivedEvents.length.should.equal(1);
receivedEvents[0].should.deepEqual(testEvent);
receivedEvents[0].EventName.should.equal('Test Event');
receivedEvents[0].EventCategory.should.equal(EventType.Other);
receivedEvents[0].EventDataType.should.equal(MessageType.PageEvent);
receivedEvents[0].UserAttributes.should.deepEqual({});
});

it('should queue event when window.Rokt.__event_stream__ is not defined', async () => {
Expand Down Expand Up @@ -4850,6 +4853,138 @@ describe('Rokt Forwarder', () => {
});
});

it('should enrich event with Kit userAttributes before sending to event stream', async () => {
var receivedEvents = [];
window.Rokt.__event_stream__ = function (event) {
receivedEvents.push(event);
};

await window.mParticle.forwarder.init(
{ accountId: '123456' },
reportService.cb,
true,
null,
{}
);

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

window.mParticle.forwarder.userAttributes = {
firstName: 'John',
lastName: 'Doe',
};

window.mParticle.forwarder.process({
EventName: 'Test Event',
EventCategory: EventType.Other,
EventDataType: MessageType.PageEvent,
});

receivedEvents.length.should.equal(1);
receivedEvents[0].UserAttributes.should.deepEqual({
firstName: 'John',
lastName: 'Doe',
});
});

it('should override event UserAttributes with Kit userAttributes', async () => {
var receivedEvents = [];
window.Rokt.__event_stream__ = function (event) {
receivedEvents.push(event);
};

await window.mParticle.forwarder.init(
{ accountId: '123456' },
reportService.cb,
true,
null,
{}
);

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

window.mParticle.forwarder.userAttributes = {
firstName: 'Jane',
};

window.mParticle.forwarder.process({
EventName: 'Test Event',
EventCategory: EventType.Other,
EventDataType: MessageType.PageEvent,
UserAttributes: {
firstName: 'Stale',
obsoleteAttr: 'should-not-appear',
},
});

receivedEvents.length.should.equal(1);
receivedEvents[0].UserAttributes.should.deepEqual({
firstName: 'Jane',
});
});

it('should not mutate the original event when enriching with userAttributes', async () => {
var receivedEvents = [];
window.Rokt.__event_stream__ = function (event) {
receivedEvents.push(event);
};

await window.mParticle.forwarder.init(
{ accountId: '123456' },
reportService.cb,
true,
null,
{}
);

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

window.mParticle.forwarder.userAttributes = {
firstName: 'John',
};

var originalEvent = {
EventName: 'Test Event',
EventCategory: EventType.Other,
EventDataType: MessageType.PageEvent,
};

window.mParticle.forwarder.process(originalEvent);

originalEvent.should.not.have.property('UserAttributes');
receivedEvents[0].UserAttributes.should.deepEqual({
firstName: 'John',
});
});

it('should send empty UserAttributes when Kit has no userAttributes', async () => {
var receivedEvents = [];
window.Rokt.__event_stream__ = function (event) {
receivedEvents.push(event);
};

await window.mParticle.forwarder.init(
{ accountId: '123456' },
reportService.cb,
true,
null,
{}
);

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

window.mParticle.forwarder.userAttributes = {};

window.mParticle.forwarder.process({
EventName: 'Test Event',
EventCategory: EventType.Other,
EventDataType: MessageType.PageEvent,
});

receivedEvents.length.should.equal(1);
receivedEvents[0].UserAttributes.should.deepEqual({});
});

it('should flush queued events in FIFO order when __event_stream__ becomes available', async () => {
await window.mParticle.forwarder.init(
{ accountId: '123456' },
Expand Down
Loading