Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: have track calls await on session replay init promise #6

Merged
merged 1 commit into from
Jul 24, 2024
Merged
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
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
Loading