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): add question branching to hosted survey editor (#72383)

posthog[bot]384e9cee4797
diff --git a/frontend/src/scenes/surveys/HostedSurveyEdit.tsx b/frontend/src/scenes/surveys/HostedSurveyEdit.tsx
index a10e509600a3..4c94ed803604 100644
--- a/frontend/src/scenes/surveys/HostedSurveyEdit.tsx
+++ b/frontend/src/scenes/surveys/HostedSurveyEdit.tsx
@@ -50,13 +50,6 @@ function getHostedSurveyUrl(surveyId: string): string {
return url.toString()
}
-function moveQuestion(questions: SurveyQuestion[], from: number, to: number): SurveyQuestion[] {
- const nextQuestions = [...questions]
- const [question] = nextQuestions.splice(from, 1)
- nextQuestions.splice(to, 0, question)
- return nextQuestions.map((q) => ({ ...q }))
-}
-
function HostedSurveyQuestionRail({
id,
survey,
@@ -407,6 +400,8 @@ export function HostedSurveyEdit({ id }: { id: string }): JSX.Element {
deleteBranchingLogic,
editingSurvey,
loadSurvey,
+ moveQuestion,
+ removeQuestion,
setSelectedPageIndex,
setSurveyManualErrors,
setSurveyValue,
@@ -440,29 +435,6 @@ export function HostedSurveyEdit({ id }: { id: string }): JSX.Element {
setSelectedPageIndex(Math.max(survey.questions.length - 1, 0))
}
- const runAfterBranchingConfirmation = (action: () => void, description: JSX.Element): void => {
- if (!hasBranchingLogic) {
- action()
- return
- }
-
- LemonDialog.open({
- title: 'Your survey has active branching logic',
- description,
- primaryButton: {
- children: 'Continue',
- status: 'danger',
- onClick: () => {
- deleteBranchingLogic()
- action()
- },
- },
- secondaryButton: {
- children: 'Cancel',
- },
- })
- }
-
const addQuestion = (type: SurveyQuestionType): void => {
const newQuestion = { ...defaultSurveyFieldValues[type].questions[0] } as SurveyQuestion
const existingLanguages = Object.keys(survey.translations || {})
@@ -486,26 +458,17 @@ export function HostedSurveyEdit({ id }: { id: string }): JSX.Element {
}
const moveSurveyQuestion = (from: number, to: number): void => {
- runAfterBranchingConfirmation(
- () => {
- setSurveyValue('questions', moveQuestion(survey.questions, from, to))
- setSelectedPageIndex(to)
- },
- <p className="py-2">Rearranging questions will remove your branching logic. Continue?</p>
- )
+ // Reorder through surveyLogic's moveQuestion so branching targets are remapped
+ // to the new question indices instead of being wiped, matching the standard editor.
+ moveQuestion(from, to)
+ setSelectedPageIndex(to)
}
const deleteSurveyQuestion = (index: number): void => {
- runAfterBranchingConfirmation(
- () => {
- setSurveyValue(
- 'questions',
- survey.questions.filter((_, questionIndex) => questionIndex !== index)
- )
- setSelectedPageIndex(Math.max(index - 1, 0))
- },
- <p className="py-2">Deleting this question will remove your branching logic. Continue?</p>
- )
+ // Delete through surveyLogic's removeQuestion so branching targets are remapped
+ // instead of being wiped, matching the standard editor.
+ setSelectedPageIndex(Math.max(index - 1, 0))
+ removeQuestion(index)
}
const handleCancelClick = (): void => {
diff --git a/frontend/src/scenes/surveys/hosted-canvas/HostedSurveySettingsPanel.tsx b/frontend/src/scenes/surveys/hosted-canvas/HostedSurveySettingsPanel.tsx
index 31fcf94df3e5..ee214ab6cc0c 100644
--- a/frontend/src/scenes/surveys/hosted-canvas/HostedSurveySettingsPanel.tsx
+++ b/frontend/src/scenes/surveys/hosted-canvas/HostedSurveySettingsPanel.tsx
@@ -1,9 +1,11 @@
import { useActions, useValues } from 'kea'
+import { Group } from 'kea-forms'
import { IconPhone, IconPlusSmall, IconTrash } from '@posthog/icons'
import { LemonButton, LemonCheckbox, LemonDialog, LemonSegmentedButton, LemonSelect } from '@posthog/lemon-ui'
import { IconMonitor } from 'lib/lemon-ui/icons'
+import { QuestionBranchingInput } from 'scenes/surveys/components/question-branching/QuestionBranchingInput'
import { SCALE_OPTIONS, SURVEY_RATING_SCALE, SurveyQuestionLabel } from 'scenes/surveys/constants'
import { surveyLogic } from 'scenes/surveys/surveyLogic'
import { canQuestionSkipSubmitButton, isThumbQuestion } from 'scenes/surveys/utils'
@@ -199,6 +201,19 @@ export function HostedSurveySettingsPanel({
onChange={(checked) => updateQuestionField('skipSubmitButton', checked)}
/>
)}
+
+ {/* Keep this block inside the `!isConfirmation && question` guard: the Group
+ binds to `questions.${activePageIndex}`, which is only a valid question
+ index for real questions, never the confirmation page. */}
+ <div className="border-t pt-3">
+ <SettingsSection
+ title="Branching"
+ subtitle="Send people to a different question based on their answer."
+ />
+ <Group name={`questions.${activePageIndex}`}>
+ <QuestionBranchingInput questionIndex={activePageIndex} question={question} />
+ </Group>
+ </div>
</>
) : null}