diff --git a/examples/example-expo-latest/package.json b/examples/example-expo-latest/package.json index 5fd9e8b4..1c63e771 100644 --- a/examples/example-expo-latest/package.json +++ b/examples/example-expo-latest/package.json @@ -21,7 +21,7 @@ "expo-localization": "~15.0.3", "expo-status-bar": "~1.12.1", "posthog-react-native": "file:.yalc/posthog-react-native", - "posthog-react-native-session-replay": "^0.1.2", + "posthog-react-native-session-replay": "^0.1.6", "react": "18.2.0", "react-native": "0.74.5" }, diff --git a/posthog-react-native/CHANGELOG.md b/posthog-react-native/CHANGELOG.md index cf26682c..2ab7d465 100644 --- a/posthog-react-native/CHANGELOG.md +++ b/posthog-react-native/CHANGELOG.md @@ -1,5 +1,9 @@ # Next +# 3.3.12 - 2024-11-18 + +1. fix: session replay forces the session id if the SDK is already enabled + # 3.3.11 - 2024-11-13 1. fix: respect the given propsToCapture autocapture option diff --git a/posthog-react-native/package.json b/posthog-react-native/package.json index 1636d1ee..a980d0ba 100644 --- a/posthog-react-native/package.json +++ b/posthog-react-native/package.json @@ -1,6 +1,6 @@ { "name": "posthog-react-native", - "version": "3.3.11", + "version": "3.3.12", "main": "lib/posthog-react-native/index.js", "files": [ "lib/" diff --git a/posthog-react-native/src/posthog-rn.ts b/posthog-react-native/src/posthog-rn.ts index 40ec82ce..eaddfc26 100644 --- a/posthog-react-native/src/posthog-rn.ts +++ b/posthog-react-native/src/posthog-rn.ts @@ -216,6 +216,17 @@ export class PostHog extends PostHogCore { return !this.isDisabled && (this._enableSessionReplay ?? false) } + _resetSessionId( + reactNativeSessionReplay: typeof OptionalReactNativeSessionReplay | undefined, + sessionId: string + ): void { + // _resetSessionId is only called if reactNativeSessionReplay not undefined, but the linter wasn't happy + if (reactNativeSessionReplay) { + reactNativeSessionReplay.endSession() + reactNativeSessionReplay.startSession(sessionId) + } + } + getSessionId(): string { const sessionId = super.getSessionId() @@ -227,8 +238,7 @@ export class PostHog extends PostHogCore { if (sessionId.length > 0 && this._currentSessionId && sessionId !== this._currentSessionId) { if (OptionalReactNativeSessionReplay) { try { - OptionalReactNativeSessionReplay.endSession() - OptionalReactNativeSessionReplay.startSession(sessionId) + this._resetSessionId(OptionalReactNativeSessionReplay, sessionId) this.logMsgIfDebug(() => console.info('PostHog Debug', `Session replay started with sessionId ${sessionId}.`)) } catch (e) { this.logMsgIfDebug(() => @@ -237,6 +247,11 @@ export class PostHog extends PostHogCore { } } this._currentSessionId = sessionId + } else { + console.log( + 'PostHog Debug', + `Session replay session id not rotated, sessionId ${sessionId} and currentSessionId ${this._currentSessionId}.` + ) } return sessionId @@ -342,7 +357,11 @@ export class PostHog extends PostHogCore { console.info('PostHog Debug', `Session replay started with sessionId ${sessionId}.`) ) } else { - this.logMsgIfDebug(() => console.log('PostHog Debug', `Session replay already started.`)) + // if somehow the SDK is already enabled with a different sessionId, we reset it + this._resetSessionId(OptionalReactNativeSessionReplay, sessionId) + this.logMsgIfDebug(() => + console.log('PostHog Debug', `Session replay already started with sessionId ${sessionId}.`) + ) } } catch (e) { this.logMsgIfDebug(() => console.error('PostHog Debug', `Session replay failed to start: ${e}.`))