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
40 changes: 21 additions & 19 deletions src/Rokt-Kit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -759,25 +766,20 @@ class RoktKit implements KitInterface {
mp().logEvent(EVENT_NAME_SELECT_PLACEMENTS, EVENT_TYPE_OTHER, attributes as Record<string, unknown>);
}

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;
}

Expand Down Expand Up @@ -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;
}

Expand Down
18 changes: 9 additions & 9 deletions test/src/tests.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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', () => {
Expand All @@ -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 () => {
Expand All @@ -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);
});
Expand Down Expand Up @@ -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);
});
Expand Down
Loading