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): order the SDK survey payload by launch date (#90486)

Lucas Faria6e3bc7bfac50
diff --git a/products/surveys/backend/api/survey.py b/products/surveys/backend/api/survey.py
index 8327fcaccd87..0e0ac033d970 100644
--- a/products/surveys/backend/api/survey.py
+++ b/products/surveys/backend/api/survey.py
@@ -3534,7 +3534,11 @@ def get_surveys_response(team: Team) -> dict[str, Any]:
# external_survey case in their type enums at all.
.exclude(type=Survey.SurveyType.EXTERNAL_SURVEY)
.select_related("linked_flag", "targeting_flag", "internal_targeting_flag")
- .prefetch_related("actions"),
+ .prefetch_related("actions")
+ # SDKs display one popover at a time and break appearance-delay ties by payload
+ # order, so this ordering decides which of two colliding surveys a user sees.
+ # Launch order (oldest first) keeps that winner deterministic across cache rebuilds.
+ .order_by("start_date", "created_at", "id"),
many=True,
).data
diff --git a/products/surveys/backend/api/test/test_survey.py b/products/surveys/backend/api/test/test_survey.py
index 8b66a5c4a310..c1a3bb1ebc3a 100644
--- a/products/surveys/backend/api/test/test_survey.py
+++ b/products/surveys/backend/api/test/test_survey.py
@@ -487,6 +487,50 @@ class TestSurvey(APIBaseTest):
assert (str(survey.id) in payload_ids) is expected_in_payload
+ def test_sdk_payload_orders_surveys_by_launch_date(self) -> None:
+ # SDKs break display ties by payload order, so it must be deterministic:
+ # oldest launch first, then created_at, then id.
+ def create_survey(name: str, start_date: datetime, created_at: datetime) -> Survey:
+ survey = Survey.objects.create(
+ team=self.team,
+ name=name,
+ type="popover",
+ start_date=start_date,
+ questions=[{"type": "open", "id": "q1", "question": "How are you?"}],
+ )
+ Survey.objects.filter(id=survey.id).update(created_at=created_at)
+ return survey
+
+ launched_last = create_survey(
+ "Launched last", datetime(2026, 1, 3, tzinfo=UTC), datetime(2026, 1, 1, tzinfo=UTC)
+ )
+ launched_first = create_survey(
+ "Launched first", datetime(2026, 1, 1, tzinfo=UTC), datetime(2026, 1, 2, tzinfo=UTC)
+ )
+ tie_created_second = create_survey(
+ "Tie created second", datetime(2026, 1, 2, tzinfo=UTC), datetime(2026, 1, 2, tzinfo=UTC)
+ )
+ tie_created_first = create_survey(
+ "Tie created first", datetime(2026, 1, 2, tzinfo=UTC), datetime(2026, 1, 1, tzinfo=UTC)
+ )
+ full_tie_a = create_survey("Full tie a", datetime(2026, 1, 2, tzinfo=UTC), datetime(2026, 1, 3, tzinfo=UTC))
+ full_tie_b = create_survey("Full tie b", datetime(2026, 1, 2, tzinfo=UTC), datetime(2026, 1, 3, tzinfo=UTC))
+ id_tie_first, id_tie_second = sorted([full_tie_a, full_tie_b], key=lambda survey: str(survey.id))
+
+ payload_ids = [str(item["id"]) for item in get_surveys_response(self.team)["surveys"]]
+
+ assert payload_ids == [
+ str(survey.id)
+ for survey in [
+ launched_first,
+ tie_created_first,
+ tie_created_second,
+ id_tie_first,
+ id_tie_second,
+ launched_last,
+ ]
+ ]
+
def test_sdk_payload_strips_non_runtime_question_fields(self) -> None:
self.team.survey_config = {"appearance": {"backgroundColor": "black"}}
self.team.save(update_fields=["survey_config"])