Skip to content

Commit

Permalink
fix: have track calls await on session replay init promise
Browse files Browse the repository at this point in the history
  • Loading branch information
Kelly Wallach committed Jul 24, 2024
1 parent 862aeab commit bfed548
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ export const updateSessionIdAndAddProperties = async (ctx: Context) => {

export const createSegmentActionsPlugin = async ({
amplitudeApiKey,
segmentInstance,
sessionReplayOptions,
segmentInstance,
}: PluginOptions) => {
let initPromise: Promise<void>;
const sessionReplayPlugin: Plugin = {
name: 'Session Replay Events',
type: 'enrichment',
Expand All @@ -52,19 +53,25 @@ export const createSegmentActionsPlugin = async ({
const user = ajs.user();
const deviceId = user.anonymousId();
const storedSessionId = getStoredSessionId();

await sessionReplay.init(amplitudeApiKey, {
initPromise = sessionReplay.init(amplitudeApiKey, {
...sessionReplayOptions,
sessionId: storedSessionId,
deviceId: deviceId || undefined,
}).promise;
},

track: updateSessionIdAndAddProperties,
track: async (ctx) => {
await initPromise;
return await updateSessionIdAndAddProperties(ctx);
},

page: updateSessionIdAndAddProperties,
page: async (ctx) => {
await initPromise;
return await updateSessionIdAndAddProperties(ctx);
},

identify: async (ctx) => {
await initPromise;
const deviceId = getUserId(ctx);
const sessionId = sessionReplay.getSessionId();
sessionId && (await sessionReplay.setSessionId(sessionId, deviceId).promise);
Expand Down

0 comments on commit bfed548

Please sign in to comment.