Skip to content

Commit

Permalink
move seenSurvey_ to a constant and reuse it
Browse files Browse the repository at this point in the history
  • Loading branch information
Phanatic committed Oct 23, 2024
1 parent 47bd7ae commit b5324d1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
18 changes: 15 additions & 3 deletions src/extensions/surveys/surveys-utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;',
Expand Down Expand Up @@ -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) {
Expand Down
9 changes: 2 additions & 7 deletions src/posthog-surveys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]'

Expand Down Expand Up @@ -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))
}

Expand Down

0 comments on commit b5324d1

Please sign in to comment.