Skip to content

Commit 50e8d67

Browse files
committed
ensure localStorage is included on persistence, otherwise log warning
1 parent aa1e83b commit 50e8d67

File tree

7 files changed

+25
-11
lines changed

7 files changed

+25
-11
lines changed

packages/browser/src/__tests__/extensions/surveys-persistence.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ describe('Surveys Persistence Migration - Simple Test', () => {
1616

1717
const config = {
1818
token: 'test-token',
19-
persistence: 'memory',
19+
persistence: 'localStorage+cookie',
2020
api_host: 'https://app.posthog.com',
2121
} as PostHogConfig
2222

packages/browser/src/__tests__/posthog-surveys.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ describe('posthog-surveys', () => {
9090
disable_surveys: false,
9191
token: 'test-token',
9292
surveys_request_timeout_ms: SURVEYS_REQUEST_TIMEOUT_MS,
93+
persistence: 'localStorage+cookie',
9394
},
9495
persistence: {
9596
register: jest.fn(),

packages/browser/src/__tests__/surveys.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ describe('surveys', () => {
189189
config = {
190190
token: 'testtoken',
191191
api_host: 'https://app.posthog.com',
192-
persistence: 'memory',
192+
persistence: 'localStorage+cookie',
193193
surveys_request_timeout_ms: SURVEYS_REQUEST_TIMEOUT_MS,
194194
} as unknown as PostHogConfig
195195

packages/browser/src/extensions/surveys.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ export class SurveyManager {
420420

421421
private _internalFlagCheckSatisfied(survey: Survey): boolean {
422422
return (
423-
canActivateRepeatedly(survey) ||
423+
canActivateRepeatedly(survey, this._posthog) ||
424424
this._isSurveyFeatureFlagEnabled(survey.internal_targeting_flag_key) ||
425425
isSurveyInProgress(survey, this._posthog)
426426
)

packages/browser/src/extensions/surveys/surveys-extension-utils.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -521,12 +521,13 @@ export const hasEvents = (survey: Pick<Survey, 'conditions'>): boolean => {
521521
}
522522

523523
export const canActivateRepeatedly = (
524-
survey: Pick<Survey, 'schedule' | 'conditions' | 'id' | 'current_iteration'>
524+
survey: Pick<Survey, 'schedule' | 'conditions' | 'id' | 'current_iteration'>,
525+
posthog?: PostHog
525526
): boolean => {
526527
return (
527528
!!(survey.conditions?.events?.repeatedActivation && hasEvents(survey)) ||
528529
survey.schedule === SurveySchedule.Always ||
529-
isSurveyInProgress(survey)
530+
isSurveyInProgress(survey, posthog)
530531
)
531532
}
532533

@@ -541,7 +542,7 @@ export const getSurveySeen = (survey: Survey, posthog?: PostHog): boolean => {
541542
if (surveySeen) {
542543
// if a survey has already been seen,
543544
// we will override it with the event repeated activation value.
544-
return !canActivateRepeatedly(survey)
545+
return !canActivateRepeatedly(survey, posthog)
545546
}
546547

547548
return false

packages/browser/src/posthog-surveys.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
doesSurveyActivateByAction,
1717
doesSurveyActivateByEvent,
1818
IN_APP_SURVEY_TYPES,
19+
isPersistenceEnabledWithLocalStorage,
1920
isSurveyRunning,
2021
LAST_SEEN_SURVEY_DATE_KEY,
2122
SURVEY_LOGGER as logger,
@@ -86,6 +87,11 @@ export class PostHogSurveys {
8687
logger.info('Not loading surveys in cookieless mode without consent.')
8788
return
8889
}
90+
if (isPersistenceEnabledWithLocalStorage(this._instance)) {
91+
logger.warn(
92+
'Persistence does not include localStorage, but surveys it to work properly. Please set persistence to include localStorage to avoid this warning, or set disable_surveys to true. Falling back to localStorage usage directly to maintain backwards compatibility.'
93+
)
94+
}
8995

9096
const phExtensions = assignableWindow?.__PosthogExtensions__
9197
if (!phExtensions) {

packages/browser/src/utils/survey-utils.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,14 @@ export const getSurveySeenKey = (survey: Pick<Survey, 'id' | 'current_iteration'
4242
return surveySeenKey
4343
}
4444

45-
function isPersistenceEnabled(posthog?: PostHog): posthog is PostHog & { persistence: PostHogPersistence } {
46-
return !!(posthog?.persistence && !posthog.persistence.isDisabled())
45+
export function isPersistenceEnabledWithLocalStorage(
46+
posthog?: PostHog
47+
): posthog is PostHog & { persistence: PostHogPersistence } {
48+
return !!(
49+
posthog?.persistence &&
50+
!posthog.persistence.isDisabled() &&
51+
posthog.config?.persistence?.includes('localStorage')
52+
)
4753
}
4854

4955
/**
@@ -52,7 +58,7 @@ function isPersistenceEnabled(posthog?: PostHog): posthog is PostHog & { persist
5258
* to maintain backwards compatibility.
5359
*/
5460
export const getFromPersistenceWithLocalStorageFallback = (key: string, posthog?: PostHog) => {
55-
if (!isPersistenceEnabled(posthog)) {
61+
if (!isPersistenceEnabledWithLocalStorage(posthog)) {
5662
try {
5763
return localStorage.getItem(key)
5864
} catch (e) {
@@ -75,7 +81,7 @@ export const getFromPersistenceWithLocalStorageFallback = (key: string, posthog?
7581
* to maintain backwards compatibility.
7682
*/
7783
export const setOnPersistenceWithLocalStorageFallback = (key: string, value: any, posthog?: PostHog) => {
78-
if (!isPersistenceEnabled(posthog)) {
84+
if (!isPersistenceEnabledWithLocalStorage(posthog)) {
7985
try {
8086
localStorage.setItem(key, value)
8187
} catch (e) {
@@ -96,7 +102,7 @@ export const setOnPersistenceWithLocalStorageFallback = (key: string, value: any
96102
* When removing a property from persistence, remove from localStorage for backwards compatibility.
97103
*/
98104
export const clearFromPersistenceWithLocalStorageFallback = (key: string, posthog?: PostHog) => {
99-
if (!isPersistenceEnabled(posthog)) {
105+
if (!isPersistenceEnabledWithLocalStorage(posthog)) {
100106
try {
101107
localStorage.removeItem(key)
102108
} catch (e) {

0 commit comments

Comments
 (0)