diff --git a/src/extensions/surveys/surveys-utils.tsx b/src/extensions/surveys/surveys-utils.tsx index 1a764d1d7..230651ab1 100644 --- a/src/extensions/surveys/surveys-utils.tsx +++ b/src/extensions/surveys/surveys-utils.tsx @@ -5,7 +5,7 @@ import { VNode, cloneElement, createContext } from 'preact' // We cast the types here which is dangerous but protected by the top level generateSurveys call const window = _window as Window & typeof globalThis const document = _document as Document - +const SurveySeenPrefix = 'seenSurvey_' export const style = (appearance: SurveyAppearance | null) => { const positions = { left: 'left: 30px;', @@ -648,14 +648,26 @@ export const getSurveySeen = (survey: Survey): boolean => { } export const getSurveySeenKey = (survey: Survey): string => { - let surveySeenKey = `seenSurvey_${survey.id}` + let surveySeenKey = `${SurveySeenPrefix}${survey.id}` if (survey.current_iteration && survey.current_iteration > 0) { - surveySeenKey = `seenSurvey_${survey.id}_${survey.current_iteration}` + surveySeenKey = `${SurveySeenPrefix}${survey.id}_${survey.current_iteration}` } return surveySeenKey } +export const getSurveySeenStorageKeys = (): string[] => { + const surveyKeys = [] + for (let i = 0; i < localStorage.length; i++) { + const key = localStorage.key(i) + if (key?.startsWith(SurveySeenPrefix)) { + surveyKeys.push(key) + } + } + + return surveyKeys +} + const getSurveyInteractionProperty = (survey: Survey, action: string): string => { let surveyProperty = `$survey_${action}/${survey.id}` if (survey.current_iteration && survey.current_iteration > 0) { diff --git a/src/posthog-surveys.ts b/src/posthog-surveys.ts index 9aca70fe3..f7c90952b 100644 --- a/src/posthog-surveys.ts +++ b/src/posthog-surveys.ts @@ -13,6 +13,7 @@ import { assignableWindow, document, window } from './utils/globals' import { DecideResponse } from './types' import { logger } from './utils/logger' import { isNullish } from './utils/type-utils' +import { getSurveySeenStorageKeys } from './extensions/surveys/surveys-utils' const LOGGER_PREFIX = '[Surveys]' @@ -75,13 +76,7 @@ export class PostHogSurveys { reset(): void { localStorage.removeItem('lastSeenSurveyDate') - const surveyKeys = [] - for (let i = 0; i < localStorage.length; i++) { - const key = localStorage.key(i) - if (key?.startsWith('seenSurvey_')) { - surveyKeys.push(key) - } - } + const surveyKeys = getSurveySeenStorageKeys() surveyKeys.forEach((key) => localStorage.removeItem(key)) }