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: session replay forces the session id if the SDK is already enabled #307

Merged
merged 6 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
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
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