From 90c64a5a82026310264de55a4e8eb0c19aaaf0d5 Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Fri, 15 Nov 2024 15:35:58 +0100 Subject: [PATCH 1/6] fix: session replay forces the session id if the SDK is already enabled --- examples/example-expo-latest/package.json | 2 +- posthog-react-native/CHANGELOG.md | 4 ++++ posthog-react-native/src/posthog-rn.ts | 19 ++++++++++++++++--- 3 files changed, 21 insertions(+), 4 deletions(-) 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/src/posthog-rn.ts b/posthog-react-native/src/posthog-rn.ts index 40ec82ce..eee23bf4 100644 --- a/posthog-react-native/src/posthog-rn.ts +++ b/posthog-react-native/src/posthog-rn.ts @@ -216,6 +216,11 @@ export class PostHog extends PostHogCore { return !this.isDisabled && (this._enableSessionReplay ?? false) } + _resetSessionId(sessionId: string): void { + OptionalReactNativeSessionReplay.endSession() + OptionalReactNativeSessionReplay.startSession(sessionId) + } + getSessionId(): string { const sessionId = super.getSessionId() @@ -227,8 +232,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(sessionId) this.logMsgIfDebug(() => console.info('PostHog Debug', `Session replay started with sessionId ${sessionId}.`)) } catch (e) { this.logMsgIfDebug(() => @@ -237,6 +241,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 +351,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(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}.`)) From 881c2136fc911a0ddfdd3742dfc7910e944292a3 Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Fri, 15 Nov 2024 15:36:44 +0100 Subject: [PATCH 2/6] versiob --- posthog-react-native/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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/" From b07ac2f13e4e0c4899070f48c8a4cf4624dfbea4 Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Fri, 15 Nov 2024 16:14:35 +0100 Subject: [PATCH 3/6] fix lint --- posthog-react-native/CHANGELOG.md | 2 ++ posthog-react-native/src/posthog-rn.ts | 10 +++++----- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/posthog-react-native/CHANGELOG.md b/posthog-react-native/CHANGELOG.md index 2ab7d465..3d45265a 100644 --- a/posthog-react-native/CHANGELOG.md +++ b/posthog-react-native/CHANGELOG.md @@ -1,5 +1,7 @@ # Next +# 3.3.13 - 2024-11-18 + # 3.3.12 - 2024-11-18 1. fix: session replay forces the session id if the SDK is already enabled diff --git a/posthog-react-native/src/posthog-rn.ts b/posthog-react-native/src/posthog-rn.ts index eee23bf4..9e7bd59d 100644 --- a/posthog-react-native/src/posthog-rn.ts +++ b/posthog-react-native/src/posthog-rn.ts @@ -216,9 +216,9 @@ export class PostHog extends PostHogCore { return !this.isDisabled && (this._enableSessionReplay ?? false) } - _resetSessionId(sessionId: string): void { - OptionalReactNativeSessionReplay.endSession() - OptionalReactNativeSessionReplay.startSession(sessionId) + _resetSessionId(reactNativeSessionReplay: OptionalReactNativeSessionReplay, sessionId: string): void { + reactNativeSessionReplay.endSession() + reactNativeSessionReplay.startSession(sessionId) } getSessionId(): string { @@ -232,7 +232,7 @@ export class PostHog extends PostHogCore { if (sessionId.length > 0 && this._currentSessionId && sessionId !== this._currentSessionId) { if (OptionalReactNativeSessionReplay) { try { - this._resetSessionId(sessionId) + this._resetSessionId(OptionalReactNativeSessionReplay, sessionId) this.logMsgIfDebug(() => console.info('PostHog Debug', `Session replay started with sessionId ${sessionId}.`)) } catch (e) { this.logMsgIfDebug(() => @@ -352,7 +352,7 @@ export class PostHog extends PostHogCore { ) } else { // if somehow the SDK is already enabled with a different sessionId, we reset it - this._resetSessionId(sessionId) + this._resetSessionId(OptionalReactNativeSessionReplay, sessionId) this.logMsgIfDebug(() => console.log('PostHog Debug', `Session replay already started with sessionId ${sessionId}.`) ) From b534e5d67bf37dad0c9fba0005e743696f3c9852 Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Mon, 18 Nov 2024 08:45:58 +0100 Subject: [PATCH 4/6] fix --- posthog-react-native/CHANGELOG.md | 2 -- posthog-react-native/src/posthog-rn.ts | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/posthog-react-native/CHANGELOG.md b/posthog-react-native/CHANGELOG.md index 3d45265a..2ab7d465 100644 --- a/posthog-react-native/CHANGELOG.md +++ b/posthog-react-native/CHANGELOG.md @@ -1,7 +1,5 @@ # Next -# 3.3.13 - 2024-11-18 - # 3.3.12 - 2024-11-18 1. fix: session replay forces the session id if the SDK is already enabled diff --git a/posthog-react-native/src/posthog-rn.ts b/posthog-react-native/src/posthog-rn.ts index 9e7bd59d..a13094f3 100644 --- a/posthog-react-native/src/posthog-rn.ts +++ b/posthog-react-native/src/posthog-rn.ts @@ -216,7 +216,7 @@ export class PostHog extends PostHogCore { return !this.isDisabled && (this._enableSessionReplay ?? false) } - _resetSessionId(reactNativeSessionReplay: OptionalReactNativeSessionReplay, sessionId: string): void { + _resetSessionId(reactNativeSessionReplay: typeof OptionalReactNativeSessionReplay, sessionId: string): void { reactNativeSessionReplay.endSession() reactNativeSessionReplay.startSession(sessionId) } From 8be7261f339e87320e0d99da8aaacc0f9b0e97c2 Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Mon, 18 Nov 2024 08:49:32 +0100 Subject: [PATCH 5/6] fix --- posthog-react-native/src/posthog-rn.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/posthog-react-native/src/posthog-rn.ts b/posthog-react-native/src/posthog-rn.ts index a13094f3..dcad32ab 100644 --- a/posthog-react-native/src/posthog-rn.ts +++ b/posthog-react-native/src/posthog-rn.ts @@ -217,8 +217,8 @@ export class PostHog extends PostHogCore { } _resetSessionId(reactNativeSessionReplay: typeof OptionalReactNativeSessionReplay, sessionId: string): void { - reactNativeSessionReplay.endSession() - reactNativeSessionReplay.startSession(sessionId) + reactNativeSessionReplay?.endSession() + reactNativeSessionReplay?.startSession(sessionId) } getSessionId(): string { From 84b883e1f7a1926179a2291a12da898b4b3e1957 Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Mon, 18 Nov 2024 08:53:04 +0100 Subject: [PATCH 6/6] fix --- posthog-react-native/src/posthog-rn.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/posthog-react-native/src/posthog-rn.ts b/posthog-react-native/src/posthog-rn.ts index dcad32ab..eaddfc26 100644 --- a/posthog-react-native/src/posthog-rn.ts +++ b/posthog-react-native/src/posthog-rn.ts @@ -216,9 +216,15 @@ export class PostHog extends PostHogCore { return !this.isDisabled && (this._enableSessionReplay ?? false) } - _resetSessionId(reactNativeSessionReplay: typeof OptionalReactNativeSessionReplay, sessionId: string): void { - reactNativeSessionReplay?.endSession() - reactNativeSessionReplay?.startSession(sessionId) + _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 {