From bfed5489ff9081f6116b35c1901f84b7ddf3a0e7 Mon Sep 17 00:00:00 2001 From: Kelly Wallach Date: Wed, 24 Jul 2024 10:35:07 -0400 Subject: [PATCH] fix: have track calls await on session replay init promise --- src/actions.ts | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/actions.ts b/src/actions.ts index 72e8c63..c0242d9 100644 --- a/src/actions.ts +++ b/src/actions.ts @@ -39,9 +39,10 @@ export const updateSessionIdAndAddProperties = async (ctx: Context) => { export const createSegmentActionsPlugin = async ({ amplitudeApiKey, - segmentInstance, sessionReplayOptions, + segmentInstance, }: PluginOptions) => { + let initPromise: Promise; const sessionReplayPlugin: Plugin = { name: 'Session Replay Events', type: 'enrichment', @@ -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);