Skip to content

Commit

Permalink
chore(surveys): do not double charge for survey sent events (#18698)
Browse files Browse the repository at this point in the history
* do not double charge for survey sent events

* fix events and test

* fix event count in test

* fix tests
  • Loading branch information
liyiy authored Nov 30, 2023
1 parent 04f1aef commit 54a2bd3
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
6 changes: 6 additions & 0 deletions posthog/tasks/test/__snapshots__/test_usage_report.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
FROM events
WHERE timestamp between '2022-01-10 00:00:00' AND '2022-01-10 23:59:59'
AND event != '$feature_flag_called'
AND event NOT IN ('survey sent',
'survey shown',
'survey dismissed')
GROUP BY team_id
'
---
Expand Down Expand Up @@ -196,6 +199,9 @@
FROM events
WHERE timestamp between '2022-01-01 00:00:00' AND '2022-01-10 23:59:59'
AND event != '$feature_flag_called'
AND event NOT IN ('survey sent',
'survey shown',
'survey dismissed')
GROUP BY team_id
'
---
Expand Down
49 changes: 45 additions & 4 deletions posthog/tasks/test/test_usage_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,8 @@ def _test_usage_report(self) -> List[dict]:
"plugins_enabled": {"Installed and enabled": 1},
"instance_tag": "none",
"event_count_lifetime": 55,
"event_count_in_period": 23,
"event_count_in_month": 43,
"event_count_in_period": 22,
"event_count_in_month": 42,
"event_count_with_groups_in_period": 2,
"recording_count_in_period": 5,
"recording_count_total": 16,
Expand Down Expand Up @@ -409,8 +409,8 @@ def _test_usage_report(self) -> List[dict]:
"teams": {
str(self.org_1_team_1.id): {
"event_count_lifetime": 44,
"event_count_in_period": 13,
"event_count_in_month": 33,
"event_count_in_period": 12,
"event_count_in_month": 32,
"event_count_with_groups_in_period": 2,
"recording_count_in_period": 0,
"recording_count_total": 0,
Expand Down Expand Up @@ -984,6 +984,47 @@ def test_usage_report_survey_responses(self, billing_task_mock: MagicMock, posth
assert org_2_report["teams"]["5"]["survey_responses_count_in_period"] == 1
assert org_2_report["teams"]["5"]["survey_responses_count_in_month"] == 7

@patch("posthog.tasks.usage_report.Client")
@patch("posthog.tasks.usage_report.send_report_to_billing_service")
def test_survey_events_are_not_double_charged(
self, billing_task_mock: MagicMock, posthog_capture_mock: MagicMock
) -> None:
self._setup_teams()
for i in range(5):
_create_event(
distinct_id="4",
event="survey sent",
properties={
"$survey_id": "see22eep-o12-as124",
"$survey_response": "correct",
},
timestamp=now() - relativedelta(hours=i),
team=self.org_1_team_1,
)
_create_event(
distinct_id="4",
event="survey shown",
timestamp=now() - relativedelta(hours=i),
team=self.org_1_team_1,
)
_create_event(
distinct_id="4",
event="survey dismissed",
timestamp=now() - relativedelta(hours=i),
team=self.org_1_team_1,
)
flush_persons_and_events()
period = get_previous_day(at=now() + relativedelta(days=1))
period_start, period_end = period
all_reports = _get_all_org_reports(period_start, period_end)
report = _get_full_org_usage_report_as_dict(
_get_full_org_usage_report(all_reports[str(self.org_1.id)], get_instance_metadata(period))
)
assert report["organization_name"] == "Org 1"
assert report["survey_responses_count_in_month"] == 5
assert report["event_count_in_period"] == 0
assert report["event_count_in_month"] == 0


@freeze_time("2022-01-10T00:01:00Z")
class TestExternalDataSyncUsageReport(ClickhouseDestroyTablesMixin, TestCase, ClickhouseTestMixin):
Expand Down
2 changes: 1 addition & 1 deletion posthog/tasks/usage_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ def get_teams_with_billable_event_count_in_period(
f"""
SELECT team_id, count({distinct_expression}) as count
FROM events
WHERE timestamp between %(begin)s AND %(end)s AND event != '$feature_flag_called'
WHERE timestamp between %(begin)s AND %(end)s AND event != '$feature_flag_called' AND event NOT IN ('survey sent', 'survey shown', 'survey dismissed')
GROUP BY team_id
""",
{"begin": begin, "end": end},
Expand Down

0 comments on commit 54a2bd3

Please sign in to comment.