Skip to content

Commit

Permalink
chore: session id will be rotate on app restart (#271)
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto authored Sep 24, 2024
1 parent c020894 commit dc24906
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 1 deletion.
1 change: 1 addition & 0 deletions posthog-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,7 @@ export abstract class PostHogCore extends PostHogCoreStateless {
resetSessionId(): void {
this.wrap(() => {
this.setPersistedProperty(PostHogPersistedProperty.SessionId, null)
this.setPersistedProperty(PostHogPersistedProperty.SessionLastTimestamp, null)
})
}

Expand Down
17 changes: 17 additions & 0 deletions posthog-react-native/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
# Next

# 3.3.0 - 2024-09-24

## Changed

1. chore: session id will be rotate on app restart.
1. To keep the session id across restarts, set the `enablePersistSessionIdAcrossRestart` option to `true` when initializing the PostHog client.

```js
export const posthog = new PostHog(
'apiKey...',
{
// ...
enablePersistSessionIdAcrossRestart: true,
},
);
```

# 3.2.1 - 2024-09-24

## Changed
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.2.1",
"version": "3.3.0",
"main": "lib/posthog-react-native/index.js",
"files": [
"lib/"
Expand Down
15 changes: 15 additions & 0 deletions posthog-react-native/src/posthog-rn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ export type PostHogOptions = PostHogCoreOptions & {
* Experimental support
*/
sessionReplayConfig?: PostHogSessionReplayConfig

/**
* If enabled, the session id ($session_id) will be persisted across app restarts.
* This is an option for back compatibility, so your current data isn't skewed with the new version of the SDK.
* If this is false, the session id will be always reset on app restart.
* Defaults to false
*/
enablePersistSessionIdAcrossRestart?: boolean
}

export class PostHog extends PostHogCore {
Expand Down Expand Up @@ -106,6 +114,13 @@ export class PostHog extends PostHogCore {
}

const initAfterStorage = (): void => {
// reset session id on app restart
const enablePersistSessionIdAcrossRestart = options?.enablePersistSessionIdAcrossRestart
if (!enablePersistSessionIdAcrossRestart) {
this.setPersistedProperty(PostHogPersistedProperty.SessionId, null)
this.setPersistedProperty(PostHogPersistedProperty.SessionLastTimestamp, null)
}

this.setupBootstrap(options)

this._isInitialized = true
Expand Down
15 changes: 15 additions & 0 deletions posthog-react-native/test/posthog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,5 +401,20 @@ describe('PostHog React Native', () => {
expect(storage.getItem).toHaveBeenCalledTimes(2)
expect(posthog.getFeatureFlag('flag')).toEqual(true)
})

it('do not rotate session id on restart', async () => {
const sessionId = '0192244d-a627-7ae2-b22a-ccd594bed71d'
storage.setItem(PostHogPersistedProperty.SessionId, sessionId)
const now = JSON.stringify(Date.now())
storage.setItem(PostHogPersistedProperty.SessionLastTimestamp, now)

posthog = new PostHog('1', {
customStorage: storage,
enablePersistSessionIdAcrossRestart: true,
})

expect(storage.getItem(PostHogPersistedProperty.SessionId)).toEqual(sessionId)
expect(storage.getItem(PostHogPersistedProperty.SessionLastTimestamp)).toEqual(now)
})
})
})

0 comments on commit dc24906

Please sign in to comment.