Skip to content

Commit 725c520

Browse files
committed
guard product_action null check in enrichCommerceEventTypes
1 parent fd50e3d commit 725c520

2 files changed

Lines changed: 32 additions & 1 deletion

File tree

src/Rokt-Kit.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -937,7 +937,7 @@ class RoktKit implements KitInterface {
937937
if (!data) continue;
938938

939939
const commerceType = (data.custom_flags as Record<string, string> | undefined)?.['Rokt.CommerceEventType'];
940-
if (commerceType) {
940+
if (commerceType && isObject(data.product_action)) {
941941
(data.product_action as Record<string, unknown>).action = commerceType;
942942
}
943943
}

test/src/tests.spec.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5935,6 +5935,37 @@ describe('Rokt Forwarder', () => {
59355935
expect(events[2].data.product_action.action).toBe('purchase');
59365936
});
59375937

5938+
it('should not throw when product_action is null', async () => {
5939+
const receivedBatches: any[] = [];
5940+
(window as any).Rokt.__batch_stream__ = function (payload: any) {
5941+
receivedBatches.push(payload);
5942+
};
5943+
5944+
const promoBatch: Batch = {
5945+
mpid: 'test-mpid-123',
5946+
events: [
5947+
{
5948+
event_type: 'commerce_event',
5949+
data: {
5950+
custom_flags: {},
5951+
promotion_action: { action: 'view', promotions: [{ id: 'promo-1', name: 'Summer Sale' }] },
5952+
},
5953+
},
5954+
],
5955+
};
5956+
5957+
await (window as any).mParticle.forwarder.init({ accountId: '123456' }, reportService.cb, true, null, {});
5958+
await waitForCondition(() => (window as any).mParticle.Rokt.attachKitCalled);
5959+
5960+
expect(() => {
5961+
(window as any).mParticle.forwarder.processBatch(promoBatch);
5962+
}).not.toThrow();
5963+
5964+
expect(receivedBatches.length).toBe(1);
5965+
expect(receivedBatches[0].events[0].data.promotion_action.action).toBe('view');
5966+
expect(receivedBatches[0].events[0].data.product_action).toBeUndefined();
5967+
});
5968+
59385969
it('should handle batch with no events', async () => {
59395970
const receivedBatches: any[] = [];
59405971
(window as any).Rokt.__batch_stream__ = function (payload: any) {

0 commit comments

Comments
 (0)