Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(surveys): switch guided form to iteration-based scheduling (#58514)

Lucas Fariad2f2a1cdda0b
diff --git a/frontend/src/scenes/surveys/wizard/steps/SuccessStep.tsx b/frontend/src/scenes/surveys/wizard/steps/SuccessStep.tsx
index 835de32ff965..94f749d34db2 100644
--- a/frontend/src/scenes/surveys/wizard/steps/SuccessStep.tsx
+++ b/frontend/src/scenes/surveys/wizard/steps/SuccessStep.tsx
@@ -5,7 +5,7 @@ import { LemonButton } from '@posthog/lemon-ui'
import { urls } from 'scenes/urls'
-import { Survey } from '~/types'
+import { Survey, SurveySchedule } from '~/types'
import { SurveyAppearancePreview } from '../../SurveyAppearancePreview'
@@ -64,11 +64,17 @@ export function SuccessStep({ survey }: SuccessStepProps): JSX.Element {
<div className="flex justify-between">
<dt className="text-secondary">Frequency</dt>
<dd>
- {survey.conditions?.seenSurveyWaitPeriodInDays == null
+ {survey.schedule === SurveySchedule.Once || !survey.iteration_frequency_days
? 'Once ever'
- : `Every ${survey.conditions?.seenSurveyWaitPeriodInDays} days`}
+ : `Up to ${survey.iteration_count ?? 10} times, every ${survey.iteration_frequency_days} days`}
</dd>
</div>
+ {survey.conditions?.seenSurveyWaitPeriodInDays != null && (
+ <div className="flex justify-between">
+ <dt className="text-secondary">Wait period across all surveys</dt>
+ <dd>{survey.conditions.seenSurveyWaitPeriodInDays} days</dd>
+ </div>
+ )}
</dl>
</div>
</div>
diff --git a/frontend/src/scenes/surveys/wizard/steps/WhenStep.tsx b/frontend/src/scenes/surveys/wizard/steps/WhenStep.tsx
index bc7cafe0f9a5..578b87e6e8ee 100644
--- a/frontend/src/scenes/surveys/wizard/steps/WhenStep.tsx
+++ b/frontend/src/scenes/surveys/wizard/steps/WhenStep.tsx
@@ -1,7 +1,14 @@
import { useActions, useValues } from 'kea'
import { IconX } from '@posthog/icons'
-import { LemonButton, LemonCheckbox, LemonInput, LemonSegmentedButton } from '@posthog/lemon-ui'
+import {
+ LemonButton,
+ LemonCheckbox,
+ LemonCollapse,
+ LemonInput,
+ LemonSegmentedButton,
+ LemonSwitch,
+} from '@posthog/lemon-ui'
import { PropertyFilters } from 'lib/components/PropertyFilters/PropertyFilters'
import { TaxonomicFilterGroupType } from 'lib/components/TaxonomicFilter/types'
@@ -27,6 +34,10 @@ import { surveyLogic } from '../../surveyLogic'
import { surveyWizardLogic } from '../surveyWizardLogic'
import { WizardPanel, WizardSection, WizardStepLayout } from '../WizardLayout'
+const DEFAULT_ITERATION_COUNT = 10
+const MIN_ITERATION_COUNT = 2
+const MAX_ITERATION_COUNT = 500
+
const FREQUENCY_OPTIONS: { value: string; days: number | undefined; label: string }[] = [
{ value: 'once', days: undefined, label: 'Once ever' },
{ value: 'yearly', days: 365, label: 'Every year' },
@@ -51,7 +62,15 @@ export function WhenStep(): JSX.Element {
const option = FREQUENCY_OPTIONS.find((opt) => opt.days === days)
return option?.value || 'monthly'
}
- const frequency = daysToFrequency(conditions.seenSurveyWaitPeriodInDays)
+ // Prefer the new iteration-based model. Fall back to the legacy wait-period for older guided-form surveys.
+ const frequency =
+ survey.schedule === SurveySchedule.Once
+ ? 'once'
+ : survey.iteration_frequency_days
+ ? daysToFrequency(survey.iteration_frequency_days)
+ : daysToFrequency(conditions.seenSurveyWaitPeriodInDays)
+ const iterationCount = survey.iteration_count ?? DEFAULT_ITERATION_COUNT
+ const seenSurveyWaitPeriodInDays = conditions.seenSurveyWaitPeriodInDays ?? null
const setTriggerMode = (mode: 'pageview' | 'event'): void => {
if (mode === 'pageview') {
@@ -68,9 +87,42 @@ export function WhenStep(): JSX.Element {
const setFrequency = (value: string): void => {
const option = FREQUENCY_OPTIONS.find((opt) => opt.value === value)
- const isOnce = value === 'once'
- setSurveyValue('schedule', isOnce ? SurveySchedule.Once : SurveySchedule.Always)
- setSurveyValue('conditions', { ...conditions, seenSurveyWaitPeriodInDays: option?.days })
+ if (value === 'once') {
+ setSurveyValue('schedule', SurveySchedule.Once)
+ setSurveyValue('iteration_count', 0)
+ setSurveyValue('iteration_frequency_days', 0)
+ return
+ }
+ setSurveyValue('schedule', SurveySchedule.Recurring)
+ setSurveyValue(
+ 'iteration_count',
+ survey.iteration_count && survey.iteration_count >= MIN_ITERATION_COUNT
+ ? survey.iteration_count
+ : DEFAULT_ITERATION_COUNT
+ )
+ setSurveyValue('iteration_frequency_days', option?.days)
+ }
+
+ const setIterationCount = (value: number | undefined): void => {
+ // Don't clamp to min on every keystroke — typing "10" briefly passes through 1, and aggressive
+ // clamping prevents the second digit from being appended. The blur handler enforces the floor.
+ if (value === undefined) {
+ return
+ }
+ setSurveyValue('iteration_count', Math.min(value, MAX_ITERATION_COUNT))
+ }
+
+ const commitIterationCount = (): void => {
+ if (!survey.iteration_count || survey.iteration_count < MIN_ITERATION_COUNT) {
+ setSurveyValue('iteration_count', MIN_ITERATION_COUNT)
+ }
+ }
+
+ const setSeenSurveyWaitPeriod = (value: number | null): void => {
+ setSurveyValue('conditions', {
+ ...conditions,
+ seenSurveyWaitPeriodInDays: value && value > 0 ? value : null,
+ })
}
const setRepeatedActivation = (enabled: boolean): void => {
@@ -213,11 +265,7 @@ export function WhenStep(): JSX.Element {
)}
</WizardSection>
- <WizardSection
- title="How often can the same person see this?"
- description="Control how frequently the same person can be shown this survey again."
- descriptionClassName="text-sm"
- >
+ <WizardSection title="How often should this survey trigger?">
<LemonSegmentedButton
value={frequency}
onChange={setFrequency}
@@ -232,8 +280,65 @@ export function WhenStep(): JSX.Element {
{recommendedFrequency.value === frequency && (
<p className="text-sm text-success mt-3">{recommendedFrequency.reason}</p>
)}
+
+ {frequency !== 'once' && (
+ <div className="flex items-center gap-2 mt-4 text-sm">
+ <span>Show up to</span>
+ <LemonInput
+ type="number"
+ min={MIN_ITERATION_COUNT}
+ max={MAX_ITERATION_COUNT}
+ value={iterationCount}
+ onChange={(val) => setIterationCount(val ?? undefined)}
+ onBlur={commitIterationCount}
+ className="w-20"
+ />
+ <span className="text-secondary">times total</span>
+ <span className="text-muted text-xs">
+ min {MIN_ITERATION_COUNT}, max {MAX_ITERATION_COUNT}
+ </span>
+ </div>
+ )}
</WizardSection>
+ <LemonCollapse
+ embedded
+ defaultActiveKey={seenSurveyWaitPeriodInDays != null ? 'wait-period' : undefined}
+ panels={[
+ {
+ key: 'wait-period',
+ header: (
+ <span>
+ Wait period across all surveys{' '}
+ <span className="text-muted font-normal">
+ {seenSurveyWaitPeriodInDays != null
+ ? `(${seenSurveyWaitPeriodInDays} days)`
+ : '(optional)'}
+ </span>
+ </span>
+ ),
+ content: (
+ <div className="flex items-center gap-2 text-sm py-1">
+ <LemonSwitch
+ checked={seenSurveyWaitPeriodInDays != null}
+ onChange={(checked) => setSeenSurveyWaitPeriod(checked ? 30 : null)}
+ size="small"
+ />
+ <span>Hide if any survey was seen in the last</span>
+ <LemonInput
+ type="number"
+ min={1}
+ value={seenSurveyWaitPeriodInDays ?? NaN}
+ onChange={(val) => setSeenSurveyWaitPeriod(val ?? null)}
+ className="w-20"
+ />
+ <span className="text-secondary">days</span>
+ </div>
+ ),
+ },
+ ]}
+ />
+
<section className="space-y-2 border-t border-border pt-5">
<label className="text-sm font-medium">Delay before showing</label>
<div className="flex items-center gap-2">
diff --git a/frontend/src/scenes/surveys/wizard/surveyWizardLogic.ts b/frontend/src/scenes/surveys/wizard/surveyWizardLogic.ts
index b781bb0efb66..218269e15cfd 100644
--- a/frontend/src/scenes/surveys/wizard/surveyWizardLogic.ts
+++ b/frontend/src/scenes/surveys/wizard/surveyWizardLogic.ts
@@ -278,11 +278,11 @@ export const surveyWizardLogic = kea<surveyWizardLogicType>([
monthly: 30,
}
const { value: frequencyValue } = values.recommendedFrequency
- actions.setSurveyValue('schedule', frequencyValue === 'once' ? SurveySchedule.Once : SurveySchedule.Always)
- actions.setSurveyValue('conditions', {
- ...template.conditions,
- seenSurveyWaitPeriodInDays: frequencyToDays[frequencyValue],
- })
+ const isOnce = frequencyValue === 'once'
+ actions.setSurveyValue('schedule', isOnce ? SurveySchedule.Once : SurveySchedule.Recurring)
+ actions.setSurveyValue('iteration_count', isOnce ? 0 : 10)
+ actions.setSurveyValue('iteration_frequency_days', isOnce ? 0 : frequencyToDays[frequencyValue])
+ actions.setSurveyValue('conditions', template.conditions ?? null)
actions.reportSurveyTemplateClicked(template.templateType, SURVEY_CREATED_SOURCE.SURVEY_WIZARD)
},