From fb8219ded609b8c4eaa9951341fcd3ed48ad9c1a Mon Sep 17 00:00:00 2001 From: Robbie Date: Tue, 30 Jul 2024 16:54:06 +0100 Subject: [PATCH] fix(web-analytics): Fix more instances where count() should have been uniq() (#24081) --- .../web_analytics/stats_table.py | 4 +-- .../test/test_web_stats_table.py | 33 ++++++++++++------- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/posthog/hogql_queries/web_analytics/stats_table.py b/posthog/hogql_queries/web_analytics/stats_table.py index 179fadf8e561d..38a2a58706afa 100644 --- a/posthog/hogql_queries/web_analytics/stats_table.py +++ b/posthog/hogql_queries/web_analytics/stats_table.py @@ -194,7 +194,7 @@ def to_path_scroll_bounce_query(self) -> ast.SelectQuery: FROM ( SELECT breakdown_value, - count(person_id) AS visitors, + uniq(person_id) AS visitors, sum(filtered_pageview_count) AS views FROM ( SELECT @@ -301,7 +301,7 @@ def to_path_bounce_query(self) -> ast.SelectQuery: FROM ( SELECT breakdown_value, - count(person_id) AS visitors, + uniq(person_id) AS visitors, sum(filtered_pageview_count) AS views FROM ( SELECT diff --git a/posthog/hogql_queries/web_analytics/test/test_web_stats_table.py b/posthog/hogql_queries/web_analytics/test/test_web_stats_table.py index 5d8d0a5ed007e..088942335c7eb 100644 --- a/posthog/hogql_queries/web_analytics/test/test_web_stats_table.py +++ b/posthog/hogql_queries/web_analytics/test/test_web_stats_table.py @@ -909,36 +909,45 @@ def test_same_user_multiple_sessions(self): team=self.team, event="$pageview", distinct_id=d1, - timestamp="2024-06-26", + timestamp="2024-07-30", properties={"$session_id": s1, "utm_source": "google", "$pathname": "/path"}, ) _create_event( team=self.team, event="$pageview", distinct_id=d1, - timestamp="2024-06-26", + timestamp="2024-07-30", properties={"$session_id": s2, "utm_source": "google", "$pathname": "/path"}, ) # Try this with a query that uses session properties results_session = self._run_web_stats_table_query( "all", - "2024-06-27", + "2024-07-31", breakdown_by=WebStatsBreakdown.INITIAL_UTM_SOURCE, ).results - self.assertEqual( - [["google", 1, 2]], - results_session, - ) + assert [["google", 1, 2]] == results_session # Try this with a query that uses event properties results_event = self._run_web_stats_table_query( "all", - "2024-06-27", + "2024-07-31", breakdown_by=WebStatsBreakdown.PAGE, ).results + assert [["/path", 1, 2]] == results_event - self.assertEqual( - [["/path", 1, 2]], - results_event, - ) + # Try this with a query using the bounce rate + results_event = self._run_web_stats_table_query( + "all", "2024-07-31", breakdown_by=WebStatsBreakdown.PAGE, include_bounce_rate=True + ).results + assert [["/path", 1, 2, None]] == results_event + + # Try this with a query using the scroll depth + results_event = self._run_web_stats_table_query( + "all", + "2024-07-31", + breakdown_by=WebStatsBreakdown.PAGE, + include_bounce_rate=True, + include_scroll_depth=True, + ).results + assert [["/path", 1, 2, None, None, None]] == results_event