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.

fix(surveys): stop reporting a cycle for fully routed branching (#95506)

Ben Lea04c5a7aeea0d
diff --git a/frontend/src/scenes/surveys/surveyLogic.test.ts b/frontend/src/scenes/surveys/surveyLogic.test.ts
index 6f22f96c14a6..56dcb89d454b 100644
--- a/frontend/src/scenes/surveys/surveyLogic.test.ts
+++ b/frontend/src/scenes/surveys/surveyLogic.test.ts
@@ -1269,6 +1269,126 @@ describe('set response-based survey branching', () => {
.toMatchValues({
hasCycle: false,
})
+
+ // A price ladder that steps back to the cheaper question on a no, with every response routed.
+ SURVEY.questions = [
+ {
+ type: SurveyQuestionType.SingleChoice,
+ choices: ['Yes', 'No'],
+ question: 'at $9 per month',
+ description: '',
+ branching: {
+ type: SurveyQuestionBranchingType.ResponseBased,
+ responseValues: { 0: SurveyQuestionBranchingType.End, 1: 2 },
+ },
+ },
+ {
+ type: SurveyQuestionType.Rating,
+ question: 'how does that price feel',
+ description: '',
+ display: 'number',
+ scale: 5,
+ lowerBoundLabel: 'Too expensive',
+ upperBoundLabel: 'Good value',
+ branching: {
+ type: SurveyQuestionBranchingType.ResponseBased,
+ responseValues: {
+ negative: SurveyQuestionBranchingType.End,
+ neutral: SurveyQuestionBranchingType.End,
+ positive: SurveyQuestionBranchingType.End,
+ },
+ },
+ },
+ {
+ type: SurveyQuestionType.SingleChoice,
+ choices: ['Yes', 'No'],
+ question: 'at $19 per month',
+ description: '',
+ branching: {
+ type: SurveyQuestionBranchingType.ResponseBased,
+ responseValues: { 0: SurveyQuestionBranchingType.End, 1: 1 },
+ },
+ },
+ ]
+ await expectLogic(logic, () => {
+ // A fresh object, because hasCycle memoizes on the survey it is given.
+ logic.actions.loadSurveySuccess({ ...SURVEY })
+ })
+ .toDispatchActions(['loadSurveySuccess'])
+ .toMatchValues({
+ hasCycle: false,
+ })
+
+ // The rule left by a deleted choice routes nobody, so its step back is unreachable.
+ SURVEY.questions = [
+ {
+ type: SurveyQuestionType.SingleChoice,
+ choices: ['Yes', 'No'],
+ question: '0',
+ description: '',
+ },
+ {
+ type: SurveyQuestionType.SingleChoice,
+ choices: ['Yes', 'No'],
+ question: '1',
+ description: '',
+ branching: {
+ type: SurveyQuestionBranchingType.ResponseBased,
+ responseValues: { 0: 2, 1: 2, 2: 0 },
+ },
+ },
+ {
+ type: SurveyQuestionType.SingleChoice,
+ choices: ['Yes', 'No'],
+ question: '2',
+ description: '',
+ },
+ ]
+ await expectLogic(logic, () => {
+ logic.actions.loadSurveySuccess({ ...SURVEY })
+ })
+ .toDispatchActions(['loadSurveySuccess'])
+ .toMatchValues({
+ hasCycle: false,
+ })
+
+ // The first question is optional, so a skip falls through into the second, which steps back.
+ SURVEY.questions = [
+ {
+ type: SurveyQuestionType.SingleChoice,
+ choices: ['Yes', 'No'],
+ question: '0',
+ description: '',
+ optional: true,
+ branching: {
+ type: SurveyQuestionBranchingType.ResponseBased,
+ responseValues: { 0: 2, 1: 2 },
+ },
+ },
+ {
+ type: SurveyQuestionType.SingleChoice,
+ choices: ['Yes', 'No'],
+ question: '1',
+ description: '',
+ branching: {
+ type: SurveyQuestionBranchingType.SpecificQuestion,
+ index: 0,
+ },
+ },
+ {
+ type: SurveyQuestionType.SingleChoice,
+ choices: ['Yes', 'No'],
+ question: '2',
+ description: '',
+ },
+ ]
+ await expectLogic(logic, () => {
+ logic.actions.loadSurveySuccess({ ...SURVEY })
+ })
+ .toDispatchActions(['loadSurveySuccess'])
+ .toMatchValues({
+ hasCycle: true,
+ })
})
})
})
diff --git a/frontend/src/scenes/surveys/surveyLogic.tsx b/frontend/src/scenes/surveys/surveyLogic.tsx
index 5a9183840704..314c9d53b1de 100644
--- a/frontend/src/scenes/surveys/surveyLogic.tsx
+++ b/frontend/src/scenes/surveys/surveyLogic.tsx
@@ -3245,11 +3245,45 @@ export const surveyLogic = kea<surveyLogicType>([
question.branching?.type === SurveyQuestionBranchingType.ResponseBased &&
isObject(question.branching?.responseValues)
) {
- for (const [_, toIndex] of Object.entries(question.branching?.responseValues)) {
+ // The responses the SDK can select; a key outside this list routes nobody.
+ let responses: (string | number)[] = []
+ if (question.type === SurveyQuestionType.SingleChoice) {
+ responses = question.choices.map((_, choiceIndex) => choiceIndex)
+ } else if (isRatingSurveyQuestion(question)) {
+ // The SDK keys rating responses by bucket. An unknown scale keeps every key.
+ if (question.scale === 2) {
+ responses = ['positive', 'negative']
+ } else if (question.scale === 10) {
+ responses = ['detractors', 'passives', 'promoters']
+ } else if ([3, 5, 7].includes(question.scale)) {
+ responses = ['negative', 'neutral', 'positive']
+ }
+ }
+
+ const { responseValues } = question.branching
+ const destinations =
+ responses.length > 0
+ ? responses.map((response) => responseValues[String(response)])
+ : Object.values(responseValues)
+ for (const toIndex of destinations) {
if (Number.isInteger(toIndex)) {
graph.get(fromIndex).add(toIndex)
}
}
+
+ // The SDK falls through to the next question only when the selected response
+ // has no destination, so a question that routes every response never gets
+ // there. An optional question is the exception, because a skip routes nowhere.
+ if (
+ !question.optional &&
+ responses.length > 0 &&
+ responses.every((response) => {
+ const destination = responseValues[String(response)]
+ return Number.isInteger(destination) || destination === SurveyQuestionBranchingType.End
+ })
+ ) {
+ return
+ }
}
// No branching - still need to connect the next question