From ec6bc533931f3094347c20c7f1dce61e20c4dad1 Mon Sep 17 00:00:00 2001 From: Robbie Coomber Date: Tue, 12 Nov 2024 15:08:58 +0000 Subject: [PATCH] Don't use session storage in memory mode --- src/__tests__/posthog-persistence.test.ts | 44 +++++++++++++++++++++++ src/posthog-core.ts | 4 +-- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/__tests__/posthog-persistence.test.ts b/src/__tests__/posthog-persistence.test.ts index 3860ad30d..086246e24 100644 --- a/src/__tests__/posthog-persistence.test.ts +++ b/src/__tests__/posthog-persistence.test.ts @@ -3,6 +3,9 @@ import { PostHogPersistence } from '../posthog-persistence' import { SESSION_ID, USER_STATE } from '../constants' import { PostHogConfig } from '../types' import Mock = jest.Mock +import { PostHog } from '../posthog-core' +import { window } from '../utils/globals' +import { uuidv7 } from '../uuidv7' let referrer = '' // No referrer by default Object.defineProperty(document, 'referrer', { get: () => referrer }) @@ -203,4 +206,45 @@ describe('persistence', () => { expect(lib.properties()).toEqual(expectedProps()) }) }) + + describe('posthog', () => { + it('should not store anything in localstorage, or cookies when in sessionStorage mode', () => { + const token = uuidv7() + const persistenceKey = `ph_${token}_posthog` + const posthog = new PostHog().init(token, { + persistence: 'sessionStorage', + }) + posthog.register({ distinct_id: 'test', test_prop: 'test_val' }) + posthog.capture('test_event') + expect(window.localStorage.getItem(persistenceKey)).toEqual(null) + expect(document.cookie).toEqual('') + expect(window.sessionStorage.getItem(persistenceKey)).toBeTruthy() + }) + + it('should not store anything in localstorage, sessionstorage, or cookies when in memory mode', () => { + const token = uuidv7() + const persistenceKey = `ph_${token}_posthog` + const posthog = new PostHog().init(token, { + persistence: 'memory', + }) + posthog.register({ distinct_id: 'test', test_prop: 'test_val' }) + posthog.capture('test_event') + expect(window.localStorage.getItem(persistenceKey)).toEqual(null) + expect(window.sessionStorage.getItem(persistenceKey)).toEqual(null) + expect(document.cookie).toEqual('') + }) + + it('should not store anything in cookies when in localstorage mode', () => { + const token = uuidv7() + const persistenceKey = `ph_${token}_posthog` + const posthog = new PostHog().init(token, { + persistence: 'localStorage', + }) + posthog.register({ distinct_id: 'test', test_prop: 'test_val' }) + posthog.capture('test_event') + expect(window.localStorage.getItem(persistenceKey)).toBeTruthy() + expect(window.sessionStorage.getItem(persistenceKey)).toBeTruthy() + expect(document.cookie).toEqual('') + }) + }) }) diff --git a/src/posthog-core.ts b/src/posthog-core.ts index 6e2bc3e7c..457a41159 100644 --- a/src/posthog-core.ts +++ b/src/posthog-core.ts @@ -412,7 +412,7 @@ export class PostHog { this.persistence = new PostHogPersistence(this.config) this.sessionPersistence = - this.config.persistence === 'sessionStorage' + this.config.persistence === 'sessionStorage' || this.config.persistence === 'memory' ? this.persistence : new PostHogPersistence({ ...this.config, persistence: 'sessionStorage' }) @@ -1786,7 +1786,7 @@ export class PostHog { this.persistence?.update_config(this.config, oldConfig) this.sessionPersistence = - this.config.persistence === 'sessionStorage' + this.config.persistence === 'sessionStorage' || this.config.persistence === 'memory' ? this.persistence : new PostHogPersistence({ ...this.config, persistence: 'sessionStorage' })