: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(cdp): resolve nested detail.name filter on activity log events (#64135)
Phill06564e5ad983
diff --git a/posthog/cdp/filters.py b/posthog/cdp/filters.pyindex 2924150b8674..955257987b77 100644--- a/posthog/cdp/filters.py+++ b/posthog/cdp/filters.py@@ -15,6 +15,48 @@ from products.cohorts.backend.models.cohort import CohortCOHORT_FILTER_TYPES = frozenset({"cohort", "static-cohort", "precalculated-cohort", "dynamic-cohort"})+# Internal events (e.g. activity logs) carry real nested JSON objects in their properties — such as+# `detail` on `$activity_log_entry_created`, which the activity log filter UI exposes as `detail.name`+# and `detail.changes`. Unlike analytics events (whose properties are a flat map), these need dotted+# keys resolved into nested field chains so the HogVM matches against the nested object.+INTERNAL_NESTED_PROPERTY_EVENTS = frozenset({"$activity_log_entry_created"})+++class _NestedPropertyKeyResolver(CloningVisitor):+ """Resolve dotted event-property keys into nested field chains.++ A property key like `detail.name` is otherwise compiled into a single flat lookup+ (`properties["detail.name"]`) that never matches the nested global. Splitting it into the+ chain `properties.detail.name` lets it resolve against the nested object at runtime.+ Only `properties.*` chains are touched, so person/group property filters are left as-is.+ """++ def visit_field(self, node: ast.Field) -> ast.Field:+ field = super().visit_field(node)+ if not field.chain or field.chain[0] != "properties":+ return field+ resolved: list[str | int] = [field.chain[0]]+ for element in field.chain[1:]:+ if isinstance(element, str) and "." in element:+ resolved.extend(element.split("."))+ else:+ resolved.append(element)+ field.chain = resolved+ return field+++def _only_targets_internal_nested_events(filters: dict) -> bool:+ """True only when every targeted event/action is an internal nested event.++ Global property filters apply across all event branches, so a dotted key there can only be+ resolved into a nested chain when *every* targeted event carries nested JSON. If an internal+ event is mixed with an analytics event (e.g. `$pageview`), the same global key may be a flat+ property on that branch — so leave it untouched and let per-branch resolution handle the+ internal event's own properties.+ """+ all_filters = filters.get("events", []) + filters.get("actions", [])+ return bool(all_filters) and all(f.get("id") in INTERNAL_NESTED_PROPERTY_EVENTS for f in all_filters)+class CohortInlineError(Exception):"""Raised when cohort test account filters can't be inlined for real-time use."""@@ -182,11 +224,18 @@ def _build_single_filter_expr(filter: dict, actions: dict[int, Action], team: Te# Return single expression or AND combinationif not filter_exprs:- return ast.Constant(value=True)+ expr: ast.Expr = ast.Constant(value=True)elif len(filter_exprs) == 1:- return filter_exprs[0]+ expr = filter_exprs[0]else:- return ast.And(exprs=filter_exprs)+ expr = ast.And(exprs=filter_exprs)++ # Internal events carry nested JSON properties, so resolve dotted keys (e.g. `detail.name`)+ # into nested chains for this branch only — never for sibling non-internal event branches.+ if filter.get("id") in INTERNAL_NESTED_PROPERTY_EVENTS:+ expr = _NestedPropertyKeyResolver().visit(expr)++ return exprdef _combine_expressions(expressions: list[ast.Expr]) -> ast.Expr:@@ -217,9 +266,17 @@ def hog_function_filters_to_expr(filters: dict, team: Team, actions: dict[int, Aif not all_filters:return _combine_expressions(test_account_filters + global_property_filters)- # Build expressions for each event/action filter+ # Build expressions for each event/action filter (dotted keys on internal events are resolved+ # per-branch inside _build_single_filter_expr).event_action_exprs = [_build_single_filter_expr(filter, actions, team) for filter in all_filters]+ # Global property filters apply across all branches; the activity log UI stores `detail.name`+ # here, so resolve dotted keys only when every targeted event is an internal nested event —+ # never when a sibling analytics event could read the same key as a flat property.+ if _only_targets_internal_nested_events(filters):+ resolver = _NestedPropertyKeyResolver()+ global_property_filters = [resolver.visit(expr) for expr in global_property_filters]+# Combine event/action filters with OR (match any of these events/actions)combined_events_expr = ast.Or(exprs=event_action_exprs) if len(event_action_exprs) > 1 else event_action_exprs[0]diff --git a/posthog/cdp/test/test_filters.py b/posthog/cdp/test/test_filters.pyindex 540bef1c2294..936a1a5f47f4 100644--- a/posthog/cdp/test/test_filters.py+++ b/posthog/cdp/test/test_filters.py@@ -3,6 +3,8 @@ import jsonfrom posthog.test.base import APIBaseTest, ClickhouseTestMixin, QueryMatchingTest+from parameterized import parameterized+from posthog.hogql.compiler.bytecode import create_bytecodefrom posthog.cdp.filters import (@@ -315,6 +317,69 @@ class TestHogFunctionFilters(ClickhouseTestMixin, APIBaseTest, QueryMatchingTest4,]+ @parameterized.expand(+ [+ # `detail` is a nested object on `$activity_log_entry_created`, so a `detail.name` filter+ # must resolve to `properties.detail.name` rather than a flat `properties["detail.name"]` key.+ (+ "activity_log_nested_detail_matches",+ {+ "events": [{"id": "$activity_log_entry_created", "type": "events", "order": 0}],+ "properties": [{"key": "detail.name", "value": "cheese", "operator": "icontains", "type": "event"}],+ },+ {"event": "$activity_log_entry_created", "properties": {"detail": {"name": "cheese wheel"}}},+ True,+ ),+ (+ "activity_log_nested_detail_does_not_match",+ {+ "events": [{"id": "$activity_log_entry_created", "type": "events", "order": 0}],+ "properties": [{"key": "detail.name", "value": "cheese", "operator": "icontains", "type": "event"}],+ },+ {"event": "$activity_log_entry_created", "properties": {"detail": {"name": "bananas"}}},+ False,+ ),+ # Outside internal events a dotted key stays a single flat property lookup, so existing+ # destinations keep matching properties whose names literally contain a dot.+ (+ "regular_event_dotted_key_stays_flat",+ {+ "events": [{"id": "$pageview", "type": "events", "order": 0}],+ "properties": [{"key": "foo.bar", "value": "baz", "operator": "exact", "type": "event"}],+ },+ {"event": "$pageview", "properties": {"foo.bar": "baz"}},+ True,+ ),+ (+ "regular_event_dotted_key_not_treated_as_nested",+ {+ "events": [{"id": "$pageview", "type": "events", "order": 0}],+ "properties": [{"key": "foo.bar", "value": "baz", "operator": "exact", "type": "event"}],+ },+ {"event": "$pageview", "properties": {"foo": {"bar": "baz"}}},+ False,+ ),+ # A global property filter applies to every event branch, so when an internal event is+ # mixed with an analytics event the dotted key must stay flat — resolving it would break+ # the `$pageview` branch that reads `sdk.version` as a literal flat property.+ (+ "mixed_events_global_dotted_key_stays_flat",+ {+ "events": [+ {"id": "$activity_log_entry_created", "type": "events", "order": 0},+ {"id": "$pageview", "type": "events", "order": 1},+ ],+ "properties": [{"key": "sdk.version", "value": "1.2", "operator": "exact", "type": "event"}],+ },+ {"event": "$pageview", "properties": {"sdk.version": "1.2"}},+ True,+ ),+ ]+ )+ def test_dotted_property_key_resolution(self, _name: str, filters: dict, hog_globals: dict, expected: bool):+ bytecode = self.filters_to_bytecode(filters=filters)+ assert execute_bytecode(bytecode, hog_globals).result is expected+class TestCohortExprHelpers(ClickhouseTestMixin, APIBaseTest, QueryMatchingTest):def test_build_behavioral_event_expr_supported_with_event_filters(self):