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: funnel trend view recording button (#57495)

Christian Rafferty7206a52a793a
diff --git a/posthog/hogql_queries/insights/funnels/funnel_trends.py b/posthog/hogql_queries/insights/funnels/funnel_trends.py
index 114aaa8bbeac..3a51d80e7688 100644
--- a/posthog/hogql_queries/insights/funnels/funnel_trends.py
+++ b/posthog/hogql_queries/insights/funnels/funnel_trends.py
@@ -94,12 +94,25 @@ class FunnelTrendsUDF(FunnelUDFMixin, FunnelBase):
return "any(person_id) as person_id,"
return ""
- def matched_event_select(self):
+ def matched_event_select(self, steps: str, exclusions: str):
if self._include_matched_events():
- return """
- groupArray(tuple(timestamp, uuid, $session_id, $window_id)) as user_events,
+ # Pick the latest funnel-step event with a $session_id at-or-before the
+ # converting event's timestamp so mixed client/server funnels still surface
+ # a recording when the converting step is server-side.
+ return f"""
+ groupArray(tuple(
+ timestamp,
+ uuid,
+ $session_id,
+ $window_id,
+ arrayFilter((x) -> x != 0, [{steps}{exclusions}])
+ )) as user_events,
mapFromArrays(arrayMap(x -> x.2, user_events), user_events) as user_events_map,
- [user_events_map[af_tuple.4]] as matching_events,
+ arrayFilter(
+ e -> e.3 != '' AND length(e.5) > 0 AND e.1 <= user_events_map[af_tuple.4].1,
+ arraySort(e -> e.1, user_events)
+ ) as session_events,
+ [if(length(session_events) > 0, session_events[-1], user_events_map[af_tuple.4])] as matching_events,
"""
return ""
@@ -183,7 +196,7 @@ class FunnelTrendsUDF(FunnelUDFMixin, FunnelBase):
toTimeZone(toDateTime(_toUInt64(af_tuple.1)), '{self.context.team.timezone}') as entrance_period_start,
af_tuple.2 as success_bool,
af_tuple.3 as breakdown,
- {self.matched_event_select()}
+ {self.matched_event_select(steps, exclusions)}
{self._person_id_select()}
aggregation_target as aggregation_target
FROM {{inner_event_query}}
diff --git a/posthog/hogql_queries/insights/funnels/test/__snapshots__/test_funnel_trends_actors.ambr b/posthog/hogql_queries/insights/funnels/test/__snapshots__/test_funnel_trends_actors.ambr
index e298dbc40b63..3fed2c592143 100644
--- a/posthog/hogql_queries/insights/funnels/test/__snapshots__/test_funnel_trends_actors.ambr
+++ b/posthog/hogql_queries/insights/funnels/test/__snapshots__/test_funnel_trends_actors.ambr
@@ -160,6 +160,87 @@
use_hive_partitioning=0
'''
# ---
+# name: TestFunnelTrendsActors.test_funnel_trend_persons_returns_recording_when_converting_step_has_no_session_id
+ '''
+ SELECT source.id,
+ source.id AS id,
+ source.matching_events AS matching_events
+ FROM
+ (SELECT aggregation_target AS actor_id,
+ matching_events AS matching_events,
+ actor_id AS id
+ FROM
+ (SELECT arraySort(t -> t.1, groupArray(tuple(accurateCastOrNull(timestamp, 'Float64'), toUInt64(toDateTime(toStartOfDay(timestamp), 'UTC')), uuid, '', arrayFilter(x -> ifNull(notEquals(x, 0), 1), [multiply(1, step_0), multiply(2, step_1), multiply(3, step_2)])))) AS events_array,
+ [''] AS prop,
+ arrayJoin(aggregate_funnel_trends(1, 2, 3, 1209600, 'first_touch', 'ordered', prop, events_array)) AS af_tuple,
+ toTimeZone(toDateTime(toUInt64(af_tuple.1), 'UTC'), 'UTC') AS entrance_period_start,
+ af_tuple.2 AS success_bool,
+ af_tuple.3 AS breakdown,
+ groupArray(tuple(timestamp, uuid, `$session_id`, `$window_id`, arrayFilter(x -> ifNull(notEquals(x, 0), 1), [multiply(1, step_0), multiply(2, step_1), multiply(3, step_2)]))) AS user_events,
+ mapFromArrays(arrayMap(x -> x.2, user_events), user_events) AS user_events_map,
+ arrayFilter(e -> and(ifNull(notEquals(e.3, ''), 1), ifNull(greater(length(e.5), 0), 0), ifNull(lessOrEquals(e.1, (user_events_map[af_tuple.4]).1), 0)), arraySort(e -> e.1, user_events)) AS session_events,
+ [if(ifNull(greater(length(session_events), 0), 0), session_events[-1], user_events_map[af_tuple.4])] AS matching_events,
+ aggregation_target AS aggregation_target
+ FROM
+ (SELECT toTimeZone(e.timestamp, 'UTC') AS timestamp,
+ if(not(empty(e__override.distinct_id)), e__override.person_id, e.person_id) AS aggregation_target,
+ e.uuid AS uuid,
+ e.`$session_id` AS `$session_id`,
+ e.`$window_id` AS `$window_id`,
+ if(equals(e.event, 'step one'), 1, 0) AS step_0,
+ if(equals(e.event, 'step two'), 1, 0) AS step_1,
+ if(equals(e.event, 'step three'), 1, 0) AS step_2
+ FROM events AS e
+ LEFT OUTER JOIN
+ (SELECT tupleElement(argMax(tuple(person_distinct_id_overrides.person_id), person_distinct_id_overrides.version), 1) AS person_id,
+ person_distinct_id_overrides.distinct_id AS distinct_id
+ FROM person_distinct_id_overrides
+ WHERE equals(person_distinct_id_overrides.team_id, 99999)
+ GROUP BY person_distinct_id_overrides.distinct_id
+ HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id_overrides.is_deleted), person_distinct_id_overrides.version), 1), 0), 0) SETTINGS optimize_aggregation_in_order=1) AS e__override ON equals(e.distinct_id, e__override.distinct_id)
+ WHERE and(equals(e.team_id, 99999), and(and(greaterOrEquals(e.timestamp, toDateTime64('today', 6, 'UTC')), lessOrEquals(e.timestamp, toDateTime64('explicit_redacted_timestamp', 6, 'UTC'))), in(e.event, tuple('step one', 'step three', 'step two'))), or(ifNull(equals(step_0, 1), 0), ifNull(equals(step_1, 1), 0), ifNull(equals(step_2, 1), 0))))
+ GROUP BY aggregation_target SETTINGS date_time_output_format='iso',
+ date_time_input_format='best_effort')
+ WHERE and(ifNull(equals(success_bool, 1), 0), ifNull(equals(entrance_period_start, toDateTime64('today', 6, 'UTC')), 0))
+ ORDER BY aggregation_target ASC SETTINGS join_algorithm='auto') AS source
+ ORDER BY source.id ASC
+ LIMIT 101
+ OFFSET 0 SETTINGS optimize_aggregation_in_order=1,
+ join_algorithm='auto',
+ readonly=2,
+ max_execution_time=60,
+ allow_experimental_object_type=1,
+ max_ast_elements=4000000,
+ max_expanded_ast_elements=4000000,
+ max_bytes_before_external_group_by=0,
+ transform_null_in=1,
+ optimize_min_equality_disjunction_chain_length=4294967295,
+ allow_experimental_join_condition=1,
+ use_hive_partitioning=0
+ '''
+# ---
+# name: TestFunnelTrendsActors.test_funnel_trend_persons_returns_recording_when_converting_step_has_no_session_id.1
+ '''
+ SELECT session_replay_events.session_id AS session_id,
+ min(toTimeZone(session_replay_events.min_first_timestamp, 'UTC')) AS start_time,
+ max(session_replay_events.retention_period_days) AS retention_period_days,
+ plus(dateTrunc('DAY', start_time), toIntervalDay(coalesce(retention_period_days, 30))) AS expiry_time
+ FROM session_replay_events
+ WHERE and(equals(session_replay_events.team_id, 99999), in(session_replay_events.session_id, ['s1a']))
+ GROUP BY session_replay_events.session_id
+ HAVING and(ifNull(greaterOrEquals(expiry_time, toDateTime64('today', 6, 'UTC')), 0), ifNull(equals(max(session_replay_events.is_deleted), 0), 0))
+ LIMIT 100 SETTINGS readonly=2,
+ max_execution_time=60,
+ allow_experimental_object_type=1,
+ max_ast_elements=4000000,
+ max_expanded_ast_elements=4000000,
+ max_bytes_before_external_group_by=0,
+ transform_null_in=1,
+ optimize_min_equality_disjunction_chain_length=4294967295,
+ allow_experimental_join_condition=1,
+ use_hive_partitioning=0
+ '''
+# ---
# name: TestFunnelTrendsActors.test_funnel_trend_persons_returns_recordings
'''
SELECT source.id,
@@ -176,9 +257,10 @@
toTimeZone(toDateTime(toUInt64(af_tuple.1), 'UTC'), 'UTC') AS entrance_period_start,
af_tuple.2 AS success_bool,
af_tuple.3 AS breakdown,
- groupArray(tuple(timestamp, uuid, `$session_id`, `$window_id`)) AS user_events,
+ groupArray(tuple(timestamp, uuid, `$session_id`, `$window_id`, arrayFilter(x -> ifNull(notEquals(x, 0), 1), [multiply(1, step_0), multiply(2, step_1), multiply(3, step_2)]))) AS user_events,
mapFromArrays(arrayMap(x -> x.2, user_events), user_events) AS user_events_map,
- [user_events_map[af_tuple.4]] AS matching_events,
+ arrayFilter(e -> and(ifNull(notEquals(e.3, ''), 1), ifNull(greater(length(e.5), 0), 0), ifNull(lessOrEquals(e.1, (user_events_map[af_tuple.4]).1), 0)), arraySort(e -> e.1, user_events)) AS session_events,
+ [if(ifNull(greater(length(session_events), 0), 0), session_events[-1], user_events_map[af_tuple.4])] AS matching_events,
aggregation_target AS aggregation_target
FROM
(SELECT toTimeZone(e.timestamp, 'UTC') AS timestamp,
@@ -256,9 +338,10 @@
toTimeZone(toDateTime(toUInt64(af_tuple.1), 'UTC'), 'UTC') AS entrance_period_start,
af_tuple.2 AS success_bool,
af_tuple.3 AS breakdown,
- groupArray(tuple(timestamp, uuid, `$session_id`, `$window_id`)) AS user_events,
+ groupArray(tuple(timestamp, uuid, `$session_id`, `$window_id`, arrayFilter(x -> ifNull(notEquals(x, 0), 1), [multiply(1, step_0), multiply(2, step_1), multiply(3, step_2)]))) AS user_events,
mapFromArrays(arrayMap(x -> x.2, user_events), user_events) AS user_events_map,
- [user_events_map[af_tuple.4]] AS matching_events,
+ arrayFilter(e -> and(ifNull(notEquals(e.3, ''), 1), ifNull(greater(length(e.5), 0), 0), ifNull(lessOrEquals(e.1, (user_events_map[af_tuple.4]).1), 0)), arraySort(e -> e.1, user_events)) AS session_events,
+ [if(ifNull(greater(length(session_events), 0), 0), session_events[-1], user_events_map[af_tuple.4])] AS matching_events,
aggregation_target AS aggregation_target
FROM
(SELECT toTimeZone(e.timestamp, 'UTC') AS timestamp,
@@ -336,9 +419,10 @@
toTimeZone(toDateTime(toUInt64(af_tuple.1), 'UTC'), 'UTC') AS entrance_period_start,
af_tuple.2 AS success_bool,
af_tuple.3 AS breakdown,
- groupArray(tuple(timestamp, uuid, `$session_id`, `$window_id`)) AS user_events,
+ groupArray(tuple(timestamp, uuid, `$session_id`, `$window_id`, arrayFilter(x -> ifNull(notEquals(x, 0), 1), [multiply(1, step_0), multiply(2, step_1), multiply(3, step_2)]))) AS user_events,
mapFromArrays(arrayMap(x -> x.2, user_events), user_events) AS user_events_map,
- [user_events_map[af_tuple.4]] AS matching_events,
+ arrayFilter(e -> and(ifNull(notEquals(e.3, ''), 1), ifNull(greater(length(e.5), 0), 0), ifNull(lessOrEquals(e.1, (user_events_map[af_tuple.4]).1), 0)), arraySort(e -> e.1, user_events)) AS session_events,
+ [if(ifNull(greater(length(session_events), 0), 0), session_events[-1], user_events_map[af_tuple.4])] AS matching_events,
aggregation_target AS aggregation_target
FROM
(SELECT toTimeZone(e.timestamp, 'UTC') AS timestamp,
diff --git a/posthog/hogql_queries/insights/funnels/test/test_funnel_trends_actors.py b/posthog/hogql_queries/insights/funnels/test/test_funnel_trends_actors.py
index b961c04eef2d..c7ce6442057b 100644
--- a/posthog/hogql_queries/insights/funnels/test/test_funnel_trends_actors.py
+++ b/posthog/hogql_queries/insights/funnels/test/test_funnel_trends_actors.py
@@ -86,6 +86,57 @@ class TestFunnelTrendsActors(ClickhouseTestMixin, APIBaseTest):
["s1b"],
)
+ @snapshot_clickhouse_queries
+ def test_funnel_trend_persons_returns_recording_when_converting_step_has_no_session_id(self):
+ # Converting step is captured server-side (no $session_id) while earlier client-side
+ # steps in the same funnel run produced a recording. We expect the recording from the
+ # latest funnel-step event with a $session_id at-or-before the converting event.
+ persons = journeys_for(
+ {
+ "user_one": [
+ {
+ "event": "step one",
+ "timestamp": datetime(2021, 5, 1),
+ "properties": {"$session_id": "s1a"},
+ },
+ {
+ "event": "step two",
+ "timestamp": datetime(2021, 5, 2),
+ },
+ {
+ "event": "step three",
+ "timestamp": datetime(2021, 5, 3),
+ },
+ ]
+ },
+ self.team,
+ )
+ timestamp = datetime(2021, 5, 1)
+ produce_replay_summary(
+ team_id=self.team.pk,
+ session_id="s1a",
+ distinct_id="user_one",
+ first_timestamp=timestamp,
+ last_timestamp=timestamp,
+ )
+
+ assert funnels_query.funnelsFilter
+ results = get_actors(
+ funnels_query.model_copy(
+ update={"funnelsFilter": funnels_query.funnelsFilter.model_copy(update={"funnelToStep": 1})}
+ ),
+ self.team,
+ funnel_trends_drop_off=False,
+ funnel_trends_entrance_period_start="2021-05-01 00:00:00",
+ include_recordings=True,
+ )
+
+ self.assertEqual(results[0][0], persons["user_one"].uuid)
+ self.assertEqual(
+ [next(iter(results[0][2]))["session_id"]],
+ ["s1a"],
+ )
+
@snapshot_clickhouse_queries
def test_funnel_trend_persons_with_no_to_step(self):
persons = journeys_for(