amplitude / Amplitude-TypeScript

TypeScript Amplitude Analytics SDK
https://amplitude.github.io/Amplitude-TypeScript/
MIT License
125 stars 35 forks source link

@amplitude/session-replay-browser v 1.11.3 does not call shutdown() #822

Open GxDesign opened 1 month ago

GxDesign commented 1 month ago

Expected Behavior

Calling sessionReplay.shutdown() should stop recording

Current Behavior

When running in debug mode, calling shutdown() logged the shutdown action in v1.9.4 (my previous version) and stopped the recording. In v1.11.3, nothing gets logged and the recording continues. I am using the segment wrapper.

Using the latest wrapper and session-replay-browser breaks shutdown functionality, reverting to wrapper version 0.1.0 and session-replay-browser v1.9.4 works.

Possible Solution

Steps to Reproduce

  1. install @amplitude/session-replay-browser v1.11.3, and @amplitude/segment-session-replay-wrapper v0.3.1 (both latest)
  2. Im using @segment/analytics-next v1.72.1 (latest)
    
    const segment = new AnalyticsBrowser()
    // 3. call action to load segment
    const initSegment = () => segment.load({ writeKey: API_KEY })
    // 4. call action to initialize SR wrapper, pass instance, API key etc
    const initSRWrapper = () => setupAmpSRSegmentWrapper({  segmentInstance: segment, ... })
    // 5. call action to shut down recording
    const shutdownSR = () => sessionReplay.shutdown()

Environment

GxDesign commented 1 month ago

Also, can we see the source code for the wrapper as we can for other packages? going to it from NPMjs returns a 404.

lewgordon-amplitude commented 1 month ago

Hey @GxDesign we're going to be looking at this relatively soon. Also we're planning on making the source code public today or early next week for the Segment wrapper.

lewgordon-amplitude commented 1 month ago

Okay just open source the repo. Check it out here: https://github.com/amplitude/segment-session-replay-wrapper

lewgordon-amplitude commented 4 days ago

Hey @GxDesign I'm curious about your setup. When I have something like this it does NOT work:

import * as sessionReplay from "@amplitude/session-replay-browser";
import setupAmpSRSegmentWrapper from "@amplitude/segment-session-replay-wrapper";

const segment = `...`;
setupAmpSRSegmentWrapper({
  segmentInstance: segment,
  amplitudeApiKey: apiKey,
  sessionReplayOptions: {
    ...
  },
});

sessionReplay.shutdown();

But if I do this is does work!

import * as sessionReplay from "@amplitude/segment-session-replay-wrapper/node_modules/@amplitude/session-replay-browser";
import setupAmpSRSegmentWrapper from "@amplitude/segment-session-replay-wrapper";

const segment = `...`;
setupAmpSRSegmentWrapper({
  segmentInstance: segment,
  amplitudeApiKey: apiKey,
  sessionReplayOptions: {
    ...
  },
});

sessionReplay.shutdown();

Just wanted to check this is not the same issue you're seeing before I dig further.

GxDesign commented 3 days ago

@lewgordon-amplitude the first is what I have.

lewgordon-amplitude commented 3 days ago

I wonder if the second works at all for you? I'm thinking overall the API could be better since I'd just except the first example to just work.

GxDesign commented 3 days ago

The second way works, I see the log saying the capturing stopped and then i see it restarted. I do keep seeing a lot of batch event logs I dont recall seeing before though: Amplitude Logger [Log]: Session replay event batch with seq id 1783 tracked successfully for session id Those continue about every 30-60 seconds even while inactive

GxDesign commented 3 days ago

Not to distract from the main issue of shutting down, but any idea why my sessions are also being duplicated? Not sure if its related: Screenshot 2024-09-04 at 12 32 59 PM

lewgordon-amplitude commented 3 days ago

Oh that's interesting. Maybe it explains part of it? I was thinking there's potentially two instances set up here. One when you pull the @amplitude/session-replay-browser and another when you pull the Segment wrapper.

GxDesign commented 3 days ago

Could you guide me on determining that? Im trying out the second implementation you presented and I still see the duplicate sessions.

GxDesign commented 3 days ago

This is my code, in short:

import setupAmpSRSegmentWrapper from '@amplitude/segment-session-replay-wrapper'
import * as sessionReplay from '@amplitude/segment-session-replay-wrapper/node_modules/@amplitude/session-replay-browser'
import {AnalyticsBrowser} from '@segment/analytics-next'

const segment = new AnalyticsBrowser()

export const segmentInit = async (
  key: string,
) => {
  segment.load({
    writeKey: key,
    cdnURL: CDN_URL
  })
}

export const segmentSessionReplayInit = () => {
  if (environment.amplitudeKey) {
    void setupAmpSRSegmentWrapper({
      segmentInstance: segment,
      amplitudeApiKey: environment.amplitudeKey,
      sessionReplayOptions: {
        sampleRate: 1,
        privacyConfig: {
          defaultMaskLevel: 'conservative',
        },
        ...(environment.nodeEnv === 'development' ? SESSION_REPLAY_DEV_OPTIONS : {}),
      },
    })
  }
}

export const segmentSessionReplayShutdown = () => sessionReplay.shutdown()
GxDesign commented 2 days ago

Okay, Ive narrowed it down to an issue with how I restart the session replay, Im calling segmentSessionReplayInit again which I think registers a new plugin. When I stay on a route without shutting it down and restarting it (which occurs when entering restricted routes and returning to non-restricted), I get only one session recording.

GxDesign commented 2 days ago

Whats the correct way to restart the plugin?

GxDesign commented 1 day ago

@lewgordon-amplitude bumping. Ive tried calling sessionReplay.init with the import from the wrapper. Im still getting duplicate sessions and shutdown occasionally fails.