: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): point at the team settings when test account filters cannot run (#104498)
Meikel Ratz70c66a545800
diff --git a/posthog/cdp/filters.py b/posthog/cdp/filters.pyindex e01b46db136e..72772e604959 100644--- a/posthog/cdp/filters.py+++ b/posthog/cdp/filters.py@@ -376,6 +376,15 @@ FILTER_FUNCTIONS: dict[str, tuple[int, Optional[int]]] = {_UNKNOWN_GLOBAL = "Unknown global variable: "+def _unknown_filter_globals(expr: ast.Expr) -> list[str]:+ """Compile once with the runtime's globals declared, and return the roots the runtime will not have."""+ context = HogQLContext(team_id=None, globals=dict.fromkeys(FILTER_GLOBALS), allowed_functions=FILTER_FUNCTIONS)+ create_bytecode(expr, context=context)+ return sorted(+ {w.message.removeprefix(_UNKNOWN_GLOBAL) for w in context.warnings if w.message.startswith(_UNKNOWN_GLOBAL)}+ )++def compile_filters_bytecode(filters: Optional[dict], team: Team, actions: Optional[dict[int, Action]] = None) -> dict:filters = filters or {}try:@@ -395,6 +404,22 @@ def compile_filters_bytecode(filters: Optional[dict], team: Team, actions: Optio{w.message.removeprefix(_UNKNOWN_GLOBAL) for w in context.warnings if w.message.startswith(_UNKNOWN_GLOBAL)})if unknown:+ # The person saving a destination did not write the team's test account filters, so a+ # message that only names the field sends them looking in the wrong place.+ team_exprs = _build_test_account_filters(filters, team)+ from_team = _unknown_filter_globals(_combine_expressions(team_exprs)) if team_exprs else []+ if from_team:+ # Compile the destination's own filters alone rather than subtracting the team's+ # roots: a field that both sources read drops out of the difference and goes unnamed.+ own = _unknown_filter_globals(+ compile_filters_expr({**filters, "filter_test_accounts": False}, team, actions)+ )+ raise Exception(+ f"Your internal/test user filters read {', '.join(from_team)}, which real-time filters "+ f"cannot read. Check the spelling, or use a field or function that real-time filters support. "+ + (f"This destination's own filters also read {', '.join(own)}. " if own else "")+ + f"Update your filters at: {_internal_user_settings_url(team.id)}"+ )raise Exception(f"Real-time filters cannot read {', '.join(unknown)}. "f"Check the spelling, or use a field or function that real-time filters support."diff --git a/posthog/cdp/test/test_filters.py b/posthog/cdp/test/test_filters.pyindex 4c13c2193208..a9c9c688da44 100644--- a/posthog/cdp/test/test_filters.py+++ b/posthog/cdp/test/test_filters.py@@ -250,6 +250,43 @@ class TestHogFunctionFilters(ClickhouseTestMixin, APIBaseTest, QueryMatchingTest)assert "bytecode_error" not in allowed+ def test_filters_name_the_team_settings_when_test_account_filters_cannot_run(self):+ self.team.test_account_filters = [{"type": "hogql", "key": "$virt_is_bot = false"}]+ self.team.save()++ result = compile_filters_bytecode({"filter_test_accounts": True}, self.team)+ assert result["bytecode"] is None+ assert _normalize_error(result["bytecode_error"]) == (+ "Your internal/test user filters read $virt_is_bot, which real-time filters cannot read. "+ "Check the spelling, or use a field or function that real-time filters support. "+ "Update your filters at: SETTINGS_URL#internal-user-filtering"+ )++ # A bad field in the destination on top of the project's names both sources, so a person+ # knows there are two places to fix.+ own = compile_filters_bytecode(+ {"filter_test_accounts": True, "properties": [{"type": "hogql", "key": "$virt_traffic_type = 'y'"}]},+ self.team,+ )+ assert _normalize_error(own["bytecode_error"]) == (+ "Your internal/test user filters read $virt_is_bot, which real-time filters cannot read. "+ "Check the spelling, or use a field or function that real-time filters support. "+ "This destination's own filters also read $virt_traffic_type. "+ "Update your filters at: SETTINGS_URL#internal-user-filtering"+ )++ # Both sources reading the same field must still name both, or the next save fails the same way.+ shared = compile_filters_bytecode(+ {"filter_test_accounts": True, "properties": [{"type": "hogql", "key": "$virt_is_bot = true"}]},+ self.team,+ )+ assert _normalize_error(shared["bytecode_error"]) == (+ "Your internal/test user filters read $virt_is_bot, which real-time filters cannot read. "+ "Check the spelling, or use a field or function that real-time filters support. "+ "This destination's own filters also read $virt_is_bot. "+ "Update your filters at: SETTINGS_URL#internal-user-filtering"+ )+def test_filters_allow_group_globals(self):response = compile_filters_bytecode(filters={