: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(insights): auto-wrap bare journeys and web stats insight queries (#81497)
Sam Pennington0585d1391f4c
diff --git a/products/product_analytics/backend/api/insight.py b/products/product_analytics/backend/api/insight.pyindex c35215ee06fd..1cef5685ece9 100644--- a/products/product_analytics/backend/api/insight.py+++ b/products/product_analytics/backend/api/insight.py@@ -551,17 +551,26 @@ class QueryFieldSerializer(serializers.Serializer):# wrapper nodes and a few standalone kinds (e.g. WebOverviewQuery) to real renderers; the# kinds below have no bare renderer and fall through to a JSON-dump fallback that paints# ~0px inside a dashboard tile, so they are safe (and necessary) to auto-wrap on save.+#+# Keep this exhaustive over the InsightVizNode source union; BARE_RENDERED_INSIGHT_VIZ_SOURCE_KINDS+# holds the exceptions. A test pins both sets against that union, so a new source kind fails+# loudly — but it can't see Query.tsx, so a renderer added or dropped there still needs a human.AUTO_WRAPPED_INSIGHT_QUERY_KINDS = frozenset({"TrendsQuery","FunnelsQuery","RetentionQuery","PathsQuery",+ "PathsV2Query","StickinessQuery","LifecycleQuery",+ "WebStatsTableQuery",})+# InsightVizNode sources the UI also renders unwrapped, so saving them bare is legitimate.+BARE_RENDERED_INSIGHT_VIZ_SOURCE_KINDS = frozenset({"WebOverviewQuery"})+class InsightFilterOverrideContext(BaseModel):dashboard: schema.DashboardFilter | None = PydanticField(diff --git a/products/product_analytics/backend/api/test/test_insight_query.py b/products/product_analytics/backend/api/test/test_insight_query.pyindex 9bb0ef53fdd9..e32a8495c705 100644--- a/products/product_analytics/backend/api/test/test_insight_query.py+++ b/products/product_analytics/backend/api/test/test_insight_query.py@@ -1,10 +1,17 @@+from typing import get_args+from posthog.test.base import APIBaseTest, ClickhouseTestMixin, QueryMatchingTestfrom parameterized import parameterizedfrom rest_framework import status+from posthog import schemafrom posthog.api.test.dashboards import DashboardAPI+from products.product_analytics.backend.api.insight import (+ AUTO_WRAPPED_INSIGHT_QUERY_KINDS,+ BARE_RENDERED_INSIGHT_VIZ_SOURCE_KINDS,+)from products.product_analytics.backend.models.insight import Insightfrom ee.api.test.base import LicensedTestMixin@@ -220,6 +227,23 @@ class TestInsight(ClickhouseTestMixin, LicensedTestMixin, APIBaseTest, QueryMatc"DataVisualizationNode","HogQLQuery",),+ (+ "bare_paths_v2_query",+ {"kind": "PathsV2Query", "dateRange": {"date_from": "-7d"}},+ "InsightVizNode",+ "PathsV2Query",+ ),+ (+ "bare_web_stats_table_query",+ {+ "kind": "WebStatsTableQuery",+ "breakdownBy": "Page",+ "properties": [],+ "dateRange": {"date_from": "-7d"},+ },+ "InsightVizNode",+ "WebStatsTableQuery",+ ),])def test_create_wraps_bare_query_sources(self, _name, query, expected_kind, expected_source_kind) -> None:@@ -266,6 +290,14 @@ class TestInsight(ClickhouseTestMixin, LicensedTestMixin, APIBaseTest, QueryMatcassert Insight.objects.get(pk=insight_id).query == query+ def test_every_insight_viz_source_kind_is_either_auto_wrapped_or_bare_rendered(self) -> None:+ source_annotation = schema.InsightVizNode.model_fields["source"].annotation+ schema_kinds = {get_args(member.model_fields["kind"].annotation)[0] for member in get_args(source_annotation)}++ assert AUTO_WRAPPED_INSIGHT_QUERY_KINDS.isdisjoint(BARE_RENDERED_INSIGHT_VIZ_SOURCE_KINDS)+ # A kind added to the union but to neither set would save bare and render as a blank tile.+ assert AUTO_WRAPPED_INSIGHT_QUERY_KINDS | BARE_RENDERED_INSIGHT_VIZ_SOURCE_KINDS == schema_kinds+def test_update_wraps_bare_query_sources(self) -> None:insight_id, _ = self.dashboard_api.create_insight({