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(funnels): keep the all users cohort in funnel trends breakdowns (#96425)

posthog[bot]1dfa5015cf82
diff --git a/products/product_analytics/backend/hogql_queries/funnels/funnel_trends.py b/products/product_analytics/backend/hogql_queries/funnels/funnel_trends.py
index 46784dd55b8b..6d4ef9871bcc 100644
--- a/products/product_analytics/backend/hogql_queries/funnels/funnel_trends.py
+++ b/products/product_analytics/backend/hogql_queries/funnels/funnel_trends.py
@@ -10,7 +10,7 @@ from posthog.hogql import ast
from posthog.hogql.constants import HogQLQuerySettings
from posthog.hogql.parser import parse_expr, parse_select
-from posthog.hogql_queries.utils.breakdowns import NOT_IN_COHORT_ID
+from posthog.hogql_queries.utils.breakdowns import ALL_USERS_COHORT_ID, NOT_IN_COHORT_ID
from posthog.hogql_queries.utils.query_date_range import QueryDateRange
from posthog.hogql_queries.utils.sampling import correct_result_for_sampling
from posthog.hogql_queries.utils.timestamp_utils import format_label_date, get_earliest_timestamp_unfiltered
@@ -387,7 +387,10 @@ class FunnelTrendsUDF(FunnelUDFMixin, FunnelBase):
if breakdown_clause:
breakdown_value = period_row[-1]
- if breakdown_value in (None, [None], 0):
+ is_all_users_cohort = (
+ self.context.breakdownType == BreakdownType.COHORT and breakdown_value == ALL_USERS_COHORT_ID
+ )
+ if not is_all_users_cohort and breakdown_value in (None, [None], 0):
serialized_result.update({"breakdown_value": ["None"]})
elif isinstance(breakdown_value, str) or (
isinstance(breakdown_value, list) and all(isinstance(item, str) for item in breakdown_value)
diff --git a/products/product_analytics/backend/hogql_queries/funnels/test/test_funnel_trends.py b/products/product_analytics/backend/hogql_queries/funnels/test/test_funnel_trends.py
index 98b6fc29a582..a86208607e33 100644
--- a/products/product_analytics/backend/hogql_queries/funnels/test/test_funnel_trends.py
+++ b/products/product_analytics/backend/hogql_queries/funnels/test/test_funnel_trends.py
@@ -1830,6 +1830,57 @@ class TestFunnelTrendsUDF(ClickhouseTestMixin, APIBaseTest):
# user_three is not in the cohort and converts on day 3
assert not_in_cohort_result["data"] == [0.0, 0.0, 100.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
+ def test_funnel_trend_cohort_breakdown_with_all_users(self):
+ _create_person(distinct_ids=["user_one"], team=self.team, properties={"key": "value"})
+ _create_person(distinct_ids=["user_two"], team=self.team, properties={"$browser": "Safari"})
+
+ journeys_for(
+ {
+ "user_one": [
+ {"event": "step one", "timestamp": datetime(2021, 5, 1)},
+ {"event": "step two", "timestamp": datetime(2021, 5, 2)},
+ ],
+ "user_two": [
+ {"event": "step one", "timestamp": datetime(2021, 5, 1)},
+ {"event": "step two", "timestamp": datetime(2021, 5, 2)},
+ ],
+ },
+ self.team,
+ )
+
+ cohort = Cohort.objects.create(
+ team=self.team,
+ name="test_cohort",
+ groups=[{"properties": [{"key": "key", "value": "value", "type": "person"}]}],
+ )
+ cohort.calculate_people_ch(pending_version=0)
+
+ query = FunnelsQuery(
+ dateRange=DateRange(
+ date_from="2021-05-01 00:00:00",
+ date_to="2021-05-07 23:59:59",
+ ),
+ interval="day",
+ series=[
+ EventsNode(event="step one"),
+ EventsNode(event="step two"),
+ ],
+ breakdownFilter=BreakdownFilter(
+ breakdown=[cohort.pk, "all"],
+ breakdown_type="cohort",
+ ),
+ funnelsFilter=FunnelsFilter(
+ funnelVizType="trends",
+ funnelWindowInterval=7,
+ funnelWindowIntervalUnit="day",
+ ),
+ )
+
+ results = FunnelsQueryRunner(query=query, team=self.team).calculate().results
+
+ breakdown_values = {r["breakdown_value"] for r in results}
+ assert breakdown_values == {"test_cohort", "all users"}
+
def test_funnel_trend_cohort_breakdown_empty_not_in_cohort(self):
_create_person(distinct_ids=["user_one"], team=self.team, properties={"key": "value"})
_create_person(distinct_ids=["user_two"], team=self.team, properties={"key": "value"})