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(web-analytics): drop properties blob read from bot classification fields (#68253)

Lucas Ricoycb9bd3c64d93
diff --git a/posthog/hogql/database/schema/test/test_traffic_type.py b/posthog/hogql/database/schema/test/test_traffic_type.py
index b052ebe47dcc..aeb0040120f2 100644
--- a/posthog/hogql/database/schema/test/test_traffic_type.py
+++ b/posthog/hogql/database/schema/test/test_traffic_type.py
@@ -23,27 +23,12 @@ FIELD_NAMES = ["$virt_is_bot", "$virt_traffic_type", "$virt_traffic_category", "
class TestUserAgentExpr:
def test_default_properties_path(self):
- expr = user_agent_expr()
- assert isinstance(expr, ast.Call)
- assert expr.name == "coalesce"
- assert len(expr.args) == 2
- # First arg should be nullIf($raw_user_agent, '') to handle empty strings
- null_if = expr.args[0]
- assert isinstance(null_if, ast.Call)
- assert null_if.name == "nullIf"
- assert null_if.args[0] == ast.Field(chain=["properties", "$raw_user_agent"])
- assert null_if.args[1] == ast.Constant(value="")
- assert expr.args[1] == ast.Field(chain=["properties", "$user_agent"])
+ # Raw-only on purpose: a $user_agent fallback would force a properties-blob read
+ assert user_agent_expr() == ast.Field(chain=["properties", "$raw_user_agent"])
def test_custom_properties_path(self):
expr = user_agent_expr(properties_path=["poe", "properties"])
- assert isinstance(expr, ast.Call)
- assert expr.name == "coalesce"
- null_if = expr.args[0]
- assert isinstance(null_if, ast.Call)
- assert null_if.name == "nullIf"
- assert null_if.args[0] == ast.Field(chain=["poe", "properties", "$raw_user_agent"])
- assert expr.args[1] == ast.Field(chain=["poe", "properties", "$user_agent"])
+ assert expr == ast.Field(chain=["poe", "properties", "$raw_user_agent"])
class TestClientIPExpr:
diff --git a/posthog/hogql/database/schema/traffic_type.py b/posthog/hogql/database/schema/traffic_type.py
index 0722073bd44b..1b3501084194 100644
--- a/posthog/hogql/database/schema/traffic_type.py
+++ b/posthog/hogql/database/schema/traffic_type.py
@@ -5,21 +5,14 @@ from posthog.hogql.database.models import ExpressionField
def user_agent_expr(properties_path: Optional[list[str]] = None) -> ast.Expr:
+ # Intentionally no fallback to properties.$user_agent: that property has no materialized
+ # column, so referencing it forces a full properties-blob read on every query using these
+ # fields, and it only carries a value on a tiny fraction of events (SDKs that send it
+ # without $raw_user_agent). Those events classify via the empty-UA path instead, same as
+ # SDKs that send no user agent at all.
if not properties_path:
properties_path = ["properties"]
- return ast.Call(
- name="coalesce",
- args=[
- ast.Call(
- name="nullIf",
- args=[
- ast.Field(chain=[*properties_path, "$raw_user_agent"]),
- ast.Constant(value=""),
- ],
- ),
- ast.Field(chain=[*properties_path, "$user_agent"]),
- ],
- )
+ return ast.Field(chain=[*properties_path, "$raw_user_agent"])
def client_ip_expr(properties_path: Optional[list[str]] = None) -> ast.Expr:
diff --git a/posthog/hogql/functions/test/test_traffic_type_real_ua.py b/posthog/hogql/functions/test/test_traffic_type_real_ua.py
index 31d74713b9b2..b626324529d0 100644
--- a/posthog/hogql/functions/test/test_traffic_type_real_ua.py
+++ b/posthog/hogql/functions/test/test_traffic_type_real_ua.py
@@ -189,7 +189,10 @@ class TestTrafficTypeIntegration(BaseTest):
assert category == "search_crawler"
assert bot_name == "Googlebot"
- def test_virt_properties_with_user_agent_fallback(self):
+ def test_virt_properties_ignore_user_agent_without_raw(self):
+ # $user_agent alone (no $raw_user_agent) is intentionally not read — it has no
+ # materialized column, so a fallback would force a properties-blob read on every
+ # query. These events classify via the empty-UA path instead.
tag = uuid4().hex
self._create_tagged_event(
tag=tag,
@@ -207,8 +210,8 @@ class TestTrafficTypeIntegration(BaseTest):
is_bot, traffic_type, category, bot_name = response.results[0]
assert is_bot == 1
assert traffic_type == "Automation"
- assert category == "http_client"
- assert bot_name == "curl"
+ assert category == "no_user_agent"
+ assert bot_name == ""
def test_virt_properties_raw_ua_takes_precedence(self):
tag = uuid4().hex
@@ -336,11 +339,11 @@ class TestTrafficTypeIntegration(BaseTest):
distinct_id=f"filter-{i}",
event="test_bot_filter",
team=self.team,
- properties={"$user_agent": ua},
+ properties={"$raw_user_agent": ua},
)
flush_persons_and_events()
- response = self._query_tagged("properties.$user_agent as ua", tag, extra_where="NOT `$virt_is_bot`")
+ response = self._query_tagged("properties.$raw_user_agent as ua", tag, extra_where="NOT `$virt_is_bot`")
result_uas = [row[0] for row in response.results]
assert len(result_uas) == len(regular_uas)
for ua in regular_uas:
@@ -361,7 +364,7 @@ class TestTrafficTypeIntegration(BaseTest):
distinct_id=f"group-{i}",
event="test_group_type",
team=self.team,
- properties={"$user_agent": ua},
+ properties={"$raw_user_agent": ua},
)
flush_persons_and_events()
@@ -386,7 +389,7 @@ class TestTrafficTypeIntegration(BaseTest):
event="http_request",
team=self.team,
properties={
- "$user_agent": "Mozilla/5.0 (compatible; AhrefsBot/7.0; +http://ahrefs.com/robot/)",
+ "$raw_user_agent": "Mozilla/5.0 (compatible; AhrefsBot/7.0; +http://ahrefs.com/robot/)",
"method": "GET",
"path": "/api/v1/health",
"status_code": 200,
@@ -415,7 +418,7 @@ class TestVirtualPropertiesWithCustomEvents(BaseTest):
distinct_id=f"cross-{i}",
event=event_name,
team=self.team,
- properties={"$user_agent": "Googlebot/2.1", "_test_tag": tag},
+ properties={"$raw_user_agent": "Googlebot/2.1", "_test_tag": tag},
)
flush_persons_and_events()