: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(surveys): log a system activity entry when a recurring survey auto-closes (#72091)
Phill1c526755ea9e
diff --git a/posthog/tasks/stop_surveys_reached_target.py b/posthog/tasks/stop_surveys_reached_target.pyindex 0731e41e04ca..a0d12666c0da 100644--- a/posthog/tasks/stop_surveys_reached_target.py+++ b/posthog/tasks/stop_surveys_reached_target.py@@ -11,6 +11,7 @@ from posthog.hogql.query import execute_hogql_queryfrom posthog.clickhouse.client.connection import Workloadfrom posthog.clickhouse.query_tagging import Feature, tag_queries+from posthog.models.activity_logging.activity_log import Change, Detail, log_activityfrom posthog.models.team.team import Teamfrom posthog.models.utils import UUIDT@@ -63,6 +64,26 @@ def _stop_survey_if_reached_limit(survey: Survey, responses_count: int) -> None:survey.end_date = timezone.now()survey.responses_limit = Nonesurvey.save(update_fields=["end_date", "responses_limit"])+ _log_survey_closed_by_responses_limit(survey)+++def _log_survey_closed_by_responses_limit(survey: Survey) -> None:+ # This task saves directly on the model, bypassing the API viewset that writes+ # activity-log entries. Without this, the survey stops with no trace and the user+ # has no way to see why. Mirror the schedule-close entry in update_survey_iteration.+ log_activity(+ organization_id=survey.team.organization_id,+ team_id=survey.team_id,+ user=None, # system action: renders as PostHog in the activity log+ was_impersonated=False,+ item_id=survey.id,+ scope="Survey",+ activity="updated",+ detail=Detail(+ name=survey.name,+ changes=[Change(type="Survey", field="end_date", action="created", after=survey.end_date)],+ ),+ )def stop_surveys_reached_target() -> None:diff --git a/posthog/tasks/test/test_stop_surveys_reached_target.py b/posthog/tasks/test/test_stop_surveys_reached_target.pyindex 703beff7d0ff..4f292fa1bd0b 100644--- a/posthog/tasks/test/test_stop_surveys_reached_target.py+++ b/posthog/tasks/test/test_stop_surveys_reached_target.py@@ -11,6 +11,7 @@ from django.utils.timezone import nowfrom dateutil.relativedelta import relativedeltafrom posthog.models import Organization, Team, User+from posthog.models.activity_logging.activity_log import ActivityLogfrom posthog.tasks.stop_surveys_reached_target import stop_surveys_reached_targetfrom products.feature_flags.backend.models.feature_flag import FeatureFlag@@ -251,3 +252,28 @@ class TestStopSurveysReachedTarget(TestCase, ClickhouseTestMixin):survey.refresh_from_db()assert survey.end_date is not Noneassert survey.responses_limit is None++ def test_stop_survey_on_responses_limit_is_logged_as_system_activity(self) -> None:+ survey = Survey.objects.create(+ name="1",+ team=self.team1,+ created_by=self.user,+ linked_flag=self.flag,+ responses_limit=1,+ created_at=now(),+ )+ self._create_event_for_survey(survey)+ flush_persons_and_events()++ stop_surveys_reached_target()++ log = ActivityLog.objects.get(scope="Survey", item_id=str(survey.id), activity="updated")+ # No user: the scheduler closed it, so the activity log shows it as a system action.+ assert log.is_system+ assert log.user is None+ # end_date going None -> value is what the frontend renders as "stopped".+ assert log.detail is not None+ change = log.detail["changes"][0]+ assert change["field"] == "end_date"+ assert change["action"] == "created"+ assert change["after"] is not Nonediff --git a/posthog/tasks/test/test_update_survey_iteration.py b/posthog/tasks/test/test_update_survey_iteration.pyindex 6745d7b58cb3..e25c77bdd65a 100644--- a/posthog/tasks/test/test_update_survey_iteration.py+++ b/posthog/tasks/test/test_update_survey_iteration.py@@ -6,6 +6,7 @@ from django.test import TestCasefrom django.utils.timezone import nowfrom posthog.models import Organization, Team, User+from posthog.models.activity_logging.activity_log import ActivityLogfrom posthog.tasks.update_survey_iteration import update_survey_iterationfrom products.feature_flags.backend.models.feature_flag import FeatureFlag@@ -62,6 +63,23 @@ class TestUpdateSurveyIteration(TestCase, ClickhouseTestMixin):self.assertIsNotNone(self.recurring_survey.end_date)self.assertEqual(self.recurring_survey.current_iteration, 1)+ def test_survey_end_after_final_iteration_is_logged_as_system_activity(self) -> None:+ self.recurring_survey.start_date = now() - timedelta(days=self.iteration_frequency_days * 3 + 1)+ self.recurring_survey.save()+ update_survey_iteration()+ self.recurring_survey.refresh_from_db()++ log = ActivityLog.objects.get(scope="Survey", item_id=str(self.recurring_survey.id), activity="updated")+ # No user: the scheduler closed it, so the activity log shows it as a system action.+ self.assertTrue(log.is_system)+ self.assertIsNone(log.user)+ # end_date going None -> value is what the frontend renders as "stopped".+ assert log.detail is not None+ change = log.detail["changes"][0]+ self.assertEqual(change["field"], "end_date")+ self.assertEqual(change["action"], "created")+ self.assertIsNotNone(change["after"])+def test_huge_iteration_frequency_does_not_crash_task(self) -> None:self.recurring_survey.iteration_count = 1self.recurring_survey.iteration_frequency_days = 2_147_483_647diff --git a/posthog/tasks/update_survey_iteration.py b/posthog/tasks/update_survey_iteration.pyindex 1fa2e7d67541..602a1ae90bf7 100644--- a/posthog/tasks/update_survey_iteration.py+++ b/posthog/tasks/update_survey_iteration.py@@ -3,6 +3,8 @@ from typing import Anyfrom django.utils import timezone+from posthog.models.activity_logging.activity_log import Change, Detail, log_activity+from products.feature_flags.backend.models.feature_flag import FeatureFlagfrom products.surveys.backend.models import Survey@@ -15,6 +17,7 @@ def _update_survey_iteration(survey: Survey) -> None:if _has_final_iteration_ended(survey):survey.end_date = timezone.now()survey.save(update_fields=["end_date"])+ _log_survey_closed_by_schedule(survey)returncurrent_iteration = _get_current_iteration(survey)@@ -30,6 +33,25 @@ def _update_survey_iteration(survey: Survey) -> None:survey.save(update_fields=["current_iteration", "current_iteration_start_date", "internal_targeting_flag_id"])+def _log_survey_closed_by_schedule(survey: Survey) -> None:+ # Record a system activity-log entry so the survey's history explains why it stopped.+ # Without this, the scheduler's direct save() leaves no trace, and a user who removes+ # the end date only ever sees their own removals logged — never the automatic close.+ log_activity(+ organization_id=survey.team.organization_id,+ team_id=survey.team_id,+ user=None, # system action: renders as PostHog in the activity log+ was_impersonated=False,+ item_id=survey.id,+ scope="Survey",+ activity="updated",+ detail=Detail(+ name=survey.name,+ changes=[Change(type="Survey", field="end_date", action="created", after=survey.end_date)],+ ),+ )++def _has_final_iteration_ended(survey: Survey) -> bool:if not survey.iteration_start_dates or not survey.iteration_frequency_days:return False