Skip to content

Surveys: an optional rating question with response-based branching throws "The response type must be an integer" when skipped #5278

Closed

Description

@AndZp

Bug description

A popover survey whose optional rating question uses response-based branching cannot be skipped. With no rating chosen, Submit stays enabled. Clicking it throws inside the click handler, and the survey neither advances nor sends a response:

Error: The response type must be an integer

The respondent sees nothing happen. They either pick a rating and submit again, or close the survey, and the response is lost.

Versions

posthog-js 1.428.3 (surveys.js from the assets CDN). Still identical in 1.438.7 and on main at 4f590b3.

Steps to reproduce

  1. Create a popover survey with an optional rating question that branches on its answer, for example:
    {
      "type": "rating", "display": "number", "scale": 5, "optional": true,
      "question": "How clear was it?",
      "branching": { "type": "response_based", "responseValues": { "negative": 1, "neutral": 1, "positive": 2 } }
    }
    followed by two more questions.
  2. Show it, e.g. posthog.displaySurvey(id, { displayType: "popover", ignoreConditions: true }).
  3. Click Submit without choosing a rating.

Expected: the survey moves on. An optional question was skipped, so the default next question applies, as it already does for an optional single-choice question with response-based branching.
Actual: Error: The response type must be an integer is thrown from the click handler, and the survey stays on the question.

Cause

  • RatingQuestion: submitDisabled={isNull(rating) && !question.optional}, then onSubmit(rating). An optional rating submits null.
  • getNextSurveyStep: for a response_based rating it throws on any non-integer response, null included. The submit handler calls it before advancing, so the throw aborts the step.

Suggested fix

In getNextSurveyStep, treat an empty response (null/undefined) to an optional question as "no branch matched" and return the default next index instead of throwing. Keep the throw for genuinely malformed values. Alternatively, the survey editor could refuse to save the combination (optional + response-based branching on a rating question), but the SDK fix also covers surveys that already exist.

A minimal standalone reproduction (posthog-js only, array.full.js + displaySurvey against stubbed /api/surveys/) is available on request.

Activity

  1. github-actions commented on Oct 11, 2026

    @github-actions
    Contributor

    AI triage for #5278

    Conclusion: Confirmed browser-survey bug: an optional, response-branched rating submits null, but the branching helper rejects it before navigation.
    Confidence: 98%
    Suggested labels: feature/surveys, web, team/client-libraries, bug

    Thanks for the precise reproduction and source references. This is confirmed in the current source: optional rating questions intentionally enable Submit with no selected value and submit null, while response-based rating branching currently throws for that value. The intended behavior is to treat an empty optional response as unmatched and continue to the default next question, while retaining validation for non-empty malformed rating values.

    Investigation

    • RatingQuestion enables its submit button when question.optional is true and its unselected state is rating === null; clicking Submit calls onSubmit(rating) with that null value.
    • onNextButtonClick records the response and then calls getNextSurveyStep before it can update the current question or emit a survey event. Therefore the thrown branching error aborts the reported submission flow.
    • For response-based rating branching, getNextSurveyStep throws The response type must be an integer unless the response is an integer. It does not currently distinguish an intentionally skipped optional rating from malformed input.
    • Response-based single-choice questions already fall through to nextQuestionIndex when no response value matches, including an unselected optional response; this supports the requested default-navigation behavior.
    • Existing branching tests cover valid rating buckets and assert that non-numeric strings still throw, but do not cover a null response for an optional rating question.
    • recordSurveyAnswer deliberately records null responses, so the failure is navigation/branching-specific rather than an inability to represent a skipped answer.

    Relevant files: packages/browser/src/extensions/surveys/components/QuestionTypes.tsx, packages/browser/src/utils/survey-branching.ts, packages/browser/src/extensions/surveys.tsx, packages/browser/src/__tests__/surveys.test.ts, packages/browser/src/__tests__/extensions/surveys/question-types.test.tsx, packages/core/src/surveys/events.ts, packages/browser-common/src/types/surveys.ts

    Fix assessment

    • Straightforward: yes
    • Risk: low
    • Reason: The defect is isolated to one validation path and does not require a public API or survey-schema change. The existing component behavior establishes that null is the intentional skipped value for optional ratings.
    • Suggested approach: In getNextSurveyStep, before rating integer validation, return nextQuestionIndex when a response-based rating question is optional and its response is null (and, if runtime callers may pass it, undefined). Add a focused regression test using an optional response-based rating followed by another question, asserting that a null response returns the default next index. Keep existing throws for non-empty invalid values such as strings and out-of-range integers.

    Draft PR: #5279

    Pi session

    The resumable JSONL pi session for this run was saved to a private gist: https://gist.github.com/marandaneto/4ad9502e6a20857d8b2e090d559ca518

    Download it locally, then fork it into your own pi session:

    pi --fork path/to/session.jsonl

    Saved session:

    Close proposal

    None. No issue close action was taken.


    Generated by posthog-watcher-action using pi.

  2. added a commit that references this issue on Oct 11, 2026
    f8f4b42
  3. self-assigned this
    on Oct 12, 2026
  4. dustinbyrne commented on Oct 12, 2026

    @dustinbyrne
    Contributor

    Thanks for the issue @AndZp! This will be fixed with #5279

  5. added a commit that references this issue on Oct 23, 2026
    4e65098
  6. added a commit that references this issue on Oct 27, 2026
    d47c184
  7. added a commit that references this issue on Nov 2, 2026
    ec4e707
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions