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(insights): use tile filter overrides when building returned query in serializer (#67098)

Sam Pennington768f44d8a9ec
diff --git a/products/product_analytics/backend/api/insight.py b/products/product_analytics/backend/api/insight.py
index dcb5db93e151..97b423848188 100644
--- a/products/product_analytics/backend/api/insight.py
+++ b/products/product_analytics/backend/api/insight.py
@@ -992,6 +992,13 @@ class InsightSerializer(InsightBasicSerializer):
request, dashboard, list(self.context["insight_variables"])
)
+ # Tile filters completely replace dashboard filters (same semantics as the compute path in
+ # calculate_results.py). Without this, the returned `query` field would reflect dashboard
+ # filters while the cached result was computed with tile filters — causing the persons modal
+ # to use a different filter set than the chart.
+ dashboard_tile = self.dashboard_tile_from_context(instance, dashboard)
+ tile_filters_override = tile_filters_override_requested_by_client(request, dashboard_tile) if request else {}
+
if instance.query is not None or instance.query_from_filters is not None:
query = instance.query or instance.query_from_filters
if (
@@ -999,15 +1006,20 @@ class InsightSerializer(InsightBasicSerializer):
or dashboard_filters_override is not None
or dashboard_variables_override is not None
):
- query = apply_dashboard_filters_to_dict(
- query,
- (
+ effective_filters = (
+ tile_filters_override
+ if tile_filters_override
+ else (
dashboard_filters_override
if dashboard_filters_override is not None
else dashboard.filters
if dashboard
else {}
- ),
+ )
+ )
+ query = apply_dashboard_filters_to_dict(
+ query,
+ effective_filters,
instance.team,
)
diff --git a/products/product_analytics/backend/api/test/test_insight.py b/products/product_analytics/backend/api/test/test_insight.py
index 2e6036806963..f5f9923432cd 100644
--- a/products/product_analytics/backend/api/test/test_insight.py
+++ b/products/product_analytics/backend/api/test/test_insight.py
@@ -4318,6 +4318,51 @@ class TestInsight(ClickhouseTestMixin, APIBaseTest, QueryMatchingTest):
assert isinstance(response["query"]["source"], dict)
assert response["query"]["source"]["dateRange"]["date_from"] == "-7d"
+ def test_tile_filters_override_replaces_dashboard_filters_in_returned_query(self) -> None:
+ insight = Insight.objects.create(
+ filters={},
+ query={
+ "kind": "InsightVizNode",
+ "source": {
+ "kind": "TrendsQuery",
+ "series": [{"event": "$pageview", "kind": "EventsNode"}],
+ "dateRange": {"date_from": "-30d"},
+ },
+ },
+ team=self.team,
+ )
+ dashboard = Dashboard.objects.create(team=self.team, name="dashboard 1", created_by=self.user)
+ # Tile has its own date-range filter (no person properties). The compute path treats
+ # this as a complete replacement for all dashboard filters.
+ DashboardTile.objects.create(
+ dashboard=dashboard,
+ insight=insight,
+ filters_overrides={"date_from": "-7d"},
+ )
+
+ # Dashboard-level filter includes a person property filter (e.g. $initial_host = readdy.ai).
+ dashboard_filters = {
+ "properties": [{"key": "$initial_host", "type": "person", "operator": "exact", "value": ["readdy.ai"]}]
+ }
+
+ response = self.client.get(
+ f"/api/projects/{self.team.id}/insights/{insight.pk}",
+ data={
+ "from_dashboard": str(dashboard.pk),
+ "filters_override": json.dumps(dashboard_filters),
+ },
+ ).json()
+
+ source = response["query"]["source"]
+ # The tile's filter (date_from = -7d, no person properties) must win over the
+ # dashboard filter. If the dashboard person properties appeared here the persons
+ # modal would use a different filter set than the chart — the bug this test guards.
+ assert source["dateRange"]["date_from"] == "-7d"
+ # properties must be absent or empty — the dashboard's person filter must not appear.
+ assert not source.get("properties"), (
+ f"Tile filters should replace dashboard filters; persons modal would diverge. Got: {source.get('properties')}"
+ )
+
def test_insight_cache_key_changes_with_variable_override_when_tile_filters_are_set(self) -> None:
dashboard = Dashboard.objects.create(
team=self.team,