: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
fix(flags): count mode 2 flag calls without a persons join (#122399)
Phil Haack9da92bc72480
diff --git a/frontend/src/scenes/hog-functions/configuration/hogFunctionConfigurationLogic.test.ts b/frontend/src/scenes/hog-functions/configuration/hogFunctionConfigurationLogic.test.tsindex 9af3e7ba6ed7..335d4873115a 100644--- a/frontend/src/scenes/hog-functions/configuration/hogFunctionConfigurationLogic.test.ts+++ b/frontend/src/scenes/hog-functions/configuration/hogFunctionConfigurationLogic.test.ts@@ -432,16 +432,31 @@ describe('hogFunctionConfigurationLogic', () => {// Mode 1 still writes flag calls to events, so only mode 2 adds the flag_evaluations count.it.each([- ['events', FlagEvaluationsModeEnumApi.Number0, 500, [0, 0, 0]],- ['read flag evaluations', FlagEvaluationsModeEnumApi.Number1, 500, [0, 0, 0]],- ['flag evaluations only', FlagEvaluationsModeEnumApi.Number2, 1203, [0, 200, 0]],- ])('estimates the calls it would receive on the %s mode', async (_name, mode, count, highVolume) => {- mountOnMode(mode)- await expectLogic(logic).toDispatchActions(['loadTemplateSuccess', 'sparklineQueryChangedSuccess'])-- expect(logic.values.sparkline?.count).toEqual(count)- expect(logic.values.sparkline?.data.find(({ name }) => name === 'High volume')?.values).toEqual(highVolume)- })+ ['events', FlagEvaluationsModeEnumApi.Number0, 500, [0, 0, 0], undefined],+ ['read flag evaluations', FlagEvaluationsModeEnumApi.Number1, 500, [0, 0, 0], undefined],+ [+ 'flag evaluations only',+ FlagEvaluationsModeEnumApi.Number2,+ 1203,+ [0, 200, 0],+ 'person_id_no_override_properties_on_events',+ ],+ ])(+ 'estimates the calls it would receive on the %s mode',+ async (_name, mode, count, highVolume, flagCallsPersonsMode) => {+ mountOnMode(mode)+ await expectLogic(logic).toDispatchActions(['loadTemplateSuccess', 'sparklineQueryChangedSuccess'])++ expect(logic.values.sparkline?.count).toEqual(count)+ expect(logic.values.sparkline?.data.find(({ name }) => name === 'High volume')?.values).toEqual(+ highVolume+ )+ const flagCallsQuery = (performQuery as jest.Mock).mock.calls+ .map(([query]) => query)+ .find((query) => query.kind === NodeKind.EventsQuery)+ expect(flagCallsQuery?.modifiers?.personsOnEventsMode).toBe(flagCallsPersonsMode)+ }+ )it.each([[diff --git a/frontend/src/scenes/hog-functions/filters/flagCallsMatching.ts b/frontend/src/scenes/hog-functions/filters/flagCallsMatching.tsindex b1f6c7a46f07..6051bc6c69be 100644--- a/frontend/src/scenes/hog-functions/filters/flagCallsMatching.ts+++ b/frontend/src/scenes/hog-functions/filters/flagCallsMatching.ts@@ -69,11 +69,12 @@ export function matchesOnlyFlagCallsOutsideEvents(/*** Daily counts of the flag calls that `filters` match over the last `days` days. The events query runner reads* flag_evaluations for a query that names only the flag call event. The window starts a day early, so the first- * day of a trends window over the same days is whole in every timezone.+ * day of a trends window over the same days is whole in every timezone. The count takes the modifiers of+ * `trendsQuery`, so a person filter reads the same person properties in both counts.*/function flagCallsDailyCountQuery(filters: PropertyGroupFilter,- filterTestAccounts: boolean | undefined,+ { filterTestAccounts, modifiers }: Pick<TrendsQuery, 'filterTestAccounts' | 'modifiers'>,days: number): EventsQuery {return setLatestVersionsOnQuery({@@ -84,6 +85,7 @@ function flagCallsDailyCountQuery(filterTestAccounts,after: `-${days + 1}d`,orderBy: ['toDate(timestamp) ASC'],+ modifiers,})}@@ -167,9 +169,7 @@ export async function performTrendsQueryWithFlagCalls(performQuery(flagCallFilters ? { ...trendsQuery, properties: withoutFlagCalls(trendsQuery.properties) } : trendsQuery),- flagCallFilters- ? performQuery(flagCallsDailyCountQuery(flagCallFilters, trendsQuery.filterTestAccounts, days))- : null,+ flagCallFilters ? performQuery(flagCallsDailyCountQuery(flagCallFilters, trendsQuery, days)) : null,])const series = trends.results?.[0] as TrendResult | undefinedconst rows = (flagCalls?.results as [string, number][] | undefined) ?? nulldiff --git a/posthog/hogql_queries/events_query_runner.py b/posthog/hogql_queries/events_query_runner.pyindex d93073f4b27c..af7f711ef7ef 100644--- a/posthog/hogql_queries/events_query_runner.py+++ b/posthog/hogql_queries/events_query_runner.py@@ -20,6 +20,7 @@ from posthog.schema import (EventsQueryResponse,FilterLogicalOperator,HogQLQueryModifiers,+ PersonsOnEventsMode,PropertyGroupFilter,PropertyGroupFilterValue,PropertyOperator,@@ -130,6 +131,13 @@ FLAG_EVALUATIONS_LIST_TABLE = EventsListTable(cache_key_variant="_flag_evaluations",)+STORED_PERSON_PROPERTIES_MODES = frozenset(+ {+ PersonsOnEventsMode.PERSON_ID_NO_OVERRIDE_PROPERTIES_ON_EVENTS,+ PersonsOnEventsMode.PERSON_ID_OVERRIDE_PROPERTIES_ON_EVENTS,+ }+)+class _FieldUnderFinder(TraversingVisitor):def __init__(self, roots: frozenset[str]) -> None:@@ -466,18 +474,26 @@ class EventsQueryRunner(AnalyticsQueryRunner[EventsQueryResponse]):right=ast.Tuple(exprs=[ast.Constant(value=key) for key in flag_keys]),)+ def _adds_persons_join(self, table: EventsListTable) -> bool:+ if table is not FLAG_EVALUATIONS_LIST_TABLE:+ return False+ # Only a mode set on the query itself skips the join. Many teams default to a mode that reads stored+ # properties. The activity list sends no mode. It must keep showing each person's current properties.+ modifiers = self.query.modifiers+ return modifiers is None or modifiers.personsOnEventsMode not in STORED_PERSON_PROPERTIES_MODES+def _query_modifiers(self, table: EventsListTable) -> HogQLQueryModifiers:# add_events_list_fields_to_flag_evaluations makes `person` a persons join. A person filter therefore joins persons.# The pushdown limits that join to the persons that the page's flag calls reach.# An explicit personIdPushdown value on the team or the query still wins.- if table is FLAG_EVALUATIONS_LIST_TABLE and self.modifiers.personIdPushdown is None:+ if self._adds_persons_join(table) and self.modifiers.personIdPushdown is None:return self.modifiers.model_copy(update={"personIdPushdown": True})return self.modifiersdef _query_context(self, table: EventsListTable) -> HogQLContext:context = self.build_hogql_context()assert context.database is not None- if table is FLAG_EVALUATIONS_LIST_TABLE:+ if self._adds_persons_join(table):add_events_list_fields_to_flag_evaluations(context.database)return contextdiff --git a/posthog/hogql_queries/test/test_events_query_runner.py b/posthog/hogql_queries/test/test_events_query_runner.pyindex 82834faee314..3021df6b536c 100644--- a/posthog/hogql_queries/test/test_events_query_runner.py+++ b/posthog/hogql_queries/test/test_events_query_runner.py@@ -9,6 +9,7 @@ from posthog.test.base import (APIBaseTest,ClickhouseTestMixin,_create_event,+ _create_flag_evaluations,_create_person,also_test_with_different_timezones,flush_persons_and_events,@@ -30,6 +31,7 @@ from posthog.schema import (HogQLPropertyFilter,HogQLQueryModifiers,PersonPropertyFilter,+ PersonsOnEventsMode,PropertyGroupFilter,PropertyGroupFilterValue,PropertyOperator,@@ -67,6 +69,11 @@ FLAG_KEY_MATCHER = PropertyGroupFilterValue(HogQLPropertyFilter(key="event = '$feature_flag_called'"),],)+PERSON_EMAIL_MATCHER = PropertyGroupFilterValue(+ type=FilterLogicalOperator.AND_,+ values=[PersonPropertyFilter(key="email", value=FLAG_EVALUATIONS_EMAIL, operator=PropertyOperator.EXACT)],+)+FLAG_EVALUATIONS_PERSONS_JOIN = re.compile(r"\bAS\s+flag_evaluations__person\s+ON\b")class TestEventsQueryRunner(ClickhouseTestMixin, APIBaseTest):@@ -1386,7 +1393,7 @@ class TestEventsQueryRunner(ClickhouseTestMixin, APIBaseTest):page_queries = [query for query in queries if re.search(r"\bFROM\s+flag_evaluations\b", query)]assert len(page_queries) == 1- assert len(re.findall(r"\bAS\s+flag_evaluations__person\s+ON\b", page_queries[0])) == 1+ assert len(FLAG_EVALUATIONS_PERSONS_JOIN.findall(page_queries[0])) == 1assert isinstance(response, CachedEventsQueryResponse)assert f"in(flag_key, tuple('{FLAG_EVALUATIONS_FLAG_KEY}'))" in response.hogqlexpect_pushdown = person_id_pushdown is not False@@ -1524,6 +1531,28 @@ class TestEventsQueryRunner(ClickhouseTestMixin, APIBaseTest):),# A destination with no event or action matcher sends an empty OR, which matches every flag call.("no_matcher", [], [("2020-01-10", 2), ("2020-01-11", 2)], False),+ (+ "a_person_filter_with_a_query_mode_that_reads_stored_properties",+ [PERSON_EMAIL_MATCHER],+ [("2020-01-11", 1)],+ False,+ PersonsOnEventsMode.PERSON_ID_NO_OVERRIDE_PROPERTIES_ON_EVENTS,+ ),+ (+ "a_person_filter_with_the_other_query_mode_that_reads_stored_properties",+ [PERSON_EMAIL_MATCHER],+ [("2020-01-11", 1)],+ False,+ PersonsOnEventsMode.PERSON_ID_OVERRIDE_PROPERTIES_ON_EVENTS,+ ),+ (+ "a_person_filter_with_no_query_mode_on_a_team_that_defaults_to_stored_properties",+ [PERSON_EMAIL_MATCHER],+ [("2020-01-10", 1)],+ False,+ None,+ True,+ ),])def test_flag_evaluations_only_organization_counts_matched_flag_calls_per_day(@@ -1532,22 +1561,43 @@ class TestEventsQueryRunner(ClickhouseTestMixin, APIBaseTest):matchers: list[PropertyGroupFilterValue],expected: list[tuple[str, int]],reads_flag_key: bool,+ query_mode: PersonsOnEventsMode | None = None,+ joins_persons: bool = False,):self._set_flag_evaluations_mode(FlagEvaluationsMode.FLAG_EVALUATIONS_ONLY)+ self.team.modifiers = {"personsOnEventsMode": PersonsOnEventsMode.PERSON_ID_NO_OVERRIDE_PROPERTIES_ON_EVENTS}+ self.team.save()self._create_events(data=[("events-user", FLAG_CALL_TIMESTAMP.isoformat(), {"$feature_flag": FLAG_EVALUATIONS_FLAG_KEY})],event="$feature_flag_called",)+ stored_match = _create_person(+ team_id=self.team.pk, distinct_ids=["stored-match-user"], properties={"email": "[email protected]"}+ )+ current_match = _create_person(+ team_id=self.team.pk, distinct_ids=["current-match-user"], properties={"email": FLAG_EVALUATIONS_EMAIL}+ )flush_persons_and_events()- for timestamp, flag_key in [- (FLAG_CALL_TIMESTAMP - timedelta(days=1), FLAG_EVALUATIONS_FLAG_KEY),- (FLAG_CALL_TIMESTAMP - timedelta(days=1), FLAG_EVALUATIONS_FLAG_KEY),- (FLAG_CALL_TIMESTAMP, FLAG_EVALUATIONS_FLAG_KEY),- (FLAG_CALL_TIMESTAMP, "another-flag"),- ]:- self._insert_flag_evaluation(- FLAG_EVALUATIONS_DISTINCT_ID, uuid.uuid4(), timestamp=timestamp, properties={"$feature_flag": flag_key}+ for timestamp in [FLAG_CALL_TIMESTAMP - timedelta(days=1), FLAG_CALL_TIMESTAMP]:+ _create_flag_evaluations(+ self.team.pk, FLAG_EVALUATIONS_FLAG_KEY, timestamp=timestamp, distinct_id=FLAG_EVALUATIONS_DISTINCT_ID)+ _create_flag_evaluations(+ self.team.pk,+ "another-flag",+ timestamp=FLAG_CALL_TIMESTAMP,+ distinct_id="stored-match-user",+ person_id=stored_match.uuid,+ person_properties={"email": FLAG_EVALUATIONS_EMAIL},+ )+ _create_flag_evaluations(+ self.team.pk,+ FLAG_EVALUATIONS_FLAG_KEY,+ timestamp=FLAG_CALL_TIMESTAMP - timedelta(days=1),+ distinct_id="current-match-user",+ person_id=current_match.uuid,+ person_properties={"email": "[email protected]"},+ )# The shape a destination's volume estimate sends: one OR branch per matcher, ANDed with the global filters.matcher = PropertyGroupFilter(type=FilterLogicalOperator.AND_,@@ -1562,12 +1612,17 @@ class TestEventsQueryRunner(ClickhouseTestMixin, APIBaseTest):fixedProperties=[matcher],after="-8d",orderBy=["toDate(timestamp) ASC"],+ modifiers=HogQLQueryModifiers(personsOnEventsMode=query_mode),)- response = EventsQueryRunner(query=query, team=self.team).run()+ with self.capture_select_queries() as queries:+ response = EventsQueryRunner(query=query, team=self.team).run()assert isinstance(response, CachedEventsQueryResponse)assert [(str(day), count) for day, count in response.results] == expectedassert (f"in(flag_key, tuple('{FLAG_EVALUATIONS_FLAG_KEY}'))" in (response.hogql or "")) is reads_flag_key+ page_queries = [sql for sql in queries if re.search(r"\bFROM\s+flag_evaluations\b", sql)]+ assert len(page_queries) == 1+ assert bool(FLAG_EVALUATIONS_PERSONS_JOIN.search(page_queries[0])) is joins_persons@parameterized.expand([("range_older_than_retention", "-180d"), ("all_time", "all")])def test_flag_evaluations_list_stops_at_the_retention_window(self, _name: str, after: str):diff --git a/products/workflows/frontend/Workflows/hogflows/steps/triggerVolumeLogic.test.ts b/products/workflows/frontend/Workflows/hogflows/steps/triggerVolumeLogic.test.tsindex ca07f64a4be2..de67fe53fa51 100644--- a/products/workflows/frontend/Workflows/hogflows/steps/triggerVolumeLogic.test.ts+++ b/products/workflows/frontend/Workflows/hogflows/steps/triggerVolumeLogic.test.ts@@ -61,12 +61,15 @@ describe('triggerVolumeLogic', () => {await expectLogic(logic).toDispatchActions(['loadVolumeSuccess'])expect(logic.values.volume).toMatchObject({ total, peakPerDay })- const trendsQuery = (performQuery as jest.Mock).mock.calls- .map(([query]) => query)- .find((query) => query.kind === NodeKind.TrendsQuery)+ const queries = (performQuery as jest.Mock).mock.calls.map(([query]) => query)+ const trendsQuery = queries.find((query) => query.kind === NodeKind.TrendsQuery)const trendsProperties = JSON.stringify(trendsQuery.properties)expect(trendsProperties).toContain(JSON.stringify(matchingFiltersToPropertyGroup(filters)))expect(trendsProperties.includes("event != '$feature_flag_called'")).toBe(excludesFlagCalls)+ const flagCallsQuery = queries.find((query) => query.kind === NodeKind.EventsQuery)+ expect(flagCallsQuery?.modifiers?.personsOnEventsMode).toBe(+ excludesFlagCalls ? 'person_id_no_override_properties_on_events' : undefined+ )})})