diff --git a/src/Rokt-Kit.ts b/src/Rokt-Kit.ts index 6970ca9..124a522 100644 --- a/src/Rokt-Kit.ts +++ b/src/Rokt-Kit.ts @@ -200,7 +200,14 @@ const moduleId = 181; const EVENT_NAME_SELECT_PLACEMENTS = 'selectPlacements'; const ADBLOCK_CONTROL_DOMAIN = 'apps.roktecommerce.com'; const INIT_LOG_SAMPLING_RATE = 0.1; -const MESSAGE_TYPE_PROFILE = 14; // mParticle MessageType.Profile +const ROKT_IDENTITY_EVENT_TYPE = { + LOGIN: 'login', + LOGOUT: 'logout', + MODIFY_USER: 'modify_user', + IDENTIFY: 'identify', +} as const; + +type RoktIdentityEventType = (typeof ROKT_IDENTITY_EVENT_TYPE)[keyof typeof ROKT_IDENTITY_EVENT_TYPE]; // ============================================================ // Reporting service constants @@ -759,25 +766,20 @@ class RoktKit implements KitInterface { mp().logEvent(EVENT_NAME_SELECT_PLACEMENTS, EVENT_TYPE_OTHER, attributes as Record); } - private buildIdentityEvent(eventName: string, filteredUser: FilteredUser): BaseEvent { + private buildIdentityEvent(eventType: RoktIdentityEventType, filteredUser: FilteredUser): BaseEvent { const mpid = filteredUser.getMPID(); - const sessionId = + const sessionUuid = mp() && mp().sessionManager && typeof mp().sessionManager!.getSession === 'function' ? mp().sessionManager!.getSession() - : null; - const userIdentities = - filteredUser.getUserIdentities && typeof filteredUser.getUserIdentities === 'function' - ? filteredUser.getUserIdentities().userIdentities - : null; + : undefined; return { - EventName: eventName, - EventDataType: MESSAGE_TYPE_PROFILE, - EventCategory: 0, - Timestamp: Date.now(), - MPID: mpid, - SessionId: sessionId, - UserIdentities: userIdentities, + event_type: eventType, + data: { + timestamp_unixtime_ms: Date.now(), + session_uuid: sessionUuid ?? undefined, + mpid, + }, } as unknown as BaseEvent; } @@ -1106,28 +1108,28 @@ class RoktKit implements KitInterface { const filteredUser = user as FilteredUser; this.filters.filteredUser = filteredUser; this.userAttributes = user.getAllUserAttributes(); - this.pendingIdentityEvents.push(this.buildIdentityEvent('identify', filteredUser)); + this.pendingIdentityEvents.push(this.buildIdentityEvent(ROKT_IDENTITY_EVENT_TYPE.IDENTIFY, filteredUser)); return 'Successfully called onUserIdentified for forwarder: ' + name; } public onLoginComplete(user: IMParticleUser, _filteredIdentityRequest: unknown): string { const filteredUser = user as FilteredUser; this.userAttributes = user.getAllUserAttributes(); - this.pendingIdentityEvents.push(this.buildIdentityEvent('login', filteredUser)); + this.pendingIdentityEvents.push(this.buildIdentityEvent(ROKT_IDENTITY_EVENT_TYPE.LOGIN, filteredUser)); return 'Successfully called onLoginComplete for forwarder: ' + name; } public onLogoutComplete(user: IMParticleUser, _filteredIdentityRequest: unknown): string { const filteredUser = user as FilteredUser; this.userAttributes = user.getAllUserAttributes(); - this.pendingIdentityEvents.push(this.buildIdentityEvent('logout', filteredUser)); + this.pendingIdentityEvents.push(this.buildIdentityEvent(ROKT_IDENTITY_EVENT_TYPE.LOGOUT, filteredUser)); return 'Successfully called onLogoutComplete for forwarder: ' + name; } public onModifyComplete(user: IMParticleUser, _filteredIdentityRequest: unknown): string { const filteredUser = user as FilteredUser; this.userAttributes = user.getAllUserAttributes(); - this.pendingIdentityEvents.push(this.buildIdentityEvent('modify', filteredUser)); + this.pendingIdentityEvents.push(this.buildIdentityEvent(ROKT_IDENTITY_EVENT_TYPE.MODIFY_USER, filteredUser)); return 'Successfully called onModifyComplete for forwarder: ' + name; } diff --git a/test/src/tests.spec.ts b/test/src/tests.spec.ts index 379285d..4bc97e5 100644 --- a/test/src/tests.spec.ts +++ b/test/src/tests.spec.ts @@ -4550,8 +4550,8 @@ describe('Rokt Forwarder', () => { const pending = (window as any).mParticle.forwarder.pendingIdentityEvents; expect(pending.length).toBe(1); - expect(pending[0].EventName).toBe('login'); - expect(pending[0].EventDataType).toBe(14); + expect(pending[0].event_type).toBe('login'); + expect(pending[0].data.timestamp_unixtime_ms).toBeTypeOf('number'); }); it('should add an identity event to pendingIdentityEvents on onLogoutComplete', () => { @@ -4565,8 +4565,8 @@ describe('Rokt Forwarder', () => { const pending = (window as any).mParticle.forwarder.pendingIdentityEvents; expect(pending.length).toBe(1); - expect(pending[0].EventName).toBe('logout'); - expect(pending[0].EventDataType).toBe(14); + expect(pending[0].event_type).toBe('logout'); + expect(pending[0].data.timestamp_unixtime_ms).toBeTypeOf('number'); }); it('should add identity events to pendingIdentityEvents on onModifyComplete and onUserIdentified', () => { @@ -4581,8 +4581,8 @@ describe('Rokt Forwarder', () => { const pending = (window as any).mParticle.forwarder.pendingIdentityEvents; expect(pending.length).toBe(2); - expect(pending[0].EventName).toBe('modify'); - expect(pending[1].EventName).toBe('identify'); + expect(pending[0].event_type).toBe('modify_user'); + expect(pending[1].event_type).toBe('identify'); }); it('should merge pendingIdentityEvents into the outgoing batch and clear the queue', async () => { @@ -4608,8 +4608,8 @@ describe('Rokt Forwarder', () => { expect(receivedBatches.length).toBe(1); // Original 1 custom_event + 1 identity event from onLoginComplete expect(receivedBatches[0].events.length).toBe(2); - expect(receivedBatches[0].events[1].EventName).toBe('login'); - expect(receivedBatches[0].events[1].EventDataType).toBe(14); + expect(receivedBatches[0].events[1].event_type).toBe('login'); + expect(receivedBatches[0].events[1].data.timestamp_unixtime_ms).toBeTypeOf('number'); // Queue should be cleared after flush expect((window as any).mParticle.forwarder.pendingIdentityEvents.length).toBe(0); }); @@ -4643,7 +4643,7 @@ describe('Rokt Forwarder', () => { // The queued batch should have the pending identity event merged in expect(receivedBatches.length).toBe(1); expect(receivedBatches[0].events.length).toBe(2); - expect(receivedBatches[0].events[1].EventDataType).toBe(14); + expect(receivedBatches[0].events[1].event_type).toBe('login'); expect((window as any).mParticle.forwarder.pendingIdentityEvents.length).toBe(0); expect((window as any).mParticle.forwarder.batchQueue.length).toBe(0); });