diff --git a/frontend/src/scenes/surveys/surveyLogic.tsx b/frontend/src/scenes/surveys/surveyLogic.tsx index 701e22440d7ef..a2fd70b7b1acc 100644 --- a/frontend/src/scenes/surveys/surveyLogic.tsx +++ b/frontend/src/scenes/surveys/surveyLogic.tsx @@ -910,7 +910,8 @@ export const surveyLogic = kea([ if (survey.id === 'new') { return null } - const createdAt = (survey as Survey).created_at + const surveyWithResults = survey as Survey + const startDate = surveyWithResults.start_date || surveyWithResults.created_at return { kind: NodeKind.DataTableNode, source: { @@ -934,7 +935,7 @@ export const surveyLogic = kea([ ], orderBy: ['timestamp DESC'], where: [`event == 'survey sent'`], - after: createdAt, + after: startDate, properties: [ { type: PropertyFilterType.Event, diff --git a/posthog/api/survey.py b/posthog/api/survey.py index cf4e965b59276..893122aaae66a 100644 --- a/posthog/api/survey.py +++ b/posthog/api/survey.py @@ -557,8 +557,8 @@ def destroy(self, request: Request, *args: Any, **kwargs: Any) -> Response: @action(methods=["GET"], detail=False) def responses_count(self, request: request.Request, **kwargs): - earliest_survey_creation_date = Survey.objects.filter(team_id=self.team_id).aggregate(Min("created_at"))[ - "created_at__min" + earliest_survey_start_date = Survey.objects.filter(team_id=self.team_id).aggregate(Min("start_date"))[ + "start_date__min" ] data = sync_execute( f""" @@ -567,7 +567,7 @@ def responses_count(self, request: request.Request, **kwargs): WHERE event = 'survey sent' AND team_id = %(team_id)s AND timestamp >= %(timestamp)s GROUP BY survey_id """, - {"team_id": self.team_id, "timestamp": earliest_survey_creation_date}, + {"team_id": self.team_id, "timestamp": earliest_survey_start_date}, ) counts = {} diff --git a/posthog/api/test/__snapshots__/test_survey.ambr b/posthog/api/test/__snapshots__/test_survey.ambr index 5efda6c771efc..24056154fe8f5 100644 --- a/posthog/api/test/__snapshots__/test_survey.ambr +++ b/posthog/api/test/__snapshots__/test_survey.ambr @@ -23,6 +23,18 @@ GROUP BY survey_id ''' # --- +# name: TestResponsesCount.test_responses_count_only_after_first_survey_started + ''' + /* user_id:0 request:_snapshot_ */ + SELECT JSONExtractString(properties, '$survey_id') as survey_id, + count() + FROM events + WHERE event = 'survey sent' + AND team_id = 2 + AND timestamp >= '2024-04-25 14:40:09' + GROUP BY survey_id + ''' +# --- # name: TestSurveysAPIList.test_list_surveys ''' SELECT COUNT(*) AS "__count" diff --git a/posthog/api/test/test_survey.py b/posthog/api/test/test_survey.py index e164cc89f256c..7f7c164e6fdcc 100644 --- a/posthog/api/test/test_survey.py +++ b/posthog/api/test/test_survey.py @@ -2685,7 +2685,7 @@ def test_responses_count(self): } earliest_survey = Survey.objects.create(team_id=self.team.id) - earliest_survey.created_at = datetime.now() - timedelta(days=101) + earliest_survey.start_date = datetime.now() - timedelta(days=101) earliest_survey.save() for survey_id, count in survey_counts.items(): @@ -2706,7 +2706,7 @@ def test_responses_count(self): @snapshot_clickhouse_queries @freeze_time("2024-05-01 14:40:09") - def test_responses_count_only_after_first_survey_created(self): + def test_responses_count_only_after_first_survey_started(self): survey_counts = { "d63bb580-01af-4819-aae5-edcf7ef2044f": 3, "fe7c4b62-8fc9-401e-b483-e4ff98fd13d5": 6, @@ -2719,7 +2719,7 @@ def test_responses_count_only_after_first_survey_created(self): } earliest_survey = Survey.objects.create(team_id=self.team.id) - earliest_survey.created_at = datetime.now() - timedelta(days=6) + earliest_survey.start_date = datetime.now() - timedelta(days=6) earliest_survey.save() for survey_id, count in survey_counts.items():