Skip to content

: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.

fix(web-analytics): handle empty web overview query results (#70513)

posthog[bot]9493548d9438
diff --git a/products/web_analytics/backend/hogql_queries/test/test_web_overview.py b/products/web_analytics/backend/hogql_queries/test/test_web_overview.py
index 062cfe06285b..954e74e12b30 100644
--- a/products/web_analytics/backend/hogql_queries/test/test_web_overview.py
+++ b/products/web_analytics/backend/hogql_queries/test/test_web_overview.py
@@ -180,6 +180,31 @@ class TestWebOverviewQueryRunner(ClickhouseTestMixin, APIBaseTest):
"conversion rate",
]
+ @parameterized.expand([("without_conversion_goal", False), ("with_conversion_goal", True)])
+ @patch("products.web_analytics.backend.hogql_queries.web_overview.execute_hogql_query")
+ def test_empty_hogql_results_do_not_crash(self, _name: str, with_conversion_goal: bool, mock_execute: MagicMock):
+ # The live events query can legitimately return zero rows; that used to hit a bare
+ # `assert response.results` and surface as a 5xx. It should return an empty overview instead.
+ mock_execute.return_value = MagicMock(results=[])
+
+ action = None
+ if with_conversion_goal:
+ action = Action.objects.create(
+ team=self.team,
+ name="Visited Foo",
+ steps_json=[{"event": "$pageview"}],
+ )
+
+ response = self._run_web_overview_query("2023-12-08", "2023-12-15", action=action)
+
+ assert all(item.value is None and item.previous is None for item in response.results)
+ expected_keys = (
+ ["visitors", "total conversions", "unique conversions", "conversion rate"]
+ if with_conversion_goal
+ else ["visitors", "views", "sessions", "session duration", "bounce rate"]
+ )
+ assert [item.key for item in response.results] == expected_keys
+
def test_increase_in_users(self):
s1a = str(uuid7("2023-12-02"))
s1b = str(uuid7("2023-12-12"))
diff --git a/products/web_analytics/backend/hogql_queries/web_overview.py b/products/web_analytics/backend/hogql_queries/web_overview.py
index 4d5cf4bff048..f4294f704296 100644
--- a/products/web_analytics/backend/hogql_queries/web_overview.py
+++ b/products/web_analytics/backend/hogql_queries/web_overview.py
@@ -280,10 +280,11 @@ CROSS JOIN {sessions_agg} AS sessions_agg
limit_context=self.limit_context,
)
- # We could have a empty result in normal conditions but also when we're recreating the tables.
- # While we're testing, if it is a empty result, let's fallback on using the
+ # We could have an empty result in normal conditions but also when we're recreating the tables.
+ # While we're testing, if it is an empty result, let's fall back on using the
# regular queries to be extra careful.
- assert response.results
+ if not response.results:
+ return None
return response
except Exception as e:
@@ -371,9 +372,10 @@ CROSS JOIN {sessions_agg} AS sessions_agg
else pre_aggregated_response
)
- assert response.results
-
- row = response.results[0]
+ # The underlying events query can legitimately return zero rows (a team and time
+ # window with no matching events), so fall back to an all-empty row rather than
+ # crashing with a bare AssertionError. `to_data` renders None values as an empty overview.
+ row = response.results[0] if response.results else [None] * 10
include_previous = bool(self.query.compareFilter and self.query.compareFilter.compare)
def get_prev_val(idx):