Skip to content

Commit ddcfa61

Browse files
authored
fix: align identity events with mParticle batch schema (#88)
* fix: align identity events with mParticle batch schema * fix: send mpid in event
1 parent 9f903ad commit ddcfa61

2 files changed

Lines changed: 30 additions & 28 deletions

File tree

src/Rokt-Kit.ts

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,14 @@ const moduleId = 181;
200200
const EVENT_NAME_SELECT_PLACEMENTS = 'selectPlacements';
201201
const ADBLOCK_CONTROL_DOMAIN = 'apps.roktecommerce.com';
202202
const INIT_LOG_SAMPLING_RATE = 0.1;
203-
const MESSAGE_TYPE_PROFILE = 14; // mParticle MessageType.Profile
203+
const ROKT_IDENTITY_EVENT_TYPE = {
204+
LOGIN: 'login',
205+
LOGOUT: 'logout',
206+
MODIFY_USER: 'modify_user',
207+
IDENTIFY: 'identify',
208+
} as const;
209+
210+
type RoktIdentityEventType = (typeof ROKT_IDENTITY_EVENT_TYPE)[keyof typeof ROKT_IDENTITY_EVENT_TYPE];
204211

205212
// ============================================================
206213
// Reporting service constants
@@ -759,25 +766,20 @@ class RoktKit implements KitInterface {
759766
mp().logEvent(EVENT_NAME_SELECT_PLACEMENTS, EVENT_TYPE_OTHER, attributes as Record<string, unknown>);
760767
}
761768

762-
private buildIdentityEvent(eventName: string, filteredUser: FilteredUser): BaseEvent {
769+
private buildIdentityEvent(eventType: RoktIdentityEventType, filteredUser: FilteredUser): BaseEvent {
763770
const mpid = filteredUser.getMPID();
764-
const sessionId =
771+
const sessionUuid =
765772
mp() && mp().sessionManager && typeof mp().sessionManager!.getSession === 'function'
766773
? mp().sessionManager!.getSession()
767-
: null;
768-
const userIdentities =
769-
filteredUser.getUserIdentities && typeof filteredUser.getUserIdentities === 'function'
770-
? filteredUser.getUserIdentities().userIdentities
771-
: null;
774+
: undefined;
772775

773776
return {
774-
EventName: eventName,
775-
EventDataType: MESSAGE_TYPE_PROFILE,
776-
EventCategory: 0,
777-
Timestamp: Date.now(),
778-
MPID: mpid,
779-
SessionId: sessionId,
780-
UserIdentities: userIdentities,
777+
event_type: eventType,
778+
data: {
779+
timestamp_unixtime_ms: Date.now(),
780+
session_uuid: sessionUuid ?? undefined,
781+
mpid,
782+
},
781783
} as unknown as BaseEvent;
782784
}
783785

@@ -1106,28 +1108,28 @@ class RoktKit implements KitInterface {
11061108
const filteredUser = user as FilteredUser;
11071109
this.filters.filteredUser = filteredUser;
11081110
this.userAttributes = user.getAllUserAttributes();
1109-
this.pendingIdentityEvents.push(this.buildIdentityEvent('identify', filteredUser));
1111+
this.pendingIdentityEvents.push(this.buildIdentityEvent(ROKT_IDENTITY_EVENT_TYPE.IDENTIFY, filteredUser));
11101112
return 'Successfully called onUserIdentified for forwarder: ' + name;
11111113
}
11121114

11131115
public onLoginComplete(user: IMParticleUser, _filteredIdentityRequest: unknown): string {
11141116
const filteredUser = user as FilteredUser;
11151117
this.userAttributes = user.getAllUserAttributes();
1116-
this.pendingIdentityEvents.push(this.buildIdentityEvent('login', filteredUser));
1118+
this.pendingIdentityEvents.push(this.buildIdentityEvent(ROKT_IDENTITY_EVENT_TYPE.LOGIN, filteredUser));
11171119
return 'Successfully called onLoginComplete for forwarder: ' + name;
11181120
}
11191121

11201122
public onLogoutComplete(user: IMParticleUser, _filteredIdentityRequest: unknown): string {
11211123
const filteredUser = user as FilteredUser;
11221124
this.userAttributes = user.getAllUserAttributes();
1123-
this.pendingIdentityEvents.push(this.buildIdentityEvent('logout', filteredUser));
1125+
this.pendingIdentityEvents.push(this.buildIdentityEvent(ROKT_IDENTITY_EVENT_TYPE.LOGOUT, filteredUser));
11241126
return 'Successfully called onLogoutComplete for forwarder: ' + name;
11251127
}
11261128

11271129
public onModifyComplete(user: IMParticleUser, _filteredIdentityRequest: unknown): string {
11281130
const filteredUser = user as FilteredUser;
11291131
this.userAttributes = user.getAllUserAttributes();
1130-
this.pendingIdentityEvents.push(this.buildIdentityEvent('modify', filteredUser));
1132+
this.pendingIdentityEvents.push(this.buildIdentityEvent(ROKT_IDENTITY_EVENT_TYPE.MODIFY_USER, filteredUser));
11311133
return 'Successfully called onModifyComplete for forwarder: ' + name;
11321134
}
11331135

test/src/tests.spec.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4550,8 +4550,8 @@ describe('Rokt Forwarder', () => {
45504550

45514551
const pending = (window as any).mParticle.forwarder.pendingIdentityEvents;
45524552
expect(pending.length).toBe(1);
4553-
expect(pending[0].EventName).toBe('login');
4554-
expect(pending[0].EventDataType).toBe(14);
4553+
expect(pending[0].event_type).toBe('login');
4554+
expect(pending[0].data.timestamp_unixtime_ms).toBeTypeOf('number');
45554555
});
45564556

45574557
it('should add an identity event to pendingIdentityEvents on onLogoutComplete', () => {
@@ -4565,8 +4565,8 @@ describe('Rokt Forwarder', () => {
45654565

45664566
const pending = (window as any).mParticle.forwarder.pendingIdentityEvents;
45674567
expect(pending.length).toBe(1);
4568-
expect(pending[0].EventName).toBe('logout');
4569-
expect(pending[0].EventDataType).toBe(14);
4568+
expect(pending[0].event_type).toBe('logout');
4569+
expect(pending[0].data.timestamp_unixtime_ms).toBeTypeOf('number');
45704570
});
45714571

45724572
it('should add identity events to pendingIdentityEvents on onModifyComplete and onUserIdentified', () => {
@@ -4581,8 +4581,8 @@ describe('Rokt Forwarder', () => {
45814581

45824582
const pending = (window as any).mParticle.forwarder.pendingIdentityEvents;
45834583
expect(pending.length).toBe(2);
4584-
expect(pending[0].EventName).toBe('modify');
4585-
expect(pending[1].EventName).toBe('identify');
4584+
expect(pending[0].event_type).toBe('modify_user');
4585+
expect(pending[1].event_type).toBe('identify');
45864586
});
45874587

45884588
it('should merge pendingIdentityEvents into the outgoing batch and clear the queue', async () => {
@@ -4608,8 +4608,8 @@ describe('Rokt Forwarder', () => {
46084608
expect(receivedBatches.length).toBe(1);
46094609
// Original 1 custom_event + 1 identity event from onLoginComplete
46104610
expect(receivedBatches[0].events.length).toBe(2);
4611-
expect(receivedBatches[0].events[1].EventName).toBe('login');
4612-
expect(receivedBatches[0].events[1].EventDataType).toBe(14);
4611+
expect(receivedBatches[0].events[1].event_type).toBe('login');
4612+
expect(receivedBatches[0].events[1].data.timestamp_unixtime_ms).toBeTypeOf('number');
46134613
// Queue should be cleared after flush
46144614
expect((window as any).mParticle.forwarder.pendingIdentityEvents.length).toBe(0);
46154615
});
@@ -4643,7 +4643,7 @@ describe('Rokt Forwarder', () => {
46434643
// The queued batch should have the pending identity event merged in
46444644
expect(receivedBatches.length).toBe(1);
46454645
expect(receivedBatches[0].events.length).toBe(2);
4646-
expect(receivedBatches[0].events[1].EventDataType).toBe(14);
4646+
expect(receivedBatches[0].events[1].event_type).toBe('login');
46474647
expect((window as any).mParticle.forwarder.pendingIdentityEvents.length).toBe(0);
46484648
expect((window as any).mParticle.forwarder.batchQueue.length).toBe(0);
46494649
});

0 commit comments

Comments
 (0)