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(flags): exclude personless calls from flag calls lifecycle (#117421)

Phil Haack0744dee8ab7c
diff --git a/posthog/hogql/database/schema/flag_evaluations.py b/posthog/hogql/database/schema/flag_evaluations.py
index 473420398033..b17783ff09fd 100644
--- a/posthog/hogql/database/schema/flag_evaluations.py
+++ b/posthog/hogql/database/schema/flag_evaluations.py
@@ -30,6 +30,9 @@ if TYPE_CHECKING:
# name is spelled out here rather than imported.
FLAG_EVALUATIONS_CLICKHOUSE_TABLE = "flag_evaluations"
+# Keep it equal to FLAG_EVALUATIONS_TABLE in frontend/src/scenes/feature-flags/flagEvaluationsTable.ts.
+FLAG_EVALUATIONS_HOGQL_TABLE = "posthog.flag_evaluations"
+
# The expression the events table uses under the persons-on-events modes that apply overrides. This table
# applies it for every team instead of following the mode, because the mode decides where person properties
diff --git a/posthog/test/base.py b/posthog/test/base.py
index 49355e11f5b2..dc1f847e5dc4 100644
--- a/posthog/test/base.py
+++ b/posthog/test/base.py
@@ -1813,7 +1813,7 @@ def _create_flag_evaluations(
distinct_id: str = "evaluator",
person_id: uuid.UUID | str | None = None,
response: str | bool = True,
- properties: dict[str, str] | None = None,
+ properties: dict[str, Any] | None = None,
) -> None:
"""Insert `count` $feature_flag_called rows for flag_key into flag_evaluations. Unlike _create_event, it writes
the rows immediately. Each row gets a new random person_id unless the caller passes one."""
diff --git a/products/product_analytics/backend/hogql_queries/lifecycle/lifecycle_query_runner.py b/products/product_analytics/backend/hogql_queries/lifecycle/lifecycle_query_runner.py
index e73533a55eab..d944700298e5 100644
--- a/products/product_analytics/backend/hogql_queries/lifecycle/lifecycle_query_runner.py
+++ b/products/product_analytics/backend/hogql_queries/lifecycle/lifecycle_query_runner.py
@@ -21,6 +21,7 @@ from posthog.schema import (
)
from posthog.hogql import ast
+from posthog.hogql.database.schema.flag_evaluations import FLAG_EVALUATIONS_HOGQL_TABLE
from posthog.hogql.parser import parse_expr, parse_select
from posthog.hogql.property import action_to_expr, property_to_expr
from posthog.hogql.query import execute_hogql_query
@@ -337,10 +338,12 @@ class LifecycleQueryRunner(AnalyticsQueryRunner[LifecycleQueryResponse]):
@cached_property
def event_filter(self) -> ast.Expr:
event_filters: list[ast.Expr] = []
- if not self.is_data_warehouse_series:
+ if not self.is_data_warehouse_series or self.data_warehouse_series.table_name == FLAG_EVALUATIONS_HOGQL_TABLE:
# Personless (anonymous) events are excluded: lifecycle classifies a user's activity
# across periods, which requires a person profile. This makes lifecycle counts lower
# than unique-user trends for projects with anonymous traffic.
+ # The flag calls table stores each event's properties. Other warehouse tables have no
+ # $process_person_profile.
event_filters.append(
ast.CompareOperation(
left=ast.Field(chain=["properties", "$process_person_profile"]),
diff --git a/products/product_analytics/backend/tests/test_flag_evaluations_insight_series.py b/products/product_analytics/backend/tests/test_flag_evaluations_insight_series.py
index b455021e4928..57afd1c8f163 100644
--- a/products/product_analytics/backend/tests/test_flag_evaluations_insight_series.py
+++ b/products/product_analytics/backend/tests/test_flag_evaluations_insight_series.py
@@ -36,6 +36,8 @@ from posthog.schema import (
TrendsQuery,
)
+from posthog.hogql.database.schema.flag_evaluations import FLAG_EVALUATIONS_HOGQL_TABLE
+
from products.product_analytics.backend.facade.queries import (
FunnelsQueryRunner,
LifecycleQueryRunner,
@@ -44,7 +46,7 @@ from products.product_analytics.backend.facade.queries import (
TrendsQueryRunner,
)
-TABLE = "posthog.flag_evaluations"
+TABLE = FLAG_EVALUATIONS_HOGQL_TABLE
NOW = datetime(2025, 1, 10, 12, 0, tzinfo=UTC)
DATE_RANGE = DateRange(date_from="2025-01-08", date_to="2025-01-10")
PROBE_FLAG = DataWarehousePropertyFilter(key="flag_key", operator=PropertyOperator.EXACT, value="probe-flag")
@@ -259,6 +261,14 @@ class TestFlagEvaluationsInsightSeries(ClickhouseTestMixin, BaseTest):
aggregation_group_type_index: int | None,
expected: dict[str, list[int]],
):
+ # Lifecycle excludes this personless call. Counting it would add a third new user or group on 2025-01-08.
+ _create_flag_evaluations(
+ self.team.pk,
+ "probe-flag",
+ timestamp=datetime(2025, 1, 8, 14, tzinfo=UTC),
+ distinct_id="anonymous",
+ properties={"$process_person_profile": False, "$group_0": "org-c"},
+ )
query = LifecycleQuery(
dateRange=DATE_RANGE,
interval=IntervalType.DAY,