diff --git a/posthog/tasks/test/__snapshots__/test_usage_report.ambr b/posthog/tasks/test/__snapshots__/test_usage_report.ambr index 4e5039c1df0b1..ef1c16871c5c5 100644 --- a/posthog/tasks/test/__snapshots__/test_usage_report.ambr +++ b/posthog/tasks/test/__snapshots__/test_usage_report.ambr @@ -15,10 +15,11 @@ count(distinct toDate(timestamp), event, cityHash64(distinct_id), cityHash64(uuid)) as count 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', + AND event NOT IN ('$feature_flag_called', + 'survey sent', 'survey shown', 'survey dismissed') + AND NOT startsWith(event, '$$') GROUP BY team_id ''' # --- @@ -199,10 +200,11 @@ count(1) as count 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', + AND event NOT IN ('$feature_flag_called', + 'survey sent', 'survey shown', 'survey dismissed') + AND NOT startsWith(event, '$$') GROUP BY team_id ''' # --- diff --git a/posthog/tasks/test/test_usage_report.py b/posthog/tasks/test/test_usage_report.py index 9f27af35da191..aa9d2efcecedb 100644 --- a/posthog/tasks/test/test_usage_report.py +++ b/posthog/tasks/test/test_usage_report.py @@ -643,6 +643,34 @@ def test_unlicensed_usage_report(self, mock_post: MagicMock, mock_client: MagicM assert mock_posthog.capture.call_count == 2 mock_posthog.capture.assert_has_calls(calls, any_order=True) + @freeze_time("2022-01-10T00:01:00Z") + @patch("os.environ", {"DEPLOYMENT": "tests"}) + @patch("posthog.tasks.usage_report.Client") + @patch("requests.post") + def test_does_not_count_internal_events(self, mock_post: MagicMock, mock_client: MagicMock) -> None: + distinct_id = str(uuid4()) + _create_person(distinct_ids=[distinct_id], team=self.team) + + _create_event( + distinct_id=distinct_id, + event="$pageview", + timestamp=now() - relativedelta(hours=12), + team=self.team, + ) + + _create_event( + distinct_id=distinct_id, + event="$$internal_event", + timestamp=now() - relativedelta(hours=12), + team=self.team, + ) + + period = get_previous_day() + period_start, period_end = period + all_reports = _get_all_org_reports(period_start, period_end) + + assert all_reports[str(self.organization.id)].event_count_in_period == 1 + class HogQLUsageReport(APIBaseTest, ClickhouseTestMixin, ClickhouseDestroyTablesMixin): def test_usage_report_hogql_queries(self) -> None: @@ -1140,12 +1168,6 @@ def setUp(self) -> None: distinct_id=1, timestamp="2021-10-09T13:01:01Z", ) - _create_event( - event="$$internal_metrics_shouldnt_be_billed", - team=self.team, - distinct_id=1, - timestamp="2021-10-09T13:01:01Z", - ) _create_event( event="$pageview", team=self.team2, diff --git a/posthog/tasks/usage_report.py b/posthog/tasks/usage_report.py index c2daef6a4ec73..827dfafe63241 100644 --- a/posthog/tasks/usage_report.py +++ b/posthog/tasks/usage_report.py @@ -401,7 +401,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' AND event NOT IN ('survey sent', 'survey shown', 'survey dismissed') + WHERE timestamp between %(begin)s AND %(end)s AND event NOT IN ('$feature_flag_called', 'survey sent', 'survey shown', 'survey dismissed') AND NOT startsWith(event, '$$') GROUP BY team_id """, {"begin": begin, "end": end},