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.

perf(marketing-analytics): bound the attribution fan-out per person (#90595)

Javier Bahamondes098981e37158
diff --git a/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_attribution_paths_query_runner.ambr b/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_attribution_paths_query_runner.ambr
index 3feeec68a052..48957d99cc5a 100644
--- a/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_attribution_paths_query_runner.ambr
+++ b/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_attribution_paths_query_runner.ambr
@@ -2,7 +2,7 @@
# name: TestMarketingAnalyticsAttributionPathsQueryRunner.test_attribution_paths_sql_0_campaign
'''
WITH person_arrays AS (
- SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS person_id, groupArrayIf(tuple(toUnixTimestamp(toTimeZone(events.timestamp, %(hogql_val_14)s)), coalesce(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_15)s), ''), 'null'), '^"|"$', ''), %(hogql_val_16)s), 0.0)), and(equals(events.event, %(hogql_val_17)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_18)s), %(hogql_val_19)s), toDateTime(%(hogql_val_20)s, %(hogql_val_21)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_22)s), %(hogql_val_23)s), toDateTime(%(hogql_val_24)s, %(hogql_val_25)s)))) AS conversions, arraySlice(arraySort(groupUniqArrayIf(tuple(toUnixTimestamp(events__session.`$start_timestamp`), toString(ifNull(events__session.`$entry_utm_campaign`, %(hogql_val_26)s))), and(equals(events.event, %(hogql_val_27)s), notEmpty(events.`$session_id`), greater(toUnixTimestamp(events__session.`$start_timestamp`), 0)))), -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_25)s)), coalesce(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_26)s), ''), 'null'), '^"|"$', ''), %(hogql_val_27)s), 0.0)), and(equals(events.event, %(hogql_val_28)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_29)s), %(hogql_val_30)s), toDateTime(%(hogql_val_31)s, %(hogql_val_32)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_33)s), %(hogql_val_34)s), toDateTime(%(hogql_val_35)s, %(hogql_val_36)s))))), -500) AS conversions, countIf(and(equals(events.event, %(hogql_val_37)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_38)s), %(hogql_val_39)s), toDateTime(%(hogql_val_40)s, %(hogql_val_41)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_42)s), %(hogql_val_43)s), toDateTime(%(hogql_val_44)s, %(hogql_val_45)s)))) AS conversion_count, arraySlice(arraySort(groupUniqArrayIf(tuple(toUnixTimestamp(events__session.`$start_timestamp`), toString(ifNull(events__session.`$entry_utm_campaign`, %(hogql_val_46)s))), and(and(equals(events.event, %(hogql_val_47)s), notEmpty(events.`$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 LEFT JOIN (
SELECT min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_0)s)) AS `$start_timestamp`, nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_campaign), %(hogql_val_1)s), %(hogql_val_2)s) AS `$entry_utm_campaign`, raw_sessions.session_id_v7 AS session_id_v7
FROM raw_sessions
@@ -13,27 +13,27 @@
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), globalIn(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), (
- SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS person_id
- FROM 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
- 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), equals(events.event, %(hogql_val_28)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_29)s), %(hogql_val_30)s), toDateTime(%(hogql_val_31)s, %(hogql_val_32)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_33)s), %(hogql_val_34)s), toDateTime(%(hogql_val_35)s, %(hogql_val_36)s)))
- GROUP BY if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id))), or(and(equals(events.event, %(hogql_val_37)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_38)s), %(hogql_val_39)s), toDateTime(%(hogql_val_40)s, %(hogql_val_41)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_42)s), %(hogql_val_43)s), toDateTime(%(hogql_val_44)s, %(hogql_val_45)s))), and(equals(events.event, %(hogql_val_46)s), greaterOrEquals(events.timestamp, minus(toDateTime(%(hogql_val_47)s, %(hogql_val_48)s), toIntervalSecond(345600))), lessOrEquals(events.timestamp, toDateTime(%(hogql_val_49)s, %(hogql_val_50)s)))))
+ 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_14)s))) AS first_conversion, max(toUnixTimestamp(toTimeZone(events.timestamp, %(hogql_val_15)s))) AS last_conversion
+ FROM 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
+ 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), equals(events.event, %(hogql_val_16)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_17)s), %(hogql_val_18)s), toDateTime(%(hogql_val_19)s, %(hogql_val_20)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_21)s), %(hogql_val_22)s), toDateTime(%(hogql_val_23)s, %(hogql_val_24)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_48)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_49)s), %(hogql_val_50)s), toDateTime(%(hogql_val_51)s, %(hogql_val_52)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_53)s), %(hogql_val_54)s), toDateTime(%(hogql_val_55)s, %(hogql_val_56)s))), and(equals(events.event, %(hogql_val_57)s), greaterOrEquals(events.timestamp, minus(toDateTime(%(hogql_val_58)s, %(hogql_val_59)s), toIntervalSecond(345600))), lessOrEquals(events.timestamp, toDateTime(%(hogql_val_60)s, %(hogql_val_61)s)), greaterOrEquals(toUnixTimestamp(toTimeZone(events.timestamp, %(hogql_val_62)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 (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
+ 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
FROM person_arrays ARRAY JOIN arrayEnumerate(person_arrays.conversions) AS i), path_rows AS (
SELECT per_conversion_path.path AS path, count() AS conversions, sum(per_conversion_path.conv_value) AS conversion_value, max(per_conversion_path.truncated) AS path_truncated, 1 AS footer_key
FROM per_conversion_path
WHERE ifNull(greater(per_conversion_path.orig_len, 0), 0)
GROUP BY per_conversion_path.path), paths_footer AS (
- SELECT count() AS total_conversions, countIf(ifNull(greater(per_conversion_path.orig_len, 0), 0)) AS attributed_conversions, 1 AS footer_key
+ SELECT sumIf(per_conversion_path.conversion_count, ifNull(equals(per_conversion_path.i, 1), 0)) AS total_conversions, countIf(ifNull(greater(per_conversion_path.orig_len, 0), 0)) AS attributed_conversions, 1 AS footer_key
FROM per_conversion_path)
SELECT path_rows.path AS path, path_rows.conversions AS conversions, path_rows.conversion_value AS conversion_value, path_rows.path_truncated AS path_truncated, paths_footer.total_conversions AS total_conversions, paths_footer.attributed_conversions AS attributed_conversions, path_rows.footer_key AS footer_key
FROM paths_footer LEFT JOIN path_rows ON equals(paths_footer.footer_key, path_rows.footer_key)
@@ -45,7 +45,7 @@
# name: TestMarketingAnalyticsAttributionPathsQueryRunner.test_attribution_paths_sql_1_source
'''
WITH person_arrays AS (
- SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS person_id, groupArrayIf(tuple(toUnixTimestamp(toTimeZone(events.timestamp, %(hogql_val_14)s)), coalesce(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_15)s), ''), 'null'), '^"|"$', ''), %(hogql_val_16)s), 0.0)), and(equals(events.event, %(hogql_val_17)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_18)s), %(hogql_val_19)s), toDateTime(%(hogql_val_20)s, %(hogql_val_21)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_22)s), %(hogql_val_23)s), toDateTime(%(hogql_val_24)s, %(hogql_val_25)s)))) AS conversions, arraySlice(arraySort(groupUniqArrayIf(tuple(toUnixTimestamp(events__session.`$start_timestamp`), if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_26)s)), events__session.`$entry_utm_source`, %(hogql_val_27)s)), [%(hogql_val_28)s, %(hogql_val_29)s, %(hogql_val_30)s]), %(hogql_val_31)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_32)s)), events__session.`$entry_utm_source`, %(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, %(hogql_val_41)s]), %(hogql_val_42)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_43)s)), events__session.`$entry_utm_source`, %(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, %(hogql_val_51)s, %(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, if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_59)s)), events__session.`$entry_utm_source`, %(hogql_val_60)s)))))), and(equals(events.event, %(hogql_val_61)s), notEmpty(events.`$session_id`), greater(toUnixTimestamp(events__session.`$start_timestamp`), 0)))), -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_25)s)), coalesce(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_26)s), ''), 'null'), '^"|"$', ''), %(hogql_val_27)s), 0.0)), and(equals(events.event, %(hogql_val_28)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_29)s), %(hogql_val_30)s), toDateTime(%(hogql_val_31)s, %(hogql_val_32)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_33)s), %(hogql_val_34)s), toDateTime(%(hogql_val_35)s, %(hogql_val_36)s))))), -500) AS conversions, countIf(and(equals(events.event, %(hogql_val_37)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_38)s), %(hogql_val_39)s), toDateTime(%(hogql_val_40)s, %(hogql_val_41)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_42)s), %(hogql_val_43)s), toDateTime(%(hogql_val_44)s, %(hogql_val_45)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_46)s)), events__session.`$entry_utm_source`, %(hogql_val_47)s)), [%(hogql_val_48)s, %(hogql_val_49)s, %(hogql_val_50)s]), %(hogql_val_51)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_52)s)), events__session.`$entry_utm_source`, %(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, 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, %(hogql_val_73)s, %(hogql_val_74)s, %(hogql_val_75)s, %(hogql_val_76)s, %(hogql_val_77)s]), %(hogql_val_78)s, if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_79)s)), events__session.`$entry_utm_source`, %(hogql_val_80)s)))))), and(and(equals(events.event, %(hogql_val_81)s), notEmpty(events.`$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 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
@@ -56,27 +56,27 @@
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), globalIn(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), (
- SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS person_id
- FROM 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
- 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), equals(events.event, %(hogql_val_62)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_63)s), %(hogql_val_64)s), toDateTime(%(hogql_val_65)s, %(hogql_val_66)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_67)s), %(hogql_val_68)s), toDateTime(%(hogql_val_69)s, %(hogql_val_70)s)))
- GROUP BY if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id))), or(and(equals(events.event, %(hogql_val_71)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_72)s), %(hogql_val_73)s), toDateTime(%(hogql_val_74)s, %(hogql_val_75)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_76)s), %(hogql_val_77)s), toDateTime(%(hogql_val_78)s, %(hogql_val_79)s))), and(equals(events.event, %(hogql_val_80)s), greaterOrEquals(events.timestamp, minus(toDateTime(%(hogql_val_81)s, %(hogql_val_82)s), toIntervalSecond(345600))), lessOrEquals(events.timestamp, toDateTime(%(hogql_val_83)s, %(hogql_val_84)s)))))
+ 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_14)s))) AS first_conversion, max(toUnixTimestamp(toTimeZone(events.timestamp, %(hogql_val_15)s))) AS last_conversion
+ FROM 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
+ 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), equals(events.event, %(hogql_val_16)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_17)s), %(hogql_val_18)s), toDateTime(%(hogql_val_19)s, %(hogql_val_20)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_21)s), %(hogql_val_22)s), toDateTime(%(hogql_val_23)s, %(hogql_val_24)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_82)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_83)s), %(hogql_val_84)s), toDateTime(%(hogql_val_85)s, %(hogql_val_86)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_87)s), %(hogql_val_88)s), toDateTime(%(hogql_val_89)s, %(hogql_val_90)s))), and(equals(events.event, %(hogql_val_91)s), greaterOrEquals(events.timestamp, minus(toDateTime(%(hogql_val_92)s, %(hogql_val_93)s), toIntervalSecond(345600))), lessOrEquals(events.timestamp, toDateTime(%(hogql_val_94)s, %(hogql_val_95)s)), greaterOrEquals(toUnixTimestamp(toTimeZone(events.timestamp, %(hogql_val_96)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 (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
+ 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
FROM person_arrays ARRAY JOIN arrayEnumerate(person_arrays.conversions) AS i), path_rows AS (
SELECT per_conversion_path.path AS path, count() AS conversions, sum(per_conversion_path.conv_value) AS conversion_value, max(per_conversion_path.truncated) AS path_truncated, 1 AS footer_key
FROM per_conversion_path
WHERE ifNull(greater(per_conversion_path.orig_len, 0), 0)
GROUP BY per_conversion_path.path), paths_footer AS (
- SELECT count() AS total_conversions, countIf(ifNull(greater(per_conversion_path.orig_len, 0), 0)) AS attributed_conversions, 1 AS footer_key
+ SELECT sumIf(per_conversion_path.conversion_count, ifNull(equals(per_conversion_path.i, 1), 0)) AS total_conversions, countIf(ifNull(greater(per_conversion_path.orig_len, 0), 0)) AS attributed_conversions, 1 AS footer_key
FROM per_conversion_path)
SELECT path_rows.path AS path, path_rows.conversions AS conversions, path_rows.conversion_value AS conversion_value, path_rows.path_truncated AS path_truncated, paths_footer.total_conversions AS total_conversions, paths_footer.attributed_conversions AS attributed_conversions, path_rows.footer_key AS footer_key
FROM paths_footer LEFT JOIN path_rows ON equals(paths_footer.footer_key, path_rows.footer_key)
@@ -88,7 +88,7 @@
# name: TestMarketingAnalyticsAttributionPathsQueryRunner.test_attribution_paths_sql_2_channel
'''
WITH person_arrays AS (
- SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS person_id, groupArrayIf(tuple(toUnixTimestamp(toTimeZone(events.timestamp, %(hogql_val_131)s)), coalesce(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_132)s), ''), 'null'), '^"|"$', ''), %(hogql_val_133)s), 0.0)), and(equals(events.event, %(hogql_val_134)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_135)s), %(hogql_val_136)s), toDateTime(%(hogql_val_137)s, %(hogql_val_138)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_139)s), %(hogql_val_140)s), toDateTime(%(hogql_val_141)s, %(hogql_val_142)s)))) AS conversions, arraySlice(arraySort(groupUniqArrayIf(tuple(toUnixTimestamp(events__session.`$start_timestamp`), if(notEmpty(ifNull(events__session.`$channel_type`, %(hogql_val_143)s)), events__session.`$channel_type`, %(hogql_val_144)s)), and(equals(events.event, %(hogql_val_145)s), notEmpty(events.`$session_id`), greater(toUnixTimestamp(events__session.`$start_timestamp`), 0)))), -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_142)s)), coalesce(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_143)s), ''), 'null'), '^"|"$', ''), %(hogql_val_144)s), 0.0)), and(equals(events.event, %(hogql_val_145)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_146)s), %(hogql_val_147)s), toDateTime(%(hogql_val_148)s, %(hogql_val_149)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_150)s), %(hogql_val_151)s), toDateTime(%(hogql_val_152)s, %(hogql_val_153)s))))), -500) AS conversions, countIf(and(equals(events.event, %(hogql_val_154)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_155)s), %(hogql_val_156)s), toDateTime(%(hogql_val_157)s, %(hogql_val_158)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_159)s), %(hogql_val_160)s), toDateTime(%(hogql_val_161)s, %(hogql_val_162)s)))) AS conversion_count, arraySlice(arraySort(groupUniqArrayIf(tuple(toUnixTimestamp(events__session.`$start_timestamp`), if(notEmpty(ifNull(events__session.`$channel_type`, %(hogql_val_163)s)), events__session.`$channel_type`, %(hogql_val_164)s)), and(and(equals(events.event, %(hogql_val_165)s), notEmpty(events.`$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 LEFT JOIN (
SELECT min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_0)s)) AS `$start_timestamp`, multiIf(match(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_campaign), %(hogql_val_1)s), %(hogql_val_2)s), %(hogql_val_3)s), %(hogql_val_4)s)), %(hogql_val_5)s), %(hogql_val_6)s, or(in(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_medium), %(hogql_val_7)s), %(hogql_val_8)s), %(hogql_val_9)s), %(hogql_val_10)s)), tuple(%(hogql_val_11)s, %(hogql_val_12)s, %(hogql_val_13)s, %(hogql_val_14)s, %(hogql_val_15)s, %(hogql_val_16)s)), startsWith(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_medium), %(hogql_val_17)s), %(hogql_val_18)s), %(hogql_val_19)s), %(hogql_val_20)s)), %(hogql_val_21)s), isNotNull(nullIf(nullIf(argMinMerge(raw_sessions.initial_gclid), %(hogql_val_22)s), %(hogql_val_23)s)), isNotNull(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_gad_source), %(hogql_val_24)s), %(hogql_val_25)s), %(hogql_val_26)s), %(hogql_val_27)s))), coalesce(coalesce(dictGetOrNull('posthog_test.channel_definition_dict', 'type_if_paid', (coalesce(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_source), %(hogql_val_28)s), %(hogql_val_29)s), %(hogql_val_30)s), %(hogql_val_31)s)), ''), 'source')) , dictGetOrNull('posthog_test.channel_definition_dict', 'type_if_paid', (cutToFirstSignificantSubdomain(coalesce(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_source), %(hogql_val_28)s), %(hogql_val_29)s), %(hogql_val_30)s), %(hogql_val_31)s)), '')), 'source'))), if(match(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_campaign), %(hogql_val_32)s), %(hogql_val_33)s), %(hogql_val_34)s), %(hogql_val_35)s)), %(hogql_val_36)s), %(hogql_val_37)s, NULL), dictGetOrNull('posthog_test.channel_definition_dict', 'type_if_paid', (coalesce(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_medium), %(hogql_val_38)s), %(hogql_val_39)s), %(hogql_val_40)s), %(hogql_val_41)s)), ''), 'medium')), coalesce(dictGetOrNull('posthog_test.channel_definition_dict', 'type_if_paid', (coalesce(nullIf(nullIf(argMinMerge(raw_sessions.initial_referring_domain), %(hogql_val_42)s), %(hogql_val_43)s), ''), 'source')) , dictGetOrNull('posthog_test.channel_definition_dict', 'type_if_paid', (cutToFirstSignificantSubdomain(coalesce(nullIf(nullIf(argMinMerge(raw_sessions.initial_referring_domain), %(hogql_val_42)s), %(hogql_val_43)s), '')), 'source'))), multiIf(ifNull(equals(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_gad_source), %(hogql_val_44)s), %(hogql_val_45)s), %(hogql_val_46)s), %(hogql_val_47)s), %(hogql_val_48)s), 0), %(hogql_val_49)s, match(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_campaign), %(hogql_val_50)s), %(hogql_val_51)s), %(hogql_val_52)s), %(hogql_val_53)s)), %(hogql_val_54)s), %(hogql_val_55)s, isNotNull(nullIf(nullIf(argMinMerge(raw_sessions.initial_fbclid), %(hogql_val_56)s), %(hogql_val_57)s)), %(hogql_val_58)s, %(hogql_val_59)s)), and(ifNull(equals(nullIf(nullIf(argMinMerge(raw_sessions.initial_referring_domain), %(hogql_val_60)s), %(hogql_val_61)s), %(hogql_val_62)s), 0), isNull(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_medium), %(hogql_val_63)s), %(hogql_val_64)s), %(hogql_val_65)s), %(hogql_val_66)s))), or(isNull(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_source), %(hogql_val_67)s), %(hogql_val_68)s), %(hogql_val_69)s), %(hogql_val_70)s))), in(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_source), %(hogql_val_71)s), %(hogql_val_72)s), %(hogql_val_73)s), %(hogql_val_74)s)), tuple(%(hogql_val_75)s, %(hogql_val_76)s, %(hogql_val_77)s))), not(isNotNull(nullIf(nullIf(argMinMerge(raw_sessions.initial_fbclid), %(hogql_val_78)s), %(hogql_val_79)s)))), %(hogql_val_80)s, coalesce(coalesce(dictGetOrNull('posthog_test.channel_definition_dict', 'type_if_organic', (coalesce(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_source), %(hogql_val_81)s), %(hogql_val_82)s), %(hogql_val_83)s), %(hogql_val_84)s)), ''), 'source')), dictGetOrNull('posthog_test.channel_definition_dict', 'type_if_organic', (cutToFirstSignificantSubdomain(coalesce(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_source), %(hogql_val_81)s), %(hogql_val_82)s), %(hogql_val_83)s), %(hogql_val_84)s)), '')), 'source'))), if(match(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_campaign), %(hogql_val_85)s), %(hogql_val_86)s), %(hogql_val_87)s), %(hogql_val_88)s)), %(hogql_val_89)s), %(hogql_val_90)s, NULL), dictGetOrNull('posthog_test.channel_definition_dict', 'type_if_organic', (coalesce(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_medium), %(hogql_val_91)s), %(hogql_val_92)s), %(hogql_val_93)s), %(hogql_val_94)s)), ''), 'medium')), coalesce(dictGetOrNull('posthog_test.channel_definition_dict', 'type_if_organic', (coalesce(nullIf(nullIf(argMinMerge(raw_sessions.initial_referring_domain), %(hogql_val_95)s), %(hogql_val_96)s), ''), 'source')), dictGetOrNull('posthog_test.channel_definition_dict', 'type_if_organic', (cutToFirstSignificantSubdomain(coalesce(nullIf(nullIf(argMinMerge(raw_sessions.initial_referring_domain), %(hogql_val_95)s), %(hogql_val_96)s), '')), 'source'))), multiIf(match(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_campaign), %(hogql_val_97)s), %(hogql_val_98)s), %(hogql_val_99)s), %(hogql_val_100)s)), %(hogql_val_101)s), %(hogql_val_102)s, match(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_medium), %(hogql_val_103)s), %(hogql_val_104)s), %(hogql_val_105)s), %(hogql_val_106)s)), %(hogql_val_107)s), %(hogql_val_108)s, isNotNull(nullIf(nullIf(argMinMerge(raw_sessions.initial_fbclid), %(hogql_val_109)s), %(hogql_val_110)s)), %(hogql_val_111)s, ifNull(equals(nullIf(nullIf(argMinMerge(raw_sessions.initial_referring_domain), %(hogql_val_112)s), %(hogql_val_113)s), %(hogql_val_114)s), 0), %(hogql_val_115)s, isNotNull(nullIf(nullIf(argMinMerge(raw_sessions.initial_referring_domain), %(hogql_val_116)s), %(hogql_val_117)s)), %(hogql_val_118)s, %(hogql_val_119)s))) AS `$channel_type`, raw_sessions.session_id_v7 AS session_id_v7
FROM raw_sessions
@@ -99,27 +99,27 @@
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), globalIn(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), (
- SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS person_id
- FROM 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
- 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), equals(events.event, %(hogql_val_146)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_147)s), %(hogql_val_148)s), toDateTime(%(hogql_val_149)s, %(hogql_val_150)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_151)s), %(hogql_val_152)s), toDateTime(%(hogql_val_153)s, %(hogql_val_154)s)))
- GROUP BY if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id))), or(and(equals(events.event, %(hogql_val_155)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_156)s), %(hogql_val_157)s), toDateTime(%(hogql_val_158)s, %(hogql_val_159)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_160)s), %(hogql_val_161)s), toDateTime(%(hogql_val_162)s, %(hogql_val_163)s))), and(equals(events.event, %(hogql_val_164)s), greaterOrEquals(events.timestamp, minus(toDateTime(%(hogql_val_165)s, %(hogql_val_166)s), toIntervalSecond(345600))), lessOrEquals(events.timestamp, toDateTime(%(hogql_val_167)s, %(hogql_val_168)s)))))
+ 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_131)s))) AS first_conversion, max(toUnixTimestamp(toTimeZone(events.timestamp, %(hogql_val_132)s))) AS last_conversion
+ FROM 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
+ 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), equals(events.event, %(hogql_val_133)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_134)s), %(hogql_val_135)s), toDateTime(%(hogql_val_136)s, %(hogql_val_137)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_138)s), %(hogql_val_139)s), toDateTime(%(hogql_val_140)s, %(hogql_val_141)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_166)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_167)s), %(hogql_val_168)s), toDateTime(%(hogql_val_169)s, %(hogql_val_170)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_171)s), %(hogql_val_172)s), toDateTime(%(hogql_val_173)s, %(hogql_val_174)s))), and(equals(events.event, %(hogql_val_175)s), greaterOrEquals(events.timestamp, minus(toDateTime(%(hogql_val_176)s, %(hogql_val_177)s), toIntervalSecond(345600))), lessOrEquals(events.timestamp, toDateTime(%(hogql_val_178)s, %(hogql_val_179)s)), greaterOrEquals(toUnixTimestamp(toTimeZone(events.timestamp, %(hogql_val_180)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 (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
+ 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
FROM person_arrays ARRAY JOIN arrayEnumerate(person_arrays.conversions) AS i), path_rows AS (
SELECT per_conversion_path.path AS path, count() AS conversions, sum(per_conversion_path.conv_value) AS conversion_value, max(per_conversion_path.truncated) AS path_truncated, 1 AS footer_key
FROM per_conversion_path
WHERE ifNull(greater(per_conversion_path.orig_len, 0), 0)
GROUP BY per_conversion_path.path), paths_footer AS (
- SELECT count() AS total_conversions, countIf(ifNull(greater(per_conversion_path.orig_len, 0), 0)) AS attributed_conversions, 1 AS footer_key
+ SELECT sumIf(per_conversion_path.conversion_count, ifNull(equals(per_conversion_path.i, 1), 0)) AS total_conversions, countIf(ifNull(greater(per_conversion_path.orig_len, 0), 0)) AS attributed_conversions, 1 AS footer_key
FROM per_conversion_path)
SELECT path_rows.path AS path, path_rows.conversions AS conversions, path_rows.conversion_value AS conversion_value, path_rows.path_truncated AS path_truncated, paths_footer.total_conversions AS total_conversions, paths_footer.attributed_conversions AS attributed_conversions, path_rows.footer_key AS footer_key
FROM paths_footer LEFT JOIN path_rows ON equals(paths_footer.footer_key, path_rows.footer_key)
diff --git a/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_attribution_table_query_runner.ambr b/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_attribution_table_query_runner.ambr
index bf038bd0c40a..2496b71e17d1 100644
--- a/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_attribution_table_query_runner.ambr
+++ b/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_attribution_table_query_runner.ambr
@@ -16,7 +16,7 @@
GROUP BY raw_sessions.session_id_v7) AS events__session ON equals(toUInt128(accurateCastOrNull(events.`$session_id`, %(hogql_val_7)s)), events__session.session_id_v7)
WHERE and(equals(events.team_id, 420), and(equals(events.event, %(hogql_val_9)s), notEmpty(events.`$session_id`), greater(toUnixTimestamp(events__session.`$start_timestamp`), 0)), greaterOrEquals(events.timestamp, minus(toDateTime(%(hogql_val_10)s, %(hogql_val_11)s), toIntervalSecond(345600))), lessOrEquals(events.timestamp, toDateTime(%(hogql_val_12)s, %(hogql_val_13)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, groupArrayIf(tuple(toUnixTimestamp(toTimeZone(events.timestamp, %(hogql_val_28)s)), coalesce(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_29)s), ''), 'null'), '^"|"$', ''), %(hogql_val_30)s), 0.0)), and(equals(events.event, %(hogql_val_31)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_32)s), %(hogql_val_33)s), toDateTime(%(hogql_val_34)s, %(hogql_val_35)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_36)s), %(hogql_val_37)s), toDateTime(%(hogql_val_38)s, %(hogql_val_39)s)))) AS conversions, arraySlice(arraySort(groupUniqArrayIf(tuple(toUnixTimestamp(events__session.`$start_timestamp`), toString(ifNull(events__session.`$entry_utm_campaign`, %(hogql_val_40)s))), and(equals(events.event, %(hogql_val_41)s), notEmpty(events.`$session_id`), greater(toUnixTimestamp(events__session.`$start_timestamp`), 0)))), -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_39)s)), coalesce(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_40)s), ''), 'null'), '^"|"$', ''), %(hogql_val_41)s), 0.0)), and(equals(events.event, %(hogql_val_42)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_43)s), %(hogql_val_44)s), toDateTime(%(hogql_val_45)s, %(hogql_val_46)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_47)s), %(hogql_val_48)s), toDateTime(%(hogql_val_49)s, %(hogql_val_50)s))))), -500) AS conversions, countIf(and(equals(events.event, %(hogql_val_51)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_52)s), %(hogql_val_53)s), toDateTime(%(hogql_val_54)s, %(hogql_val_55)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_56)s), %(hogql_val_57)s), toDateTime(%(hogql_val_58)s, %(hogql_val_59)s)))) AS conversion_count, arraySlice(arraySort(groupUniqArrayIf(tuple(toUnixTimestamp(events__session.`$start_timestamp`), toString(ifNull(events__session.`$entry_utm_campaign`, %(hogql_val_60)s))), and(and(equals(events.event, %(hogql_val_61)s), notEmpty(events.`$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 LEFT JOIN (
SELECT min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_14)s)) AS `$start_timestamp`, nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_campaign), %(hogql_val_15)s), %(hogql_val_16)s) AS `$entry_utm_campaign`, raw_sessions.session_id_v7 AS session_id_v7
FROM raw_sessions
@@ -27,21 +27,21 @@
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), globalIn(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), (
- SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS person_id
- FROM 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
- 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), equals(events.event, %(hogql_val_42)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_43)s), %(hogql_val_44)s), toDateTime(%(hogql_val_45)s, %(hogql_val_46)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_47)s), %(hogql_val_48)s), toDateTime(%(hogql_val_49)s, %(hogql_val_50)s)))
- GROUP BY if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id))), or(and(equals(events.event, %(hogql_val_51)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_52)s), %(hogql_val_53)s), toDateTime(%(hogql_val_54)s, %(hogql_val_55)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_56)s), %(hogql_val_57)s), toDateTime(%(hogql_val_58)s, %(hogql_val_59)s))), and(equals(events.event, %(hogql_val_60)s), greaterOrEquals(events.timestamp, minus(toDateTime(%(hogql_val_61)s, %(hogql_val_62)s), toIntervalSecond(345600))), lessOrEquals(events.timestamp, toDateTime(%(hogql_val_63)s, %(hogql_val_64)s)))))
+ 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_28)s))) AS first_conversion, max(toUnixTimestamp(toTimeZone(events.timestamp, %(hogql_val_29)s))) AS last_conversion
+ FROM 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
+ 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), equals(events.event, %(hogql_val_30)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_31)s), %(hogql_val_32)s), toDateTime(%(hogql_val_33)s, %(hogql_val_34)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_35)s), %(hogql_val_36)s), toDateTime(%(hogql_val_37)s, %(hogql_val_38)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_62)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_63)s), %(hogql_val_64)s), toDateTime(%(hogql_val_65)s, %(hogql_val_66)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_67)s), %(hogql_val_68)s), toDateTime(%(hogql_val_69)s, %(hogql_val_70)s))), and(equals(events.event, %(hogql_val_71)s), greaterOrEquals(events.timestamp, minus(toDateTime(%(hogql_val_72)s, %(hogql_val_73)s), toIntervalSecond(345600))), lessOrEquals(events.timestamp, toDateTime(%(hogql_val_74)s, %(hogql_val_75)s)), greaterOrEquals(toUnixTimestamp(toTimeZone(events.timestamp, %(hogql_val_76)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.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_65)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_66)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_67)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_77)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_78)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_79)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 (
@@ -51,9 +51,9 @@
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_68)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_80)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 count() AS total_conversions, countIf(ifNull(greater(length(per_conversion.tps), 0), 0)) AS attributed_conversions, 1 AS footer_key
+ 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)
SELECT attribution_rows.breakdown_value AS breakdown_value, attribution_rows.visitors AS visitors, attribution_rows.influenced_conversions AS influenced_conversions, attribution_rows.influenced_value AS influenced_value, attribution_rows.w_first_conversions AS w_first_conversions, attribution_rows.w_first_value AS w_first_value, attribution_rows.w_last_conversions AS w_last_conversions, attribution_rows.w_last_value AS w_last_value, attribution_rows.w_linear_conversions AS w_linear_conversions, attribution_rows.w_linear_value AS w_linear_value, attribution_rows.w_decay_conversions AS w_decay_conversions, attribution_rows.w_decay_value AS w_decay_value, attribution_rows.w_position_conversions AS w_position_conversions, attribution_rows.w_position_value AS w_position_value, attribution_footer.total_conversions AS total_conversions, attribution_footer.attributed_conversions AS attributed_conversions, attribution_rows.footer_key AS footer_key
FROM attribution_footer LEFT JOIN attribution_rows ON equals(attribution_footer.footer_key, attribution_rows.footer_key)
@@ -79,7 +79,7 @@
GROUP BY raw_sessions.session_id_v7) AS events__session ON equals(toUInt128(accurateCastOrNull(events.`$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.`$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, toDateTime(%(hogql_val_46)s, %(hogql_val_47)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, groupArrayIf(tuple(toUnixTimestamp(toTimeZone(events.timestamp, %(hogql_val_62)s)), coalesce(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_63)s), ''), 'null'), '^"|"$', ''), %(hogql_val_64)s), 0.0)), and(equals(events.event, %(hogql_val_65)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_66)s), %(hogql_val_67)s), toDateTime(%(hogql_val_68)s, %(hogql_val_69)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_70)s), %(hogql_val_71)s), toDateTime(%(hogql_val_72)s, %(hogql_val_73)s)))) AS conversions, arraySlice(arraySort(groupUniqArrayIf(tuple(toUnixTimestamp(events__session.`$start_timestamp`), if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_74)s)), events__session.`$entry_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(events__session.`$entry_utm_source`, %(hogql_val_80)s)), events__session.`$entry_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(events__session.`$entry_utm_source`, %(hogql_val_91)s)), events__session.`$entry_utm_source`, %(hogql_val_92)s)), [%(hogql_val_93)s]), %(hogql_val_94)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_95)s)), events__session.`$entry_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(events__session.`$entry_utm_source`, %(hogql_val_107)s)), events__session.`$entry_utm_source`, %(hogql_val_108)s)))))), and(equals(events.event, %(hogql_val_109)s), notEmpty(events.`$session_id`), greater(toUnixTimestamp(events__session.`$start_timestamp`), 0)))), -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(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_74)s), ''), 'null'), '^"|"$', ''), %(hogql_val_75)s), 0.0)), and(equals(events.event, %(hogql_val_76)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_77)s), %(hogql_val_78)s), toDateTime(%(hogql_val_79)s, %(hogql_val_80)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_81)s), %(hogql_val_82)s), toDateTime(%(hogql_val_83)s, %(hogql_val_84)s))))), -500) AS conversions, countIf(and(equals(events.event, %(hogql_val_85)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_86)s), %(hogql_val_87)s), toDateTime(%(hogql_val_88)s, %(hogql_val_89)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_90)s), %(hogql_val_91)s), toDateTime(%(hogql_val_92)s, %(hogql_val_93)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_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(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.`$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 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
FROM raw_sessions
@@ -90,21 +90,21 @@
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), globalIn(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), (
- SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS person_id
- FROM 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
- 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), equals(events.event, %(hogql_val_110)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_111)s), %(hogql_val_112)s), toDateTime(%(hogql_val_113)s, %(hogql_val_114)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_115)s), %(hogql_val_116)s), toDateTime(%(hogql_val_117)s, %(hogql_val_118)s)))
- GROUP BY if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id))), or(and(equals(events.event, %(hogql_val_119)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_120)s), %(hogql_val_121)s), toDateTime(%(hogql_val_122)s, %(hogql_val_123)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_124)s), %(hogql_val_125)s), toDateTime(%(hogql_val_126)s, %(hogql_val_127)s))), and(equals(events.event, %(hogql_val_128)s), greaterOrEquals(events.timestamp, minus(toDateTime(%(hogql_val_129)s, %(hogql_val_130)s), toIntervalSecond(345600))), lessOrEquals(events.timestamp, toDateTime(%(hogql_val_131)s, %(hogql_val_132)s)))))
+ 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_62)s))) AS first_conversion, max(toUnixTimestamp(toTimeZone(events.timestamp, %(hogql_val_63)s))) AS last_conversion
+ FROM 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
+ 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), equals(events.event, %(hogql_val_64)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_65)s), %(hogql_val_66)s), toDateTime(%(hogql_val_67)s, %(hogql_val_68)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_69)s), %(hogql_val_70)s), toDateTime(%(hogql_val_71)s, %(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_130)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_131)s), %(hogql_val_132)s), toDateTime(%(hogql_val_133)s, %(hogql_val_134)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_135)s), %(hogql_val_136)s), toDateTime(%(hogql_val_137)s, %(hogql_val_138)s))), and(equals(events.event, %(hogql_val_139)s), greaterOrEquals(events.timestamp, minus(toDateTime(%(hogql_val_140)s, %(hogql_val_141)s), toIntervalSecond(345600))), lessOrEquals(events.timestamp, toDateTime(%(hogql_val_142)s, %(hogql_val_143)s)), greaterOrEquals(toUnixTimestamp(toTimeZone(events.timestamp, %(hogql_val_144)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.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_133)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_134)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_135)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_145)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_146)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_147)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,9 +114,9 @@
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_136)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_148)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 count() AS total_conversions, countIf(ifNull(greater(length(per_conversion.tps), 0), 0)) AS attributed_conversions, 1 AS footer_key
+ 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)
SELECT attribution_rows.breakdown_value AS breakdown_value, attribution_rows.visitors AS visitors, attribution_rows.influenced_conversions AS influenced_conversions, attribution_rows.influenced_value AS influenced_value, attribution_rows.w_first_conversions AS w_first_conversions, attribution_rows.w_first_value AS w_first_value, attribution_rows.w_last_conversions AS w_last_conversions, attribution_rows.w_last_value AS w_last_value, attribution_rows.w_linear_conversions AS w_linear_conversions, attribution_rows.w_linear_value AS w_linear_value, attribution_rows.w_decay_conversions AS w_decay_conversions, attribution_rows.w_decay_value AS w_decay_value, attribution_rows.w_position_conversions AS w_position_conversions, attribution_rows.w_position_value AS w_position_value, attribution_footer.total_conversions AS total_conversions, attribution_footer.attributed_conversions AS attributed_conversions, attribution_rows.footer_key AS footer_key
FROM attribution_footer LEFT JOIN attribution_rows ON equals(attribution_footer.footer_key, attribution_rows.footer_key)
@@ -142,7 +142,7 @@
GROUP BY raw_sessions.session_id_v7) AS events__session ON equals(toUInt128(accurateCastOrNull(events.`$session_id`, %(hogql_val_124)s)), events__session.session_id_v7)
WHERE and(equals(events.team_id, 420), and(equals(events.event, %(hogql_val_127)s), notEmpty(events.`$session_id`), greater(toUnixTimestamp(events__session.`$start_timestamp`), 0)), greaterOrEquals(events.timestamp, minus(toDateTime(%(hogql_val_128)s, %(hogql_val_129)s), toIntervalSecond(345600))), lessOrEquals(events.timestamp, toDateTime(%(hogql_val_130)s, %(hogql_val_131)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, groupArrayIf(tuple(toUnixTimestamp(toTimeZone(events.timestamp, %(hogql_val_263)s)), coalesce(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_264)s), ''), 'null'), '^"|"$', ''), %(hogql_val_265)s), 0.0)), and(equals(events.event, %(hogql_val_266)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_267)s), %(hogql_val_268)s), toDateTime(%(hogql_val_269)s, %(hogql_val_270)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_271)s), %(hogql_val_272)s), toDateTime(%(hogql_val_273)s, %(hogql_val_274)s)))) AS conversions, arraySlice(arraySort(groupUniqArrayIf(tuple(toUnixTimestamp(events__session.`$start_timestamp`), if(notEmpty(ifNull(events__session.`$channel_type`, %(hogql_val_275)s)), events__session.`$channel_type`, %(hogql_val_276)s)), and(equals(events.event, %(hogql_val_277)s), notEmpty(events.`$session_id`), greater(toUnixTimestamp(events__session.`$start_timestamp`), 0)))), -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_274)s)), coalesce(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_275)s), ''), 'null'), '^"|"$', ''), %(hogql_val_276)s), 0.0)), and(equals(events.event, %(hogql_val_277)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_278)s), %(hogql_val_279)s), toDateTime(%(hogql_val_280)s, %(hogql_val_281)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_282)s), %(hogql_val_283)s), toDateTime(%(hogql_val_284)s, %(hogql_val_285)s))))), -500) AS conversions, countIf(and(equals(events.event, %(hogql_val_286)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_287)s), %(hogql_val_288)s), toDateTime(%(hogql_val_289)s, %(hogql_val_290)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_291)s), %(hogql_val_292)s), toDateTime(%(hogql_val_293)s, %(hogql_val_294)s)))) AS conversion_count, arraySlice(arraySort(groupUniqArrayIf(tuple(toUnixTimestamp(events__session.`$start_timestamp`), if(notEmpty(ifNull(events__session.`$channel_type`, %(hogql_val_295)s)), events__session.`$channel_type`, %(hogql_val_296)s)), and(and(equals(events.event, %(hogql_val_297)s), notEmpty(events.`$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 LEFT JOIN (
SELECT min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_132)s)) AS `$start_timestamp`, multiIf(match(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_campaign), %(hogql_val_133)s), %(hogql_val_134)s), %(hogql_val_135)s), %(hogql_val_136)s)), %(hogql_val_137)s), %(hogql_val_138)s, or(in(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_medium), %(hogql_val_139)s), %(hogql_val_140)s), %(hogql_val_141)s), %(hogql_val_142)s)), tuple(%(hogql_val_143)s, %(hogql_val_144)s, %(hogql_val_145)s, %(hogql_val_146)s, %(hogql_val_147)s, %(hogql_val_148)s)), startsWith(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_medium), %(hogql_val_149)s), %(hogql_val_150)s), %(hogql_val_151)s), %(hogql_val_152)s)), %(hogql_val_153)s), isNotNull(nullIf(nullIf(argMinMerge(raw_sessions.initial_gclid), %(hogql_val_154)s), %(hogql_val_155)s)), isNotNull(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_gad_source), %(hogql_val_156)s), %(hogql_val_157)s), %(hogql_val_158)s), %(hogql_val_159)s))), coalesce(coalesce(dictGetOrNull('posthog_test.channel_definition_dict', 'type_if_paid', (coalesce(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_source), %(hogql_val_160)s), %(hogql_val_161)s), %(hogql_val_162)s), %(hogql_val_163)s)), ''), 'source')) , dictGetOrNull('posthog_test.channel_definition_dict', 'type_if_paid', (cutToFirstSignificantSubdomain(coalesce(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_source), %(hogql_val_160)s), %(hogql_val_161)s), %(hogql_val_162)s), %(hogql_val_163)s)), '')), 'source'))), if(match(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_campaign), %(hogql_val_164)s), %(hogql_val_165)s), %(hogql_val_166)s), %(hogql_val_167)s)), %(hogql_val_168)s), %(hogql_val_169)s, NULL), dictGetOrNull('posthog_test.channel_definition_dict', 'type_if_paid', (coalesce(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_medium), %(hogql_val_170)s), %(hogql_val_171)s), %(hogql_val_172)s), %(hogql_val_173)s)), ''), 'medium')), coalesce(dictGetOrNull('posthog_test.channel_definition_dict', 'type_if_paid', (coalesce(nullIf(nullIf(argMinMerge(raw_sessions.initial_referring_domain), %(hogql_val_174)s), %(hogql_val_175)s), ''), 'source')) , dictGetOrNull('posthog_test.channel_definition_dict', 'type_if_paid', (cutToFirstSignificantSubdomain(coalesce(nullIf(nullIf(argMinMerge(raw_sessions.initial_referring_domain), %(hogql_val_174)s), %(hogql_val_175)s), '')), 'source'))), multiIf(ifNull(equals(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_gad_source), %(hogql_val_176)s), %(hogql_val_177)s), %(hogql_val_178)s), %(hogql_val_179)s), %(hogql_val_180)s), 0), %(hogql_val_181)s, match(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_campaign), %(hogql_val_182)s), %(hogql_val_183)s), %(hogql_val_184)s), %(hogql_val_185)s)), %(hogql_val_186)s), %(hogql_val_187)s, isNotNull(nullIf(nullIf(argMinMerge(raw_sessions.initial_fbclid), %(hogql_val_188)s), %(hogql_val_189)s)), %(hogql_val_190)s, %(hogql_val_191)s)), and(ifNull(equals(nullIf(nullIf(argMinMerge(raw_sessions.initial_referring_domain), %(hogql_val_192)s), %(hogql_val_193)s), %(hogql_val_194)s), 0), isNull(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_medium), %(hogql_val_195)s), %(hogql_val_196)s), %(hogql_val_197)s), %(hogql_val_198)s))), or(isNull(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_source), %(hogql_val_199)s), %(hogql_val_200)s), %(hogql_val_201)s), %(hogql_val_202)s))), in(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_source), %(hogql_val_203)s), %(hogql_val_204)s), %(hogql_val_205)s), %(hogql_val_206)s)), tuple(%(hogql_val_207)s, %(hogql_val_208)s, %(hogql_val_209)s))), not(isNotNull(nullIf(nullIf(argMinMerge(raw_sessions.initial_fbclid), %(hogql_val_210)s), %(hogql_val_211)s)))), %(hogql_val_212)s, coalesce(coalesce(dictGetOrNull('posthog_test.channel_definition_dict', 'type_if_organic', (coalesce(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_source), %(hogql_val_213)s), %(hogql_val_214)s), %(hogql_val_215)s), %(hogql_val_216)s)), ''), 'source')), dictGetOrNull('posthog_test.channel_definition_dict', 'type_if_organic', (cutToFirstSignificantSubdomain(coalesce(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_source), %(hogql_val_213)s), %(hogql_val_214)s), %(hogql_val_215)s), %(hogql_val_216)s)), '')), 'source'))), if(match(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_campaign), %(hogql_val_217)s), %(hogql_val_218)s), %(hogql_val_219)s), %(hogql_val_220)s)), %(hogql_val_221)s), %(hogql_val_222)s, NULL), dictGetOrNull('posthog_test.channel_definition_dict', 'type_if_organic', (coalesce(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_medium), %(hogql_val_223)s), %(hogql_val_224)s), %(hogql_val_225)s), %(hogql_val_226)s)), ''), 'medium')), coalesce(dictGetOrNull('posthog_test.channel_definition_dict', 'type_if_organic', (coalesce(nullIf(nullIf(argMinMerge(raw_sessions.initial_referring_domain), %(hogql_val_227)s), %(hogql_val_228)s), ''), 'source')), dictGetOrNull('posthog_test.channel_definition_dict', 'type_if_organic', (cutToFirstSignificantSubdomain(coalesce(nullIf(nullIf(argMinMerge(raw_sessions.initial_referring_domain), %(hogql_val_227)s), %(hogql_val_228)s), '')), 'source'))), multiIf(match(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_campaign), %(hogql_val_229)s), %(hogql_val_230)s), %(hogql_val_231)s), %(hogql_val_232)s)), %(hogql_val_233)s), %(hogql_val_234)s, match(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_medium), %(hogql_val_235)s), %(hogql_val_236)s), %(hogql_val_237)s), %(hogql_val_238)s)), %(hogql_val_239)s), %(hogql_val_240)s, isNotNull(nullIf(nullIf(argMinMerge(raw_sessions.initial_fbclid), %(hogql_val_241)s), %(hogql_val_242)s)), %(hogql_val_243)s, ifNull(equals(nullIf(nullIf(argMinMerge(raw_sessions.initial_referring_domain), %(hogql_val_244)s), %(hogql_val_245)s), %(hogql_val_246)s), 0), %(hogql_val_247)s, isNotNull(nullIf(nullIf(argMinMerge(raw_sessions.initial_referring_domain), %(hogql_val_248)s), %(hogql_val_249)s)), %(hogql_val_250)s, %(hogql_val_251)s))) AS `$channel_type`, raw_sessions.session_id_v7 AS session_id_v7
FROM raw_sessions
@@ -153,21 +153,21 @@
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), globalIn(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), (
- SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS person_id
- FROM 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
- 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), equals(events.event, %(hogql_val_278)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_279)s), %(hogql_val_280)s), toDateTime(%(hogql_val_281)s, %(hogql_val_282)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_283)s), %(hogql_val_284)s), toDateTime(%(hogql_val_285)s, %(hogql_val_286)s)))
- GROUP BY if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id))), or(and(equals(events.event, %(hogql_val_287)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_288)s), %(hogql_val_289)s), toDateTime(%(hogql_val_290)s, %(hogql_val_291)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_292)s), %(hogql_val_293)s), toDateTime(%(hogql_val_294)s, %(hogql_val_295)s))), and(equals(events.event, %(hogql_val_296)s), greaterOrEquals(events.timestamp, minus(toDateTime(%(hogql_val_297)s, %(hogql_val_298)s), toIntervalSecond(345600))), lessOrEquals(events.timestamp, toDateTime(%(hogql_val_299)s, %(hogql_val_300)s)))))
+ 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_263)s))) AS first_conversion, max(toUnixTimestamp(toTimeZone(events.timestamp, %(hogql_val_264)s))) AS last_conversion
+ FROM 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
+ 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), equals(events.event, %(hogql_val_265)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_266)s), %(hogql_val_267)s), toDateTime(%(hogql_val_268)s, %(hogql_val_269)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_270)s), %(hogql_val_271)s), toDateTime(%(hogql_val_272)s, %(hogql_val_273)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_298)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_299)s), %(hogql_val_300)s), toDateTime(%(hogql_val_301)s, %(hogql_val_302)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_303)s), %(hogql_val_304)s), toDateTime(%(hogql_val_305)s, %(hogql_val_306)s))), and(equals(events.event, %(hogql_val_307)s), greaterOrEquals(events.timestamp, minus(toDateTime(%(hogql_val_308)s, %(hogql_val_309)s), toIntervalSecond(345600))), lessOrEquals(events.timestamp, toDateTime(%(hogql_val_310)s, %(hogql_val_311)s)), greaterOrEquals(toUnixTimestamp(toTimeZone(events.timestamp, %(hogql_val_312)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.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_301)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_302)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_303)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_313)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_314)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_315)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 (
@@ -177,9 +177,9 @@
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_304)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_316)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 count() AS total_conversions, countIf(ifNull(greater(length(per_conversion.tps), 0), 0)) AS attributed_conversions, 1 AS footer_key
+ 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)
SELECT attribution_rows.breakdown_value AS breakdown_value, attribution_rows.visitors AS visitors, attribution_rows.influenced_conversions AS influenced_conversions, attribution_rows.influenced_value AS influenced_value, attribution_rows.w_first_conversions AS w_first_conversions, attribution_rows.w_first_value AS w_first_value, attribution_rows.w_last_conversions AS w_last_conversions, attribution_rows.w_last_value AS w_last_value, attribution_rows.w_linear_conversions AS w_linear_conversions, attribution_rows.w_linear_value AS w_linear_value, attribution_rows.w_decay_conversions AS w_decay_conversions, attribution_rows.w_decay_value AS w_decay_value, attribution_rows.w_position_conversions AS w_position_conversions, attribution_rows.w_position_value AS w_position_value, attribution_footer.total_conversions AS total_conversions, attribution_footer.attributed_conversions AS attributed_conversions, attribution_rows.footer_key AS footer_key
FROM attribution_footer LEFT JOIN attribution_rows ON equals(attribution_footer.footer_key, attribution_rows.footer_key)
diff --git a/products/marketing_analytics/backend/hogql_queries/attribution_base.py b/products/marketing_analytics/backend/hogql_queries/attribution_base.py
index 829840a5ea6e..2b85326be6b6 100644
--- a/products/marketing_analytics/backend/hogql_queries/attribution_base.py
+++ b/products/marketing_analytics/backend/hogql_queries/attribution_base.py
@@ -34,6 +34,10 @@ ConversionGoal = ConversionGoalFilter1 | ConversionGoalFilter2 | ConversionGoalF
# Both runners collect per-person arrays under this name before diverging.
PERSON_ARRAYS_CTE = "person_arrays"
+# The person's conversion total, carried alongside the arrays because MAX_CONVERSIONS_PER_PERSON can
+# make the array shorter than it.
+PERSON_CONVERSION_COUNT = "conversion_count"
+
# Ceiling on how many sessions of one person can earn credit. Bots and shared devices would otherwise
# fan out touchpoints x conversions far enough to dominate the query. Touchpoints are sorted before
# truncating and the *most recent* are kept, because only touches within a lookback window of a
@@ -42,6 +46,13 @@ PERSON_ARRAYS_CTE = "person_arrays"
# table; this way they keep their credit, and only first touch becomes approximate for such a person.
MAX_TOUCHPOINTS_PER_PERSON = 500
+# The same ceiling for the other side of the fan-out. Without it, a person with many conversions
+# multiplies the two downstream ARRAY JOINs by an unbounded factor, which is the shape that makes the
+# query run out of memory. The most recent are kept for the mirror of the reason above: a conversion
+# is credited by touchpoints that precede it, and the recent ones are those with touchpoints still in
+# the collected window.
+MAX_CONVERSIONS_PER_PERSON = 500
+
class AttributionQueryRunnerBase(MarketingSessionBreakdownQueryRunnerBase[ResponseType], Generic[ResponseType]):
# Narrower than the session-breakdown base's union: everything below reads attribution-only fields.
@@ -174,7 +185,7 @@ class AttributionQueryRunnerBase(MarketingSessionBreakdownQueryRunnerBase[Respon
),
]
- def _build_converters_select(self, date_range: QueryDateRange) -> ast.SelectQuery:
+ def _build_converters_select(self, date_range: QueryDateRange, *, with_bounds: bool = False) -> ast.SelectQuery:
"""Persons who converted in the window.
This is not an optimization — it is what makes the query affordable. Widening touchpoints from
@@ -182,8 +193,32 @@ class AttributionQueryRunnerBase(MarketingSessionBreakdownQueryRunnerBase[Respon
it to converters (typically low single-digit percent of persons) more than pays that back. Removing
this semi-join changes no results and costs one to two orders of magnitude more.
"""
+ select: list[ast.Expr] = [ast.Field(chain=["events", "person_id"])]
+ if with_bounds:
+ # The window of conversions this person can credit. A touchpoint outside it earns nothing,
+ # so carrying the bounds lets the caller drop it before it reaches the arrays.
+ select.append(
+ ast.Alias(
+ alias="first_conversion",
+ expr=ast.Call(
+ name="min",
+ args=[ast.Call(name="toUnixTimestamp", args=[ast.Field(chain=["events", "timestamp"])])],
+ ),
+ )
+ )
+ # Only the repeat-conversion mode needs an upper end of its own; see the caller.
+ if self.allows_multiple_conversions_per_visitor:
+ select.append(
+ ast.Alias(
+ alias="last_conversion",
+ expr=ast.Call(
+ name="max",
+ args=[ast.Call(name="toUnixTimestamp", args=[ast.Field(chain=["events", "timestamp"])])],
+ ),
+ )
+ )
return ast.SelectQuery(
- select=[ast.Field(chain=["events", "person_id"])],
+ select=select,
select_from=ast.JoinExpr(table=ast.Field(chain=["events"])),
where=ast.And(
exprs=[
@@ -228,6 +263,35 @@ class AttributionQueryRunnerBase(MarketingSessionBreakdownQueryRunnerBase[Respon
],
)
+ # Only the repeat-conversion mode can credit anything after the person's first conversion; the
+ # other mode keeps that conversion alone, so `first_conversion` closes the window on both sides.
+ upper_bound_field = "last_conversion" if self.allows_multiple_conversions_per_visitor else "first_conversion"
+ session_start = ast.Call(
+ name="toUnixTimestamp", args=[ast.Field(chain=["events", "session", "$start_timestamp"])]
+ )
+ window_opens = ast.ArithmeticOperation(
+ left=ast.Field(chain=["conv_bounds", "first_conversion"]),
+ op=ast.ArithmeticOperationOp.Sub,
+ right=ast.Constant(value=self.attribution_window_seconds),
+ )
+
+ # A session that starts before this person's window opens, or after their last creditable
+ # conversion, earns credit from none of them, so it never has to enter the array. Bounds the
+ # session start rather than the event timestamp because that is the value stored as the
+ # touchpoint: a session starting before the last conversion is creditable even when its only
+ # pageview lands after it.
+ creditable_touchpoint = ast.And(
+ exprs=[
+ self._touchpoint_condition(),
+ ast.CompareOperation(left=session_start, op=ast.CompareOperationOp.GtEq, right=window_opens),
+ ast.CompareOperation(
+ left=session_start,
+ op=ast.CompareOperationOp.LtEq,
+ right=ast.Field(chain=["conv_bounds", upper_bound_field]),
+ ),
+ ]
+ )
+
# Sorted before truncating: `groupUniqArray` is hash-backed, so its order is unrelated to time and
# slicing it raw would keep an arbitrary subset. The negative offset takes the tail of the sorted
# array, i.e. the most recent sessions — see MAX_TOUCHPOINTS_PER_PERSON for why that direction.
@@ -249,7 +313,7 @@ class AttributionQueryRunnerBase(MarketingSessionBreakdownQueryRunnerBase[Respon
self._breakdown_expr(),
]
),
- self._touchpoint_condition(),
+ creditable_touchpoint,
],
)
],
@@ -258,6 +322,11 @@ class AttributionQueryRunnerBase(MarketingSessionBreakdownQueryRunnerBase[Respon
],
)
+ # The person's conversion total before MAX_CONVERSIONS_PER_PERSON truncates the array. The
+ # footers report "N of M" as an exact count, so M has to be counted here rather than off the
+ # capped array; a conversion the cap removed is reported as unattributed, not as absent.
+ conversion_count: ast.Expr = ast.Call(name="length", args=[ast.Field(chain=["conversions"])])
+
if not self.allows_multiple_conversions_per_visitor:
# One conversion per person: keep the earliest in the window, so the models attribute the
# journey that led to them first converting rather than an arbitrary later repeat.
@@ -269,21 +338,51 @@ class AttributionQueryRunnerBase(MarketingSessionBreakdownQueryRunnerBase[Respon
ast.Constant(value=1),
],
)
+ else:
+ conversion_count = ast.Call(
+ name="countIf",
+ args=[
+ ast.And(
+ exprs=[
+ self.conversion_condition,
+ *self._get_where_conditions(date_range, date_field="events.timestamp"),
+ ]
+ )
+ ],
+ )
+ conversions = ast.Call(
+ name="arraySlice",
+ args=[
+ ast.Call(name="arraySort", args=[conversions]),
+ ast.Constant(value=-MAX_CONVERSIONS_PER_PERSON),
+ ],
+ )
return ast.SelectQuery(
select=[
ast.Field(chain=["events", "person_id"]),
ast.Alias(alias="conversions", expr=conversions),
+ ast.Alias(alias=PERSON_CONVERSION_COUNT, expr=conversion_count),
ast.Alias(alias="touchpoints", expr=touchpoints),
],
- select_from=ast.JoinExpr(table=ast.Field(chain=["events"])),
+ select_from=ast.JoinExpr(
+ table=ast.Field(chain=["events"]),
+ next_join=ast.JoinExpr(
+ join_type="INNER JOIN",
+ table=self._build_converters_select(date_range, with_bounds=True),
+ alias="conv_bounds",
+ constraint=ast.JoinConstraint(
+ expr=ast.CompareOperation(
+ left=ast.Field(chain=["events", "person_id"]),
+ op=ast.CompareOperationOp.Eq,
+ right=ast.Field(chain=["conv_bounds", "person_id"]),
+ ),
+ constraint_type="ON",
+ ),
+ ),
+ ),
where=ast.And(
exprs=[
- ast.CompareOperation(
- left=ast.Field(chain=["events", "person_id"]),
- op=ast.CompareOperationOp.In,
- right=self._build_converters_select(date_range),
- ),
ast.Or(
exprs=[
ast.And(
@@ -296,6 +395,21 @@ class AttributionQueryRunnerBase(MarketingSessionBreakdownQueryRunnerBase[Respon
exprs=[
self._pageview_condition(),
*self._lookback_date_conditions(date_range),
+ # Narrower than the lookback bound above, and per person: an event
+ # before this person's window opens belongs to a session that
+ # started before it too, so no conversion of theirs can credit it.
+ #
+ # Reads the event timestamp, not the session start the touchpoint
+ # is keyed on, so the scan drops these rows without the session
+ # join having to resolve them first. The upper end has no such
+ # cheap form and is applied on the collected array instead.
+ ast.CompareOperation(
+ left=ast.Call(
+ name="toUnixTimestamp", args=[ast.Field(chain=["events", "timestamp"])]
+ ),
+ op=ast.CompareOperationOp.GtEq,
+ right=window_opens,
+ ),
]
),
]
@@ -310,6 +424,24 @@ class AttributionQueryRunnerBase(MarketingSessionBreakdownQueryRunnerBase[Respon
),
)
+ def _total_conversions_expr(self, conversion_index: str) -> ast.Expr:
+ """The exact conversion total, for a footer reading a CTE exploded by conversion.
+
+ Counting the exploded rows would report the capped total. Every row of one person carries that
+ person's true count instead, so summing it on the first index alone counts each person once.
+ """
+ return ast.Call(
+ name="sumIf",
+ args=[
+ ast.Field(chain=[PERSON_CONVERSION_COUNT]),
+ ast.CompareOperation(
+ left=ast.Field(chain=[conversion_index]),
+ op=ast.CompareOperationOp.Eq,
+ right=ast.Constant(value=1),
+ ),
+ ],
+ )
+
def _in_window_touchpoints_expr(self, conversion_time: ast.Expr) -> ast.Expr:
"""The person's touchpoints that fall inside one conversion's attribution window.
diff --git a/products/marketing_analytics/backend/hogql_queries/attribution_paths_query_runner.py b/products/marketing_analytics/backend/hogql_queries/attribution_paths_query_runner.py
index dac92d0027ad..aaa11a80514b 100644
--- a/products/marketing_analytics/backend/hogql_queries/attribution_paths_query_runner.py
+++ b/products/marketing_analytics/backend/hogql_queries/attribution_paths_query_runner.py
@@ -25,7 +25,7 @@ from posthog.hogql import ast
from posthog.hogql.constants import MAX_BYTES_BEFORE_EXTERNAL_GROUP_BY, HogQLGlobalSettings, LimitContext
from posthog.hogql.query import execute_hogql_query
-from .attribution_base import PERSON_ARRAYS_CTE, AttributionQueryRunnerBase
+from .attribution_base import PERSON_ARRAYS_CTE, PERSON_CONVERSION_COUNT, AttributionQueryRunnerBase
from .constants import PAGINATION_EXTRA
# Paths longer than this are grouped on their most recent steps — the same truncation direction as
@@ -90,6 +90,9 @@ class MarketingAnalyticsAttributionPathsQueryRunner(
return ast.SelectQuery(
select=[
+ # Both ride along so the footer can report the person's exact conversion total.
+ ast.Field(chain=[_CONVERSION_INDEX]),
+ ast.Field(chain=[PERSON_CONVERSION_COUNT]),
ast.Alias(alias="conv_value", expr=ast.TupleAccess(tuple=conversion, index=2)),
ast.Alias(
alias="tps",
@@ -189,7 +192,7 @@ class MarketingAnalyticsAttributionPathsQueryRunner(
"""
return ast.SelectQuery(
select=[
- ast.Alias(alias=_TOTAL_CONVERSIONS, expr=ast.Call(name="count", args=[])),
+ ast.Alias(alias=_TOTAL_CONVERSIONS, expr=self._total_conversions_expr(_CONVERSION_INDEX)),
ast.Alias(
alias=_ATTRIBUTED_CONVERSIONS,
expr=ast.Call(
diff --git a/products/marketing_analytics/backend/hogql_queries/attribution_table_query_runner.py b/products/marketing_analytics/backend/hogql_queries/attribution_table_query_runner.py
index bcdb3923586d..e7e17f332710 100644
--- a/products/marketing_analytics/backend/hogql_queries/attribution_table_query_runner.py
+++ b/products/marketing_analytics/backend/hogql_queries/attribution_table_query_runner.py
@@ -34,7 +34,7 @@ from posthog.hogql.query import execute_hogql_query
from posthog.hogql_queries.utils.query_date_range import QueryDateRange
-from .attribution_base import PERSON_ARRAYS_CTE, AttributionQueryRunnerBase
+from .attribution_base import PERSON_ARRAYS_CTE, PERSON_CONVERSION_COUNT, AttributionQueryRunnerBase
from .attribution_weights import (
build_first_touch_weights,
build_last_touch_weights,
@@ -143,6 +143,8 @@ class MarketingAnalyticsAttributionQueryRunner(AttributionQueryRunnerBase[Market
# because timestamps don't: they're truncated to whole seconds, so two conversions in the
# same second would otherwise group together as one.
ast.Field(chain=[_CONVERSION_INDEX]),
+ # Rides along so the footer can report the person's exact conversion total.
+ ast.Field(chain=[PERSON_CONVERSION_COUNT]),
ast.Alias(alias="conversion_time", expr=ast.TupleAccess(tuple=conversion, index=1)),
ast.Alias(alias="conversion_value", expr=ast.TupleAccess(tuple=conversion, index=2)),
ast.Alias(alias="tps", expr=in_window),
@@ -322,7 +324,7 @@ class MarketingAnalyticsAttributionQueryRunner(AttributionQueryRunnerBase[Market
"""
return ast.SelectQuery(
select=[
- ast.Alias(alias=_TOTAL_CONVERSIONS, expr=ast.Call(name="count", args=[])),
+ ast.Alias(alias=_TOTAL_CONVERSIONS, expr=self._total_conversions_expr(_CONVERSION_INDEX)),
ast.Alias(
alias=_ATTRIBUTED_CONVERSIONS,
expr=ast.Call(
diff --git a/products/marketing_analytics/backend/hogql_queries/test_attribution_table_query_runner.py b/products/marketing_analytics/backend/hogql_queries/test_attribution_table_query_runner.py
index 71be949e221a..c024b92f0b8b 100644
--- a/products/marketing_analytics/backend/hogql_queries/test_attribution_table_query_runner.py
+++ b/products/marketing_analytics/backend/hogql_queries/test_attribution_table_query_runner.py
@@ -21,7 +21,6 @@ from posthog.hogql import ast
from posthog.hogql.printer import prepare_and_print_ast
from posthog.hogql.property_access_types import RestrictedProperty
from posthog.hogql.test.utils import pretty_print_in_tests
-from posthog.hogql.visitor import TraversingVisitor
from posthog.models import PropertyDefinition
from posthog.models.team.team_marketing_analytics_config import MAX_ATTRIBUTION_WINDOW_DAYS
@@ -607,6 +606,64 @@ class TestMarketingAnalyticsAttributionQueryRunner(ClickhouseTestMixin, BaseTest
self.assertEqual(response.totalConversions, 2)
self.assertEqual(response.unattributedConversions, 1)
+ def test_a_session_credits_by_its_start_not_by_when_its_pageview_landed(self):
+ # A touchpoint is keyed on the session start, so the per-person bounds have to be judged there
+ # too. Judging them on the pageview's own timestamp drops a session that opened before the
+ # conversion and only recorded its pageview afterwards.
+ create_person(team=self.team, distinct_ids=["p1"])
+ session_id = str(uuid7(ONE_DAY_BEFORE))
+ # The session opens on a click, which is what sets `$start_timestamp`.
+ _create_event(
+ team=self.team,
+ event="$autocapture",
+ distinct_id="p1",
+ timestamp=ONE_DAY_BEFORE,
+ properties={
+ "$session_id": session_id,
+ "$current_url": "https://example.com/",
+ "$pathname": "/",
+ "$referring_domain": "$direct",
+ "utm_campaign": "late-pageview",
+ },
+ )
+ self._conversion("p1", CONVERSION_AT)
+ _create_event(
+ team=self.team,
+ event="$pageview",
+ distinct_id="p1",
+ timestamp="2023-01-10T13:00:00Z",
+ properties={
+ "$session_id": session_id,
+ "$current_url": "https://example.com/",
+ "$pathname": "/",
+ "$referring_domain": "$direct",
+ "utm_campaign": "late-pageview",
+ },
+ )
+
+ response = self._run(MarketingAnalyticsAttributionBreakdown.CAMPAIGN)
+
+ by_campaign = self._by_breakdown(response)
+ self.assertAlmostEqual(by_campaign["late-pageview"][AttributionMode.LAST_TOUCH], 1.0, places=4)
+
+ @patch(
+ "products.marketing_analytics.backend.hogql_queries.attribution_base.MAX_CONVERSIONS_PER_PERSON",
+ 2,
+ )
+ def test_the_conversion_ceiling_leaves_the_reported_total_exact(self):
+ # The footer publishes "N of M" as an exact count. Counting the ceiling-truncated array would
+ # under-report M for a person above the ceiling, and no field tells a client it happened.
+ create_person(team=self.team, distinct_ids=["p1"])
+ self._session("p1", THREE_DAYS_BEFORE, utm_campaign="a")
+ self._conversion("p1", TWO_DAYS_BEFORE)
+ self._conversion("p1", ONE_DAY_BEFORE)
+ self._conversion("p1", CONVERSION_AT)
+
+ response = self._run(MarketingAnalyticsAttributionBreakdown.CAMPAIGN, allow_multiple_conversions=True)
+
+ self.assertEqual(response.totalConversions, 3)
+ self.assertEqual(response.unattributedConversions, 1)
+
def test_value_columns_are_populated_only_for_revenue_goals(self):
# `hasValue` gates the value columns in the table. If the flag and the numbers disagree the UI
# either hides real revenue or shows a column of nulls.
@@ -641,18 +698,17 @@ class TestMarketingAnalyticsAttributionQueryRunner(ClickhouseTestMixin, BaseTest
person_arrays = ctes["person_arrays"].expr
assert isinstance(person_arrays, ast.SelectQuery)
- class FindSubqueryIn(TraversingVisitor):
- def __init__(self) -> None:
- self.found = False
-
- def visit_compare_operation(self, node: ast.CompareOperation) -> None:
- if node.op == ast.CompareOperationOp.In and isinstance(node.right, ast.SelectQuery):
- self.found = True
- super().visit_compare_operation(node)
-
- finder = FindSubqueryIn()
- finder.visit(person_arrays.where)
- self.assertTrue(finder.found, "person_arrays must restrict the events scan to converting persons")
+ # The restriction is a join against the converters subquery. Asserting on the join rather than
+ # on a bare `IN` keeps the test about the property (only converters are scanned) instead of the
+ # operator that happens to express it.
+ join = person_arrays.select_from
+ assert join is not None
+ restrictions = []
+ while join is not None:
+ if isinstance(join.table, ast.SelectQuery):
+ restrictions.append(join)
+ join = join.next_join
+ self.assertTrue(restrictions, "person_arrays must restrict the events scan to converting persons")
def test_action_goals_credit_the_events_the_action_matches(self):
# The action branch resolves the goal through Postgres and `action_to_expr` rather than a plain