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 shipping draft and stopped surveys to SDKs (#78535)

Lucas Faria956906c2e1df
diff --git a/posthog/models/test/test_remote_config.py b/posthog/models/test/test_remote_config.py
index 8183969bbbc8..584bb46a5eb3 100644
--- a/posthog/models/test/test_remote_config.py
+++ b/posthog/models/test/test_remote_config.py
@@ -1,3 +1,4 @@
+from datetime import timedelta
from decimal import Decimal
from typing import Any
@@ -320,6 +321,27 @@ class TestRemoteConfigSurveys(_RemoteConfigBase):
self.sync_remote_config()
assert "survey_config" not in self.remote_config.config
+ def test_surveys_disabled_when_only_draft_and_stopped_surveys_exist(self):
+ Survey.objects.create(
+ team=self.team,
+ created_by=self.user,
+ name="Draft survey",
+ type="popover",
+ questions=[{"type": "open", "question": "What's a survey?"}],
+ )
+ Survey.objects.create(
+ team=self.team,
+ created_by=self.user,
+ name="Stopped survey",
+ type="popover",
+ questions=[{"type": "open", "question": "What's a hedgehog?"}],
+ start_date=timezone.now() - timedelta(days=2),
+ end_date=timezone.now() - timedelta(days=1),
+ )
+
+ self.sync_remote_config()
+ assert self.remote_config.config["surveys"] is False
+
def test_includes_range_of_survey_types(self):
survey_basic = Survey.objects.create(
team=self.team,
diff --git a/products/surveys/backend/api/survey.py b/products/surveys/backend/api/survey.py
index 715f65921b3c..765b85eabdf2 100644
--- a/products/surveys/backend/api/survey.py
+++ b/products/surveys/backend/api/survey.py
@@ -3470,10 +3470,15 @@ def get_surveys_count(team: Team) -> int:
def get_surveys_response(team: Team) -> dict[str, Any]:
+ # Every SDK discards surveys that aren't running before evaluating eligibility, so drafts and
+ # stopped surveys are payload every client downloads and throws away. The nullness check mirrors
+ # the SDKs' own `isSurveyRunning`; keeping it time-independent matters because this response is
+ # cached and only rebuilt when a survey is saved, so a comparison against `now` would go stale.
surveys = SurveyAPISerializer(
Survey.objects.db_manager(READ_DB_FOR_SURVEYS)
.filter(team__project_id=team.project_id)
.exclude(archived=True)
+ .filter(start_date__isnull=False, end_date__isnull=True)
.select_related("linked_flag", "targeting_flag", "internal_targeting_flag")
.prefetch_related("actions"),
many=True,
diff --git a/products/surveys/backend/api/test/test_survey.py b/products/surveys/backend/api/test/test_survey.py
index 51c1ac81399a..93242c12b6f8 100644
--- a/products/surveys/backend/api/test/test_survey.py
+++ b/products/surveys/backend/api/test/test_survey.py
@@ -457,6 +457,33 @@ class TestSurvey(APIBaseTest):
assert response.status_code == status.HTTP_400_BAD_REQUEST, response.content
assert "base_language" in response.json() or "translation" in response.content.decode().lower()
+ @parameterized.expand(
+ [
+ ("running", {"start_date": datetime(2026, 1, 1, tzinfo=UTC)}, True),
+ ("draft", {}, False),
+ (
+ "stopped",
+ {"start_date": datetime(2026, 1, 1, tzinfo=UTC), "end_date": datetime(2026, 2, 1, tzinfo=UTC)},
+ False,
+ ),
+ ("archived", {"start_date": datetime(2026, 1, 1, tzinfo=UTC), "archived": True}, False),
+ ]
+ )
+ def test_sdk_payload_only_includes_running_surveys(
+ self, _name: str, lifecycle_fields: dict[str, Any], expected_in_payload: bool
+ ) -> None:
+ survey = Survey.objects.create(
+ team=self.team,
+ name="Lifecycle survey",
+ type="popover",
+ questions=[{"type": "open", "id": "q1", "question": "How are you?"}],
+ **lifecycle_fields,
+ )
+
+ payload_ids = {str(item["id"]) for item in get_surveys_response(self.team)["surveys"]}
+
+ assert (str(survey.id) in payload_ids) is expected_in_payload
+
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"])
@@ -465,6 +492,7 @@ class TestSurvey(APIBaseTest):
name="Q-level legacy",
type="popover",
base_language="en",
+ start_date=datetime(2026, 1, 1, tzinfo=UTC),
questions=[
{
"id": "q1",