Skip to content

Commit

Permalink
test(session-replay): fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
junjie-amplitude committed Dec 6, 2024
1 parent b37da79 commit ec1f760
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 12 deletions.
35 changes: 25 additions & 10 deletions packages/plugin-session-replay-browser/src/session-replay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,24 +74,39 @@ export class SessionReplayPlugin implements EnrichmentPlugin {

async execute(event: Event) {
try {
let sessionId: string | number | undefined = this.config.sessionId;
if (this.options.customSessionId) {
sessionId = this.options.customSessionId(event);
}
const sessionId = this.options.customSessionId(event);
if (sessionId) {
// On event, synchronize the session id to the custom session id from the event. This may
// suffer from offline/delayed events messing up the state stored
if (sessionId !== sessionReplay.getSessionId()) {
await sessionReplay.setSessionId(sessionId).promise;
}

if (sessionId) {
const sessionRecordingProperties = sessionReplay.getSessionReplayProperties();
event.event_properties = {
...event.event_properties,
...sessionRecordingProperties,
};
}
} else {
// On event, synchronize the session id to the what's on the browserConfig (source of truth)
// Choosing not to read from event object here, concerned about offline/delayed events messing up the state stored
// in SR.
if (sessionId !== sessionReplay.getSessionId()) {
const sessionId: string | number | undefined = this.config.sessionId;
if (sessionId && sessionId !== sessionReplay.getSessionId()) {
await sessionReplay.setSessionId(sessionId).promise;
}

const sessionRecordingProperties = sessionReplay.getSessionReplayProperties();
event.event_properties = {
...event.event_properties,
...sessionRecordingProperties,
};
// Treating config.sessionId as source of truth, if the event's session id doesn't match, the
// event is not of the current session (offline/late events). In that case, don't tag the events
if (sessionId && sessionId === event.session_id) {
const sessionRecordingProperties = sessionReplay.getSessionReplayProperties();
event.event_properties = {
...event.event_properties,
...sessionRecordingProperties,
};
}
}

return Promise.resolve(event);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BrowserClient, BrowserConfig, LogLevel, Logger, Plugin } from '@amplitude/analytics-types';
import { BrowserClient, BrowserConfig, LogLevel, Logger, Plugin, Event } from '@amplitude/analytics-types';
import * as sessionReplayBrowser from '@amplitude/session-replay-browser';
import { SessionReplayPlugin, sessionReplayPlugin } from '../src/session-replay';
import { VERSION } from '../src/version';
Expand Down Expand Up @@ -304,6 +304,84 @@ describe('SessionReplayPlugin', () => {
});
await sessionReplay.teardown?.();
});

test('should update the session id on any event when using custom session id', async () => {
const sessionReplay = sessionReplayPlugin({
customSessionId: (event: Event) => {
const event_properties = event.event_properties as { [key: string]: any };
if (!event_properties) {
return;
}
return event_properties['custom_session_id'] as string | undefined;
},
});
await sessionReplay.setup({ ...mockConfig });
getSessionId.mockReturnValueOnce('test_122');
const event = {
event_type: 'event_type',
event_properties: {
property_a: true,
property_b: 123,
custom_session_id: 'test_123',
},
session_id: 124,
};

await sessionReplay.execute(event);
expect(setSessionId).toHaveBeenCalledTimes(1);
expect(setSessionId).toHaveBeenCalledWith('test_123');
});

test('should not update the session id when using custom session id and it does not change', async () => {
const sessionReplay = sessionReplayPlugin({
customSessionId: (event: Event) => {
const event_properties = event.event_properties as { [key: string]: any };
if (!event_properties) {
return;
}
return event_properties['custom_session_id'] as string | undefined;
},
});
await sessionReplay.setup({ ...mockConfig });
getSessionId.mockReturnValueOnce('test_123');
const event = {
event_type: 'event_type',
event_properties: {
property_a: true,
property_b: 123,
custom_session_id: 'test_123',
},
session_id: 124,
};

await sessionReplay.execute(event);
expect(setSessionId).not.toHaveBeenCalled();
});

test('should do nothing when the custom session id cannot be found', async () => {
const sessionReplay = sessionReplayPlugin({
customSessionId: (event: Event) => {
const event_properties = event.event_properties as { [key: string]: any };
if (!event_properties) {
return;
}
return event_properties['custom_session_id'] as string | undefined;
},
});
await sessionReplay.setup({ ...mockConfig });
const event = {
event_type: 'event_type',
event_properties: {
property_a: true,
property_b: 123,
},
session_id: 124,
};

const enrichedEvent = await sessionReplay.execute(event);
expect(setSessionId).not.toHaveBeenCalled();
expect(enrichedEvent).toEqual(event);
});
});

describe('getSessionReplayProperties', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export type EventType = 'replay' | 'interaction';
export interface SessionReplayDestinationSessionMetadata {
type: EventType;
sessionId: string | number;
deviceId: string;
deviceId: string | undefined;
version?: SessionReplayVersion;
}

Expand Down

0 comments on commit ec1f760

Please sign in to comment.