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(phai): normalize time-to-convert bins to percentages (#117471)

posthog[bot]eab1387e624b
diff --git a/ee/hogai/context/insight/format/funnel.py b/ee/hogai/context/insight/format/funnel.py
index 80c163318fe3..b5afc84bb48b 100644
--- a/ee/hogai/context/insight/format/funnel.py
+++ b/ee/hogai/context/insight/format/funnel.py
@@ -134,8 +134,10 @@ class FunnelResultsFormatter:
matrix: list[list[Any]] = [
["Average time to convert", aggregation.distribution_label],
]
- for series in results.get("bins") or []:
- matrix.append([format_duration(series[0]), format_percentage(series[1])])
+ bins = results.get("bins") or []
+ total = sum(count for _, count in bins)
+ for bin_from_seconds, count in bins:
+ matrix.append([format_duration(bin_from_seconds), format_percentage(count / total if total else 0)])
hint = f"The {aggregation.distribution_label.lower()} is the percentage of {aggregation.unit} who completed the funnel in the given period."
return f"{self._format_time_range()}\n\nEvents: {self._format_filter_series_label()}\n{format_matrix(matrix)}\n\n{hint}"
diff --git a/ee/hogai/context/insight/format/test/test_funnel.py b/ee/hogai/context/insight/format/test/test_funnel.py
index 02df85fc16ff..e132446543e9 100644
--- a/ee/hogai/context/insight/format/test/test_funnel.py
+++ b/ee/hogai/context/insight/format/test/test_funnel.py
@@ -319,12 +319,21 @@ class TestFunnelResultsFormatter(BaseTest):
dateRange=AssistantDateRange(date_from="2025-01-20", date_to="2025-01-22"),
funnelsFilter=AssistantFunnelsFilter(funnelVizType=FunnelVizType.TIME_TO_CONVERT),
)
- results = {"average_conversion_time": 600, "bins": [[600, 1], [601, 0]]}
+ results = {"average_conversion_time": 600, "bins": [[600, 15], [601, 0], [602, 5]]}
self.assertEqual(
FunnelResultsFormatter(query, results, self.team, datetime.now()).format(),
- "Date range: 2025-01-20 00:00:00 to 2025-01-22 23:59:59 (UTC)\n\nEvents: $pageview (custom) -> $ai_trace\nAverage time to convert|User distribution\n10m|100%\n10m 1s|0%\n\nThe user distribution is the percentage of users who completed the funnel in the given period.",
+ "Date range: 2025-01-20 00:00:00 to 2025-01-22 23:59:59 (UTC)\n\nEvents: $pageview (custom) -> $ai_trace\nAverage time to convert|User distribution\n10m|75%\n10m 1s|0%\n10m 2s|25%\n\nThe user distribution is the percentage of users who completed the funnel in the given period.",
)
+ def test_funnels_time_to_convert_with_only_empty_bins(self):
+ query = AssistantFunnelsQuery(
+ series=[AssistantFunnelsEventsNode(event="$pageview"), AssistantFunnelsEventsNode(event="signup")],
+ funnelsFilter=AssistantFunnelsFilter(funnelVizType=FunnelVizType.TIME_TO_CONVERT),
+ )
+ results = {"average_conversion_time": None, "bins": [[600, 0], [601, 0]]}
+ output = FunnelResultsFormatter(query, results, self.team, datetime.now()).format()
+ self.assertIn("Average time to convert|User distribution\n10m|0%\n10m 1s|0%", output)
+
def test_funnel_trends(self):
results = [
{