Skip to content

Commit

Permalink
fix: session replay forces the session id if the SDK is already enabl…
Browse files Browse the repository at this point in the history
…ed (#307)
  • Loading branch information
marandaneto authored Nov 18, 2024
1 parent 1571cfc commit be654a8
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
2 changes: 1 addition & 1 deletion examples/example-expo-latest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
4 changes: 4 additions & 0 deletions posthog-react-native/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion posthog-react-native/package.json
Original file line number Diff line number Diff line change
@@ -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/"
Expand Down
25 changes: 22 additions & 3 deletions posthog-react-native/src/posthog-rn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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(() =>
Expand All @@ -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
Expand Down Expand Up @@ -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}.`))
Expand Down

0 comments on commit be654a8

Please sign in to comment.