: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(surveys): show auto-close date for recurring surveys (#72955)
Phill0eb0aea2cfcb
diff --git a/frontend/src/scenes/surveys/SurveyOverview.tsx b/frontend/src/scenes/surveys/SurveyOverview.tsxindex a95bb06ba927..9ccee19d8183 100644--- a/frontend/src/scenes/surveys/SurveyOverview.tsx+++ b/frontend/src/scenes/surveys/SurveyOverview.tsx@@ -5,6 +5,7 @@ import { IconComment, IconGitBranch } from '@posthog/icons'import { LemonButton, LemonDivider, Link } from '@posthog/lemon-ui'import { TZLabel } from 'lib/components/TZLabel'+import { dayjs } from 'lib/dayjs'import { IconAreaChart, IconGridView, IconLink, IconListView } from 'lib/lemon-ui/icons'import { pluralize } from 'lib/utils/strings'import { FirstSurveyHelper } from 'scenes/surveys/components/empty-state/FirstSurveyHelper'@@ -14,6 +15,7 @@ import { SurveyDisplaySummary } from 'scenes/surveys/Survey'import { SurveyAPIEditor } from 'scenes/surveys/SurveyAPIEditor'import { SurveyFormAppearance } from 'scenes/surveys/SurveyFormAppearance'import { surveyLogic } from 'scenes/surveys/surveyLogic'+import { getRecurringSurveyScheduleInfo } from 'scenes/surveys/utils'import { SurveyQuestionType, SurveySchedule as SurveyScheduleEnum, SurveyType } from '~/types'@@ -22,11 +24,28 @@ import { SurveyBranchingFlowModal } from './branching-flow/SurveyBranchingFlowMofunction SurveySchedule(): JSX.Element {const { survey } = useValues(surveyLogic)if (survey.schedule === SurveyScheduleEnum.Recurring && survey.iteration_count && survey.iteration_frequency_days) {+ const scheduleInfo = getRecurringSurveyScheduleInfo(survey)return (- <span>- Repeats every {survey.iteration_frequency_days}{' '}- {pluralize(survey.iteration_frequency_days, 'day', 'days', false)}, {survey.iteration_count}{' '}- {pluralize(survey.iteration_count, 'time', 'times', false)}+ <span className="flex flex-col">+ <span>+ Repeats every {survey.iteration_frequency_days}{' '}+ {pluralize(survey.iteration_frequency_days, 'day', 'days', false)}, {survey.iteration_count}{' '}+ {pluralize(survey.iteration_count, 'time', 'times', false)}+ </span>+ {scheduleInfo &&+ (!scheduleInfo.autoCloseDate ? (+ <span className="text-xs text-muted">+ Runs for {pluralize(scheduleInfo.totalDurationDays, 'day')} once launched+ </span>+ ) : scheduleInfo.autoCloseDate.isBefore(dayjs()) ? (+ <span className="text-xs text-muted">+ Schedule ended {scheduleInfo.autoCloseDate.format('MMMM D, YYYY')}; closing soon+ </span>+ ) : (+ <span className="text-xs text-muted">+ Automatically closes on {scheduleInfo.autoCloseDate.format('MMMM D, YYYY')}+ </span>+ ))}</span>)}diff --git a/frontend/src/scenes/surveys/SurveyRepeatSchedule.tsx b/frontend/src/scenes/surveys/SurveyRepeatSchedule.tsxindex b62df71da165..99a5366854fe 100644--- a/frontend/src/scenes/surveys/SurveyRepeatSchedule.tsx+++ b/frontend/src/scenes/surveys/SurveyRepeatSchedule.tsx@@ -5,12 +5,13 @@ import { useActions, useValues } from 'kea'import { IconInfo } from '@posthog/icons'import { LemonBanner, LemonInput, LemonSnack, Link } from '@posthog/lemon-ui'+import { dayjs } from 'lib/dayjs'import { LemonField } from 'lib/lemon-ui/LemonField'import { LemonRadio } from 'lib/lemon-ui/LemonRadio'import { pluralize } from 'lib/utils/strings'import { LinkToSurveyFormSection } from 'scenes/surveys/components/LinkToSurveyFormSection'import { SURVEY_FORM_INPUT_IDS } from 'scenes/surveys/constants'-import { doesSurveyRepeatOnEveryEvent } from 'scenes/surveys/utils'+import { doesSurveyRepeatOnEveryEvent, getRecurringSurveyScheduleInfo } from 'scenes/surveys/utils'import { Survey, SurveySchedule, SurveyType } from '~/types'@@ -89,6 +90,45 @@ function AlwaysScheduleBanner({)}+function SurveyAutoCloseHelper({+ survey,+}: {+ survey: Pick<Survey, 'iteration_count' | 'iteration_frequency_days' | 'start_date' | 'end_date'>+}): JSX.Element | null {+ const scheduleInfo = getRecurringSurveyScheduleInfo(survey)++ if (!scheduleInfo) {+ return null+ }++ const { autoCloseDate, totalDurationDays } = scheduleInfo+ const hasCloseDatePassed = autoCloseDate?.isBefore(dayjs()) ?? false++ return (+ <LemonBanner type={hasCloseDatePassed ? 'warning' : 'info'} className="text-xs">+ {!autoCloseDate ? (+ <span>+ Once launched, this survey will run for{' '}+ <span className="font-semibold">{pluralize(totalDurationDays, 'day')}</span> before it automatically+ closes.+ </span>+ ) : hasCloseDatePassed ? (+ <span>+ This survey's schedule ended on{' '}+ <span className="font-semibold">{autoCloseDate.format('MMMM D, YYYY')}</span>, so it will close+ automatically soon.+ </span>+ ) : (+ <span>+ This survey will automatically close on{' '}+ <span className="font-semibold">{autoCloseDate.format('MMMM D, YYYY')}</span>, after its last+ repeat.+ </span>+ )}+ </LemonBanner>+ )+}+function SurveyIterationOptions(): JSX.Element {const { showSurveyRepeatSchedule, survey } = useValues(surveyLogic)const { setSurveyValue } = useActions(surveyLogic)@@ -179,6 +219,7 @@ function SurveyIterationOptions(): JSX.Element {: `This survey runs for ${survey.iteration_count} iterations of ${pluralize(survey.iteration_frequency_days, 'day')} each, counted from the launch date. Each user can respond once per iteration.`}</div>)}+ <SurveyAutoCloseHelper survey={survey} /></div>) : undefined,},diff --git a/frontend/src/scenes/surveys/SurveyViewRedesign/SurveySidebar.tsx b/frontend/src/scenes/surveys/SurveyViewRedesign/SurveySidebar.tsxindex 51c338f9013a..8e62f31c1748 100644--- a/frontend/src/scenes/surveys/SurveyViewRedesign/SurveySidebar.tsx+++ b/frontend/src/scenes/surveys/SurveyViewRedesign/SurveySidebar.tsx@@ -7,12 +7,17 @@ import { LemonButton, LemonMenu, LemonSelect, Link } from '@posthog/lemon-ui'import { exportsLogic } from 'lib/components/ExportButton/exportsLogic'import { TZLabel } from 'lib/components/TZLabel'+import { dayjs } from 'lib/dayjs'import { getAccessControlDisabledReason } from 'lib/utils/accessControlUtils'import { pluralize } from 'lib/utils/strings'import { SURVEY_TYPE_LABEL_MAP } from 'scenes/surveys/constants'import { SurveyAppearancePreview } from 'scenes/surveys/SurveyAppearancePreview'import { surveyLogic } from 'scenes/surveys/surveyLogic'-import { getSurveyCollectionLimitSummary, getSurveyDisplayConditionsSummary } from 'scenes/surveys/utils'+import {+ getRecurringSurveyScheduleInfo,+ getSurveyCollectionLimitSummary,+ getSurveyDisplayConditionsSummary,+} from 'scenes/surveys/utils'import {AccessControlLevel,@@ -77,6 +82,7 @@ export function SurveyDetailsPanel(): JSX.Element {const statusLabel = !survey.start_date ? 'Draft' : survey.end_date ? 'Complete' : 'Running'const conditionsSummary = hasTargetingSet ? getSurveyDisplayConditionsSummary(survey as Survey) : []const collectionLimitSummary = getSurveyCollectionLimitSummary(survey)+ const scheduleInfo = getRecurringSurveyScheduleInfo(survey)return (<div className="flex flex-col gap-6">@@ -138,6 +144,18 @@ export function SurveyDetailsPanel(): JSX.Element {<span className="text-muted">Schedule</span><span>{formatSurveySchedule(survey as Survey)}</span></div>+ {scheduleInfo && (+ <div className="flex justify-between">+ <span className="text-muted">Auto-closes</span>+ <span>+ {scheduleInfo.autoCloseDate+ ? scheduleInfo.autoCloseDate.isBefore(dayjs())+ ? 'Soon'+ : scheduleInfo.autoCloseDate.format('MMMM D, YYYY')+ : `After ${pluralize(scheduleInfo.totalDurationDays, 'day')}`}+ </span>+ </div>+ )}<div className="flex justify-between"><span className="text-muted">Audience</span><span>{hasTargetingSet ? 'Targeted' : 'All users'}</span>diff --git a/frontend/src/scenes/surveys/constants.tsx b/frontend/src/scenes/surveys/constants.tsxindex e08a55819d33..10bb5e3f3ee8 100644--- a/frontend/src/scenes/surveys/constants.tsx+++ b/frontend/src/scenes/surveys/constants.tsx@@ -23,6 +23,10 @@ export const SURVEY_PAGE_SIZE = 100export const LINK_PAGE_SIZE = 100+// Max recurring-survey iterations. Mirrors MAX_ITERATION_COUNT in products/surveys/backend/models.py,+// which caps the generated iteration windows (and the API enforces the same limit).+export const MAX_ITERATION_COUNT = 500+export const SurveyQuestionLabel: Record<SurveyQuestionType, string> = {[SurveyQuestionType.Open]: 'Freeform text',[SurveyQuestionType.Rating]: 'Rating',diff --git a/frontend/src/scenes/surveys/utils.test.ts b/frontend/src/scenes/surveys/utils.test.tsindex 42b336374294..b71732dc0ad7 100644--- a/frontend/src/scenes/surveys/utils.test.ts+++ b/frontend/src/scenes/surveys/utils.test.ts@@ -13,6 +13,7 @@ import {SurveyEventProperties,SurveyQuestion,SurveyQuestionType,+ SurveySchedule,SurveyType,SurveyWidgetType,} from '~/types'@@ -26,6 +27,7 @@ import {createAnswerFilterHogQLExpression,doesSurveyRepeatOnEveryEvent,getSurveyNotificationFilters,+ getRecurringSurveyScheduleInfo,getResolvedSurveyDateRange,getSurveyAudienceSummaryValue,getSurveyDisplayConditionsSummary,@@ -1465,3 +1467,110 @@ describe('doesSurveyRepeatOnEveryEvent', () => {expect(doesSurveyRepeatOnEveryEvent(survey)).toBe(expected)})})++describe('getRecurringSurveyScheduleInfo', () => {+ it('computes the total run duration as count * frequency days', () => {+ const info = getRecurringSurveyScheduleInfo({+ schedule: SurveySchedule.Recurring,+ iteration_count: 2,+ iteration_frequency_days: 30,+ start_date: null,+ end_date: null,+ })+ expect(info).not.toBeNull()+ expect(info?.totalDurationDays).toBe(60)+ expect(info?.autoCloseDate).toBeNull()+ })++ it('computes the auto-close date from the start date in UTC', () => {+ const info = getRecurringSurveyScheduleInfo({+ schedule: SurveySchedule.Recurring,+ iteration_count: 2,+ iteration_frequency_days: 30,+ // Time-of-day near a UTC midnight boundary must not shift the calendar day the backend uses+ start_date: '2026-01-01T01:00:00Z',+ end_date: null,+ })+ // 2 iterations of 30 days -> closes 60 days after launch+ expect(info?.autoCloseDate?.format('YYYY-MM-DD')).toBe('2026-03-02')+ })++ it('returns null once the survey has already ended', () => {+ const info = getRecurringSurveyScheduleInfo({+ schedule: SurveySchedule.Recurring,+ iteration_count: 2,+ iteration_frequency_days: 30,+ start_date: '2026-01-01T00:00:00Z',+ end_date: '2026-01-15T00:00:00Z',+ })+ expect(info).toBeNull()+ })++ it('returns null for a non-recurring survey even with leftover iteration fields', () => {+ const info = getRecurringSurveyScheduleInfo({+ schedule: SurveySchedule.Once,+ iteration_count: 2,+ iteration_frequency_days: 30,+ start_date: '2026-01-01T00:00:00Z',+ end_date: null,+ })+ expect(info).toBeNull()+ })++ it('clamps the run duration to the backend iteration cap', () => {+ const info = getRecurringSurveyScheduleInfo({+ schedule: SurveySchedule.Recurring,+ // Above MAX_ITERATION_COUNT (500) — the backend only generates 500 windows+ iteration_count: 1000,+ iteration_frequency_days: 30,+ start_date: null,+ end_date: null,+ })+ expect(info?.totalDurationDays).toBe(500 * 30)+ })++ it.each([+ [+ 'zero count',+ {+ schedule: SurveySchedule.Recurring,+ iteration_count: 0,+ iteration_frequency_days: 30,+ start_date: null,+ end_date: null,+ },+ ],+ [+ 'zero frequency',+ {+ schedule: SurveySchedule.Recurring,+ iteration_count: 2,+ iteration_frequency_days: 0,+ start_date: null,+ end_date: null,+ },+ ],+ [+ 'null count',+ {+ schedule: SurveySchedule.Recurring,+ iteration_count: null,+ iteration_frequency_days: 30,+ start_date: null,+ end_date: null,+ },+ ],+ [+ 'null frequency',+ {+ schedule: SurveySchedule.Recurring,+ iteration_count: 2,+ iteration_frequency_days: null,+ start_date: null,+ end_date: null,+ },+ ],+ ])('returns null for %s', (_name, survey) => {+ expect(getRecurringSurveyScheduleInfo(survey)).toBeNull()+ })+})diff --git a/frontend/src/scenes/surveys/utils.ts b/frontend/src/scenes/surveys/utils.tsindex 6302c7a9d187..cc31cf10e0d0 100644--- a/frontend/src/scenes/surveys/utils.ts+++ b/frontend/src/scenes/surveys/utils.ts@@ -5,7 +5,13 @@ import posthog from 'posthog-js'import { dayjs } from 'lib/dayjs'import { dateStringToDayJs } from 'lib/utils/dateFilters'import { getAppContext } from 'lib/utils/getAppContext'-import { NEW_SURVEY, NewSurvey, SURVEY_CREATED_SOURCE, SURVEY_RATING_SCALE } from 'scenes/surveys/constants'+import {+ MAX_ITERATION_COUNT,+ NEW_SURVEY,+ NewSurvey,+ SURVEY_CREATED_SOURCE,+ SURVEY_RATING_SCALE,+} from 'scenes/surveys/constants'import { SurveyRatingResults } from 'scenes/surveys/surveyLogic'import {@@ -551,6 +557,46 @@ export function doesSurveyRepeatOnEveryEvent(survey: Pick<Survey, 'conditions'>)return !!(survey.conditions?.events?.repeatedActivation && (survey.conditions?.events?.values?.length ?? 0) > 0)}+export interface RecurringSurveyScheduleInfo {+ /** Total number of days the survey runs from its launch date before auto-closing. */+ totalDurationDays: number+ /** The date the survey will automatically close, or null if it hasn't been launched yet. */+ autoCloseDate: dayjs.Dayjs | null+}++/**+ * A recurring survey ("Repeat on a schedule") auto-closes once its final iteration window has passed.+ * The last iteration starts on `start_date + (count - 1) * frequency` days and lasts `frequency` more days,+ * so the survey runs for `count * frequency` days total and closes at the end of that span.+ * Mirrors the backend logic in posthog/tasks/update_survey_iteration.py, which computes iteration windows+ * on the UTC calendar day — so we do the arithmetic in UTC too.+ *+ * Returns null once the survey has already ended: it then shows its real end date, so a projected one would+ * only contradict it.+ */+export function getRecurringSurveyScheduleInfo(+ survey: Pick<Survey, 'schedule' | 'iteration_count' | 'iteration_frequency_days' | 'start_date' | 'end_date'>+): RecurringSurveyScheduleInfo | null {+ const count = survey.iteration_count+ const frequency = survey.iteration_frequency_days+ if (+ survey.schedule !== SurveySchedule.Recurring ||+ survey.end_date ||+ !count ||+ !frequency ||+ count < 1 ||+ frequency < 1+ ) {+ return null+ }+ // The backend caps the generated iteration windows at MAX_ITERATION_COUNT, so anything above that never+ // extends the schedule — mirror the cap here to match the real close date.+ const effectiveCount = Math.min(count, MAX_ITERATION_COUNT)+ const totalDurationDays = effectiveCount * frequency+ const autoCloseDate = survey.start_date ? dayjs.utc(survey.start_date).add(totalDurationDays, 'day') : null+ return { totalDurationDays, autoCloseDate }+}+export function doesSurveyHaveDisplayConditions(survey: Survey | NewSurvey): boolean {const conditions = sanitizeSurveyDisplayConditions(survey.conditions)if (!conditions) {diff --git a/frontend/src/scenes/surveys/wizard/steps/SuccessStep.tsx b/frontend/src/scenes/surveys/wizard/steps/SuccessStep.tsxindex 575562591e8e..30bade4050c7 100644--- a/frontend/src/scenes/surveys/wizard/steps/SuccessStep.tsx+++ b/frontend/src/scenes/surveys/wizard/steps/SuccessStep.tsx@@ -3,7 +3,7 @@ import { useEffect, useState } from 'react'import { LemonButton } from '@posthog/lemon-ui'-import { doesSurveyRepeatOnEveryEvent } from 'scenes/surveys/utils'+import { doesSurveyRepeatOnEveryEvent, getRecurringSurveyScheduleInfo } from 'scenes/surveys/utils'import { urls } from 'scenes/urls'import { Survey, SurveySchedule, SurveyType } from '~/types'@@ -19,6 +19,7 @@ interface SuccessStepProps {export function SuccessStep({ survey }: SuccessStepProps): JSX.Element {const isHostedSurvey = survey.type === SurveyType.ExternalSurveyconst repeatsOnEveryEvent = doesSurveyRepeatOnEveryEvent(survey)+ const scheduleInfo = !repeatsOnEveryEvent ? getRecurringSurveyScheduleInfo(survey) : nullconst [countdown, setCountdown] = useState(5)useEffect(() => {@@ -97,6 +98,16 @@ export function SuccessStep({ survey }: SuccessStepProps): JSX.Element {: `Up to ${survey.iteration_count ?? 10} times, every ${survey.iteration_frequency_days} days from launch`}</dd></div>+ {scheduleInfo && (+ <div className="flex justify-between">+ <dt className="text-secondary">Automatically closes</dt>+ <dd>+ {scheduleInfo.autoCloseDate+ ? scheduleInfo.autoCloseDate.format('MMMM D, YYYY')+ : `After ${scheduleInfo.totalDurationDays} days`}+ </dd>+ </div>+ )}{survey.conditions?.seenSurveyWaitPeriodInDays != null && (<div className="flex justify-between"><dt className="text-secondary">Wait period across all surveys</dt>diff --git a/frontend/src/scenes/surveys/wizard/steps/WhenStep.tsx b/frontend/src/scenes/surveys/wizard/steps/WhenStep.tsxindex 236c50158cf6..1b59c8b23bd3 100644--- a/frontend/src/scenes/surveys/wizard/steps/WhenStep.tsx+++ b/frontend/src/scenes/surveys/wizard/steps/WhenStep.tsx@@ -7,6 +7,7 @@ import { PropertyFilters } from 'lib/components/PropertyFilters/PropertyFilters'import { TaxonomicFilterGroupType } from 'lib/components/TaxonomicFilter/types'import { LemonRadio } from 'lib/lemon-ui/LemonRadio'import { AddEventButton } from 'scenes/surveys/AddEventButton'+import { MAX_ITERATION_COUNT } from 'scenes/surveys/constants'import { doesSurveyRepeatOnEveryEvent } from 'scenes/surveys/utils'import {@@ -30,7 +31,6 @@ import { WizardPanel, WizardSection, WizardStepLayout } from '../WizardLayout'const DEFAULT_ITERATION_COUNT = 10const MIN_ITERATION_COUNT = 2-const MAX_ITERATION_COUNT = 500const FREQUENCY_OPTIONS: { value: string; days: number | undefined; label: string }[] = [{ value: 'once', days: undefined, label: 'Once ever' },