: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): add auto-submit toggle to hosted survey editor (#71770)
Ben Leab870c22dc192
diff --git a/frontend/src/scenes/surveys/SurveyEditQuestionRow.tsx b/frontend/src/scenes/surveys/SurveyEditQuestionRow.tsxindex e75a719f93cf..591ebea39b5c 100644--- a/frontend/src/scenes/surveys/SurveyEditQuestionRow.tsx+++ b/frontend/src/scenes/surveys/SurveyEditQuestionRow.tsx@@ -27,7 +27,7 @@ import { NewSurvey, SCALE_OPTIONS, SURVEY_RATING_SCALE, SurveyQuestionLabel } frimport { HTMLEditor } from './SurveyAppearanceUtils'import { SurveyDragHandle } from './SurveyDragHandle'import { surveyLogic } from './surveyLogic'-import { isThumbQuestion, splitChoicesOnPaste } from './utils'+import { canQuestionSkipSubmitButton, isThumbQuestion, splitChoicesOnPaste } from './utils'type SurveyQuestionHeaderProps = {index: number@@ -114,15 +114,6 @@ export function SurveyEditQuestionHeader({)}-function canQuestionSkipSubmitButton(- question: SurveyQuestion-): question is RatingSurveyQuestion | MultipleSurveyQuestion {- return (- question.type === SurveyQuestionType.Rating ||- (question.type === SurveyQuestionType.SingleChoice && !question.hasOpenChoice)- )-}-export function SurveyEditQuestionGroup({ index, question }: { index: number; question: SurveyQuestion }): JSX.Element {const { survey, descriptionContentType, editingLanguage, translationErrorsForField, aiGeneratedTranslationFields } =useValues(surveyLogic)diff --git a/frontend/src/scenes/surveys/hosted-canvas/HostedSurveyCanvas.tsx b/frontend/src/scenes/surveys/hosted-canvas/HostedSurveyCanvas.tsxindex e2389755beb6..4712d06d2f63 100644--- a/frontend/src/scenes/surveys/hosted-canvas/HostedSurveyCanvas.tsx+++ b/frontend/src/scenes/surveys/hosted-canvas/HostedSurveyCanvas.tsx@@ -11,7 +11,7 @@ import { IconPlus, IconTrash } from '@posthog/icons'import { SortableDragIcon } from 'lib/lemon-ui/icons'import { defaultSurveyAppearance } from 'scenes/surveys/constants'import { surveyLogic } from 'scenes/surveys/surveyLogic'-import { sanitizeHTML } from 'scenes/surveys/utils'+import { canQuestionSkipSubmitButton, sanitizeHTML } from 'scenes/surveys/utils'import {type MultipleSurveyQuestion,@@ -286,6 +286,8 @@ function QuestionCanvas({ question, index }: { question: SurveyQuestion; index:}const buttonTextDefault = question.type === SurveyQuestionType.Link ? 'Continue' : 'Submit'+ // Auto-submit hides the submit button at render time, so the preview should match.+ const hidesSubmitButton = canQuestionSkipSubmitButton(question) && !!question.skipSubmitButtonreturn (<form className="survey-form" name="surveyForm" onSubmit={(event) => event.preventDefault()}>@@ -357,15 +359,17 @@ function QuestionCanvas({ question, index }: { question: SurveyQuestion; index:) : null}<div className="bottom-section">- <InlineEditable- as="span"- value={question.buttonText || ''}- onChange={(value) => updateField('buttonText', value)}- placeholder={buttonTextDefault}- ariaLabel="Submit button label"- className="form-submit"- data-attr={`canvas-question-${index}-button-text`}- />+ {!hidesSubmitButton && (+ <InlineEditable+ as="span"+ value={question.buttonText || ''}+ onChange={(value) => updateField('buttonText', value)}+ placeholder={buttonTextDefault}+ ariaLabel="Submit button label"+ className="form-submit"+ data-attr={`canvas-question-${index}-button-text`}+ />+ )}<KeyboardHints questionType={question.type} /></div></div>diff --git a/frontend/src/scenes/surveys/hosted-canvas/HostedSurveySettingsPanel.tsx b/frontend/src/scenes/surveys/hosted-canvas/HostedSurveySettingsPanel.tsxindex d9dd25c3f128..31fcf94df3e5 100644--- a/frontend/src/scenes/surveys/hosted-canvas/HostedSurveySettingsPanel.tsx+++ b/frontend/src/scenes/surveys/hosted-canvas/HostedSurveySettingsPanel.tsx@@ -6,7 +6,7 @@ import { LemonButton, LemonCheckbox, LemonDialog, LemonSegmentedButton, LemonSelimport { IconMonitor } from 'lib/lemon-ui/icons'import { SCALE_OPTIONS, SURVEY_RATING_SCALE, SurveyQuestionLabel } from 'scenes/surveys/constants'import { surveyLogic } from 'scenes/surveys/surveyLogic'-import { isThumbQuestion } from 'scenes/surveys/utils'+import { canQuestionSkipSubmitButton, isThumbQuestion } from 'scenes/surveys/utils'import {type MultipleSurveyQuestion,@@ -190,6 +190,15 @@ export function HostedSurveySettingsPanel({}}/>)}++ {canQuestionSkipSubmitButton(question) && (+ <LemonCheckbox+ label="Automatically submit on selection"+ info="If enabled, the survey submits immediately after the user makes a selection (for rating or single-choice questions without an open-ended option), and the submit button is hidden. Requires at least version 1.244.0 of posthog-js. Not available for the mobile SDKs at the moment."+ checked={!!question.skipSubmitButton}+ onChange={(checked) => updateQuestionField('skipSubmitButton', checked)}+ />+ )}</>) : null}diff --git a/frontend/src/scenes/surveys/utils.ts b/frontend/src/scenes/surveys/utils.tsindex b907e7ffb586..6302c7a9d187 100644--- a/frontend/src/scenes/surveys/utils.ts+++ b/frontend/src/scenes/surveys/utils.ts@@ -509,6 +509,17 @@ export function isSurveyRunning(survey: Pick<Survey, 'start_date' | 'end_date'>)return !!(survey.start_date && !survey.end_date)}+// Auto-submit only makes sense for questions where a single selection is a complete+// answer: any rating, or a single-choice question without a free-text "open" option.+export function canQuestionSkipSubmitButton(+ question: SurveyQuestion+): question is RatingSurveyQuestion | MultipleSurveyQuestion {+ return (+ question.type === SurveyQuestionType.Rating ||+ (question.type === SurveyQuestionType.SingleChoice && !question.hasOpenChoice)+ )+}+// Some fields can only be edited in the full editor — opening such a survey// in the wizard would hide those values from the user, so we route them to// the full editor regardless of their general editor preference. Keep this@@ -788,6 +799,11 @@ export function sanitizeSurvey(survey: Partial<Survey>, options?: SanitizeSurvey) {sanitized.choices = sanitized.choices.map((choice) => choice.trim())}+ // Drop a stale auto-submit flag if the question is no longer eligible for it+ // (e.g. an open-ended choice was added, or the type was switched).+ if ('skipSubmitButton' in sanitized && !canQuestionSkipSubmitButton(sanitized)) {+ delete (sanitized as { skipSubmitButton?: boolean }).skipSubmitButton+ }return sanitized}) || []