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.

feat(customer-analytics): list only identified persons in account users (#129756)

Arthur Moreira de Deusfa6c2b3facfa
diff --git a/products/customer_analytics/backend/hogql_queries/account_users_query_runner.py b/products/customer_analytics/backend/hogql_queries/account_users_query_runner.py
index 1a460fac1094..d6b7e72a86e0 100644
--- a/products/customer_analytics/backend/hogql_queries/account_users_query_runner.py
+++ b/products/customer_analytics/backend/hogql_queries/account_users_query_runner.py
@@ -35,6 +35,14 @@ class AccountUsersStrategy(PersonStrategy):
paginator=self.paginator,
query=self.query.model_copy(update={"search": None}),
).filter_conditions()
+ # Live ingestion admits any profile-backed event, so anonymous persons are dropped here.
+ conditions.append(
+ ast.CompareOperation(
+ op=ast.CompareOperationOp.Eq,
+ left=ast.Field(chain=["persons", "is_identified"]),
+ right=ast.Constant(value=True),
+ )
+ )
if self.query.search and self.query.search.strip():
source = self.query.source
assert isinstance(source, AccountUsersQuery)
diff --git a/products/customer_analytics/backend/hogql_queries/test/__snapshots__/test_account_users_query_runner.ambr b/products/customer_analytics/backend/hogql_queries/test/__snapshots__/test_account_users_query_runner.ambr
index 18216816420c..7843397fc499 100644
--- a/products/customer_analytics/backend/hogql_queries/test/__snapshots__/test_account_users_query_runner.ambr
+++ b/products/customer_analytics/backend/hogql_queries/test/__snapshots__/test_account_users_query_runner.ambr
@@ -27,7 +27,8 @@
replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_4)s), ''), 'null'), '^"|"$', '') AS properties___email,
replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_5)s), ''), 'null'), '^"|"$', '') AS properties___name,
replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_6)s), ''), 'null'), '^"|"$', '') AS properties___username,
- replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_7)s), ''), 'null'), '^"|"$', '') AS properties___plan
+ replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_7)s), ''), 'null'), '^"|"$', '') AS properties___plan,
+ person.is_identified AS is_identified
FROM person PREWHERE in(tuple(person.id, person.version),
(SELECT person.id AS id,
max(person.version) AS version
@@ -51,6 +52,7 @@
GROUP BY person.id
HAVING and(equals(argMax(person.is_deleted, person.version), 0), less(argMax(toTimeZone(person.created_at, %(hogql_val_12)s), person.version), plus(now64(6, %(hogql_val_13)s), toIntervalDay(1))))))
WHERE equals(person.team_id, 99999) SETTINGS optimize_aggregation_in_order=0) AS persons ON equals(persons.id, source.person_id)
+ WHERE equals(persons.is_identified, 1)
ORDER BY source.account_last_seen DESC
LIMIT 2
OFFSET 0 SETTINGS optimize_aggregation_in_order=0,
@@ -82,7 +84,8 @@
replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '') AS properties___email,
replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_1)s), ''), 'null'), '^"|"$', '') AS properties___name,
replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_2)s), ''), 'null'), '^"|"$', '') AS properties___username,
- replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_3)s), ''), 'null'), '^"|"$', '') AS properties___plan
+ replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_3)s), ''), 'null'), '^"|"$', '') AS properties___plan,
+ person.is_identified AS is_identified
FROM person PREWHERE in(tuple(person.id, person.version),
(SELECT person.id AS id,
max(person.version) AS version
@@ -93,6 +96,7 @@
GROUP BY person.id
HAVING and(equals(argMax(person.is_deleted, person.version), 0), less(argMax(toTimeZone(person.created_at, %(hogql_val_4)s), person.version), plus(now64(6, %(hogql_val_5)s), toIntervalDay(1))))))
WHERE equals(person.team_id, 99999) SETTINGS optimize_aggregation_in_order=0) AS persons ON equals(persons.id, source.person_id)
+ WHERE equals(persons.is_identified, 1)
ORDER BY toTimeZone(source.account_last_seen, %(hogql_val_14)s) DESC
LIMIT 2
OFFSET 0 SETTINGS optimize_aggregation_in_order=0,
diff --git a/products/customer_analytics/backend/hogql_queries/test/test_account_users_query_runner.py b/products/customer_analytics/backend/hogql_queries/test/test_account_users_query_runner.py
index 93b8cb69e038..230c792d5d0f 100644
--- a/products/customer_analytics/backend/hogql_queries/test/test_account_users_query_runner.py
+++ b/products/customer_analytics/backend/hogql_queries/test/test_account_users_query_runner.py
@@ -89,15 +89,21 @@ class TestAccountUsersQuery(ClickhouseTestMixin, APIBaseTest, QueryMatchingTest)
team=self.team,
distinct_ids=["member-one", "member-one-alias"],
properties={"email": "[email protected]", "name": "First member", "plan": "paid", "private_note": "hidden"},
+ is_identified=True,
)
self.second = create_person(
- team=self.team, distinct_ids=["member-two"], properties={"email": "[email protected]", "plan": "free"}
+ team=self.team,
+ distinct_ids=["member-two"],
+ properties={"email": "[email protected]", "plan": "free"},
+ is_identified=True,
)
+ create_person(team=self.team, distinct_ids=["anonymous-member"], properties={"email": "[email protected]"})
create_person(team=self.team, distinct_ids=["non-member"], properties={"email": "[email protected]"})
flush_persons_and_events()
self._insert_membership("member-one", "2024-01-01", "2024-01-05")
self._insert_membership("member-one-alias", "2024-01-02", "2024-01-09")
self._insert_membership("member-two", "2024-01-03", "2024-01-06")
+ self._insert_membership("anonymous-member", "2024-01-01", "2024-01-10")
self._insert_membership("non-member", "2024-01-01", "2024-01-10", group_key="another-workspace")
self._insert_membership("non-member", "2024-01-01", "2024-01-10", group_type_index=1)
self._insert_membership(