: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
feat(insights): run data checks on action and combined series (#121590)
Sam Penningtona4255d179dd0
diff --git a/products/product_analytics/backend/chart_events.py b/products/product_analytics/backend/chart_events.pyindex 122cd2e2b7ac..af6c5e550bfb 100644--- a/products/product_analytics/backend/chart_events.py+++ b/products/product_analytics/backend/chart_events.py@@ -5,7 +5,7 @@ from typing import Any, castfrom pydantic import ValidationError-from posthog.schema import EventsNode, HogQLQueryModifiers+from posthog.schema import ActionsNode, EventsNode, GroupNode, HogQLQueryModifiersfrom posthog.hogql import astfrom posthog.hogql.database.database import Database@@ -85,8 +85,8 @@ def chart_events(query: Mapping[str, Any], team: Team, user: User) -> ChartEventreturn Noneif not isinstance(runner, _CHART_RUNNERS) or type(runner) not in _CHART_RUNNERS:return None- # An action or a data warehouse series has no event name to check.- if not runner.query.series or not all(isinstance(series, EventsNode) for series in runner.query.series):+ # A data warehouse series reads no events.+ if not runner.query.series or not all(_reads_events(series) for series in runner.query.series):return Nonerunner.validate()database = runner.shared_database@@ -106,12 +106,7 @@ def chart_events(query: Mapping[str, Any], team: Team, user: User) -> ChartEventreturn ChartEvents(alias=alias,where=_any_of(wheres),- sort_key_where=_any_of(- [- _all_of([part for part in top_level_conjuncts(where) if _reads_sort_key_only(part, alias)])- for where in wheres- ]- ),+ sort_key_where=_any_of([_sort_key_part(where, alias) or ast.Constant(value=True) for where in wheres]),date_from=min(date_range.date_from() for date_range in date_ranges),date_to=max(date_range.date_to() for date_range in date_ranges),filters_test_accounts=bool(runner.query.filterTestAccounts),@@ -120,6 +115,12 @@ def chart_events(query: Mapping[str, Any], team: Team, user: User) -> ChartEvent)+def _reads_events(series: object) -> bool:+ if isinstance(series, GroupNode):+ return all(isinstance(node, EventsNode | ActionsNode) for node in series.nodes)+ return isinstance(series, EventsNode | ActionsNode)++def _chart_queries(runner: TrendsQueryRunner | FunnelsQueryRunner | StickinessQueryRunner | LifecycleQueryRunner,) -> list[tuple[ast.Expr, QueryDateRange]]:@@ -268,6 +269,17 @@ def _reads_sort_key_only(expr: ast.Expr, alias: str | None) -> bool:return not finder.found and not contains_subquery(expr)+def _sort_key_part(expr: ast.Expr, alias: str | None) -> ast.Expr | None:+ """A filter on event names and dates only that keeps every event `expr` keeps, or None when there is none."""+ if isinstance(expr, ast.And):+ parts = [part for part in (_sort_key_part(child, alias) for child in expr.exprs) if part is not None]+ return _all_of(parts) if parts else None+ if isinstance(expr, ast.Or):+ branches = [_sort_key_part(child, alias) for child in expr.exprs]+ return None if any(branch is None for branch in branches) else _any_of(cast(list[ast.Expr], branches))+ return expr if _reads_sort_key_only(expr, alias) else None++def _all_of(conditions: list[ast.Expr]) -> ast.Expr:if not conditions:return ast.Constant(value=True)diff --git a/products/product_analytics/backend/tests/test_chart_events.py b/products/product_analytics/backend/tests/test_chart_events.pyindex f2699ba511ea..6945a619a3dd 100644--- a/products/product_analytics/backend/tests/test_chart_events.py+++ b/products/product_analytics/backend/tests/test_chart_events.py@@ -1,3 +1,4 @@+import jsonfrom datetime import UTC, datetime, timedeltafrom typing import Any@@ -6,13 +7,22 @@ from posthog.test.base import BaseTest, ClickhouseTestMixin, _create_event, flusfrom parameterized import parameterized+from posthog.hogql import astfrom posthog.hogql.context import HogQLContextfrom posthog.hogql.query import execute_hogql_query-from products.product_analytics.backend.chart_events import chart_events+from products.actions.backend.models.action import Action+from products.product_analytics.backend.chart_events import ChartEvents, chart_eventsSERIES = {"kind": "EventsNode", "event": "signed up"}STEPS = [SERIES, {"kind": "EventsNode", "event": "paid"}]+PAID_PRO = {+ "kind": "EventsNode",+ "event": "paid",+ "properties": [{"key": "plan", "value": "pro", "operator": "exact", "type": "event"}],+}+COMBINED = {"kind": "GroupNode", "operator": "OR", "nodes": [SERIES, PAID_PRO]}+ACTION = {"kind": "ActionsNode", "id": "ACTION_ID"}NOW = datetime(2026, 3, 10, 12, tzinfo=UTC)LAST_WEEK = {"date_from": "-7d"}@@ -59,7 +69,36 @@ class TestChartEventsCounts(ClickhouseTestMixin, BaseTest):_create_event(team=self.team, event="signed up", distinct_id="person", timestamp=NOW - timedelta(days=days_ago))+ for days_ago, plan in ((2, "pro"), (4, "free")):+ _create_event(+ team=self.team,+ event="paid",+ distinct_id="person",+ timestamp=NOW - timedelta(days=days_ago),+ properties={"plan": plan},+ )+ _create_event(team=self.team, event="other", distinct_id="person", timestamp=NOW - timedelta(days=1))flush_persons_and_events()+ self.action = Action.objects.create(+ team=self.team,+ name="Signed up or paid for pro",+ steps_json=[{"event": "paid", "properties": PAID_PRO["properties"]}, {"event": "signed up"}],+ )++ def _chart_events(self, query: dict[str, Any]) -> ChartEvents:+ query = json.loads(json.dumps(query).replace('"ACTION_ID"', str(self.action.pk)))+ events = chart_events({**query, "dateRange": LAST_WEEK}, self.team, self.user)+ assert events is not None+ return events++ def _count(self, events: ChartEvents, where: ast.Expr) -> int:+ response = execute_hogql_query(+ events.select("SELECT count() FROM events", where=where),+ team=self.team,+ modifiers=events.modifiers,+ context=HogQLContext(team_id=self.team.pk, database=events.database),+ )+ return response.results[0][0]@parameterized.expand([@@ -78,19 +117,27 @@ class TestChartEventsCounts(ClickhouseTestMixin, BaseTest):},2,),+ ("funnel_combined_step", {"kind": "FunnelsQuery", "series": [COMBINED, SERIES]}, 3),+ ("stickiness_action_series", {"kind": "StickinessQuery", "series": [ACTION]}, 3),+ (+ "funnel_action_in_a_combined_step",+ {+ "kind": "FunnelsQuery",+ "series": [{"kind": "GroupNode", "operator": "OR", "nodes": [SERIES, ACTION]}, SERIES],+ },+ 3,+ ),])def test_reads_the_events_of_every_period_the_chart_reads(self, _name: str, query: dict[str, Any], expected: int) -> None:- events = chart_events({**query, "dateRange": LAST_WEEK}, self.team, self.user)- assert events is not None+ events = self._chart_events(query)- response = execute_hogql_query(- events.select("SELECT count() FROM events", where=events.where),- team=self.team,- modifiers=events.modifiers,- context=HogQLContext(team_id=self.team.pk, database=events.database),- )+ assert self._count(events, events.where) == expected++ @parameterized.expand([("combined_series", COMBINED, 4), ("action_series", ACTION, 4)])+ def test_sizes_a_series_by_its_event_names(self, _name: str, series: dict[str, Any], expected: int) -> None:+ events = self._chart_events({"kind": "FunnelsQuery", "series": [series, SERIES]})- assert response.results[0][0] == expected+ assert self._count(events, events.sort_key_where) == expecteddiff --git a/products/product_analytics/backend/tests/test_insight_data_checks.py b/products/product_analytics/backend/tests/test_insight_data_checks.pyindex a89ae7182b2c..bd496806a12d 100644--- a/products/product_analytics/backend/tests/test_insight_data_checks.py+++ b/products/product_analytics/backend/tests/test_insight_data_checks.py@@ -750,7 +750,6 @@ class TestInsightDataChecks(ClickhouseTestMixin, APIBaseTest):@parameterized.expand([("unknown_event", [{"kind": "EventsNode", "event": "never sent"}], 1_000_000_000, "no_events"),- ("action_series", [{"kind": "ActionsNode", "id": 1}], 1_000_000_000, "unsupported"),("read_limit_reached_while_sizing", ONE_STEP, 1, "inconclusive"),])diff --git a/products/product_analytics/frontend/insights/dataChecks/insightDataChecksLogic.test.ts b/products/product_analytics/frontend/insights/dataChecks/insightDataChecksLogic.test.tsindex b2d053791405..e31db25ecfa3 100644--- a/products/product_analytics/frontend/insights/dataChecks/insightDataChecksLogic.test.ts+++ b/products/product_analytics/frontend/insights/dataChecks/insightDataChecksLogic.test.ts@@ -177,6 +177,22 @@ describe('insightDataChecksLogic', () => {expect(sentSources.at(-1)).toEqual(source)})+ const PAID = { kind: NodeKind.EventsNode, event: 'paid' }+ const ACTION = { kind: NodeKind.ActionsNode, id: 1 }+ it.each([+ ['events combined into one series', { kind: NodeKind.GroupNode, operator: 'OR', nodes: [PAID] }],+ ['an action', ACTION],+ ['an action combined with an event', { kind: NodeKind.GroupNode, operator: 'OR', nodes: [PAID, ACTION] }],+ ])('checks a chart with %s', async (_name, series) => {+ dataChecks.mockImplementation(answer([200, checksOf('passed')]))+ const source = { kind: NodeKind.TrendsQuery, series: [series] }+ insightDataLogic(props).actions.setQuery({ kind: NodeKind.InsightVizNode, source } as InsightVizNode)++ await expectLogic(logic, turnTheFlagOn).toFinishAllListeners()++ expect(sentSources.at(-1)).toEqual(source)+ })+it('checks again after a refresh of the chart that ends during a check', async () => {const first = holdTheFirstAnswer()turnTheFlagOn()diff --git a/products/product_analytics/frontend/insights/dataChecks/insightDataChecksLogic.ts b/products/product_analytics/frontend/insights/dataChecks/insightDataChecksLogic.tsindex 174e4351123b..0f2f2f86c5fa 100644--- a/products/product_analytics/frontend/insights/dataChecks/insightDataChecksLogic.ts+++ b/products/product_analytics/frontend/insights/dataChecks/insightDataChecksLogic.ts@@ -29,7 +29,14 @@ import type {StickinessQuery,TrendsQuery,} from '~/queries/schema/schema-general'-import { isEventsNode, isInsightQueryWithSeries, isInsightVizNode, shouldQueryBeAsync } from '~/queries/utils'+import {+ isActionsNode,+ isEventsNode,+ isGroupNode,+ isInsightQueryWithSeries,+ isInsightVizNode,+ shouldQueryBeAsync,+} from '~/queries/utils'import { SidePanelTab } from '~/types'import type { AnyPropertyFilter, InsightLogicProps, TeamPublicType, TeamType } from '~/types'@@ -303,6 +310,15 @@ const FORCED_REFRESHES: (RefreshType | undefined)[] = ['force_async', 'force_bloconst DATA_CHECKS_TIMEOUT_MS = 120_000const LABEL_OPEN_MS = 2500const YIELDED_TO_CHART = 'yielded to chart'++function isEventsOrActionsNode(node: Node): boolean {+ return isEventsNode(node) || isActionsNode(node)+}++function readsEvents(series: Node): boolean {+ return isEventsOrActionsNode(series) || (isGroupNode(series) && series.nodes.every(isEventsOrActionsNode))+}+export const insightDataChecksLogic: LogicWrapper<insightDataChecksLogicType> = kea<insightDataChecksLogicType>([props({} as InsightLogicProps),key(keyForInsightLogicProps('new')),@@ -445,7 +461,7 @@ export const insightDataChecksLogic: LogicWrapper<insightDataChecksLogicType> =(currentTeam: TeamPublicType | TeamType | null): boolean =>removesBotTraffic(currentTeam?.test_account_filters ?? []),],- // The server only checks insights whose series are all events, so no request goes out for the rest.+ // The server only checks insights whose series all read events, so no request goes out for a data warehouse series.// Compared by value, so a render that rebuilds the same query is not a new query.checkedQuery: [(s) => [s.query, s.featureFlags, s.hasOverrides],@@ -455,7 +471,7 @@ export const insightDataChecksLogic: LogicWrapper<insightDataChecksLogicType> =return null}const source = query.source- if (!isInsightQueryWithSeries(source) || !source.series.length || !source.series.every(isEventsNode)) {+ if (!isInsightQueryWithSeries(source) || !source.series.length || !source.series.every(readsEvents)) {return null}return { kind: NodeKind.InsightVizNode, source }