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.

feat(insights): emit a top-level order on stickiness, lifecycle and heatmap rows (#120252)

Thomas Obermüller8c66be839634
diff --git a/posthog/hogql_queries/query_runner.py b/posthog/hogql_queries/query_runner.py
index 5e412c4563db..fb0e3c46a568 100644
--- a/posthog/hogql_queries/query_runner.py
+++ b/posthog/hogql_queries/query_runner.py
@@ -2253,6 +2253,7 @@ class QueryRunner(ABC, Generic[Q, R, CR]):
)
if isinstance(cached_response, CachedResponse):
+ self._fill_cached_result_order(cached_response)
# Apply current query's custom_name values to cached response
# (custom_name is excluded from cache key, so cached values may be stale)
cached_response, custom_names_modified = self.apply_series_custom_names(cached_response)
@@ -3286,6 +3287,24 @@ class QueryRunner(ABC, Generic[Q, R, CR]):
return self._apply_funnels_custom_names(cached_response)
return cached_response, False
+ def _fill_cached_result_order(self, cached_response: CR) -> None:
+ """Copy `action.order` to the top level of cached trends, stickiness and lifecycle rows that lack it.
+
+ Stickiness, lifecycle and calendar heatmap rows cached before those runners emitted a top-level `order`
+ carry it only on `action`, and readers take a row's series from the top-level `order`.
+ """
+ if not isinstance(self.query, TrendsQuery | StickinessQuery | LifecycleQuery):
+ return
+ results = getattr(cached_response, "results", None)
+ if not isinstance(results, list):
+ return
+ for result in results:
+ if not isinstance(result, dict) or "order" in result:
+ continue
+ action = result.get("action")
+ if isinstance(action, dict) and isinstance(action.get("order"), int):
+ result["order"] = action["order"]
+
def _apply_trends_custom_names(self, cached_response: CR) -> tuple[CR, bool]:
"""Apply custom_name values to TrendsQuery results (nested under action)."""
series = getattr(self.query, "series", None)
diff --git a/products/product_analytics/backend/hogql_queries/lifecycle/lifecycle_query_runner.py b/products/product_analytics/backend/hogql_queries/lifecycle/lifecycle_query_runner.py
index c792a1436d2d..54b2617ad283 100644
--- a/products/product_analytics/backend/hogql_queries/lifecycle/lifecycle_query_runner.py
+++ b/products/product_analytics/backend/hogql_queries/lifecycle/lifecycle_query_runner.py
@@ -203,7 +203,7 @@ class LifecycleQueryRunner(AnalyticsQueryRunner[LifecycleQueryResponse]):
Columns are accessed by name (date, total, status) to support arbitrary
column ordering (materialized tables return columns alphabetically).
- Returns list of dicts with: action, data, count, labels, days, label, status.
+ Returns list of dicts with: action, data, count, labels, days, order, label, status.
"""
series = self.first_series
cols = {name: i for i, name in enumerate(response.columns or [])}
@@ -281,6 +281,7 @@ class LifecycleQueryRunner(AnalyticsQueryRunner[LifecycleQueryResponse]):
"count": float(sum(data)),
"labels": labels,
"days": days,
+ "order": 0,
**additional_values,
}
)
diff --git a/products/product_analytics/backend/hogql_queries/lifecycle/test/test_lifecycle_query_runner.py b/products/product_analytics/backend/hogql_queries/lifecycle/test/test_lifecycle_query_runner.py
index 319773594478..7cc6c3e5b0bb 100644
--- a/products/product_analytics/backend/hogql_queries/lifecycle/test/test_lifecycle_query_runner.py
+++ b/products/product_analytics/backend/hogql_queries/lifecycle/test/test_lifecycle_query_runner.py
@@ -187,6 +187,7 @@ class TestLifecycleQueryRetentionGroupAggregation(ClickhouseTestMixin, APIBaseTe
"18-Jan-2020",
"19-Jan-2020",
],
+ "order": 0,
"status": "new",
"action": {
"id": "$pageview",
@@ -238,6 +239,7 @@ class TestLifecycleQueryRetentionGroupAggregation(ClickhouseTestMixin, APIBaseTe
"18-Jan-2020",
"19-Jan-2020",
],
+ "order": 0,
"status": "returning",
"action": {
"id": "$pageview",
@@ -289,6 +291,7 @@ class TestLifecycleQueryRetentionGroupAggregation(ClickhouseTestMixin, APIBaseTe
"18-Jan-2020",
"19-Jan-2020",
],
+ "order": 0,
"status": "resurrecting",
"action": {
"id": "$pageview",
@@ -340,6 +343,7 @@ class TestLifecycleQueryRetentionGroupAggregation(ClickhouseTestMixin, APIBaseTe
"18-Jan-2020",
"19-Jan-2020",
],
+ "order": 0,
"status": "dormant",
"action": {
"id": "$pageview",
@@ -461,6 +465,7 @@ class TestLifecycleQueryRetentionGroupAggregation(ClickhouseTestMixin, APIBaseTe
"18-Jan-2020",
"19-Jan-2020",
],
+ "order": 0,
"status": "new",
"action": {
"id": "$pageview",
@@ -512,6 +517,7 @@ class TestLifecycleQueryRetentionGroupAggregation(ClickhouseTestMixin, APIBaseTe
"18-Jan-2020",
"19-Jan-2020",
],
+ "order": 0,
"status": "returning",
"action": {
"id": "$pageview",
@@ -563,6 +569,7 @@ class TestLifecycleQueryRetentionGroupAggregation(ClickhouseTestMixin, APIBaseTe
"18-Jan-2020",
"19-Jan-2020",
],
+ "order": 0,
"status": "resurrecting",
"action": {
"id": "$pageview",
@@ -614,6 +621,7 @@ class TestLifecycleQueryRetentionGroupAggregation(ClickhouseTestMixin, APIBaseTe
"18-Jan-2020",
"19-Jan-2020",
],
+ "order": 0,
"status": "dormant",
"action": {
"id": "$pageview",
@@ -961,6 +969,7 @@ class TestLifecycleQueryRunner(ClickhouseTestMixin, APIBaseTest):
"18-Jan-2020",
"19-Jan-2020",
],
+ "order": 0,
"status": "new",
"action": {
"id": "$pageview",
@@ -1012,6 +1021,7 @@ class TestLifecycleQueryRunner(ClickhouseTestMixin, APIBaseTest):
"18-Jan-2020",
"19-Jan-2020",
],
+ "order": 0,
"status": "returning",
"action": {
"id": "$pageview",
@@ -1063,6 +1073,7 @@ class TestLifecycleQueryRunner(ClickhouseTestMixin, APIBaseTest):
"18-Jan-2020",
"19-Jan-2020",
],
+ "order": 0,
"status": "resurrecting",
"action": {
"id": "$pageview",
@@ -1114,6 +1125,7 @@ class TestLifecycleQueryRunner(ClickhouseTestMixin, APIBaseTest):
"18-Jan-2020",
"19-Jan-2020",
],
+ "order": 0,
"status": "dormant",
"action": {
"id": "$pageview",
diff --git a/products/product_analytics/backend/hogql_queries/stickiness/stickiness_query_runner.py b/products/product_analytics/backend/hogql_queries/stickiness/stickiness_query_runner.py
index 30f19432d356..592930e6427f 100644
--- a/products/product_analytics/backend/hogql_queries/stickiness/stickiness_query_runner.py
+++ b/products/product_analytics/backend/hogql_queries/stickiness/stickiness_query_runner.py
@@ -358,6 +358,7 @@ class StickinessQueryRunner(AnalyticsQueryRunner[StickinessQueryResponse]):
"count": count,
"data": data,
"days": val[1],
+ "order": series_with_extra.series_order,
"label": "All events" if series_label is None else series_label,
"labels": [
f"{day} {self.query_date_range.interval_name}{'' if day == 1 else 's'} or more"
diff --git a/products/product_analytics/backend/hogql_queries/stickiness/test/test_stickiness_query_runner.py b/products/product_analytics/backend/hogql_queries/stickiness/test/test_stickiness_query_runner.py
index 5fc720117d5f..2c29c2dbe1a3 100644
--- a/products/product_analytics/backend/hogql_queries/stickiness/test/test_stickiness_query_runner.py
+++ b/products/product_analytics/backend/hogql_queries/stickiness/test/test_stickiness_query_runner.py
@@ -55,7 +55,7 @@ from posthog.hogql.constants import LimitContext
from posthog.hogql.query import execute_hogql_query
from posthog.clickhouse.client.execute import sync_execute
-from posthog.hogql_queries.query_runner import get_query_runner
+from posthog.hogql_queries.query_runner import ExecutionMode, get_query_runner
from posthog.models.group.util import create_group
from posthog.settings import HOGQL_INCREASED_MAX_EXECUTION_TIME
from posthog.test.test_utils import create_group_type_mapping_without_created_at
@@ -1642,6 +1642,26 @@ class TestStickinessSeriesCustomNames(BaseTest):
assert patched_response.results == expected_results
assert was_modified is expect_modified
+ def test_a_cached_row_without_a_top_level_order_takes_it_from_action(self) -> None:
+ query = StickinessQuery(series=[EventsNode(event="$pageview"), EventsNode(event="sign up")])
+ cached_rows = [
+ {"label": "$pageview", "data": [3], "action": {"order": 0, "name": "$pageview"}},
+ {"label": "sign up", "data": [1], "action": {"order": 1, "name": "sign up"}},
+ ]
+ with patch.object(
+ StickinessQueryRunner, "_calculate", return_value=StickinessQueryResponse(results=cached_rows)
+ ):
+ StickinessQueryRunner(query=query, team=self.team).run(
+ execution_mode=ExecutionMode.CALCULATE_BLOCKING_ALWAYS
+ )
+
+ response = StickinessQueryRunner(query=query, team=self.team).run(
+ execution_mode=ExecutionMode.CACHE_ONLY_NEVER_CALCULATE
+ )
+
+ assert isinstance(response, CachedStickinessQueryResponse)
+ assert [row["order"] for row in response.results] == [0, 1]
+
class TestStickinessSeriesFanOut(BaseTest):
def test_runner_rejects_an_expansion_over_the_limit_before_it_expands_the_series(self) -> None:
diff --git a/products/product_analytics/backend/hogql_queries/trends/calendar_heatmap_trends_query_runner.py b/products/product_analytics/backend/hogql_queries/trends/calendar_heatmap_trends_query_runner.py
index 856a1d748208..15ee64bbac3a 100644
--- a/products/product_analytics/backend/hogql_queries/trends/calendar_heatmap_trends_query_runner.py
+++ b/products/product_analytics/backend/hogql_queries/trends/calendar_heatmap_trends_query_runner.py
@@ -74,6 +74,7 @@ class CalendarHeatmapTrendsQueryRunner(TrendsQueryRunner):
"math": self.query.series[0].math if self.query.series else "total",
},
"label": self.series_event(self.query.series[0]) if self.query.series else "Heatmap",
+ "order": 0,
"data": [], # Empty array for non-time-series data
"aggregated_value": results.allAggregations,
"calendar_heatmap_data": results, # Store the original heatmap data