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(customer-analytics): match partial domains in account search (#117377)

posthog[bot]879fb0b3ffe1
diff --git a/products/customer_analytics/backend/logic/account_filters.py b/products/customer_analytics/backend/logic/account_filters.py
index cc376f55c0e8..ba5e962c3f70 100644
--- a/products/customer_analytics/backend/logic/account_filters.py
+++ b/products/customer_analytics/backend/logic/account_filters.py
@@ -9,12 +9,14 @@ from django.core.exceptions import ValidationError as DjangoValidationError
from django.core.validators import validate_email
from django.db.models import Exists, OuterRef, Q, QuerySet
from django.db.models.fields.json import KeyTextTransform
+from django.db.models.lookups import IContains
from django.utils import timezone
from django.utils.dateparse import parse_date, parse_datetime
from posthog.models.tagged_item import TaggedItem
from posthog.utils import relative_date_parse
+from products.customer_analytics.backend.domain import parse_company_domain
from products.customer_analytics.backend.facade import contracts
from products.customer_analytics.backend.models import Account, AccountRelationship, CustomPropertyValue, DisplayType
from products.customer_analytics.backend.models.custom_property_definition import DATA_TYPE_BY_DISPLAY_TYPE, DataType
@@ -44,6 +46,8 @@ ACCOUNT_DATETIME_FIELDS = frozenset(
}
)
RELATIVE_DATE_VALUE_RE = re.compile(r"[+-]?\d+[hdwmqysHDWMQY](?:Start|End)?")
+# Hostname characters only, so a fragment can't match across the JSON quotes and commas of `email_domains`.
+DOMAIN_FRAGMENT_RE = re.compile(r"[a-z0-9.-]+")
UTC_TIMEZONE = ZoneInfo("UTC")
ACCOUNT_DIRECT_FIELDS = {
@@ -77,11 +81,12 @@ def account_search_q(query: str, *, member_external_ids: Collection[str] = ()) -
predicate |= Q(_properties__known_emails__contains=[email])
if member_external_ids:
predicate |= Q(external_id__in=member_external_ids)
- domain = email.rsplit("@", 1)[-1]
- else:
- domain = normalized if "@" not in normalized else ""
- if "." in domain:
- predicate |= Q(_properties__email_domains__contains=[domain])
+ predicate |= Q(_properties__email_domains__contains=[email.rsplit("@", 1)[-1]])
+ return predicate
+ domain_fragment = parse_company_domain(normalized) or normalized
+ if DOMAIN_FRAGMENT_RE.fullmatch(domain_fragment):
+ predicate |= Q(IContains(KeyTextTransform("website_domain", "_properties"), domain_fragment))
+ predicate |= Q(IContains(KeyTextTransform("email_domains", "_properties"), domain_fragment))
return predicate
diff --git a/products/customer_analytics/backend/test/test_facade.py b/products/customer_analytics/backend/test/test_facade.py
index 638312fd02c4..444c93aa8562 100644
--- a/products/customer_analytics/backend/test/test_facade.py
+++ b/products/customer_analytics/backend/test/test_facade.py
@@ -147,6 +147,10 @@ class TestCustomerAnalyticsFacade(BaseTest):
("known email", "[email protected]", ["Acme Corp"]),
("email domain of an address", "[email protected]", ["Acme Corp"]),
("bare email domain", "acme.example", ["Acme Corp"]),
+ ("partial email domain", "acme.exa", ["Acme Corp"]),
+ ("email domain with a leading at sign", "@acme.example", ["Acme Corp"]),
+ ("partial website domain", "globex-corp.ex", ["Globex"]),
+ ("website url", "https://www.globex-corp.example/pricing", ["Globex"]),
("address nobody owns", "[email protected]", []),
]
)
@@ -157,7 +161,12 @@ class TestCustomerAnalyticsFacade(BaseTest):
external_id="acme-123",
properties={"email_domains": ["acme.example"], "known_emails": ["[email protected]"]},
)
- create_account(team_id=self.team.id, name="Globex", external_id="globex-9")
+ create_account(
+ team_id=self.team.id,
+ name="Globex",
+ external_id="globex-9",
+ properties={"website_domain": "globex-corp.example"},
+ )
rows, count = facade.search_accounts(self.team.id, query, self._uac(), limit=10)
diff --git a/products/customer_analytics/frontend/components/Accounts/AGENTS.md b/products/customer_analytics/frontend/components/Accounts/AGENTS.md
index 47956f34e9ec..943afbd277a5 100644
--- a/products/customer_analytics/frontend/components/Accounts/AGENTS.md
+++ b/products/customer_analytics/frontend/components/Accounts/AGENTS.md
@@ -70,7 +70,8 @@ Search matches the account name and external ID as substrings.
A complete email address also matches accounts that list it under `known_emails`.
For staff in organizations with `CUSTOMER_ANALYTICS_CSP`, exact email search resolves active US organization memberships by indexed Postgres email lookup, then falls back to the `eu_org_members` warehouse view when US has no match; organization IDs join to `Account.external_id`.
The member lookup is strictly gated on a valid complete email, so name, ID, partial-email, and bare-domain searches issue no member or warehouse queries.
-A domain (bare, or the domain part of a complete address) matches accounts that own it under `email_domains`.
+The domain part of a complete address matches accounts that own it under `email_domains`.
+Other text that holds only hostname characters (after `parse_company_domain` strips a URL, `www.`, or a leading `@`) matches `email_domains` and `website_domain` as a substring.
`account_search_q` in `backend/logic/account_filters.py` defines the Postgres predicate, while `backend/logic/account_member_search.py` owns the staff-only member lookup.
The list query, account search endpoint, and agent entity search share both paths.