: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(web-analytics): add more custom bot rule properties (#94268)
Lucas Ricoy63939f874f38
diff --git a/frontend/src/queries/schema.json b/frontend/src/queries/schema.jsonindex e9359785174d..73fba8b24778 100644--- a/frontend/src/queries/schema.json+++ b/frontend/src/queries/schema.json@@ -17300,7 +17300,22 @@},"CustomBotField": {"description": "The event properties a project's own bot rule can match on.",- "enum": ["$raw_user_agent", "$ip", "$lib", "$host", "$pathname", "$current_url"],+ "enum": [+ "$raw_user_agent",+ "$ip",+ "$lib",+ "$host",+ "$pathname",+ "$current_url",+ "$browser",+ "$os",+ "$browser_language",+ "$screen_width",+ "$screen_height",+ "$geoip_country_code",+ "$referrer",+ "$referring_domain"+ ],"type": "string"},"CustomBotMatcher": {diff --git a/frontend/src/queries/schema/schema-general.ts b/frontend/src/queries/schema/schema-general.tsindex e5c89a4e5648..7708b5d08b73 100644--- a/frontend/src/queries/schema/schema-general.ts+++ b/frontend/src/queries/schema/schema-general.ts@@ -6811,6 +6811,14 @@ export enum CustomBotField {Host = '$host',Pathname = '$pathname',CurrentURL = '$current_url',+ Browser = '$browser',+ OS = '$os',+ BrowserLanguage = '$browser_language',+ ScreenWidth = '$screen_width',+ ScreenHeight = '$screen_height',+ CountryCode = '$geoip_country_code',+ Referrer = '$referrer',+ ReferringDomain = '$referring_domain',}export enum CustomBotMatcher {diff --git a/frontend/src/scenes/settings/environment/customBotDefinitionsUtils.ts b/frontend/src/scenes/settings/environment/customBotDefinitionsUtils.tsindex 652fa35895ae..a1a3ba9d197a 100644--- a/frontend/src/scenes/settings/environment/customBotDefinitionsUtils.ts+++ b/frontend/src/scenes/settings/environment/customBotDefinitionsUtils.ts@@ -14,6 +14,14 @@ export const CUSTOM_BOT_FIELD_OPTIONS: { value: CustomBotField; label: string }[{ value: CustomBotField.Host, label: 'Host' },{ value: CustomBotField.Pathname, label: 'Path name' },{ value: CustomBotField.CurrentURL, label: 'Current URL' },+ { value: CustomBotField.Browser, label: 'Browser' },+ { value: CustomBotField.OS, label: 'OS' },+ { value: CustomBotField.BrowserLanguage, label: 'Browser language' },+ { value: CustomBotField.ScreenWidth, label: 'Screen width' },+ { value: CustomBotField.ScreenHeight, label: 'Screen height' },+ { value: CustomBotField.CountryCode, label: 'Country code' },+ { value: CustomBotField.Referrer, label: 'Referrer' },+ { value: CustomBotField.ReferringDomain, label: 'Referring domain' },]const MATCHER_LABELS: Record<CustomBotMatcher, string> = {@@ -69,6 +77,14 @@ export function patternPlaceholderFor(key: CustomBotField, matcher: CustomBotMat[CustomBotField.Host]: 'scraper.example.com',[CustomBotField.Pathname]: '/api/products',[CustomBotField.CurrentURL]: 'example.com/api',+ [CustomBotField.Browser]: 'Chrome',+ [CustomBotField.OS]: 'Linux',+ [CustomBotField.BrowserLanguage]: '@posix',+ [CustomBotField.ScreenWidth]: '800',+ [CustomBotField.ScreenHeight]: '600',+ [CustomBotField.CountryCode]: 'US',+ [CustomBotField.Referrer]: 'scraper.example.com',+ [CustomBotField.ReferringDomain]: 'scraper.example.com',}[key] ?? 'AcmeBot')}diff --git a/posthog/api/test/test_team.py b/posthog/api/test/test_team.pyindex a2afe5306d03..c7eef4e19f97 100644--- a/posthog/api/test/test_team.py+++ b/posthog/api/test/test_team.py@@ -1229,7 +1229,11 @@ def team_api_test_factory():{"key": "$raw_user_agent", "pattern": "192.0.2.0/24", "matcher": "cidr"},False,),- ("unsupported property", {"key": "$referrer", "pattern": "acme", "matcher": "contains"}, False),+ (+ "unsupported property",+ {"key": "$some_other_property", "pattern": "acme", "matcher": "contains"},+ False,+ ),])def test_modifiers_customBotDefinitions_validation(diff --git a/posthog/hogql/functions/test/test_traffic_type_functions.py b/posthog/hogql/functions/test/test_traffic_type_functions.pyindex a0a3b27df030..0711375333ee 100644--- a/posthog/hogql/functions/test/test_traffic_type_functions.py+++ b/posthog/hogql/functions/test/test_traffic_type_functions.py@@ -790,6 +790,17 @@ class TestCustomBotDefinitions(ClickhouseTestMixin, BaseTest):assert (is_bot, name) == (True, "Load test")+ def test_a_rule_on_a_numeric_property_matches_its_string_value(self):+ # Screen width and height are stored as numbers. The rule matches against the string form,+ # so the pattern "800" catches a screen width of 800. Without the cast, multiMatchAllIndices+ # gets a number and the query fails to compile rather than matching.+ is_bot, name, _category = self._classify(+ [_custom_bot(name="Headless viewport", key=CustomBotField.FIELD_SCREEN_WIDTH, pattern="800")],+ {"$raw_user_agent": CHROME_USER_AGENT, "$screen_width": 800},+ )++ assert (is_bot, name) == (True, "Headless viewport")+@parameterized.expand([# Someone who writes a rule for a bot PostHog already knows means to relabel it, sodiff --git a/posthog/hogql/functions/traffic_type.py b/posthog/hogql/functions/traffic_type.pyindex b8f618230d53..00bc6423f8e0 100644--- a/posthog/hogql/functions/traffic_type.py+++ b/posthog/hogql/functions/traffic_type.py@@ -37,6 +37,7 @@ from products.web_analytics.backend.hogql_queries.bot_ip_definitions import ()from products.web_analytics.backend.hogql_queries.custom_bot_definitions import (IP_FIELD,+ NUMERIC_FIELDS,USER_AGENT_FIELD,CidrGroup,CustomBotGroup,@@ -118,7 +119,12 @@ def _custom_group_branch(group: CustomBotGroup, args: list[ast.Expr], attr: str)multi_if_args.append(ast.Constant(value=0))index_call: ast.Expr = ast.Call(name="multiIf", args=multi_if_args)else:- safe_property = ast.Call(name="ifNull", args=[property_expr, ast.Constant(value="")])+ # A numeric property (screen width/height) reaches multiMatchAllIndices as a string, so+ # a rule pattern like "800" matches the value 800. String properties skip the cast.+ matched_property = (+ ast.Call(name="toString", args=[property_expr]) if group.key in NUMERIC_FIELDS else property_expr+ )+ safe_property = ast.Call(name="ifNull", args=[matched_property, ast.Constant(value="")])# arrayMin over ALL matching patterns, not multiMatchAnyIndex: when two of a project's own# rules match the same value, the one listed first wins. multiMatchAnyIndex would report# whichever pattern matches earliest in the string, so a specific rule listed above a broaddiff --git a/posthog/schema_enums.py b/posthog/schema_enums.pyindex 648cd9093f98..a4ad1ad38ed6 100644--- a/posthog/schema_enums.py+++ b/posthog/schema_enums.py@@ -891,6 +891,14 @@ class CustomBotField(StrEnum):FIELD_HOST = "$host"FIELD_PATHNAME = "$pathname"FIELD_CURRENT_URL = "$current_url"+ FIELD_BROWSER = "$browser"+ FIELD_OS = "$os"+ FIELD_BROWSER_LANGUAGE = "$browser_language"+ FIELD_SCREEN_WIDTH = "$screen_width"+ FIELD_SCREEN_HEIGHT = "$screen_height"+ FIELD_GEOIP_COUNTRY_CODE = "$geoip_country_code"+ FIELD_REFERRER = "$referrer"+ FIELD_REFERRING_DOMAIN = "$referring_domain"class CustomBotMatcher(StrEnum):diff --git a/products/customer_analytics/frontend/generated/api.schemas.ts b/products/customer_analytics/frontend/generated/api.schemas.tsindex d7911a6ab22d..7956f8d52720 100644--- a/products/customer_analytics/frontend/generated/api.schemas.ts+++ b/products/customer_analytics/frontend/generated/api.schemas.ts@@ -1151,6 +1151,14 @@ export const CustomBotFieldApi = {Host: '$host',Pathname: '$pathname',CurrentUrl: '$current_url',+ Browser: '$browser',+ Os: '$os',+ BrowserLanguage: '$browser_language',+ ScreenWidth: '$screen_width',+ ScreenHeight: '$screen_height',+ GeoipCountryCode: '$geoip_country_code',+ Referrer: '$referrer',+ ReferringDomain: '$referring_domain',} as constexport type CustomBotMatcherApi = (typeof CustomBotMatcherApi)[keyof typeof CustomBotMatcherApi]diff --git a/products/dashboards/frontend/generated/api.schemas.ts b/products/dashboards/frontend/generated/api.schemas.tsindex b3bbf95dd321..a3bc3c75b6ac 100644--- a/products/dashboards/frontend/generated/api.schemas.ts+++ b/products/dashboards/frontend/generated/api.schemas.ts@@ -1348,6 +1348,14 @@ export const CustomBotFieldApi = {Host: '$host',Pathname: '$pathname',CurrentUrl: '$current_url',+ Browser: '$browser',+ Os: '$os',+ BrowserLanguage: '$browser_language',+ ScreenWidth: '$screen_width',+ ScreenHeight: '$screen_height',+ GeoipCountryCode: '$geoip_country_code',+ Referrer: '$referrer',+ ReferringDomain: '$referring_domain',} as constexport type CustomBotMatcherApi = (typeof CustomBotMatcherApi)[keyof typeof CustomBotMatcherApi]diff --git a/products/product_analytics/frontend/generated/api.schemas.ts b/products/product_analytics/frontend/generated/api.schemas.tsindex b03b5e73602b..0eb63e4c03dd 100644--- a/products/product_analytics/frontend/generated/api.schemas.ts+++ b/products/product_analytics/frontend/generated/api.schemas.ts@@ -343,6 +343,14 @@ export const CustomBotFieldApi = {Host: '$host',Pathname: '$pathname',CurrentUrl: '$current_url',+ Browser: '$browser',+ Os: '$os',+ BrowserLanguage: '$browser_language',+ ScreenWidth: '$screen_width',+ ScreenHeight: '$screen_height',+ GeoipCountryCode: '$geoip_country_code',+ Referrer: '$referrer',+ ReferringDomain: '$referring_domain',} as constexport type CustomBotMatcherApi = (typeof CustomBotMatcherApi)[keyof typeof CustomBotMatcherApi]diff --git a/products/web_analytics/backend/hogql_queries/custom_bot_definitions.py b/products/web_analytics/backend/hogql_queries/custom_bot_definitions.pyindex 29d62a67debe..ada60e3df9f7 100644--- a/products/web_analytics/backend/hogql_queries/custom_bot_definitions.py+++ b/products/web_analytics/backend/hogql_queries/custom_bot_definitions.py@@ -52,7 +52,8 @@ IP_FIELD = "$ip"# Event properties a rule can match on, labelled the way the taxonomy already labels them.# Deliberately short: each property used adds a read to every query that selects a classification-# field, so the list stays to properties that say who is calling rather than what they found.+# field, so the list stays to properties that say who is calling rather than what they found. A+# property is read only when a project has a rule on it, so a project with no such rule pays nothing.CUSTOM_BOT_FIELDS: dict[str, str] = {USER_AGENT_FIELD: "Raw user agent",IP_FIELD: "IP address",@@ -60,8 +61,21 @@ CUSTOM_BOT_FIELDS: dict[str, str] = {"$host": "Host","$pathname": "Path name","$current_url": "Current URL",+ "$browser": "Browser",+ "$os": "OS",+ "$browser_language": "Browser language",+ "$screen_width": "Screen width",+ "$screen_height": "Screen height",+ "$geoip_country_code": "Country code",+ "$referrer": "Referrer",+ "$referring_domain": "Referring domain",}+# Fields stored as numbers. multiMatchAllIndices needs a String, so a rule on one of these matches+# against toString(value): a screen width of 800 matches the pattern "800". The other fields are+# already strings, so they skip the cast and their query expressions stay byte-identical.+NUMERIC_FIELDS: frozenset[str] = frozenset({"$screen_width", "$screen_height"})+CIDR_MATCHER = "cidr"PATTERN_MATCHERS = ("contains", "regex")diff --git a/products/web_analytics/backend/hogql_queries/test/test_custom_bot_definitions.py b/products/web_analytics/backend/hogql_queries/test/test_custom_bot_definitions.pyindex a71a91248abd..26a8588239d0 100644--- a/products/web_analytics/backend/hogql_queries/test/test_custom_bot_definitions.py+++ b/products/web_analytics/backend/hogql_queries/test/test_custom_bot_definitions.py@@ -110,6 +110,8 @@ class TestValidation:("substring with metacharacters", {"pattern": "Acme (bot) v1.0"}),("anchored regex", {"pattern": "^AcmeBot/[0-9]+$", "matcher": CustomBotMatcher.REGEX}),("a non-default property", {"key": CustomBotField.FIELD_LIB, "pattern": "posthog-python"}),+ ("a numeric property", {"key": CustomBotField.FIELD_SCREEN_WIDTH, "pattern": "800"}),+ ("browser language", {"key": CustomBotField.FIELD_BROWSER_LANGUAGE, "pattern": "@posix"}),])def test_accepts_usable_definitions(self, _name: str, overrides: dict):diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.tsindex 1619fd13cb4c..e9622dae09b6 100644--- a/services/mcp/src/api/generated.ts+++ b/services/mcp/src/api/generated.ts@@ -795,6 +795,14 @@ export namespace Schemas {Host: '$host',Pathname: '$pathname',CurrentUrl: '$current_url',+ Browser: '$browser',+ Os: '$os',+ BrowserLanguage: '$browser_language',+ ScreenWidth: '$screen_width',+ ScreenHeight: '$screen_height',+ GeoipCountryCode: '$geoip_country_code',+ Referrer: '$referrer',+ ReferringDomain: '$referring_domain',} as const;export type CustomBotMatcher = typeof CustomBotMatcher[keyof typeof CustomBotMatcher];