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(slack_app): invite a follow-up under link unfurls (#106485)

Vojta Bartoš3d5231e036a9
diff --git a/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_attribution_paths_query_runner.new_events_schema.ambr b/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_attribution_paths_query_runner.new_events_schema.ambr
index 7ad05023e1bf..b124854ac9bb 100644
--- a/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_attribution_paths_query_runner.new_events_schema.ambr
+++ b/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_attribution_paths_query_runner.new_events_schema.ambr
@@ -45,7 +45,7 @@
# name: TestMarketingAnalyticsAttributionPathsQueryRunner.test_attribution_paths_sql_1_source[new_events_schema]
'''
WITH person_arrays AS (
- SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS person_id, arraySlice(arraySort(groupArrayIf(tuple(toUnixTimestamp(toTimeZone(events.timestamp, %(hogql_val_19)s)), coalesce(accurateCastOrNull(events.properties.revenue, 'Float64'), 0.0)), and(equals(events.event, %(hogql_val_20)s), greaterOrEquals(toTimeZone(events.timestamp, %(hogql_val_21)s), toDateTime(%(hogql_val_22)s, %(hogql_val_23)s)), lessOrEquals(toTimeZone(events.timestamp, %(hogql_val_24)s), toDateTime64(%(hogql_val_25)s, 6, %(hogql_val_26)s))))), -500) AS conversions, countIf(and(equals(events.event, %(hogql_val_27)s), greaterOrEquals(toTimeZone(events.timestamp, %(hogql_val_28)s), toDateTime(%(hogql_val_29)s, %(hogql_val_30)s)), lessOrEquals(toTimeZone(events.timestamp, %(hogql_val_31)s), toDateTime64(%(hogql_val_32)s, 6, %(hogql_val_33)s)))) AS conversion_count, arraySlice(arraySort(groupUniqArrayIf(tuple(toUnixTimestamp(events__session.`$start_timestamp`), if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_34)s)), events__session.`$entry_utm_source`, %(hogql_val_35)s)), [%(hogql_val_36)s, %(hogql_val_37)s, %(hogql_val_38)s]), %(hogql_val_39)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_40)s)), events__session.`$entry_utm_source`, %(hogql_val_41)s)), [%(hogql_val_42)s, %(hogql_val_43)s, %(hogql_val_44)s, %(hogql_val_45)s, %(hogql_val_46)s, %(hogql_val_47)s, %(hogql_val_48)s, %(hogql_val_49)s]), %(hogql_val_50)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_51)s)), events__session.`$entry_utm_source`, %(hogql_val_52)s)), [%(hogql_val_53)s]), %(hogql_val_54)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_55)s)), events__session.`$entry_utm_source`, %(hogql_val_56)s)), [%(hogql_val_57)s, %(hogql_val_58)s, %(hogql_val_59)s, %(hogql_val_60)s, %(hogql_val_61)s, %(hogql_val_62)s, %(hogql_val_63)s, %(hogql_val_64)s, %(hogql_val_65)s]), %(hogql_val_66)s, if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_67)s)), events__session.`$entry_utm_source`, %(hogql_val_68)s)))))), and(and(equals(events.event, %(hogql_val_69)s), notEmpty(events.properties.`$session_id`), greater(toUnixTimestamp(events__session.`$start_timestamp`), 0)), greaterOrEquals(toUnixTimestamp(events__session.`$start_timestamp`), minus(conv_bounds.first_conversion, 345600)), lessOrEquals(toUnixTimestamp(events__session.`$start_timestamp`), conv_bounds.last_conversion)))), -500) AS touchpoints
+ SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS person_id, arraySlice(arraySort(groupArrayIf(tuple(toUnixTimestamp(toTimeZone(events.timestamp, %(hogql_val_19)s)), coalesce(accurateCastOrNull(events.properties.revenue, 'Float64'), 0.0)), and(equals(events.event, %(hogql_val_20)s), greaterOrEquals(toTimeZone(events.timestamp, %(hogql_val_21)s), toDateTime(%(hogql_val_22)s, %(hogql_val_23)s)), lessOrEquals(toTimeZone(events.timestamp, %(hogql_val_24)s), toDateTime64(%(hogql_val_25)s, 6, %(hogql_val_26)s))))), -500) AS conversions, countIf(and(equals(events.event, %(hogql_val_27)s), greaterOrEquals(toTimeZone(events.timestamp, %(hogql_val_28)s), toDateTime(%(hogql_val_29)s, %(hogql_val_30)s)), lessOrEquals(toTimeZone(events.timestamp, %(hogql_val_31)s), toDateTime64(%(hogql_val_32)s, 6, %(hogql_val_33)s)))) AS conversion_count, arraySlice(arraySort(groupUniqArrayIf(tuple(toUnixTimestamp(events__session.`$start_timestamp`), if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_34)s)), events__session.`$entry_utm_source`, %(hogql_val_35)s)), [%(hogql_val_36)s, %(hogql_val_37)s, %(hogql_val_38)s]), %(hogql_val_39)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_40)s)), events__session.`$entry_utm_source`, %(hogql_val_41)s)), [%(hogql_val_42)s, %(hogql_val_43)s, %(hogql_val_44)s, %(hogql_val_45)s, %(hogql_val_46)s, %(hogql_val_47)s, %(hogql_val_48)s, %(hogql_val_49)s]), %(hogql_val_50)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_51)s)), events__session.`$entry_utm_source`, %(hogql_val_52)s)), [%(hogql_val_53)s]), %(hogql_val_54)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_55)s)), events__session.`$entry_utm_source`, %(hogql_val_56)s)), [%(hogql_val_57)s, %(hogql_val_58)s, %(hogql_val_59)s, %(hogql_val_60)s, %(hogql_val_61)s, %(hogql_val_62)s, %(hogql_val_63)s, %(hogql_val_64)s, %(hogql_val_65)s]), %(hogql_val_66)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_67)s)), events__session.`$entry_utm_source`, %(hogql_val_68)s)), [%(hogql_val_69)s, %(hogql_val_70)s, %(hogql_val_71)s]), %(hogql_val_72)s, if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_73)s)), events__session.`$entry_utm_source`, %(hogql_val_74)s))))))), and(and(equals(events.event, %(hogql_val_75)s), notEmpty(events.properties.`$session_id`), greater(toUnixTimestamp(events__session.`$start_timestamp`), 0)), greaterOrEquals(toUnixTimestamp(events__session.`$start_timestamp`), minus(conv_bounds.first_conversion, 345600)), lessOrEquals(toUnixTimestamp(events__session.`$start_timestamp`), conv_bounds.last_conversion)))), -500) AS touchpoints
FROM events_json AS events LEFT JOIN (
SELECT min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_0)s)) AS `$start_timestamp`, nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_source), %(hogql_val_1)s), %(hogql_val_2)s) AS `$entry_utm_source`, raw_sessions.session_id_v7 AS session_id_v7
FROM raw_sessions
@@ -67,7 +67,7 @@
SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)
WHERE and(equals(events.team_id, 420), equals(events.event, %(hogql_val_14)s), greaterOrEquals(events.timestamp, toDateTime(%(hogql_val_15)s, %(hogql_val_16)s)), lessOrEquals(events.timestamp, toDateTime64(%(hogql_val_17)s, 6, %(hogql_val_18)s)))
GROUP BY if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id)) AS conv_bounds ON equals(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), conv_bounds.person_id)
- WHERE and(equals(events.team_id, 420), or(and(equals(events.event, %(hogql_val_70)s), greaterOrEquals(events.timestamp, toDateTime(%(hogql_val_71)s, %(hogql_val_72)s)), lessOrEquals(events.timestamp, toDateTime64(%(hogql_val_73)s, 6, %(hogql_val_74)s))), and(equals(events.event, %(hogql_val_75)s), greaterOrEquals(events.timestamp, minus(toDateTime(%(hogql_val_76)s, %(hogql_val_77)s), toIntervalSecond(345600))), lessOrEquals(events.timestamp, toDateTime64(%(hogql_val_78)s, 6, %(hogql_val_79)s)), greaterOrEquals(toUnixTimestamp(toTimeZone(events.timestamp, %(hogql_val_80)s)), minus(conv_bounds.first_conversion, 345600)))))
+ WHERE and(equals(events.team_id, 420), or(and(equals(events.event, %(hogql_val_76)s), greaterOrEquals(events.timestamp, toDateTime(%(hogql_val_77)s, %(hogql_val_78)s)), lessOrEquals(events.timestamp, toDateTime64(%(hogql_val_79)s, 6, %(hogql_val_80)s))), and(equals(events.event, %(hogql_val_81)s), greaterOrEquals(events.timestamp, minus(toDateTime(%(hogql_val_82)s, %(hogql_val_83)s), toIntervalSecond(345600))), lessOrEquals(events.timestamp, toDateTime64(%(hogql_val_84)s, 6, %(hogql_val_85)s)), greaterOrEquals(toUnixTimestamp(toTimeZone(events.timestamp, %(hogql_val_86)s)), minus(conv_bounds.first_conversion, 345600)))))
GROUP BY if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id)
HAVING greater(length(conversions), 0)), per_conversion_path AS MATERIALIZED (
SELECT i, person_arrays.conversion_count AS conversion_count, (person_arrays.conversions[i]).2 AS conv_value, arrayFilter(t -> and(ifNull(lessOrEquals(t.1, (person_arrays.conversions[i]).1), 0), ifNull(greaterOrEquals(t.1, minus((person_arrays.conversions[i]).1, 345600)), 0)), person_arrays.touchpoints) AS tps, arrayMap(t -> t.2, tps) AS path_full, length(path_full) AS orig_len, arraySlice(path_full, -10) AS path, ifNull(greater(orig_len, 10), 0) AS truncated
@@ -254,7 +254,7 @@
FROM cached_session_conversions) AS c LEFT JOIN (
SELECT d.person_id AS person_id, arraySlice(arraySort(groupUniqArray(tuple(d.session_ts, d.session_dim))), -500) AS touchpoints
FROM (
- SELECT conv.conv_person_id AS person_id, toUnixTimestamp(cached_sessions.start_timestamp) AS session_ts, if(in(lower(if(notEmpty(ifNull(cached_sessions.utm_source, %(hogql_val_39)s)), cached_sessions.utm_source, %(hogql_val_40)s)), [%(hogql_val_41)s, %(hogql_val_42)s, %(hogql_val_43)s]), %(hogql_val_44)s, if(in(lower(if(notEmpty(ifNull(cached_sessions.utm_source, %(hogql_val_45)s)), cached_sessions.utm_source, %(hogql_val_46)s)), [%(hogql_val_47)s, %(hogql_val_48)s, %(hogql_val_49)s, %(hogql_val_50)s, %(hogql_val_51)s, %(hogql_val_52)s, %(hogql_val_53)s, %(hogql_val_54)s]), %(hogql_val_55)s, if(in(lower(if(notEmpty(ifNull(cached_sessions.utm_source, %(hogql_val_56)s)), cached_sessions.utm_source, %(hogql_val_57)s)), [%(hogql_val_58)s]), %(hogql_val_59)s, if(in(lower(if(notEmpty(ifNull(cached_sessions.utm_source, %(hogql_val_60)s)), cached_sessions.utm_source, %(hogql_val_61)s)), [%(hogql_val_62)s, %(hogql_val_63)s, %(hogql_val_64)s, %(hogql_val_65)s, %(hogql_val_66)s, %(hogql_val_67)s, %(hogql_val_68)s, %(hogql_val_69)s, %(hogql_val_70)s]), %(hogql_val_71)s, if(notEmpty(ifNull(cached_sessions.utm_source, %(hogql_val_72)s)), cached_sessions.utm_source, %(hogql_val_73)s))))) AS session_dim, conv.first_conversion AS first_conversion, conv.last_conversion AS upper_bound
+ SELECT conv.conv_person_id AS person_id, toUnixTimestamp(cached_sessions.start_timestamp) AS session_ts, if(in(lower(if(notEmpty(ifNull(cached_sessions.utm_source, %(hogql_val_39)s)), cached_sessions.utm_source, %(hogql_val_40)s)), [%(hogql_val_41)s, %(hogql_val_42)s, %(hogql_val_43)s]), %(hogql_val_44)s, if(in(lower(if(notEmpty(ifNull(cached_sessions.utm_source, %(hogql_val_45)s)), cached_sessions.utm_source, %(hogql_val_46)s)), [%(hogql_val_47)s, %(hogql_val_48)s, %(hogql_val_49)s, %(hogql_val_50)s, %(hogql_val_51)s, %(hogql_val_52)s, %(hogql_val_53)s, %(hogql_val_54)s]), %(hogql_val_55)s, if(in(lower(if(notEmpty(ifNull(cached_sessions.utm_source, %(hogql_val_56)s)), cached_sessions.utm_source, %(hogql_val_57)s)), [%(hogql_val_58)s]), %(hogql_val_59)s, if(in(lower(if(notEmpty(ifNull(cached_sessions.utm_source, %(hogql_val_60)s)), cached_sessions.utm_source, %(hogql_val_61)s)), [%(hogql_val_62)s, %(hogql_val_63)s, %(hogql_val_64)s, %(hogql_val_65)s, %(hogql_val_66)s, %(hogql_val_67)s, %(hogql_val_68)s, %(hogql_val_69)s, %(hogql_val_70)s]), %(hogql_val_71)s, if(in(lower(if(notEmpty(ifNull(cached_sessions.utm_source, %(hogql_val_72)s)), cached_sessions.utm_source, %(hogql_val_73)s)), [%(hogql_val_74)s, %(hogql_val_75)s, %(hogql_val_76)s]), %(hogql_val_77)s, if(notEmpty(ifNull(cached_sessions.utm_source, %(hogql_val_78)s)), cached_sessions.utm_source, %(hogql_val_79)s)))))) AS session_dim, conv.first_conversion AS first_conversion, conv.last_conversion AS upper_bound
FROM resolved_cached_sessions AS cached_sessions INNER JOIN (
SELECT cached_session_conversions.conv_person_id AS conv_person_id, cached_session_conversions.first_conversion AS first_conversion, cached_session_conversions.last_conversion AS last_conversion
FROM cached_session_conversions) AS conv ON equals(cached_sessions.person_id, conv.conv_person_id)
diff --git a/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_attribution_table_query_runner.new_events_schema.ambr b/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_attribution_table_query_runner.new_events_schema.ambr
index feba086c4ad6..104ad0531dd3 100644
--- a/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_attribution_table_query_runner.new_events_schema.ambr
+++ b/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_attribution_table_query_runner.new_events_schema.ambr
@@ -65,7 +65,7 @@
# name: TestMarketingAnalyticsAttributionQueryRunner.test_attribution_table_sql_1_source[new_events_schema]
'''
WITH influenced_reach AS (
- SELECT if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_8)s)), events__session.`$entry_utm_source`, %(hogql_val_9)s)), [%(hogql_val_10)s, %(hogql_val_11)s, %(hogql_val_12)s]), %(hogql_val_13)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_14)s)), events__session.`$entry_utm_source`, %(hogql_val_15)s)), [%(hogql_val_16)s, %(hogql_val_17)s, %(hogql_val_18)s, %(hogql_val_19)s, %(hogql_val_20)s, %(hogql_val_21)s, %(hogql_val_22)s, %(hogql_val_23)s]), %(hogql_val_24)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_25)s)), events__session.`$entry_utm_source`, %(hogql_val_26)s)), [%(hogql_val_27)s]), %(hogql_val_28)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_29)s)), events__session.`$entry_utm_source`, %(hogql_val_30)s)), [%(hogql_val_31)s, %(hogql_val_32)s, %(hogql_val_33)s, %(hogql_val_34)s, %(hogql_val_35)s, %(hogql_val_36)s, %(hogql_val_37)s, %(hogql_val_38)s, %(hogql_val_39)s]), %(hogql_val_40)s, if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_41)s)), events__session.`$entry_utm_source`, %(hogql_val_42)s))))) AS breakdown_value, uniq(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id)) AS visitors
+ SELECT if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_8)s)), events__session.`$entry_utm_source`, %(hogql_val_9)s)), [%(hogql_val_10)s, %(hogql_val_11)s, %(hogql_val_12)s]), %(hogql_val_13)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_14)s)), events__session.`$entry_utm_source`, %(hogql_val_15)s)), [%(hogql_val_16)s, %(hogql_val_17)s, %(hogql_val_18)s, %(hogql_val_19)s, %(hogql_val_20)s, %(hogql_val_21)s, %(hogql_val_22)s, %(hogql_val_23)s]), %(hogql_val_24)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_25)s)), events__session.`$entry_utm_source`, %(hogql_val_26)s)), [%(hogql_val_27)s]), %(hogql_val_28)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_29)s)), events__session.`$entry_utm_source`, %(hogql_val_30)s)), [%(hogql_val_31)s, %(hogql_val_32)s, %(hogql_val_33)s, %(hogql_val_34)s, %(hogql_val_35)s, %(hogql_val_36)s, %(hogql_val_37)s, %(hogql_val_38)s, %(hogql_val_39)s]), %(hogql_val_40)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_41)s)), events__session.`$entry_utm_source`, %(hogql_val_42)s)), [%(hogql_val_43)s, %(hogql_val_44)s, %(hogql_val_45)s]), %(hogql_val_46)s, if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_47)s)), events__session.`$entry_utm_source`, %(hogql_val_48)s)))))) AS breakdown_value, uniq(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id)) AS visitors
FROM events_json AS events LEFT OUTER JOIN (
SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id, person_distinct_id_overrides.distinct_id AS distinct_id
FROM person_distinct_id_overrides
@@ -77,21 +77,21 @@
FROM raw_sessions
WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(minus(toDateTime(%(hogql_val_3)s, %(hogql_val_4)s), toIntervalSecond(345600)), toIntervalDay(3))), lessOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), plus(toDateTime64(%(hogql_val_5)s, 6, %(hogql_val_6)s), toIntervalDay(3))))
GROUP BY raw_sessions.session_id_v7) AS events__session ON equals(toUInt128(accurateCastOrNull(events.properties.`$session_id`, %(hogql_val_7)s)), events__session.session_id_v7)
- WHERE and(equals(events.team_id, 420), and(equals(events.event, %(hogql_val_43)s), notEmpty(events.properties.`$session_id`), greater(toUnixTimestamp(events__session.`$start_timestamp`), 0)), greaterOrEquals(events.timestamp, minus(toDateTime(%(hogql_val_44)s, %(hogql_val_45)s), toIntervalSecond(345600))), lessOrEquals(events.timestamp, toDateTime64(%(hogql_val_46)s, 6, %(hogql_val_47)s)))
+ WHERE and(equals(events.team_id, 420), and(equals(events.event, %(hogql_val_49)s), notEmpty(events.properties.`$session_id`), greater(toUnixTimestamp(events__session.`$start_timestamp`), 0)), greaterOrEquals(events.timestamp, minus(toDateTime(%(hogql_val_50)s, %(hogql_val_51)s), toIntervalSecond(345600))), lessOrEquals(events.timestamp, toDateTime64(%(hogql_val_52)s, 6, %(hogql_val_53)s)))
GROUP BY breakdown_value), person_arrays AS (
- SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS person_id, arraySlice(arraySort(groupArrayIf(tuple(toUnixTimestamp(toTimeZone(events.timestamp, %(hogql_val_67)s)), coalesce(accurateCastOrNull(events.properties.revenue, 'Float64'), 0.0)), and(equals(events.event, %(hogql_val_68)s), greaterOrEquals(toTimeZone(events.timestamp, %(hogql_val_69)s), toDateTime(%(hogql_val_70)s, %(hogql_val_71)s)), lessOrEquals(toTimeZone(events.timestamp, %(hogql_val_72)s), toDateTime64(%(hogql_val_73)s, 6, %(hogql_val_74)s))))), -500) AS conversions, countIf(and(equals(events.event, %(hogql_val_75)s), greaterOrEquals(toTimeZone(events.timestamp, %(hogql_val_76)s), toDateTime(%(hogql_val_77)s, %(hogql_val_78)s)), lessOrEquals(toTimeZone(events.timestamp, %(hogql_val_79)s), toDateTime64(%(hogql_val_80)s, 6, %(hogql_val_81)s)))) AS conversion_count, arraySlice(arraySort(groupUniqArrayIf(tuple(toUnixTimestamp(events__session.`$start_timestamp`), if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_82)s)), events__session.`$entry_utm_source`, %(hogql_val_83)s)), [%(hogql_val_84)s, %(hogql_val_85)s, %(hogql_val_86)s]), %(hogql_val_87)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_88)s)), events__session.`$entry_utm_source`, %(hogql_val_89)s)), [%(hogql_val_90)s, %(hogql_val_91)s, %(hogql_val_92)s, %(hogql_val_93)s, %(hogql_val_94)s, %(hogql_val_95)s, %(hogql_val_96)s, %(hogql_val_97)s]), %(hogql_val_98)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_99)s)), events__session.`$entry_utm_source`, %(hogql_val_100)s)), [%(hogql_val_101)s]), %(hogql_val_102)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_103)s)), events__session.`$entry_utm_source`, %(hogql_val_104)s)), [%(hogql_val_105)s, %(hogql_val_106)s, %(hogql_val_107)s, %(hogql_val_108)s, %(hogql_val_109)s, %(hogql_val_110)s, %(hogql_val_111)s, %(hogql_val_112)s, %(hogql_val_113)s]), %(hogql_val_114)s, if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_115)s)), events__session.`$entry_utm_source`, %(hogql_val_116)s)))))), and(and(equals(events.event, %(hogql_val_117)s), notEmpty(events.properties.`$session_id`), greater(toUnixTimestamp(events__session.`$start_timestamp`), 0)), greaterOrEquals(toUnixTimestamp(events__session.`$start_timestamp`), minus(conv_bounds.first_conversion, 345600)), lessOrEquals(toUnixTimestamp(events__session.`$start_timestamp`), conv_bounds.last_conversion)))), -500) AS touchpoints
+ SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS person_id, arraySlice(arraySort(groupArrayIf(tuple(toUnixTimestamp(toTimeZone(events.timestamp, %(hogql_val_73)s)), coalesce(accurateCastOrNull(events.properties.revenue, 'Float64'), 0.0)), and(equals(events.event, %(hogql_val_74)s), greaterOrEquals(toTimeZone(events.timestamp, %(hogql_val_75)s), toDateTime(%(hogql_val_76)s, %(hogql_val_77)s)), lessOrEquals(toTimeZone(events.timestamp, %(hogql_val_78)s), toDateTime64(%(hogql_val_79)s, 6, %(hogql_val_80)s))))), -500) AS conversions, countIf(and(equals(events.event, %(hogql_val_81)s), greaterOrEquals(toTimeZone(events.timestamp, %(hogql_val_82)s), toDateTime(%(hogql_val_83)s, %(hogql_val_84)s)), lessOrEquals(toTimeZone(events.timestamp, %(hogql_val_85)s), toDateTime64(%(hogql_val_86)s, 6, %(hogql_val_87)s)))) AS conversion_count, arraySlice(arraySort(groupUniqArrayIf(tuple(toUnixTimestamp(events__session.`$start_timestamp`), if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_88)s)), events__session.`$entry_utm_source`, %(hogql_val_89)s)), [%(hogql_val_90)s, %(hogql_val_91)s, %(hogql_val_92)s]), %(hogql_val_93)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_94)s)), events__session.`$entry_utm_source`, %(hogql_val_95)s)), [%(hogql_val_96)s, %(hogql_val_97)s, %(hogql_val_98)s, %(hogql_val_99)s, %(hogql_val_100)s, %(hogql_val_101)s, %(hogql_val_102)s, %(hogql_val_103)s]), %(hogql_val_104)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_105)s)), events__session.`$entry_utm_source`, %(hogql_val_106)s)), [%(hogql_val_107)s]), %(hogql_val_108)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_109)s)), events__session.`$entry_utm_source`, %(hogql_val_110)s)), [%(hogql_val_111)s, %(hogql_val_112)s, %(hogql_val_113)s, %(hogql_val_114)s, %(hogql_val_115)s, %(hogql_val_116)s, %(hogql_val_117)s, %(hogql_val_118)s, %(hogql_val_119)s]), %(hogql_val_120)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_121)s)), events__session.`$entry_utm_source`, %(hogql_val_122)s)), [%(hogql_val_123)s, %(hogql_val_124)s, %(hogql_val_125)s]), %(hogql_val_126)s, if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_127)s)), events__session.`$entry_utm_source`, %(hogql_val_128)s))))))), and(and(equals(events.event, %(hogql_val_129)s), notEmpty(events.properties.`$session_id`), greater(toUnixTimestamp(events__session.`$start_timestamp`), 0)), greaterOrEquals(toUnixTimestamp(events__session.`$start_timestamp`), minus(conv_bounds.first_conversion, 345600)), lessOrEquals(toUnixTimestamp(events__session.`$start_timestamp`), conv_bounds.last_conversion)))), -500) AS touchpoints
FROM events_json AS events LEFT JOIN (
- SELECT min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_48)s)) AS `$start_timestamp`, nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_source), %(hogql_val_49)s), %(hogql_val_50)s) AS `$entry_utm_source`, raw_sessions.session_id_v7 AS session_id_v7
+ SELECT min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_54)s)) AS `$start_timestamp`, nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_source), %(hogql_val_55)s), %(hogql_val_56)s) AS `$entry_utm_source`, raw_sessions.session_id_v7 AS session_id_v7
FROM raw_sessions
- WHERE and(equals(raw_sessions.team_id, 420), or(and(greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(toDateTime(%(hogql_val_51)s, %(hogql_val_52)s), toIntervalDay(3))), lessOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), plus(toDateTime64(%(hogql_val_53)s, 6, %(hogql_val_54)s), toIntervalDay(3)))), and(greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(minus(toDateTime(%(hogql_val_55)s, %(hogql_val_56)s), toIntervalSecond(345600)), toIntervalDay(3))), lessOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), plus(toDateTime64(%(hogql_val_57)s, 6, %(hogql_val_58)s), toIntervalDay(3))))))
- GROUP BY raw_sessions.session_id_v7) AS events__session ON equals(toUInt128(accurateCastOrNull(events.properties.`$session_id`, %(hogql_val_59)s)), events__session.session_id_v7) LEFT OUTER JOIN (
+ WHERE and(equals(raw_sessions.team_id, 420), or(and(greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(toDateTime(%(hogql_val_57)s, %(hogql_val_58)s), toIntervalDay(3))), lessOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), plus(toDateTime64(%(hogql_val_59)s, 6, %(hogql_val_60)s), toIntervalDay(3)))), and(greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(minus(toDateTime(%(hogql_val_61)s, %(hogql_val_62)s), toIntervalSecond(345600)), toIntervalDay(3))), lessOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), plus(toDateTime64(%(hogql_val_63)s, 6, %(hogql_val_64)s), toIntervalDay(3))))))
+ GROUP BY raw_sessions.session_id_v7) AS events__session ON equals(toUInt128(accurateCastOrNull(events.properties.`$session_id`, %(hogql_val_65)s)), events__session.session_id_v7) LEFT OUTER JOIN (
SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) 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, 420)
GROUP BY person_distinct_id_overrides.distinct_id
HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0)
SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id) INNER JOIN (
- SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS person_id, min(toUnixTimestamp(toTimeZone(events.timestamp, %(hogql_val_60)s))) AS first_conversion, max(toUnixTimestamp(toTimeZone(events.timestamp, %(hogql_val_61)s))) AS last_conversion
+ SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS person_id, min(toUnixTimestamp(toTimeZone(events.timestamp, %(hogql_val_66)s))) AS first_conversion, max(toUnixTimestamp(toTimeZone(events.timestamp, %(hogql_val_67)s))) AS last_conversion
FROM events_json AS events LEFT OUTER JOIN (
SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id, person_distinct_id_overrides.distinct_id AS distinct_id
FROM person_distinct_id_overrides
@@ -99,12 +99,12 @@
GROUP BY person_distinct_id_overrides.distinct_id
HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0)
SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)
- WHERE and(equals(events.team_id, 420), equals(events.event, %(hogql_val_62)s), greaterOrEquals(events.timestamp, toDateTime(%(hogql_val_63)s, %(hogql_val_64)s)), lessOrEquals(events.timestamp, toDateTime64(%(hogql_val_65)s, 6, %(hogql_val_66)s)))
+ WHERE and(equals(events.team_id, 420), equals(events.event, %(hogql_val_68)s), greaterOrEquals(events.timestamp, toDateTime(%(hogql_val_69)s, %(hogql_val_70)s)), lessOrEquals(events.timestamp, toDateTime64(%(hogql_val_71)s, 6, %(hogql_val_72)s)))
GROUP BY if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id)) AS conv_bounds ON equals(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), conv_bounds.person_id)
- WHERE and(equals(events.team_id, 420), or(and(equals(events.event, %(hogql_val_118)s), greaterOrEquals(events.timestamp, toDateTime(%(hogql_val_119)s, %(hogql_val_120)s)), lessOrEquals(events.timestamp, toDateTime64(%(hogql_val_121)s, 6, %(hogql_val_122)s))), and(equals(events.event, %(hogql_val_123)s), greaterOrEquals(events.timestamp, minus(toDateTime(%(hogql_val_124)s, %(hogql_val_125)s), toIntervalSecond(345600))), lessOrEquals(events.timestamp, toDateTime64(%(hogql_val_126)s, 6, %(hogql_val_127)s)), greaterOrEquals(toUnixTimestamp(toTimeZone(events.timestamp, %(hogql_val_128)s)), minus(conv_bounds.first_conversion, 345600)))))
+ WHERE and(equals(events.team_id, 420), or(and(equals(events.event, %(hogql_val_130)s), greaterOrEquals(events.timestamp, toDateTime(%(hogql_val_131)s, %(hogql_val_132)s)), lessOrEquals(events.timestamp, toDateTime64(%(hogql_val_133)s, 6, %(hogql_val_134)s))), and(equals(events.event, %(hogql_val_135)s), greaterOrEquals(events.timestamp, minus(toDateTime(%(hogql_val_136)s, %(hogql_val_137)s), toIntervalSecond(345600))), lessOrEquals(events.timestamp, toDateTime64(%(hogql_val_138)s, 6, %(hogql_val_139)s)), greaterOrEquals(toUnixTimestamp(toTimeZone(events.timestamp, %(hogql_val_140)s)), minus(conv_bounds.first_conversion, 345600)))))
GROUP BY if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id)
HAVING greater(length(conversions), 0)), per_conversion AS MATERIALIZED (
- SELECT person_arrays.person_id AS person_id, i, person_arrays.conversion_count AS conversion_count, (person_arrays.conversions[i]).1 AS conversion_time, (person_arrays.conversions[i]).2 AS conversion_value, arrayFilter(t -> and(ifNull(lessOrEquals(t.1, conversion_time), 0), ifNull(greaterOrEquals(t.1, minus(conversion_time, 345600)), 0)), person_arrays.touchpoints) AS tps, arrayMap(t -> t.1, tps) AS touch_ts, arrayMap(t -> t.2, tps) AS dims, arrayMap(_j -> if(ifNull(equals(_j, indexOf(touch_ts, arrayMin(touch_ts))), isNull(_j) and isNull(indexOf(touch_ts, arrayMin(touch_ts)))), 1.0, 0.0), arrayEnumerate(touch_ts)) AS w_first, arrayMap(_j -> if(ifNull(equals(_j, indexOf(touch_ts, arrayMax(touch_ts))), isNull(_j) and isNull(indexOf(touch_ts, arrayMax(touch_ts)))), 1.0, 0.0), arrayEnumerate(touch_ts)) AS w_last, arrayMap(_x -> divide(1.0, greatest(accurateCastOrNull(length(touch_ts), %(hogql_val_129)s), 1.0)), touch_ts) AS w_linear, arrayMap(w -> divide(w, greatest(arraySum(arrayMap(ts -> exp(multiply(-0.6931471805599453, divide(minus(conversion_time, ts), 86400))), touch_ts)), 1e-06)), arrayMap(ts -> exp(multiply(-0.6931471805599453, divide(minus(conversion_time, ts), 86400))), touch_ts)) AS w_decay, if(ifNull(equals(length(touch_ts), 0), 0), [], if(ifNull(equals(length(touch_ts), 1), 0), [1.0], if(ifNull(equals(length(touch_ts), 2), 0), [0.5, 0.5], arrayMap(w -> divide(w, greatest(arraySum(arrayMap(_pw -> ifNull(_pw, 0.0), arrayMap(ts -> if(ifNull(equals(ts, arrayMin(touch_ts)), isNull(ts) and isNull(arrayMin(touch_ts))), 0.4, if(ifNull(equals(ts, arrayMax(touch_ts)), isNull(ts) and isNull(arrayMax(touch_ts))), 0.4, divide(0.2, greatest(minus(accurateCastOrNull(length(touch_ts), %(hogql_val_130)s), 2.0), 1.0)))), touch_ts))), 1e-06)), arrayMap(_pw -> ifNull(_pw, 0.0), arrayMap(ts -> if(ifNull(equals(ts, arrayMin(touch_ts)), isNull(ts) and isNull(arrayMin(touch_ts))), 0.4, if(ifNull(equals(ts, arrayMax(touch_ts)), isNull(ts) and isNull(arrayMax(touch_ts))), 0.4, divide(0.2, greatest(minus(accurateCastOrNull(length(touch_ts), %(hogql_val_131)s), 2.0), 1.0)))), touch_ts)))))) AS w_position
+ SELECT person_arrays.person_id AS person_id, i, person_arrays.conversion_count AS conversion_count, (person_arrays.conversions[i]).1 AS conversion_time, (person_arrays.conversions[i]).2 AS conversion_value, arrayFilter(t -> and(ifNull(lessOrEquals(t.1, conversion_time), 0), ifNull(greaterOrEquals(t.1, minus(conversion_time, 345600)), 0)), person_arrays.touchpoints) AS tps, arrayMap(t -> t.1, tps) AS touch_ts, arrayMap(t -> t.2, tps) AS dims, arrayMap(_j -> if(ifNull(equals(_j, indexOf(touch_ts, arrayMin(touch_ts))), isNull(_j) and isNull(indexOf(touch_ts, arrayMin(touch_ts)))), 1.0, 0.0), arrayEnumerate(touch_ts)) AS w_first, arrayMap(_j -> if(ifNull(equals(_j, indexOf(touch_ts, arrayMax(touch_ts))), isNull(_j) and isNull(indexOf(touch_ts, arrayMax(touch_ts)))), 1.0, 0.0), arrayEnumerate(touch_ts)) AS w_last, arrayMap(_x -> divide(1.0, greatest(accurateCastOrNull(length(touch_ts), %(hogql_val_141)s), 1.0)), touch_ts) AS w_linear, arrayMap(w -> divide(w, greatest(arraySum(arrayMap(ts -> exp(multiply(-0.6931471805599453, divide(minus(conversion_time, ts), 86400))), touch_ts)), 1e-06)), arrayMap(ts -> exp(multiply(-0.6931471805599453, divide(minus(conversion_time, ts), 86400))), touch_ts)) AS w_decay, if(ifNull(equals(length(touch_ts), 0), 0), [], if(ifNull(equals(length(touch_ts), 1), 0), [1.0], if(ifNull(equals(length(touch_ts), 2), 0), [0.5, 0.5], arrayMap(w -> divide(w, greatest(arraySum(arrayMap(_pw -> ifNull(_pw, 0.0), arrayMap(ts -> if(ifNull(equals(ts, arrayMin(touch_ts)), isNull(ts) and isNull(arrayMin(touch_ts))), 0.4, if(ifNull(equals(ts, arrayMax(touch_ts)), isNull(ts) and isNull(arrayMax(touch_ts))), 0.4, divide(0.2, greatest(minus(accurateCastOrNull(length(touch_ts), %(hogql_val_142)s), 2.0), 1.0)))), touch_ts))), 1e-06)), arrayMap(_pw -> ifNull(_pw, 0.0), arrayMap(ts -> if(ifNull(equals(ts, arrayMin(touch_ts)), isNull(ts) and isNull(arrayMin(touch_ts))), 0.4, if(ifNull(equals(ts, arrayMax(touch_ts)), isNull(ts) and isNull(arrayMax(touch_ts))), 0.4, divide(0.2, greatest(minus(accurateCastOrNull(length(touch_ts), %(hogql_val_143)s), 2.0), 1.0)))), touch_ts)))))) AS w_position
FROM person_arrays ARRAY JOIN arrayEnumerate(person_arrays.conversions) AS i), per_touchpoint AS (
SELECT per_conversion.person_id AS person_id, per_conversion.i AS i, per_conversion.conversion_value AS conversion_value, per_conversion.dims[k] AS breakdown_value, per_conversion.w_first[k] AS w_first, per_conversion.w_last[k] AS w_last, per_conversion.w_linear[k] AS w_linear, per_conversion.w_decay[k] AS w_decay, per_conversion.w_position[k] AS w_position
FROM per_conversion ARRAY JOIN arrayEnumerate(per_conversion.touch_ts) AS k), per_conversion_dim AS (
@@ -114,7 +114,7 @@
SELECT per_conversion_dim.breakdown_value AS breakdown_value, count() AS influenced_conversions, sum(per_conversion_dim.conv_value) AS influenced_value, sum(per_conversion_dim.w_first) AS w_first_conversions, sum(multiply(per_conversion_dim.conv_value, per_conversion_dim.w_first)) AS w_first_value, sum(per_conversion_dim.w_last) AS w_last_conversions, sum(multiply(per_conversion_dim.conv_value, per_conversion_dim.w_last)) AS w_last_value, sum(per_conversion_dim.w_linear) AS w_linear_conversions, sum(multiply(per_conversion_dim.conv_value, per_conversion_dim.w_linear)) AS w_linear_value, sum(per_conversion_dim.w_decay) AS w_decay_conversions, sum(multiply(per_conversion_dim.conv_value, per_conversion_dim.w_decay)) AS w_decay_value, sum(per_conversion_dim.w_position) AS w_position_conversions, sum(multiply(per_conversion_dim.conv_value, per_conversion_dim.w_position)) AS w_position_value
FROM per_conversion_dim
GROUP BY per_conversion_dim.breakdown_value), attribution_rows AS (
- SELECT coalesce(nullIf(attribution_totals.breakdown_value, %(hogql_val_132)s), influenced_reach.breakdown_value) AS breakdown_value, ifNull(influenced_reach.visitors, 0) AS visitors, ifNull(attribution_totals.influenced_conversions, 0) AS influenced_conversions, ifNull(attribution_totals.influenced_value, 0.0) AS influenced_value, ifNull(attribution_totals.w_first_conversions, 0.0) AS w_first_conversions, ifNull(attribution_totals.w_first_value, 0.0) AS w_first_value, ifNull(attribution_totals.w_last_conversions, 0.0) AS w_last_conversions, ifNull(attribution_totals.w_last_value, 0.0) AS w_last_value, ifNull(attribution_totals.w_linear_conversions, 0.0) AS w_linear_conversions, ifNull(attribution_totals.w_linear_value, 0.0) AS w_linear_value, ifNull(attribution_totals.w_decay_conversions, 0.0) AS w_decay_conversions, ifNull(attribution_totals.w_decay_value, 0.0) AS w_decay_value, ifNull(attribution_totals.w_position_conversions, 0.0) AS w_position_conversions, ifNull(attribution_totals.w_position_value, 0.0) AS w_position_value, 1 AS footer_key
+ SELECT coalesce(nullIf(attribution_totals.breakdown_value, %(hogql_val_144)s), influenced_reach.breakdown_value) AS breakdown_value, ifNull(influenced_reach.visitors, 0) AS visitors, ifNull(attribution_totals.influenced_conversions, 0) AS influenced_conversions, ifNull(attribution_totals.influenced_value, 0.0) AS influenced_value, ifNull(attribution_totals.w_first_conversions, 0.0) AS w_first_conversions, ifNull(attribution_totals.w_first_value, 0.0) AS w_first_value, ifNull(attribution_totals.w_last_conversions, 0.0) AS w_last_conversions, ifNull(attribution_totals.w_last_value, 0.0) AS w_last_value, ifNull(attribution_totals.w_linear_conversions, 0.0) AS w_linear_conversions, ifNull(attribution_totals.w_linear_value, 0.0) AS w_linear_value, ifNull(attribution_totals.w_decay_conversions, 0.0) AS w_decay_conversions, ifNull(attribution_totals.w_decay_value, 0.0) AS w_decay_value, ifNull(attribution_totals.w_position_conversions, 0.0) AS w_position_conversions, ifNull(attribution_totals.w_position_value, 0.0) AS w_position_value, 1 AS footer_key
FROM attribution_totals FULL OUTER JOIN influenced_reach ON equals(attribution_totals.breakdown_value, influenced_reach.breakdown_value)), attribution_footer AS (
SELECT sumIf(per_conversion.conversion_count, ifNull(equals(per_conversion.i, 1), 0)) AS total_conversions, countIf(ifNull(greater(length(per_conversion.tps), 0), 0)) AS attributed_conversions, 1 AS footer_key
FROM per_conversion)
@@ -322,7 +322,7 @@
GROUP BY if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id)), influenced_reach AS (
SELECT s.breakdown_value AS breakdown_value, uniq(s.person_id) AS visitors
FROM (
- SELECT cached_sessions.person_id AS person_id, if(in(lower(if(notEmpty(ifNull(cached_sessions.utm_source, %(hogql_val_39)s)), cached_sessions.utm_source, %(hogql_val_40)s)), [%(hogql_val_41)s, %(hogql_val_42)s, %(hogql_val_43)s]), %(hogql_val_44)s, if(in(lower(if(notEmpty(ifNull(cached_sessions.utm_source, %(hogql_val_45)s)), cached_sessions.utm_source, %(hogql_val_46)s)), [%(hogql_val_47)s, %(hogql_val_48)s, %(hogql_val_49)s, %(hogql_val_50)s, %(hogql_val_51)s, %(hogql_val_52)s, %(hogql_val_53)s, %(hogql_val_54)s]), %(hogql_val_55)s, if(in(lower(if(notEmpty(ifNull(cached_sessions.utm_source, %(hogql_val_56)s)), cached_sessions.utm_source, %(hogql_val_57)s)), [%(hogql_val_58)s]), %(hogql_val_59)s, if(in(lower(if(notEmpty(ifNull(cached_sessions.utm_source, %(hogql_val_60)s)), cached_sessions.utm_source, %(hogql_val_61)s)), [%(hogql_val_62)s, %(hogql_val_63)s, %(hogql_val_64)s, %(hogql_val_65)s, %(hogql_val_66)s, %(hogql_val_67)s, %(hogql_val_68)s, %(hogql_val_69)s, %(hogql_val_70)s]), %(hogql_val_71)s, if(notEmpty(ifNull(cached_sessions.utm_source, %(hogql_val_72)s)), cached_sessions.utm_source, %(hogql_val_73)s))))) AS breakdown_value
+ SELECT cached_sessions.person_id AS person_id, if(in(lower(if(notEmpty(ifNull(cached_sessions.utm_source, %(hogql_val_39)s)), cached_sessions.utm_source, %(hogql_val_40)s)), [%(hogql_val_41)s, %(hogql_val_42)s, %(hogql_val_43)s]), %(hogql_val_44)s, if(in(lower(if(notEmpty(ifNull(cached_sessions.utm_source, %(hogql_val_45)s)), cached_sessions.utm_source, %(hogql_val_46)s)), [%(hogql_val_47)s, %(hogql_val_48)s, %(hogql_val_49)s, %(hogql_val_50)s, %(hogql_val_51)s, %(hogql_val_52)s, %(hogql_val_53)s, %(hogql_val_54)s]), %(hogql_val_55)s, if(in(lower(if(notEmpty(ifNull(cached_sessions.utm_source, %(hogql_val_56)s)), cached_sessions.utm_source, %(hogql_val_57)s)), [%(hogql_val_58)s]), %(hogql_val_59)s, if(in(lower(if(notEmpty(ifNull(cached_sessions.utm_source, %(hogql_val_60)s)), cached_sessions.utm_source, %(hogql_val_61)s)), [%(hogql_val_62)s, %(hogql_val_63)s, %(hogql_val_64)s, %(hogql_val_65)s, %(hogql_val_66)s, %(hogql_val_67)s, %(hogql_val_68)s, %(hogql_val_69)s, %(hogql_val_70)s]), %(hogql_val_71)s, if(in(lower(if(notEmpty(ifNull(cached_sessions.utm_source, %(hogql_val_72)s)), cached_sessions.utm_source, %(hogql_val_73)s)), [%(hogql_val_74)s, %(hogql_val_75)s, %(hogql_val_76)s]), %(hogql_val_77)s, if(notEmpty(ifNull(cached_sessions.utm_source, %(hogql_val_78)s)), cached_sessions.utm_source, %(hogql_val_79)s)))))) AS breakdown_value
FROM resolved_cached_sessions AS cached_sessions
WHERE and(greaterOrEquals(cached_sessions.max_event_timestamp, toDateTime64('2022-12-28 00:00:00.000000', 6, 'UTC')), lessOrEquals(cached_sessions.min_event_timestamp, toDateTime64('2023-01-31 23:59:59.999999', 6, 'UTC')))) AS s
GROUP BY s.breakdown_value), person_arrays AS (
@@ -332,7 +332,7 @@
FROM cached_session_conversions) AS c LEFT JOIN (
SELECT d.person_id AS person_id, arraySlice(arraySort(groupUniqArray(tuple(d.session_ts, d.session_dim))), -500) AS touchpoints
FROM (
- SELECT conv.conv_person_id AS person_id, toUnixTimestamp(cached_sessions.start_timestamp) AS session_ts, if(in(lower(if(notEmpty(ifNull(cached_sessions.utm_source, %(hogql_val_74)s)), cached_sessions.utm_source, %(hogql_val_75)s)), [%(hogql_val_76)s, %(hogql_val_77)s, %(hogql_val_78)s]), %(hogql_val_79)s, if(in(lower(if(notEmpty(ifNull(cached_sessions.utm_source, %(hogql_val_80)s)), cached_sessions.utm_source, %(hogql_val_81)s)), [%(hogql_val_82)s, %(hogql_val_83)s, %(hogql_val_84)s, %(hogql_val_85)s, %(hogql_val_86)s, %(hogql_val_87)s, %(hogql_val_88)s, %(hogql_val_89)s]), %(hogql_val_90)s, if(in(lower(if(notEmpty(ifNull(cached_sessions.utm_source, %(hogql_val_91)s)), cached_sessions.utm_source, %(hogql_val_92)s)), [%(hogql_val_93)s]), %(hogql_val_94)s, if(in(lower(if(notEmpty(ifNull(cached_sessions.utm_source, %(hogql_val_95)s)), cached_sessions.utm_source, %(hogql_val_96)s)), [%(hogql_val_97)s, %(hogql_val_98)s, %(hogql_val_99)s, %(hogql_val_100)s, %(hogql_val_101)s, %(hogql_val_102)s, %(hogql_val_103)s, %(hogql_val_104)s, %(hogql_val_105)s]), %(hogql_val_106)s, if(notEmpty(ifNull(cached_sessions.utm_source, %(hogql_val_107)s)), cached_sessions.utm_source, %(hogql_val_108)s))))) AS session_dim, conv.first_conversion AS first_conversion, conv.last_conversion AS upper_bound
+ SELECT conv.conv_person_id AS person_id, toUnixTimestamp(cached_sessions.start_timestamp) AS session_ts, if(in(lower(if(notEmpty(ifNull(cached_sessions.utm_source, %(hogql_val_80)s)), cached_sessions.utm_source, %(hogql_val_81)s)), [%(hogql_val_82)s, %(hogql_val_83)s, %(hogql_val_84)s]), %(hogql_val_85)s, if(in(lower(if(notEmpty(ifNull(cached_sessions.utm_source, %(hogql_val_86)s)), cached_sessions.utm_source, %(hogql_val_87)s)), [%(hogql_val_88)s, %(hogql_val_89)s, %(hogql_val_90)s, %(hogql_val_91)s, %(hogql_val_92)s, %(hogql_val_93)s, %(hogql_val_94)s, %(hogql_val_95)s]), %(hogql_val_96)s, if(in(lower(if(notEmpty(ifNull(cached_sessions.utm_source, %(hogql_val_97)s)), cached_sessions.utm_source, %(hogql_val_98)s)), [%(hogql_val_99)s]), %(hogql_val_100)s, if(in(lower(if(notEmpty(ifNull(cached_sessions.utm_source, %(hogql_val_101)s)), cached_sessions.utm_source, %(hogql_val_102)s)), [%(hogql_val_103)s, %(hogql_val_104)s, %(hogql_val_105)s, %(hogql_val_106)s, %(hogql_val_107)s, %(hogql_val_108)s, %(hogql_val_109)s, %(hogql_val_110)s, %(hogql_val_111)s]), %(hogql_val_112)s, if(in(lower(if(notEmpty(ifNull(cached_sessions.utm_source, %(hogql_val_113)s)), cached_sessions.utm_source, %(hogql_val_114)s)), [%(hogql_val_115)s, %(hogql_val_116)s, %(hogql_val_117)s]), %(hogql_val_118)s, if(notEmpty(ifNull(cached_sessions.utm_source, %(hogql_val_119)s)), cached_sessions.utm_source, %(hogql_val_120)s)))))) AS session_dim, conv.first_conversion AS first_conversion, conv.last_conversion AS upper_bound
FROM resolved_cached_sessions AS cached_sessions INNER JOIN (
SELECT cached_session_conversions.conv_person_id AS conv_person_id, cached_session_conversions.first_conversion AS first_conversion, cached_session_conversions.last_conversion AS last_conversion
FROM cached_session_conversions) AS conv ON equals(cached_sessions.person_id, conv.conv_person_id)
@@ -340,7 +340,7 @@
WHERE and(ifNull(greaterOrEquals(d.session_ts, minus(d.first_conversion, 345600)), 0), ifNull(lessOrEquals(d.session_ts, d.upper_bound), 0))
GROUP BY d.person_id) AS t ON equals(c.conv_person_id, t.person_id)
WHERE ifNull(greater(length(c.conversions), 0), 0)), per_conversion AS MATERIALIZED (
- SELECT person_arrays.person_id AS person_id, i, person_arrays.conversion_count AS conversion_count, (person_arrays.conversions[i]).1 AS conversion_time, (person_arrays.conversions[i]).2 AS conversion_value, arrayFilter(t -> and(ifNull(lessOrEquals(t.1, conversion_time), 0), ifNull(greaterOrEquals(t.1, minus(conversion_time, 345600)), 0)), person_arrays.touchpoints) AS tps, arrayMap(t -> t.1, tps) AS touch_ts, arrayMap(t -> t.2, tps) AS dims, arrayMap(_j -> if(ifNull(equals(_j, indexOf(touch_ts, arrayMin(touch_ts))), isNull(_j) and isNull(indexOf(touch_ts, arrayMin(touch_ts)))), 1.0, 0.0), arrayEnumerate(touch_ts)) AS w_first, arrayMap(_j -> if(ifNull(equals(_j, indexOf(touch_ts, arrayMax(touch_ts))), isNull(_j) and isNull(indexOf(touch_ts, arrayMax(touch_ts)))), 1.0, 0.0), arrayEnumerate(touch_ts)) AS w_last, arrayMap(_x -> divide(1.0, greatest(accurateCastOrNull(length(touch_ts), %(hogql_val_109)s), 1.0)), touch_ts) AS w_linear, arrayMap(w -> divide(w, greatest(arraySum(arrayMap(ts -> exp(multiply(-0.6931471805599453, divide(minus(conversion_time, ts), 86400))), touch_ts)), 1e-06)), arrayMap(ts -> exp(multiply(-0.6931471805599453, divide(minus(conversion_time, ts), 86400))), touch_ts)) AS w_decay, if(ifNull(equals(length(touch_ts), 0), 0), [], if(ifNull(equals(length(touch_ts), 1), 0), [1.0], if(ifNull(equals(length(touch_ts), 2), 0), [0.5, 0.5], arrayMap(w -> divide(w, greatest(arraySum(arrayMap(_pw -> ifNull(_pw, 0.0), arrayMap(ts -> if(ifNull(equals(ts, arrayMin(touch_ts)), isNull(ts) and isNull(arrayMin(touch_ts))), 0.4, if(ifNull(equals(ts, arrayMax(touch_ts)), isNull(ts) and isNull(arrayMax(touch_ts))), 0.4, divide(0.2, greatest(minus(accurateCastOrNull(length(touch_ts), %(hogql_val_110)s), 2.0), 1.0)))), touch_ts))), 1e-06)), arrayMap(_pw -> ifNull(_pw, 0.0), arrayMap(ts -> if(ifNull(equals(ts, arrayMin(touch_ts)), isNull(ts) and isNull(arrayMin(touch_ts))), 0.4, if(ifNull(equals(ts, arrayMax(touch_ts)), isNull(ts) and isNull(arrayMax(touch_ts))), 0.4, divide(0.2, greatest(minus(accurateCastOrNull(length(touch_ts), %(hogql_val_111)s), 2.0), 1.0)))), touch_ts)))))) AS w_position
+ SELECT person_arrays.person_id AS person_id, i, person_arrays.conversion_count AS conversion_count, (person_arrays.conversions[i]).1 AS conversion_time, (person_arrays.conversions[i]).2 AS conversion_value, arrayFilter(t -> and(ifNull(lessOrEquals(t.1, conversion_time), 0), ifNull(greaterOrEquals(t.1, minus(conversion_time, 345600)), 0)), person_arrays.touchpoints) AS tps, arrayMap(t -> t.1, tps) AS touch_ts, arrayMap(t -> t.2, tps) AS dims, arrayMap(_j -> if(ifNull(equals(_j, indexOf(touch_ts, arrayMin(touch_ts))), isNull(_j) and isNull(indexOf(touch_ts, arrayMin(touch_ts)))), 1.0, 0.0), arrayEnumerate(touch_ts)) AS w_first, arrayMap(_j -> if(ifNull(equals(_j, indexOf(touch_ts, arrayMax(touch_ts))), isNull(_j) and isNull(indexOf(touch_ts, arrayMax(touch_ts)))), 1.0, 0.0), arrayEnumerate(touch_ts)) AS w_last, arrayMap(_x -> divide(1.0, greatest(accurateCastOrNull(length(touch_ts), %(hogql_val_121)s), 1.0)), touch_ts) AS w_linear, arrayMap(w -> divide(w, greatest(arraySum(arrayMap(ts -> exp(multiply(-0.6931471805599453, divide(minus(conversion_time, ts), 86400))), touch_ts)), 1e-06)), arrayMap(ts -> exp(multiply(-0.6931471805599453, divide(minus(conversion_time, ts), 86400))), touch_ts)) AS w_decay, if(ifNull(equals(length(touch_ts), 0), 0), [], if(ifNull(equals(length(touch_ts), 1), 0), [1.0], if(ifNull(equals(length(touch_ts), 2), 0), [0.5, 0.5], arrayMap(w -> divide(w, greatest(arraySum(arrayMap(_pw -> ifNull(_pw, 0.0), arrayMap(ts -> if(ifNull(equals(ts, arrayMin(touch_ts)), isNull(ts) and isNull(arrayMin(touch_ts))), 0.4, if(ifNull(equals(ts, arrayMax(touch_ts)), isNull(ts) and isNull(arrayMax(touch_ts))), 0.4, divide(0.2, greatest(minus(accurateCastOrNull(length(touch_ts), %(hogql_val_122)s), 2.0), 1.0)))), touch_ts))), 1e-06)), arrayMap(_pw -> ifNull(_pw, 0.0), arrayMap(ts -> if(ifNull(equals(ts, arrayMin(touch_ts)), isNull(ts) and isNull(arrayMin(touch_ts))), 0.4, if(ifNull(equals(ts, arrayMax(touch_ts)), isNull(ts) and isNull(arrayMax(touch_ts))), 0.4, divide(0.2, greatest(minus(accurateCastOrNull(length(touch_ts), %(hogql_val_123)s), 2.0), 1.0)))), touch_ts)))))) AS w_position
FROM person_arrays ARRAY JOIN arrayEnumerate(person_arrays.conversions) AS i), per_touchpoint AS (
SELECT per_conversion.person_id AS person_id, per_conversion.i AS i, per_conversion.conversion_value AS conversion_value, per_conversion.dims[k] AS breakdown_value, per_conversion.w_first[k] AS w_first, per_conversion.w_last[k] AS w_last, per_conversion.w_linear[k] AS w_linear, per_conversion.w_decay[k] AS w_decay, per_conversion.w_position[k] AS w_position
FROM per_conversion ARRAY JOIN arrayEnumerate(per_conversion.touch_ts) AS k), per_conversion_dim AS (
@@ -350,7 +350,7 @@
SELECT per_conversion_dim.breakdown_value AS breakdown_value, count() AS influenced_conversions, sum(per_conversion_dim.conv_value) AS influenced_value, sum(per_conversion_dim.w_first) AS w_first_conversions, sum(multiply(per_conversion_dim.conv_value, per_conversion_dim.w_first)) AS w_first_value, sum(per_conversion_dim.w_last) AS w_last_conversions, sum(multiply(per_conversion_dim.conv_value, per_conversion_dim.w_last)) AS w_last_value, sum(per_conversion_dim.w_linear) AS w_linear_conversions, sum(multiply(per_conversion_dim.conv_value, per_conversion_dim.w_linear)) AS w_linear_value, sum(per_conversion_dim.w_decay) AS w_decay_conversions, sum(multiply(per_conversion_dim.conv_value, per_conversion_dim.w_decay)) AS w_decay_value, sum(per_conversion_dim.w_position) AS w_position_conversions, sum(multiply(per_conversion_dim.conv_value, per_conversion_dim.w_position)) AS w_position_value
FROM per_conversion_dim
GROUP BY per_conversion_dim.breakdown_value), attribution_rows AS (
- SELECT coalesce(nullIf(attribution_totals.breakdown_value, %(hogql_val_112)s), influenced_reach.breakdown_value) AS breakdown_value, ifNull(influenced_reach.visitors, 0) AS visitors, ifNull(attribution_totals.influenced_conversions, 0) AS influenced_conversions, ifNull(attribution_totals.influenced_value, 0.0) AS influenced_value, ifNull(attribution_totals.w_first_conversions, 0.0) AS w_first_conversions, ifNull(attribution_totals.w_first_value, 0.0) AS w_first_value, ifNull(attribution_totals.w_last_conversions, 0.0) AS w_last_conversions, ifNull(attribution_totals.w_last_value, 0.0) AS w_last_value, ifNull(attribution_totals.w_linear_conversions, 0.0) AS w_linear_conversions, ifNull(attribution_totals.w_linear_value, 0.0) AS w_linear_value, ifNull(attribution_totals.w_decay_conversions, 0.0) AS w_decay_conversions, ifNull(attribution_totals.w_decay_value, 0.0) AS w_decay_value, ifNull(attribution_totals.w_position_conversions, 0.0) AS w_position_conversions, ifNull(attribution_totals.w_position_value, 0.0) AS w_position_value, 1 AS footer_key
+ SELECT coalesce(nullIf(attribution_totals.breakdown_value, %(hogql_val_124)s), influenced_reach.breakdown_value) AS breakdown_value, ifNull(influenced_reach.visitors, 0) AS visitors, ifNull(attribution_totals.influenced_conversions, 0) AS influenced_conversions, ifNull(attribution_totals.influenced_value, 0.0) AS influenced_value, ifNull(attribution_totals.w_first_conversions, 0.0) AS w_first_conversions, ifNull(attribution_totals.w_first_value, 0.0) AS w_first_value, ifNull(attribution_totals.w_last_conversions, 0.0) AS w_last_conversions, ifNull(attribution_totals.w_last_value, 0.0) AS w_last_value, ifNull(attribution_totals.w_linear_conversions, 0.0) AS w_linear_conversions, ifNull(attribution_totals.w_linear_value, 0.0) AS w_linear_value, ifNull(attribution_totals.w_decay_conversions, 0.0) AS w_decay_conversions, ifNull(attribution_totals.w_decay_value, 0.0) AS w_decay_value, ifNull(attribution_totals.w_position_conversions, 0.0) AS w_position_conversions, ifNull(attribution_totals.w_position_value, 0.0) AS w_position_value, 1 AS footer_key
FROM attribution_totals FULL OUTER JOIN influenced_reach ON equals(attribution_totals.breakdown_value, influenced_reach.breakdown_value)), attribution_footer AS (
SELECT sumIf(per_conversion.conversion_count, ifNull(equals(per_conversion.i, 1), 0)) AS total_conversions, countIf(ifNull(greater(length(per_conversion.tps), 0), 0)) AS attributed_conversions, 1 AS footer_key
FROM per_conversion)
diff --git a/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_marketing_retention_query_runner.new_events_schema.ambr b/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_marketing_retention_query_runner.new_events_schema.ambr
index 6e171b41b55c..78ae490761c6 100644
--- a/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_marketing_retention_query_runner.new_events_schema.ambr
+++ b/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_marketing_retention_query_runner.new_events_schema.ambr
@@ -71,7 +71,7 @@
# name: TestMarketingAnalyticsRetentionQueryRunner.test_retention_sql_1_source[new_events_schema]
'''
WITH acquisition AS MATERIALIZED (
- SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS actor_id, min(events__session.`$start_timestamp`) AS cohort_at, argMin(if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_8)s)), events__session.`$entry_utm_source`, %(hogql_val_9)s)), [%(hogql_val_10)s, %(hogql_val_11)s, %(hogql_val_12)s]), %(hogql_val_13)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_14)s)), events__session.`$entry_utm_source`, %(hogql_val_15)s)), [%(hogql_val_16)s, %(hogql_val_17)s, %(hogql_val_18)s, %(hogql_val_19)s, %(hogql_val_20)s, %(hogql_val_21)s, %(hogql_val_22)s, %(hogql_val_23)s]), %(hogql_val_24)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_25)s)), events__session.`$entry_utm_source`, %(hogql_val_26)s)), [%(hogql_val_27)s]), %(hogql_val_28)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_29)s)), events__session.`$entry_utm_source`, %(hogql_val_30)s)), [%(hogql_val_31)s, %(hogql_val_32)s, %(hogql_val_33)s, %(hogql_val_34)s, %(hogql_val_35)s, %(hogql_val_36)s, %(hogql_val_37)s, %(hogql_val_38)s, %(hogql_val_39)s]), %(hogql_val_40)s, if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_41)s)), events__session.`$entry_utm_source`, %(hogql_val_42)s))))), events__session.`$start_timestamp`) AS breakdown_value
+ SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS actor_id, min(events__session.`$start_timestamp`) AS cohort_at, argMin(if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_8)s)), events__session.`$entry_utm_source`, %(hogql_val_9)s)), [%(hogql_val_10)s, %(hogql_val_11)s, %(hogql_val_12)s]), %(hogql_val_13)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_14)s)), events__session.`$entry_utm_source`, %(hogql_val_15)s)), [%(hogql_val_16)s, %(hogql_val_17)s, %(hogql_val_18)s, %(hogql_val_19)s, %(hogql_val_20)s, %(hogql_val_21)s, %(hogql_val_22)s, %(hogql_val_23)s]), %(hogql_val_24)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_25)s)), events__session.`$entry_utm_source`, %(hogql_val_26)s)), [%(hogql_val_27)s]), %(hogql_val_28)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_29)s)), events__session.`$entry_utm_source`, %(hogql_val_30)s)), [%(hogql_val_31)s, %(hogql_val_32)s, %(hogql_val_33)s, %(hogql_val_34)s, %(hogql_val_35)s, %(hogql_val_36)s, %(hogql_val_37)s, %(hogql_val_38)s, %(hogql_val_39)s]), %(hogql_val_40)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_41)s)), events__session.`$entry_utm_source`, %(hogql_val_42)s)), [%(hogql_val_43)s, %(hogql_val_44)s, %(hogql_val_45)s]), %(hogql_val_46)s, if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_47)s)), events__session.`$entry_utm_source`, %(hogql_val_48)s)))))), events__session.`$start_timestamp`) AS breakdown_value
FROM events_json AS events LEFT JOIN (
SELECT min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_0)s)) AS `$start_timestamp`, nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_source), %(hogql_val_1)s), %(hogql_val_2)s) AS `$entry_utm_source`, raw_sessions.session_id_v7 AS session_id_v7
FROM raw_sessions
@@ -83,7 +83,7 @@
GROUP BY person_distinct_id_overrides.distinct_id
HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0)
SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)
- WHERE and(equals(events.team_id, 420), and(equals(events.event, %(hogql_val_43)s), notEmpty(events.properties.`$session_id`), greater(toUnixTimestamp(events__session.`$start_timestamp`), 0)), greaterOrEquals(events.timestamp, toDateTime(%(hogql_val_44)s, %(hogql_val_45)s)), lessOrEquals(events.timestamp, toDateTime(%(hogql_val_46)s, %(hogql_val_47)s)), ifNull(greaterOrEquals(events__session.`$start_timestamp`, toDateTime(%(hogql_val_48)s, %(hogql_val_49)s)), 0), ifNull(lessOrEquals(events__session.`$start_timestamp`, toDateTime(%(hogql_val_50)s, %(hogql_val_51)s)), 0), globalNotIn(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), (
+ WHERE and(equals(events.team_id, 420), and(equals(events.event, %(hogql_val_49)s), notEmpty(events.properties.`$session_id`), greater(toUnixTimestamp(events__session.`$start_timestamp`), 0)), greaterOrEquals(events.timestamp, toDateTime(%(hogql_val_50)s, %(hogql_val_51)s)), lessOrEquals(events.timestamp, toDateTime(%(hogql_val_52)s, %(hogql_val_53)s)), ifNull(greaterOrEquals(events__session.`$start_timestamp`, toDateTime(%(hogql_val_54)s, %(hogql_val_55)s)), 0), ifNull(lessOrEquals(events__session.`$start_timestamp`, toDateTime(%(hogql_val_56)s, %(hogql_val_57)s)), 0), globalNotIn(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), (
SELECT DISTINCT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS person_id
FROM events_json AS events LEFT OUTER JOIN (
SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id, person_distinct_id_overrides.distinct_id AS distinct_id
@@ -92,9 +92,9 @@
GROUP BY person_distinct_id_overrides.distinct_id
HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0)
SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)
- WHERE and(equals(events.team_id, 420), equals(events.event, %(hogql_val_52)s), notEmpty(events.properties.`$session_id`), greaterOrEquals(events.timestamp, minus(toDateTime(%(hogql_val_53)s, %(hogql_val_54)s), toIntervalDay(90))), less(events.timestamp, toDateTime(%(hogql_val_55)s, %(hogql_val_56)s))))))
+ WHERE and(equals(events.team_id, 420), equals(events.event, %(hogql_val_58)s), notEmpty(events.properties.`$session_id`), greaterOrEquals(events.timestamp, minus(toDateTime(%(hogql_val_59)s, %(hogql_val_60)s), toIntervalDay(90))), less(events.timestamp, toDateTime(%(hogql_val_61)s, %(hogql_val_62)s))))))
GROUP BY actor_id), activity AS (
- SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS actor_id, dateDiff(%(hogql_val_57)s, toStartOfWeek(toDateTime(%(hogql_val_58)s, %(hogql_val_59)s), 0), toStartOfWeek(toTimeZone(events.timestamp, %(hogql_val_60)s), 0)) AS activity_index
+ SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS actor_id, dateDiff(%(hogql_val_63)s, toStartOfWeek(toDateTime(%(hogql_val_64)s, %(hogql_val_65)s), 0), toStartOfWeek(toTimeZone(events.timestamp, %(hogql_val_66)s), 0)) AS activity_index
FROM events_json AS events LEFT OUTER JOIN (
SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id, person_distinct_id_overrides.distinct_id AS distinct_id
FROM person_distinct_id_overrides
@@ -102,14 +102,14 @@
GROUP BY person_distinct_id_overrides.distinct_id
HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0)
SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)
- WHERE and(equals(events.team_id, 420), equals(events.event, %(hogql_val_61)s), greaterOrEquals(events.timestamp, toDateTime(%(hogql_val_62)s, %(hogql_val_63)s)), lessOrEquals(events.timestamp, toDateTime(%(hogql_val_64)s, %(hogql_val_65)s)), globalIn(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), (
+ WHERE and(equals(events.team_id, 420), equals(events.event, %(hogql_val_67)s), greaterOrEquals(events.timestamp, toDateTime(%(hogql_val_68)s, %(hogql_val_69)s)), lessOrEquals(events.timestamp, toDateTime(%(hogql_val_70)s, %(hogql_val_71)s)), globalIn(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), (
SELECT acquisition.actor_id AS actor_id
FROM acquisition)))
GROUP BY actor_id, activity_index), cohort_sizes AS MATERIALIZED (
- SELECT acquisition.breakdown_value AS breakdown_value, dateDiff(%(hogql_val_66)s, toStartOfWeek(toDateTime(%(hogql_val_67)s, %(hogql_val_68)s), 0), toStartOfWeek(toTimeZone(acquisition.cohort_at, %(hogql_val_69)s), 0)) AS cohort_index, count(acquisition.actor_id) AS cohort_size
+ SELECT acquisition.breakdown_value AS breakdown_value, dateDiff(%(hogql_val_72)s, toStartOfWeek(toDateTime(%(hogql_val_73)s, %(hogql_val_74)s), 0), toStartOfWeek(toTimeZone(acquisition.cohort_at, %(hogql_val_75)s), 0)) AS cohort_index, count(acquisition.actor_id) AS cohort_size
FROM acquisition
GROUP BY acquisition.breakdown_value, cohort_index), matrix AS (
- SELECT acquisition.breakdown_value AS breakdown_value, dateDiff(%(hogql_val_70)s, toStartOfWeek(toDateTime(%(hogql_val_71)s, %(hogql_val_72)s), 0), toStartOfWeek(toTimeZone(acquisition.cohort_at, %(hogql_val_73)s), 0)) AS cohort_index, minus(activity.activity_index, dateDiff(%(hogql_val_74)s, toStartOfWeek(toDateTime(%(hogql_val_75)s, %(hogql_val_76)s), 0), toStartOfWeek(toTimeZone(acquisition.cohort_at, %(hogql_val_77)s), 0))) AS intervals_from_base, count(DISTINCT acquisition.actor_id) AS count
+ SELECT acquisition.breakdown_value AS breakdown_value, dateDiff(%(hogql_val_76)s, toStartOfWeek(toDateTime(%(hogql_val_77)s, %(hogql_val_78)s), 0), toStartOfWeek(toTimeZone(acquisition.cohort_at, %(hogql_val_79)s), 0)) AS cohort_index, minus(activity.activity_index, dateDiff(%(hogql_val_80)s, toStartOfWeek(toDateTime(%(hogql_val_81)s, %(hogql_val_82)s), 0), toStartOfWeek(toTimeZone(acquisition.cohort_at, %(hogql_val_83)s), 0))) AS intervals_from_base, count(DISTINCT acquisition.actor_id) AS count
FROM acquisition INNER JOIN activity ON equals(activity.actor_id, acquisition.actor_id)
GROUP BY acquisition.breakdown_value, cohort_index, intervals_from_base
HAVING and(ifNull(greaterOrEquals(intervals_from_base, 0), 0), ifNull(less(intervals_from_base, 4), 0))), folded_sizes AS (
@@ -118,7 +118,7 @@
FROM cohort_sizes
GROUP BY cohort_sizes.breakdown_value
ORDER BY sum(cohort_sizes.cohort_size) DESC, cohort_sizes.breakdown_value ASC
- LIMIT 20)), cohort_sizes.breakdown_value, %(hogql_val_78)s) AS breakdown_value, cohort_sizes.cohort_index AS cohort_index, sum(cohort_sizes.cohort_size) AS cohort_size
+ LIMIT 20)), cohort_sizes.breakdown_value, %(hogql_val_84)s) AS breakdown_value, cohort_sizes.cohort_index AS cohort_index, sum(cohort_sizes.cohort_size) AS cohort_size
FROM cohort_sizes
GROUP BY breakdown_value, cohort_sizes.cohort_index), folded_matrix AS (
SELECT if(globalIn(matrix.breakdown_value, (
@@ -126,7 +126,7 @@
FROM cohort_sizes
GROUP BY cohort_sizes.breakdown_value
ORDER BY sum(cohort_sizes.cohort_size) DESC, cohort_sizes.breakdown_value ASC
- LIMIT 20)), matrix.breakdown_value, %(hogql_val_79)s) AS breakdown_value, matrix.cohort_index AS cohort_index, matrix.intervals_from_base AS intervals_from_base, sum(matrix.count) AS count
+ LIMIT 20)), matrix.breakdown_value, %(hogql_val_85)s) AS breakdown_value, matrix.cohort_index AS cohort_index, matrix.intervals_from_base AS intervals_from_base, sum(matrix.count) AS count
FROM matrix
GROUP BY breakdown_value, matrix.cohort_index, matrix.intervals_from_base), summary AS (
SELECT uniqExact(cohort_sizes.breakdown_value) AS distinct_breakdowns
@@ -209,7 +209,7 @@
# name: TestMarketingAnalyticsRetentionQueryRunner.test_retention_sql_3_all_users[new_events_schema]
'''
WITH acquisition AS MATERIALIZED (
- SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS actor_id, min(events__session.`$start_timestamp`) AS cohort_at, argMin(if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_8)s)), events__session.`$entry_utm_source`, %(hogql_val_9)s)), [%(hogql_val_10)s, %(hogql_val_11)s, %(hogql_val_12)s]), %(hogql_val_13)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_14)s)), events__session.`$entry_utm_source`, %(hogql_val_15)s)), [%(hogql_val_16)s, %(hogql_val_17)s, %(hogql_val_18)s, %(hogql_val_19)s, %(hogql_val_20)s, %(hogql_val_21)s, %(hogql_val_22)s, %(hogql_val_23)s]), %(hogql_val_24)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_25)s)), events__session.`$entry_utm_source`, %(hogql_val_26)s)), [%(hogql_val_27)s]), %(hogql_val_28)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_29)s)), events__session.`$entry_utm_source`, %(hogql_val_30)s)), [%(hogql_val_31)s, %(hogql_val_32)s, %(hogql_val_33)s, %(hogql_val_34)s, %(hogql_val_35)s, %(hogql_val_36)s, %(hogql_val_37)s, %(hogql_val_38)s, %(hogql_val_39)s]), %(hogql_val_40)s, if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_41)s)), events__session.`$entry_utm_source`, %(hogql_val_42)s))))), events__session.`$start_timestamp`) AS breakdown_value
+ SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS actor_id, min(events__session.`$start_timestamp`) AS cohort_at, argMin(if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_8)s)), events__session.`$entry_utm_source`, %(hogql_val_9)s)), [%(hogql_val_10)s, %(hogql_val_11)s, %(hogql_val_12)s]), %(hogql_val_13)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_14)s)), events__session.`$entry_utm_source`, %(hogql_val_15)s)), [%(hogql_val_16)s, %(hogql_val_17)s, %(hogql_val_18)s, %(hogql_val_19)s, %(hogql_val_20)s, %(hogql_val_21)s, %(hogql_val_22)s, %(hogql_val_23)s]), %(hogql_val_24)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_25)s)), events__session.`$entry_utm_source`, %(hogql_val_26)s)), [%(hogql_val_27)s]), %(hogql_val_28)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_29)s)), events__session.`$entry_utm_source`, %(hogql_val_30)s)), [%(hogql_val_31)s, %(hogql_val_32)s, %(hogql_val_33)s, %(hogql_val_34)s, %(hogql_val_35)s, %(hogql_val_36)s, %(hogql_val_37)s, %(hogql_val_38)s, %(hogql_val_39)s]), %(hogql_val_40)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_41)s)), events__session.`$entry_utm_source`, %(hogql_val_42)s)), [%(hogql_val_43)s, %(hogql_val_44)s, %(hogql_val_45)s]), %(hogql_val_46)s, if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_47)s)), events__session.`$entry_utm_source`, %(hogql_val_48)s)))))), events__session.`$start_timestamp`) AS breakdown_value
FROM events_json AS events LEFT JOIN (
SELECT min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_0)s)) AS `$start_timestamp`, nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_source), %(hogql_val_1)s), %(hogql_val_2)s) AS `$entry_utm_source`, raw_sessions.session_id_v7 AS session_id_v7
FROM raw_sessions
@@ -221,9 +221,9 @@
GROUP BY person_distinct_id_overrides.distinct_id
HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0)
SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)
- WHERE and(equals(events.team_id, 420), and(equals(events.event, %(hogql_val_43)s), notEmpty(events.properties.`$session_id`), greater(toUnixTimestamp(events__session.`$start_timestamp`), 0)), greaterOrEquals(events.timestamp, toDateTime(%(hogql_val_44)s, %(hogql_val_45)s)), lessOrEquals(events.timestamp, toDateTime(%(hogql_val_46)s, %(hogql_val_47)s)), ifNull(greaterOrEquals(events__session.`$start_timestamp`, toDateTime(%(hogql_val_48)s, %(hogql_val_49)s)), 0), ifNull(lessOrEquals(events__session.`$start_timestamp`, toDateTime(%(hogql_val_50)s, %(hogql_val_51)s)), 0))
+ WHERE and(equals(events.team_id, 420), and(equals(events.event, %(hogql_val_49)s), notEmpty(events.properties.`$session_id`), greater(toUnixTimestamp(events__session.`$start_timestamp`), 0)), greaterOrEquals(events.timestamp, toDateTime(%(hogql_val_50)s, %(hogql_val_51)s)), lessOrEquals(events.timestamp, toDateTime(%(hogql_val_52)s, %(hogql_val_53)s)), ifNull(greaterOrEquals(events__session.`$start_timestamp`, toDateTime(%(hogql_val_54)s, %(hogql_val_55)s)), 0), ifNull(lessOrEquals(events__session.`$start_timestamp`, toDateTime(%(hogql_val_56)s, %(hogql_val_57)s)), 0))
GROUP BY actor_id), activity AS (
- SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS actor_id, dateDiff(%(hogql_val_52)s, toStartOfWeek(toDateTime(%(hogql_val_53)s, %(hogql_val_54)s), 0), toStartOfWeek(toTimeZone(events.timestamp, %(hogql_val_55)s), 0)) AS activity_index
+ SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS actor_id, dateDiff(%(hogql_val_58)s, toStartOfWeek(toDateTime(%(hogql_val_59)s, %(hogql_val_60)s), 0), toStartOfWeek(toTimeZone(events.timestamp, %(hogql_val_61)s), 0)) AS activity_index
FROM events_json AS events LEFT OUTER JOIN (
SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id, person_distinct_id_overrides.distinct_id AS distinct_id
FROM person_distinct_id_overrides
@@ -231,14 +231,14 @@
GROUP BY person_distinct_id_overrides.distinct_id
HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0)
SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)
- WHERE and(equals(events.team_id, 420), equals(events.event, %(hogql_val_56)s), greaterOrEquals(events.timestamp, toDateTime(%(hogql_val_57)s, %(hogql_val_58)s)), lessOrEquals(events.timestamp, toDateTime(%(hogql_val_59)s, %(hogql_val_60)s)), globalIn(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), (
+ WHERE and(equals(events.team_id, 420), equals(events.event, %(hogql_val_62)s), greaterOrEquals(events.timestamp, toDateTime(%(hogql_val_63)s, %(hogql_val_64)s)), lessOrEquals(events.timestamp, toDateTime(%(hogql_val_65)s, %(hogql_val_66)s)), globalIn(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), (
SELECT acquisition.actor_id AS actor_id
FROM acquisition)))
GROUP BY actor_id, activity_index), cohort_sizes AS MATERIALIZED (
- SELECT acquisition.breakdown_value AS breakdown_value, dateDiff(%(hogql_val_61)s, toStartOfWeek(toDateTime(%(hogql_val_62)s, %(hogql_val_63)s), 0), toStartOfWeek(toTimeZone(acquisition.cohort_at, %(hogql_val_64)s), 0)) AS cohort_index, count(acquisition.actor_id) AS cohort_size
+ SELECT acquisition.breakdown_value AS breakdown_value, dateDiff(%(hogql_val_67)s, toStartOfWeek(toDateTime(%(hogql_val_68)s, %(hogql_val_69)s), 0), toStartOfWeek(toTimeZone(acquisition.cohort_at, %(hogql_val_70)s), 0)) AS cohort_index, count(acquisition.actor_id) AS cohort_size
FROM acquisition
GROUP BY acquisition.breakdown_value, cohort_index), matrix AS (
- SELECT acquisition.breakdown_value AS breakdown_value, dateDiff(%(hogql_val_65)s, toStartOfWeek(toDateTime(%(hogql_val_66)s, %(hogql_val_67)s), 0), toStartOfWeek(toTimeZone(acquisition.cohort_at, %(hogql_val_68)s), 0)) AS cohort_index, minus(activity.activity_index, dateDiff(%(hogql_val_69)s, toStartOfWeek(toDateTime(%(hogql_val_70)s, %(hogql_val_71)s), 0), toStartOfWeek(toTimeZone(acquisition.cohort_at, %(hogql_val_72)s), 0))) AS intervals_from_base, count(DISTINCT acquisition.actor_id) AS count
+ SELECT acquisition.breakdown_value AS breakdown_value, dateDiff(%(hogql_val_71)s, toStartOfWeek(toDateTime(%(hogql_val_72)s, %(hogql_val_73)s), 0), toStartOfWeek(toTimeZone(acquisition.cohort_at, %(hogql_val_74)s), 0)) AS cohort_index, minus(activity.activity_index, dateDiff(%(hogql_val_75)s, toStartOfWeek(toDateTime(%(hogql_val_76)s, %(hogql_val_77)s), 0), toStartOfWeek(toTimeZone(acquisition.cohort_at, %(hogql_val_78)s), 0))) AS intervals_from_base, count(DISTINCT acquisition.actor_id) AS count
FROM acquisition INNER JOIN activity ON equals(activity.actor_id, acquisition.actor_id)
GROUP BY acquisition.breakdown_value, cohort_index, intervals_from_base
HAVING and(ifNull(greaterOrEquals(intervals_from_base, 0), 0), ifNull(less(intervals_from_base, 4), 0))), folded_sizes AS (
@@ -247,7 +247,7 @@
FROM cohort_sizes
GROUP BY cohort_sizes.breakdown_value
ORDER BY sum(cohort_sizes.cohort_size) DESC, cohort_sizes.breakdown_value ASC
- LIMIT 20)), cohort_sizes.breakdown_value, %(hogql_val_73)s) AS breakdown_value, cohort_sizes.cohort_index AS cohort_index, sum(cohort_sizes.cohort_size) AS cohort_size
+ LIMIT 20)), cohort_sizes.breakdown_value, %(hogql_val_79)s) AS breakdown_value, cohort_sizes.cohort_index AS cohort_index, sum(cohort_sizes.cohort_size) AS cohort_size
FROM cohort_sizes
GROUP BY breakdown_value, cohort_sizes.cohort_index), folded_matrix AS (
SELECT if(globalIn(matrix.breakdown_value, (
@@ -255,7 +255,7 @@
FROM cohort_sizes
GROUP BY cohort_sizes.breakdown_value
ORDER BY sum(cohort_sizes.cohort_size) DESC, cohort_sizes.breakdown_value ASC
- LIMIT 20)), matrix.breakdown_value, %(hogql_val_74)s) AS breakdown_value, matrix.cohort_index AS cohort_index, matrix.intervals_from_base AS intervals_from_base, sum(matrix.count) AS count
+ LIMIT 20)), matrix.breakdown_value, %(hogql_val_80)s) AS breakdown_value, matrix.cohort_index AS cohort_index, matrix.intervals_from_base AS intervals_from_base, sum(matrix.count) AS count
FROM matrix
GROUP BY breakdown_value, matrix.cohort_index, matrix.intervals_from_base), summary AS (
SELECT uniqExact(cohort_sizes.breakdown_value) AS distinct_breakdowns
@@ -269,7 +269,7 @@
# name: TestMarketingAnalyticsRetentionQueryRunner.test_retention_sql_4_summary[new_events_schema]
'''
WITH acquisition AS MATERIALIZED (
- SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS actor_id, min(events__session.`$start_timestamp`) AS first_session_at, argMin(if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_30)s)), events__session.`$entry_utm_source`, %(hogql_val_31)s)), [%(hogql_val_32)s, %(hogql_val_33)s, %(hogql_val_34)s]), %(hogql_val_35)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_36)s)), events__session.`$entry_utm_source`, %(hogql_val_37)s)), [%(hogql_val_38)s, %(hogql_val_39)s, %(hogql_val_40)s, %(hogql_val_41)s, %(hogql_val_42)s, %(hogql_val_43)s, %(hogql_val_44)s, %(hogql_val_45)s]), %(hogql_val_46)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_47)s)), events__session.`$entry_utm_source`, %(hogql_val_48)s)), [%(hogql_val_49)s]), %(hogql_val_50)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_51)s)), events__session.`$entry_utm_source`, %(hogql_val_52)s)), [%(hogql_val_53)s, %(hogql_val_54)s, %(hogql_val_55)s, %(hogql_val_56)s, %(hogql_val_57)s, %(hogql_val_58)s, %(hogql_val_59)s, %(hogql_val_60)s, %(hogql_val_61)s]), %(hogql_val_62)s, if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_63)s)), events__session.`$entry_utm_source`, %(hogql_val_64)s))))), events__session.`$start_timestamp`) AS breakdown_value, 0 AS previous, %(hogql_val_65)s AS observation_end, argMin(events.properties.`$session_id`, events__session.`$start_timestamp`) AS first_session_id
+ SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS actor_id, min(events__session.`$start_timestamp`) AS first_session_at, argMin(if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_30)s)), events__session.`$entry_utm_source`, %(hogql_val_31)s)), [%(hogql_val_32)s, %(hogql_val_33)s, %(hogql_val_34)s]), %(hogql_val_35)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_36)s)), events__session.`$entry_utm_source`, %(hogql_val_37)s)), [%(hogql_val_38)s, %(hogql_val_39)s, %(hogql_val_40)s, %(hogql_val_41)s, %(hogql_val_42)s, %(hogql_val_43)s, %(hogql_val_44)s, %(hogql_val_45)s]), %(hogql_val_46)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_47)s)), events__session.`$entry_utm_source`, %(hogql_val_48)s)), [%(hogql_val_49)s]), %(hogql_val_50)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_51)s)), events__session.`$entry_utm_source`, %(hogql_val_52)s)), [%(hogql_val_53)s, %(hogql_val_54)s, %(hogql_val_55)s, %(hogql_val_56)s, %(hogql_val_57)s, %(hogql_val_58)s, %(hogql_val_59)s, %(hogql_val_60)s, %(hogql_val_61)s]), %(hogql_val_62)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_63)s)), events__session.`$entry_utm_source`, %(hogql_val_64)s)), [%(hogql_val_65)s, %(hogql_val_66)s, %(hogql_val_67)s]), %(hogql_val_68)s, if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_69)s)), events__session.`$entry_utm_source`, %(hogql_val_70)s)))))), events__session.`$start_timestamp`) AS breakdown_value, 0 AS previous, %(hogql_val_71)s AS observation_end, argMin(events.properties.`$session_id`, events__session.`$start_timestamp`) AS first_session_id
FROM events_json AS events LEFT JOIN (
SELECT min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_22)s)) AS `$start_timestamp`, nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_source), %(hogql_val_23)s), %(hogql_val_24)s) AS `$entry_utm_source`, raw_sessions.session_id_v7 AS session_id_v7
FROM raw_sessions
@@ -281,7 +281,7 @@
GROUP BY person_distinct_id_overrides.distinct_id
HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0)
SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)
- WHERE and(equals(events.team_id, 420), and(equals(events.event, %(hogql_val_66)s), notEmpty(events.properties.`$session_id`), greater(toUnixTimestamp(events__session.`$start_timestamp`), 0)), greaterOrEquals(events.timestamp, toDateTime(%(hogql_val_67)s, %(hogql_val_68)s)), lessOrEquals(events.timestamp, toDateTime(%(hogql_val_69)s, %(hogql_val_70)s)), ifNull(greaterOrEquals(events__session.`$start_timestamp`, toDateTime(%(hogql_val_71)s, %(hogql_val_72)s)), 0), ifNull(lessOrEquals(events__session.`$start_timestamp`, toDateTime(%(hogql_val_73)s, %(hogql_val_74)s)), 0), globalNotIn(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), (
+ WHERE and(equals(events.team_id, 420), and(equals(events.event, %(hogql_val_72)s), notEmpty(events.properties.`$session_id`), greater(toUnixTimestamp(events__session.`$start_timestamp`), 0)), greaterOrEquals(events.timestamp, toDateTime(%(hogql_val_73)s, %(hogql_val_74)s)), lessOrEquals(events.timestamp, toDateTime(%(hogql_val_75)s, %(hogql_val_76)s)), ifNull(greaterOrEquals(events__session.`$start_timestamp`, toDateTime(%(hogql_val_77)s, %(hogql_val_78)s)), 0), ifNull(lessOrEquals(events__session.`$start_timestamp`, toDateTime(%(hogql_val_79)s, %(hogql_val_80)s)), 0), globalNotIn(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), (
SELECT DISTINCT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS person_id
FROM events_json AS events LEFT OUTER JOIN (
SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id, person_distinct_id_overrides.distinct_id AS distinct_id
@@ -290,21 +290,21 @@
GROUP BY person_distinct_id_overrides.distinct_id
HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0)
SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)
- WHERE and(equals(events.team_id, 420), equals(events.event, %(hogql_val_75)s), notEmpty(events.properties.`$session_id`), greaterOrEquals(events.timestamp, minus(toDateTime(%(hogql_val_76)s, %(hogql_val_77)s), toIntervalDay(90))), less(events.timestamp, toDateTime(%(hogql_val_78)s, %(hogql_val_79)s))))))
+ WHERE and(equals(events.team_id, 420), equals(events.event, %(hogql_val_81)s), notEmpty(events.properties.`$session_id`), greaterOrEquals(events.timestamp, minus(toDateTime(%(hogql_val_82)s, %(hogql_val_83)s), toIntervalDay(90))), less(events.timestamp, toDateTime(%(hogql_val_84)s, %(hogql_val_85)s))))))
GROUP BY actor_id UNION ALL
- SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS actor_id, min(events__session.`$start_timestamp`) AS first_session_at, argMin(if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_88)s)), events__session.`$entry_utm_source`, %(hogql_val_89)s)), [%(hogql_val_90)s, %(hogql_val_91)s, %(hogql_val_92)s]), %(hogql_val_93)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_94)s)), events__session.`$entry_utm_source`, %(hogql_val_95)s)), [%(hogql_val_96)s, %(hogql_val_97)s, %(hogql_val_98)s, %(hogql_val_99)s, %(hogql_val_100)s, %(hogql_val_101)s, %(hogql_val_102)s, %(hogql_val_103)s]), %(hogql_val_104)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_105)s)), events__session.`$entry_utm_source`, %(hogql_val_106)s)), [%(hogql_val_107)s]), %(hogql_val_108)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_109)s)), events__session.`$entry_utm_source`, %(hogql_val_110)s)), [%(hogql_val_111)s, %(hogql_val_112)s, %(hogql_val_113)s, %(hogql_val_114)s, %(hogql_val_115)s, %(hogql_val_116)s, %(hogql_val_117)s, %(hogql_val_118)s, %(hogql_val_119)s]), %(hogql_val_120)s, if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_121)s)), events__session.`$entry_utm_source`, %(hogql_val_122)s))))), events__session.`$start_timestamp`) AS breakdown_value, 1 AS previous, %(hogql_val_123)s AS observation_end, argMin(events.properties.`$session_id`, events__session.`$start_timestamp`) AS first_session_id
+ SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS actor_id, min(events__session.`$start_timestamp`) AS first_session_at, argMin(if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_94)s)), events__session.`$entry_utm_source`, %(hogql_val_95)s)), [%(hogql_val_96)s, %(hogql_val_97)s, %(hogql_val_98)s]), %(hogql_val_99)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_100)s)), events__session.`$entry_utm_source`, %(hogql_val_101)s)), [%(hogql_val_102)s, %(hogql_val_103)s, %(hogql_val_104)s, %(hogql_val_105)s, %(hogql_val_106)s, %(hogql_val_107)s, %(hogql_val_108)s, %(hogql_val_109)s]), %(hogql_val_110)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_111)s)), events__session.`$entry_utm_source`, %(hogql_val_112)s)), [%(hogql_val_113)s]), %(hogql_val_114)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_115)s)), events__session.`$entry_utm_source`, %(hogql_val_116)s)), [%(hogql_val_117)s, %(hogql_val_118)s, %(hogql_val_119)s, %(hogql_val_120)s, %(hogql_val_121)s, %(hogql_val_122)s, %(hogql_val_123)s, %(hogql_val_124)s, %(hogql_val_125)s]), %(hogql_val_126)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_127)s)), events__session.`$entry_utm_source`, %(hogql_val_128)s)), [%(hogql_val_129)s, %(hogql_val_130)s, %(hogql_val_131)s]), %(hogql_val_132)s, if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_133)s)), events__session.`$entry_utm_source`, %(hogql_val_134)s)))))), events__session.`$start_timestamp`) AS breakdown_value, 1 AS previous, %(hogql_val_135)s AS observation_end, argMin(events.properties.`$session_id`, events__session.`$start_timestamp`) AS first_session_id
FROM events_json AS events LEFT JOIN (
- SELECT min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_80)s)) AS `$start_timestamp`, nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_source), %(hogql_val_81)s), %(hogql_val_82)s) AS `$entry_utm_source`, raw_sessions.session_id_v7 AS session_id_v7
+ SELECT min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_86)s)) AS `$start_timestamp`, nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_source), %(hogql_val_87)s), %(hogql_val_88)s) AS `$entry_utm_source`, raw_sessions.session_id_v7 AS session_id_v7
FROM raw_sessions
- WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(toDateTime(%(hogql_val_83)s, %(hogql_val_84)s), toIntervalDay(3))), lessOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), plus(toDateTime(%(hogql_val_85)s, %(hogql_val_86)s), toIntervalDay(3))))
- GROUP BY raw_sessions.session_id_v7) AS events__session ON equals(toUInt128(accurateCastOrNull(events.properties.`$session_id`, %(hogql_val_87)s)), events__session.session_id_v7) LEFT OUTER JOIN (
+ WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(toDateTime(%(hogql_val_89)s, %(hogql_val_90)s), toIntervalDay(3))), lessOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), plus(toDateTime(%(hogql_val_91)s, %(hogql_val_92)s), toIntervalDay(3))))
+ GROUP BY raw_sessions.session_id_v7) AS events__session ON equals(toUInt128(accurateCastOrNull(events.properties.`$session_id`, %(hogql_val_93)s)), events__session.session_id_v7) LEFT OUTER JOIN (
SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) 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, 420)
GROUP BY person_distinct_id_overrides.distinct_id
HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0)
SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)
- WHERE and(equals(events.team_id, 420), and(equals(events.event, %(hogql_val_124)s), notEmpty(events.properties.`$session_id`), greater(toUnixTimestamp(events__session.`$start_timestamp`), 0)), greaterOrEquals(events.timestamp, toDateTime(%(hogql_val_125)s, %(hogql_val_126)s)), lessOrEquals(events.timestamp, toDateTime(%(hogql_val_127)s, %(hogql_val_128)s)), ifNull(greaterOrEquals(events__session.`$start_timestamp`, toDateTime(%(hogql_val_129)s, %(hogql_val_130)s)), 0), ifNull(lessOrEquals(events__session.`$start_timestamp`, toDateTime(%(hogql_val_131)s, %(hogql_val_132)s)), 0), globalNotIn(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), (
+ WHERE and(equals(events.team_id, 420), and(equals(events.event, %(hogql_val_136)s), notEmpty(events.properties.`$session_id`), greater(toUnixTimestamp(events__session.`$start_timestamp`), 0)), greaterOrEquals(events.timestamp, toDateTime(%(hogql_val_137)s, %(hogql_val_138)s)), lessOrEquals(events.timestamp, toDateTime(%(hogql_val_139)s, %(hogql_val_140)s)), ifNull(greaterOrEquals(events__session.`$start_timestamp`, toDateTime(%(hogql_val_141)s, %(hogql_val_142)s)), 0), ifNull(lessOrEquals(events__session.`$start_timestamp`, toDateTime(%(hogql_val_143)s, %(hogql_val_144)s)), 0), globalNotIn(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), (
SELECT DISTINCT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS person_id
FROM events_json AS events LEFT OUTER JOIN (
SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id, person_distinct_id_overrides.distinct_id AS distinct_id
@@ -313,23 +313,23 @@
GROUP BY person_distinct_id_overrides.distinct_id
HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0)
SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)
- WHERE and(equals(events.team_id, 420), equals(events.event, %(hogql_val_133)s), notEmpty(events.properties.`$session_id`), greaterOrEquals(events.timestamp, minus(toDateTime(%(hogql_val_134)s, %(hogql_val_135)s), toIntervalDay(90))), less(events.timestamp, toDateTime(%(hogql_val_136)s, %(hogql_val_137)s))))))
+ WHERE and(equals(events.team_id, 420), equals(events.event, %(hogql_val_145)s), notEmpty(events.properties.`$session_id`), greaterOrEquals(events.timestamp, minus(toDateTime(%(hogql_val_146)s, %(hogql_val_147)s), toIntervalDay(90))), less(events.timestamp, toDateTime(%(hogql_val_148)s, %(hogql_val_149)s))))))
GROUP BY actor_id), activity AS (
SELECT acquisition.actor_id AS actor_id, acquisition.previous AS previous, min(events__session.`$start_timestamp`) AS returned_at
FROM events_json AS events LEFT JOIN (
- SELECT min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_138)s)) AS `$start_timestamp`, raw_sessions.session_id_v7 AS session_id_v7
+ SELECT min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_150)s)) AS `$start_timestamp`, raw_sessions.session_id_v7 AS session_id_v7
FROM raw_sessions
- WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(toDateTime(%(hogql_val_139)s, %(hogql_val_140)s), toIntervalDay(3))), lessOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), plus(toDateTime(%(hogql_val_141)s, %(hogql_val_142)s), toIntervalDay(3))))
- GROUP BY raw_sessions.session_id_v7) AS events__session ON equals(toUInt128(accurateCastOrNull(events.properties.`$session_id`, %(hogql_val_143)s)), events__session.session_id_v7) LEFT OUTER JOIN (
+ WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(toDateTime(%(hogql_val_151)s, %(hogql_val_152)s), toIntervalDay(3))), lessOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), plus(toDateTime(%(hogql_val_153)s, %(hogql_val_154)s), toIntervalDay(3))))
+ GROUP BY raw_sessions.session_id_v7) AS events__session ON equals(toUInt128(accurateCastOrNull(events.properties.`$session_id`, %(hogql_val_155)s)), events__session.session_id_v7) LEFT OUTER JOIN (
SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) 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, 420)
GROUP BY person_distinct_id_overrides.distinct_id
HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0)
SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id) GLOBAL INNER JOIN acquisition ON equals(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), acquisition.actor_id)
- WHERE and(equals(events.team_id, 420), equals(events.event, %(hogql_val_144)s), notEmpty(events.properties.`$session_id`), greaterOrEquals(events.timestamp, toDateTime(%(hogql_val_145)s, %(hogql_val_146)s)), lessOrEquals(events.timestamp, toDateTime(%(hogql_val_147)s, %(hogql_val_148)s)), notEquals(events.properties.`$session_id`, acquisition.first_session_id), greater(toDate(events__session.`$start_timestamp`), toDate(toTimeZone(acquisition.first_session_at, %(hogql_val_149)s))), lessOrEquals(events__session.`$start_timestamp`, plus(toTimeZone(acquisition.first_session_at, %(hogql_val_150)s), toIntervalDay(30))), ifNull(lessOrEquals(events__session.`$start_timestamp`, parseDateTime64BestEffortOrNull(acquisition.observation_end, 6, %(hogql_val_151)s)), 0))
+ WHERE and(equals(events.team_id, 420), equals(events.event, %(hogql_val_156)s), notEmpty(events.properties.`$session_id`), greaterOrEquals(events.timestamp, toDateTime(%(hogql_val_157)s, %(hogql_val_158)s)), lessOrEquals(events.timestamp, toDateTime(%(hogql_val_159)s, %(hogql_val_160)s)), notEquals(events.properties.`$session_id`, acquisition.first_session_id), greater(toDate(events__session.`$start_timestamp`), toDate(toTimeZone(acquisition.first_session_at, %(hogql_val_161)s))), lessOrEquals(events__session.`$start_timestamp`, plus(toTimeZone(acquisition.first_session_at, %(hogql_val_162)s), toIntervalDay(30))), ifNull(lessOrEquals(events__session.`$start_timestamp`, parseDateTime64BestEffortOrNull(acquisition.observation_end, 6, %(hogql_val_163)s)), 0))
GROUP BY actor_id, previous), people AS MATERIALIZED (
- SELECT acquisition.actor_id AS actor_id, acquisition.breakdown_value AS breakdown_value, acquisition.previous AS previous, toTimeZone(acquisition.first_session_at, %(hogql_val_152)s) AS first_session_at, parseDateTime64BestEffortOrNull(acquisition.observation_end, 6, %(hogql_val_153)s) AS observation_end, toTimeZone(activity.returned_at, %(hogql_val_154)s) AS returned_at
+ SELECT acquisition.actor_id AS actor_id, acquisition.breakdown_value AS breakdown_value, acquisition.previous AS previous, toTimeZone(acquisition.first_session_at, %(hogql_val_164)s) AS first_session_at, parseDateTime64BestEffortOrNull(acquisition.observation_end, 6, %(hogql_val_165)s) AS observation_end, toTimeZone(activity.returned_at, %(hogql_val_166)s) AS returned_at
FROM acquisition LEFT JOIN activity ON and(equals(acquisition.actor_id, activity.actor_id), equals(acquisition.previous, activity.previous)))
SELECT if(globalIn(people.breakdown_value, (
SELECT people.breakdown_value AS breakdown_value
diff --git a/products/slack_app/backend/services/followup_invite.py b/products/slack_app/backend/services/followup_invite.py
index 5dafa8d32f40..7568b1c7e783 100644
--- a/products/slack_app/backend/services/followup_invite.py
+++ b/products/slack_app/backend/services/followup_invite.py
@@ -14,7 +14,13 @@ from products.slack_app.backend.services.slack_scopes import bot_is_ready
BOT_SETUP_DOCS_URL = "https://posthog.com/docs/slack-app"
-def build_followup_invite_text(integration: Integration | None, *, utm_tags: str, ai_enabled: bool) -> str | None:
+def build_followup_invite_text(
+ integration: Integration | None,
+ *,
+ utm_tags: str,
+ ai_enabled: bool,
+ subject: str | None = None,
+) -> str | None:
"""mrkdwn nudging the channel to @PostHog this report (or to set the bot up).
Returns ``None`` when there's no Slack install or the org hasn't approved AI data processing —
@@ -28,20 +34,30 @@ def build_followup_invite_text(integration: Integration | None, *, utm_tags: str
``utm_tags`` attributes an install that starts from the setup link, so each caller passes the
campaign that names its own surface.
+
+ ``subject`` names what the reader would be asking about, for a surface that isn't a report.
+ The unfurl path passes the resource it just expanded, so the line reads "dig into this
+ dashboard" rather than naming a report the reader never saw.
"""
if integration is None or not ai_enabled:
return None
if bot_is_ready(integration):
- return "💬 Reply in this thread and mention *@PostHog* with a question to dig into this report."
- return f"💬 <{BOT_SETUP_DOCS_URL}?{utm_tags}|Set up the @PostHog bot> to ask follow-up questions about your reports here."
+ return f"💬 Reply in this thread and mention *@PostHog* with a question to dig into {subject or 'this report'}."
+ return f"💬 <{BOT_SETUP_DOCS_URL}?{utm_tags}|Set up the @PostHog bot> to ask follow-up questions about {subject or 'your reports'} here."
-def build_followup_invite(integration: Integration | None, *, utm_tags: str, ai_enabled: bool) -> dict[str, Any] | None:
+def build_followup_invite(
+ integration: Integration | None,
+ *,
+ utm_tags: str,
+ ai_enabled: bool,
+ subject: str | None = None,
+) -> dict[str, Any] | None:
"""Slack context block nudging the channel to @PostHog this report (or to set the bot up).
Returns ``None`` in the same cases as ``build_followup_invite_text``.
"""
- text = build_followup_invite_text(integration, utm_tags=utm_tags, ai_enabled=ai_enabled)
+ text = build_followup_invite_text(integration, utm_tags=utm_tags, ai_enabled=ai_enabled, subject=subject)
if text is None:
return None
return {"type": "context", "elements": [{"type": "mrkdwn", "text": text}]}
diff --git a/products/slack_app/backend/slack_link_unfurl.py b/products/slack_app/backend/slack_link_unfurl.py
index 06faf2910d5e..a401019b2fa4 100644
--- a/products/slack_app/backend/slack_link_unfurl.py
+++ b/products/slack_app/backend/slack_link_unfurl.py
@@ -17,6 +17,7 @@ from posthog.models import Team
from posthog.models.comment import Comment
from posthog.models.integration import Integration, SlackIntegration
from posthog.models.user_integration import UserIntegration
+from posthog.redis import get_client
from posthog.slack.formatting import escape_slack_mrkdwn
from posthog.utils import get_instance_region
@@ -28,6 +29,7 @@ from products.conversations.backend.models.ticket import Ticket
from products.dashboards.backend.models.dashboard import Dashboard
from products.product_analytics.backend.facade.models import Insight
from products.slack_app.backend.analytics import capture_slack_event
+from products.slack_app.backend.services.followup_invite import build_followup_invite
from products.slack_app.backend.services.slack_messages import UNFURL_OPT_OUT_PARAM
from products.tasks.backend.facade import api as tasks_facade
from products.tasks.backend.facade.contracts import TaskSlackUnfurlDTO
@@ -38,6 +40,13 @@ _MAX_DESCRIPTION_CHARS = 2800
# Keep title + status + message under Slack's 3000-char section limit; the client shows "Show more".
_MAX_OPENING_MESSAGE_CHARS = 2000
+_INVITE_UTM_TAGS = "utm_source=slack&utm_medium=unfurl&utm_campaign=link_unfurl_followup"
+# A channel that shares PostHog links all day would otherwise carry the invite on every unfurl,
+# which reads as chrome and stops being noticed. One a day per channel keeps it legible.
+_INVITE_THROTTLE_SECONDS = 24 * 60 * 60
+# The throttle must fail fast rather than correctly: it sits inside the webhook's ack budget.
+_INVITE_REDIS_TIMEOUT_SECONDS = 0.3
+
# Query `source.kind` / top-level `kind` → short name before " insight" (sync with InsightType / query kinds).
_QUERY_KIND_TO_SHORT_NAME: dict[str, str] = {
"TrendsQuery": "Trends",
@@ -286,6 +295,57 @@ def _ticket_unfurl_payload(*, url: str, ticket: Ticket, requester: str, opening_
return {"blocks": blocks}
+def _claim_invite_slot(slack_team_id: str, channel: str) -> bool:
+ """Whether this channel may carry the follow-up invite now, claiming the day's slot if so.
+
+ ``SET NX EX`` makes the claim atomic, so two unfurls racing in the same channel produce one
+ invite. A Redis failure returns False: skipping a nudge costs nothing, while failing open
+ would turn an outage into an invite on every unfurl in every channel at once.
+
+ The raw client rather than ``cache.add``, which the product's other one-shot claims use,
+ because this one needs an explicit timeout. ``CACHES`` sets no ``SOCKET_TIMEOUT``, so a cache
+ call waits on a stalled Redis for as long as it takes, and the ack budget does not allow that.
+ """
+ try:
+ client = get_client(
+ socket_timeout=_INVITE_REDIS_TIMEOUT_SECONDS,
+ socket_connect_timeout=_INVITE_REDIS_TIMEOUT_SECONDS,
+ )
+ claimed = client.set(
+ f"slack_app:unfurl_invite:v1:{slack_team_id}:{channel}",
+ "1",
+ nx=True,
+ ex=_INVITE_THROTTLE_SECONDS,
+ )
+ except Exception:
+ logger.warning("slack_app_unfurl_invite_throttle_unavailable", channel=channel, exc_info=True)
+ return False
+ return bool(claimed)
+
+
+def _build_unfurl_invite(integration: Integration, kind: str, channel: str) -> dict | None:
+ """The invite block for the first resource we unfurled, or None when it should stay quiet.
+
+ Consent is checked before the slot is claimed: the other way round, a workspace that never
+ approved AI would burn its channel's daily slot on every paste, and an approval later that
+ day would stay suppressed until the key expired.
+ """
+ slack_team_id = integration.integration_id
+ if not slack_team_id:
+ return None
+ ai_enabled = bool(integration.team.organization.is_ai_data_processing_approved)
+ if not ai_enabled:
+ return None
+ if not _claim_invite_slot(slack_team_id, channel):
+ return None
+ return build_followup_invite(
+ integration,
+ utm_tags=_INVITE_UTM_TAGS,
+ ai_enabled=ai_enabled,
+ subject=f"this {kind}",
+ )
+
+
def _is_normal_public_channel(slack: SlackIntegration, channel: str, cache: dict[str, bool]) -> bool:
if channel in cache:
return cache[channel]
@@ -428,6 +488,8 @@ def handle_posthog_link_unfurl(event: dict, integration: Integration) -> None:
unfurls: dict[str, dict] = {}
unfurled_kinds: list[str] = []
+ # Carries the kind alongside the url so the invite never has to guess which payload is first.
+ first_unfurled: tuple[str, str] | None = None
# Every resource we recognized but chose not to unfurl, so a report of "no unfurl appeared"
# can be answered from logs instead of by re-deriving the path by hand.
skipped: list[dict[str, str]] = []
@@ -536,6 +598,8 @@ def handle_posthog_link_unfurl(event: dict, integration: Integration) -> None:
if raw_url in unfurls:
unfurled_kinds.append(kind)
+ if first_unfurled is None:
+ first_unfurled = (raw_url, kind)
logger.info(
"slack_app_link_unfurl_result",
@@ -549,6 +613,13 @@ def handle_posthog_link_unfurl(event: dict, integration: Integration) -> None:
if not unfurls:
return
+ invite = None
+ if first_unfurled is not None:
+ invite_url, invite_kind = first_unfurled
+ invite = _build_unfurl_invite(integration, invite_kind, channel)
+ if invite:
+ unfurls[invite_url]["blocks"].append(invite)
+
unfurl_kwargs: dict = {"channel": channel, "ts": message_ts, "unfurls": unfurls}
if unfurl_id:
unfurl_kwargs["unfurl_id"] = unfurl_id
@@ -568,4 +639,5 @@ def handle_posthog_link_unfurl(event: dict, integration: Integration) -> None:
kinds=sorted(set(unfurled_kinds)),
unfurled_count=len(unfurls),
skipped_count=len(skipped),
+ followup_invite_shown=invite is not None,
)
diff --git a/products/slack_app/backend/tests/test_link_unfurl.py b/products/slack_app/backend/tests/test_link_unfurl.py
index 172c867ae445..dce0eb127c9f 100644
--- a/products/slack_app/backend/tests/test_link_unfurl.py
+++ b/products/slack_app/backend/tests/test_link_unfurl.py
@@ -2,6 +2,7 @@ import pytest
from posthog.test.base import APIBaseTest
from unittest.mock import MagicMock, patch
+from django.core.exceptions import ImproperlyConfigured
from django.test import TestCase, override_settings
from django.test.client import RequestFactory
from django.utils import timezone
@@ -297,6 +298,133 @@ class TestHandlePosthogLinkUnfurl(APIBaseTest):
result = next(log for log in logs if log["event"] == "slack_app_link_unfurl_result")
assert result["skipped"] == [{"kind": "insight", "ref": self.insight.short_id, "reason": "other_region"}]
+ def _make_bot_ready(self) -> None:
+ self.integration.config = {"scope": ",".join(sorted(REQUIRED_SLACK_SCOPES))}
+ self.integration.save()
+
+ def _unfurl_links(self, urls: list[str], channel: str = "C1") -> None:
+ handle_posthog_link_unfurl(
+ {
+ "channel": channel,
+ "message_ts": "123.456",
+ "user": "U1",
+ "links": [{"url": url, "domain": "testserver"} for url in urls],
+ },
+ self.integration,
+ )
+
+ @staticmethod
+ def _invite_blocks(mock_client: MagicMock) -> list[dict]:
+ blocks = [
+ block
+ for payload in mock_client.chat_unfurl.call_args.kwargs["unfurls"].values()
+ for block in payload["blocks"]
+ ]
+ return [block for block in blocks if block["type"] == "context" and "@PostHog" in block["elements"][0]["text"]]
+
+ @parameterized.expand([("ai_approved", True, 1), ("ai_not_approved", False, 0)])
+ @patch("products.slack_app.backend.api.resolve_slack_user")
+ @patch("products.slack_app.backend.slack_link_unfurl.SlackIntegration")
+ def test_unfurl_invites_a_follow_up_only_when_the_org_approved_ai(
+ self,
+ _name: str,
+ ai_approved: bool,
+ expected_invites: int,
+ mock_slack_integration_class: MagicMock,
+ mock_resolve: MagicMock,
+ ) -> None:
+ self.organization.is_ai_data_processing_approved = ai_approved
+ self.organization.save()
+ self._make_bot_ready()
+ mock_resolve.return_value = MagicMock(user=self.user)
+ mock_client = MagicMock()
+ mock_slack_integration_class.return_value.client = mock_client
+
+ self._unfurl_links([f"http://testserver/project/{self.team.pk}/insights/{self.insight.short_id}"])
+
+ invites = self._invite_blocks(mock_client)
+ assert len(invites) == expected_invites
+ if expected_invites:
+ assert "dig into this insight" in invites[0]["elements"][0]["text"]
+
+ @patch("products.slack_app.backend.api.resolve_slack_user")
+ @patch("products.slack_app.backend.slack_link_unfurl.SlackIntegration")
+ def test_unfurl_invite_offers_setup_when_the_install_cannot_be_mentioned(
+ self, mock_slack_integration_class: MagicMock, mock_resolve: MagicMock
+ ) -> None:
+ # An install predating app_mentions:read can unfurl but not answer, so inviting a mention
+ # would point at a bot that stays silent.
+ self.integration.config = {"scope": "links:read,chat:write"}
+ self.integration.save()
+ mock_resolve.return_value = MagicMock(user=self.user)
+ mock_client = MagicMock()
+ mock_slack_integration_class.return_value.client = mock_client
+
+ self._unfurl_links([f"http://testserver/project/{self.team.pk}/insights/{self.insight.short_id}"])
+
+ text = self._invite_blocks(mock_client)[0]["elements"][0]["text"]
+ assert "Set up the @PostHog bot" in text
+ assert "about this insight here" in text
+ assert "mention" not in text
+
+ @patch("products.slack_app.backend.api.resolve_slack_user")
+ @patch("products.slack_app.backend.slack_link_unfurl.SlackIntegration")
+ def test_unfurl_invite_appears_once_per_channel_per_day(
+ self, mock_slack_integration_class: MagicMock, mock_resolve: MagicMock
+ ) -> None:
+ self._make_bot_ready()
+ mock_resolve.return_value = MagicMock(user=self.user)
+ mock_client = MagicMock()
+ mock_slack_integration_class.return_value.client = mock_client
+ url = f"http://testserver/project/{self.team.pk}/insights/{self.insight.short_id}"
+
+ self._unfurl_links([url])
+ assert len(self._invite_blocks(mock_client)) == 1
+
+ self._unfurl_links([url])
+ assert self._invite_blocks(mock_client) == []
+
+ self._unfurl_links([url], channel="C2")
+ assert len(self._invite_blocks(mock_client)) == 1
+
+ @patch("products.slack_app.backend.slack_link_unfurl.get_client")
+ @patch("products.slack_app.backend.api.resolve_slack_user")
+ @patch("products.slack_app.backend.slack_link_unfurl.SlackIntegration")
+ def test_unfurl_still_sent_when_redis_is_unreachable(
+ self, mock_slack_integration_class: MagicMock, mock_resolve: MagicMock, mock_get_client: MagicMock
+ ) -> None:
+ self._make_bot_ready()
+ mock_resolve.return_value = MagicMock(user=self.user)
+ mock_client = MagicMock()
+ mock_slack_integration_class.return_value.client = mock_client
+ mock_get_client.side_effect = ImproperlyConfigured("Redis not configured!")
+
+ self._unfurl_links([f"http://testserver/project/{self.team.pk}/insights/{self.insight.short_id}"])
+
+ assert len(mock_client.chat_unfurl.call_args.kwargs["unfurls"]) == 1
+ assert self._invite_blocks(mock_client) == []
+
+ @patch("products.slack_app.backend.api.resolve_slack_user")
+ @patch("products.slack_app.backend.slack_link_unfurl.SlackIntegration")
+ def test_message_with_several_links_carries_one_invite(
+ self, mock_slack_integration_class: MagicMock, mock_resolve: MagicMock
+ ) -> None:
+ self._make_bot_ready()
+ dashboard = Dashboard.objects.create(team=self.team, name="Growth")
+ mock_resolve.return_value = MagicMock(user=self.user)
+ mock_client = MagicMock()
+ mock_slack_integration_class.return_value.client = mock_client
+
+ self._unfurl_links(
+ [
+ f"http://testserver/project/{self.team.pk}/insights/{self.insight.short_id}",
+ f"http://testserver/project/{self.team.pk}/dashboard/{dashboard.pk}",
+ ],
+ )
+
+ assert len(mock_client.chat_unfurl.call_args.kwargs["unfurls"]) == 2
+ assert len(self._invite_blocks(mock_client)) == 1
+
@patch("products.slack_app.backend.api.resolve_slack_user")
@patch("products.slack_app.backend.slack_link_unfurl.SlackIntegration")
def test_reports_why_recognized_links_were_not_unfurled(