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.

chore(hogql): indent nested subqueries in test pretty-printer (#61708)

Robbied9d2a40c7ad0
diff --git a/posthog/hogql/database/test/__snapshots__/test_database.ambr b/posthog/hogql/database/test/__snapshots__/test_database.ambr
index f65bfe3825f9..2d11964291a8 100644
--- a/posthog/hogql/database/test/__snapshots__/test_database.ambr
+++ b/posthog/hogql/database/test/__snapshots__/test_database.ambr
@@ -4,12 +4,12 @@
SELECT events__some_field.key AS key
FROM events LEFT JOIN (
- SELECT groups.key AS key, key AS events__some_field___key
- FROM (
- SELECT groups.group_type_index AS index, groups.group_key AS key
- FROM groups
- WHERE equals(groups.team_id, 420)
- GROUP BY groups.group_type_index, groups.group_key) AS groups) AS events__some_field ON equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.person_properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', ''), events__some_field.events__some_field___key)
+ SELECT groups.key AS key, key AS events__some_field___key
+ FROM (
+ SELECT groups.group_type_index AS index, groups.group_key AS key
+ FROM groups
+ WHERE equals(groups.team_id, 420)
+ GROUP BY groups.group_type_index, groups.group_key) AS groups) AS events__some_field ON equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.person_properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', ''), events__some_field.events__some_field___key)
WHERE equals(events.team_id, 420)
LIMIT 50000
'''
@@ -19,12 +19,12 @@
SELECT events__some_field.key AS key
FROM events LEFT JOIN (
- SELECT groups.key AS key, key AS events__some_field___key
- FROM (
- SELECT groups.group_type_index AS index, groups.group_key AS key
- FROM groups
- WHERE equals(groups.team_id, 420)
- GROUP BY groups.group_type_index, groups.group_key) AS groups) AS events__some_field ON equals(toString(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.person_properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '')), events__some_field.events__some_field___key)
+ SELECT groups.key AS key, key AS events__some_field___key
+ FROM (
+ SELECT groups.group_type_index AS index, groups.group_key AS key
+ FROM groups
+ WHERE equals(groups.team_id, 420)
+ GROUP BY groups.group_type_index, groups.group_key) AS groups) AS events__some_field ON equals(toString(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.person_properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '')), events__some_field.events__some_field___key)
WHERE equals(events.team_id, 420)
LIMIT 50000
'''
diff --git a/posthog/hogql/functions/test/__snapshots__/test_action.ambr b/posthog/hogql/functions/test/__snapshots__/test_action.ambr
index 0dc357a56665..b3cdac79299c 100644
--- a/posthog/hogql/functions/test/__snapshots__/test_action.ambr
+++ b/posthog/hogql/functions/test/__snapshots__/test_action.ambr
@@ -3,21 +3,21 @@
'''
-- ClickHouse
SELECT
- e.event AS event
+ e.event AS event
FROM
- events AS e
+ events AS e
WHERE
- and(equals(e.team_id, 99999), equals(e.event, %(hogql_val_0)s), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(e.properties, %(hogql_val_1)s), ''), 'null'), '^"|"$', ''), %(hogql_val_2)s), 0))
+ and(equals(e.team_id, 99999), equals(e.event, %(hogql_val_0)s), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(e.properties, %(hogql_val_1)s), ''), 'null'), '^"|"$', ''), %(hogql_val_2)s), 0))
LIMIT 100
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0, transform_null_in=1, optimize_min_equality_disjunction_chain_length=4294967295, allow_experimental_join_condition=1, use_hive_partitioning=0
-- HogQL
SELECT
- event
+ event
FROM
- events AS e
+ events AS e
WHERE
- and(equals(event, 'RANDOM_TEST_ID::UUID'), equals(e.properties.$current_url, 'https://posthog.com/feedback/123?vip=1'))
+ and(equals(event, 'RANDOM_TEST_ID::UUID'), equals(e.properties.$current_url, 'https://posthog.com/feedback/123?vip=1'))
LIMIT 100
'''
# ---
diff --git a/posthog/hogql/functions/test/__snapshots__/test_cohort.ambr b/posthog/hogql/functions/test/__snapshots__/test_cohort.ambr
index ac0d881be78a..1512e3315509 100644
--- a/posthog/hogql/functions/test/__snapshots__/test_cohort.ambr
+++ b/posthog/hogql/functions/test/__snapshots__/test_cohort.ambr
@@ -5,11 +5,11 @@
SELECT events.event AS event
FROM events
WHERE and(equals(events.team_id, 99999), in(events.person_id, (
- SELECT cohortpeople.person_id AS person_id
- FROM cohortpeople
- WHERE and(equals(cohortpeople.team_id, 99999), equals(cohortpeople.cohort_id, XX))
- GROUP BY cohortpeople.person_id, cohortpeople.cohort_id, cohortpeople.version
- HAVING ifNull(greater(sum(cohortpeople.sign), 0), 0))), equals(events.event, %(hogql_val_0)s))
+ SELECT cohortpeople.person_id AS person_id
+ FROM cohortpeople
+ WHERE and(equals(cohortpeople.team_id, 99999), equals(cohortpeople.cohort_id, XX))
+ GROUP BY cohortpeople.person_id, cohortpeople.cohort_id, cohortpeople.version
+ HAVING ifNull(greater(sum(cohortpeople.sign), 0), 0))), equals(events.event, %(hogql_val_0)s))
LIMIT 100
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0, transform_null_in=1, optimize_min_equality_disjunction_chain_length=4294967295, allow_experimental_join_condition=1, use_hive_partitioning=0
@@ -17,11 +17,11 @@
SELECT event
FROM events
WHERE and(in(person_id, (
- SELECT person_id
- FROM raw_cohort_people
- WHERE equals(cohort_id, XX)
- GROUP BY person_id, cohort_id, version
- HAVING greater(sum(sign), 0))), equals(event, 'RANDOM_TEST_ID::UUID'))
+ SELECT person_id
+ FROM raw_cohort_people
+ WHERE equals(cohort_id, XX)
+ GROUP BY person_id, cohort_id, version
+ HAVING greater(sum(sign), 0))), equals(event, 'RANDOM_TEST_ID::UUID'))
LIMIT 100
'''
# ---
@@ -31,11 +31,11 @@
SELECT events.event AS event
FROM events
WHERE and(equals(events.team_id, 99999), in(events.person_id, (
- SELECT cohortpeople.person_id AS person_id
- FROM cohortpeople
- WHERE and(equals(cohortpeople.team_id, 99999), equals(cohortpeople.cohort_id, XX))
- GROUP BY cohortpeople.person_id, cohortpeople.cohort_id, cohortpeople.version
- HAVING ifNull(greater(sum(cohortpeople.sign), 0), 0))), equals(events.event, %(hogql_val_0)s))
+ SELECT cohortpeople.person_id AS person_id
+ FROM cohortpeople
+ WHERE and(equals(cohortpeople.team_id, 99999), equals(cohortpeople.cohort_id, XX))
+ GROUP BY cohortpeople.person_id, cohortpeople.cohort_id, cohortpeople.version
+ HAVING ifNull(greater(sum(cohortpeople.sign), 0), 0))), equals(events.event, %(hogql_val_0)s))
LIMIT 100
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0, transform_null_in=1, optimize_min_equality_disjunction_chain_length=4294967295, allow_experimental_join_condition=1, use_hive_partitioning=0
@@ -43,11 +43,11 @@
SELECT event
FROM events
WHERE and(in(person_id, (
- SELECT person_id
- FROM raw_cohort_people
- WHERE equals(cohort_id, XX)
- GROUP BY person_id, cohort_id, version
- HAVING greater(sum(sign), 0))), equals(event, 'RANDOM_TEST_ID::UUID'))
+ SELECT person_id
+ FROM raw_cohort_people
+ WHERE equals(cohort_id, XX)
+ GROUP BY person_id, cohort_id, version
+ HAVING greater(sum(sign), 0))), equals(event, 'RANDOM_TEST_ID::UUID'))
LIMIT 100
'''
# ---
@@ -57,9 +57,9 @@
SELECT events.event AS event
FROM events
WHERE and(equals(events.team_id, 99999), in(events.person_id, (
- SELECT person_static_cohort.person_id AS person_id
- FROM person_static_cohort
- WHERE and(equals(person_static_cohort.team_id, 99999), equals(person_static_cohort.cohort_id, XX)))))
+ SELECT person_static_cohort.person_id AS person_id
+ FROM person_static_cohort
+ WHERE and(equals(person_static_cohort.team_id, 99999), equals(person_static_cohort.cohort_id, XX)))))
LIMIT 100
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0, transform_null_in=1, optimize_min_equality_disjunction_chain_length=4294967295, allow_experimental_join_condition=1, use_hive_partitioning=0
@@ -67,9 +67,9 @@
SELECT event
FROM events
WHERE in(person_id, (
- SELECT person_id
- FROM static_cohort_people
- WHERE equals(cohort_id, XX)))
+ SELECT person_id
+ FROM static_cohort_people
+ WHERE equals(cohort_id, XX)))
LIMIT 100
'''
# ---
@@ -79,9 +79,9 @@
SELECT events.event AS event
FROM events
WHERE and(equals(events.team_id, 99999), in(events.person_id, (
- SELECT person_static_cohort.person_id AS person_id
- FROM person_static_cohort
- WHERE and(equals(person_static_cohort.team_id, 99999), equals(person_static_cohort.cohort_id, XX)))))
+ SELECT person_static_cohort.person_id AS person_id
+ FROM person_static_cohort
+ WHERE and(equals(person_static_cohort.team_id, 99999), equals(person_static_cohort.cohort_id, XX)))))
LIMIT 100
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0, transform_null_in=1, optimize_min_equality_disjunction_chain_length=4294967295, allow_experimental_join_condition=1, use_hive_partitioning=0
@@ -89,9 +89,9 @@
SELECT event
FROM events
WHERE in(person_id, (
- SELECT person_id
- FROM static_cohort_people
- WHERE equals(cohort_id, XX)))
+ SELECT person_id
+ FROM static_cohort_people
+ WHERE equals(cohort_id, XX)))
LIMIT 100
'''
# ---
diff --git a/posthog/hogql/test/__snapshots__/test_query.ambr b/posthog/hogql/test/__snapshots__/test_query.ambr
index c6099eb77aec..7ce079ac092e 100644
--- a/posthog/hogql/test/__snapshots__/test_query.ambr
+++ b/posthog/hogql/test/__snapshots__/test_query.ambr
@@ -4,12 +4,12 @@
-- ClickHouse
SELECT if(not(empty(events__exception_issue_override.issue_id)), events__exception_issue_override.issue_id, accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_1)s), ''), 'null'), '^"|"$', ''), %(hogql_val_2)s)) AS id, count(DISTINCT events.uuid) AS occurrences, count(DISTINCT nullIf(events.`$session_id`, %(hogql_val_3)s)) AS sessions, count(DISTINCT events.distinct_id) AS users, max(toTimeZone(events.timestamp, %(hogql_val_4)s)) AS last_seen, min(toTimeZone(events.timestamp, %(hogql_val_5)s)) AS first_seen, reverse(arrayMap(x -> countEqual(groupArray(dateDiff(%(hogql_val_6)s, toStartOfHour(toTimeZone(events.timestamp, %(hogql_val_7)s)), toStartOfHour(now64(6, %(hogql_val_8)s)))), x), range(24))) AS volumeDay, reverse(arrayMap(x -> countEqual(groupArray(dateDiff(%(hogql_val_9)s, toStartOfDay(toTimeZone(events.timestamp, %(hogql_val_10)s)), toStartOfDay(now64(6, %(hogql_val_11)s)))), x), range(31))) AS volumeMonth, reverse(arrayMap(x -> countEqual(groupArray(dateDiff(%(hogql_val_12)s, toStartOfHour(toTimeZone(events.timestamp, %(hogql_val_13)s)), toStartOfHour(now64(6, %(hogql_val_14)s)))), x), range(168))) AS customVolume
FROM events LEFT OUTER JOIN (
- SELECT tupleElement(argMax(tuple(error_tracking_issue_fingerprint_overrides.issue_id), error_tracking_issue_fingerprint_overrides.version), 1) AS issue_id, error_tracking_issue_fingerprint_overrides.fingerprint AS fingerprint
- FROM error_tracking_issue_fingerprint_overrides
- WHERE equals(error_tracking_issue_fingerprint_overrides.team_id, 99999)
- GROUP BY error_tracking_issue_fingerprint_overrides.fingerprint
- HAVING ifNull(equals(tupleElement(argMax(tuple(error_tracking_issue_fingerprint_overrides.is_deleted), error_tracking_issue_fingerprint_overrides.version), 1), 0), 0)
- SETTINGS optimize_aggregation_in_order=1) AS events__exception_issue_override ON equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', ''), events__exception_issue_override.fingerprint)
+ SELECT tupleElement(argMax(tuple(error_tracking_issue_fingerprint_overrides.issue_id), error_tracking_issue_fingerprint_overrides.version), 1) AS issue_id, error_tracking_issue_fingerprint_overrides.fingerprint AS fingerprint
+ FROM error_tracking_issue_fingerprint_overrides
+ WHERE equals(error_tracking_issue_fingerprint_overrides.team_id, 99999)
+ GROUP BY error_tracking_issue_fingerprint_overrides.fingerprint
+ HAVING ifNull(equals(tupleElement(argMax(tuple(error_tracking_issue_fingerprint_overrides.is_deleted), error_tracking_issue_fingerprint_overrides.version), 1), 0), 0)
+ SETTINGS optimize_aggregation_in_order=1) AS events__exception_issue_override ON equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', ''), events__exception_issue_override.fingerprint)
WHERE and(equals(events.team_id, 99999), and(equals(events.event, %(hogql_val_15)s), isNotNull(if(not(empty(events__exception_issue_override.issue_id)), events__exception_issue_override.issue_id, accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_16)s), ''), 'null'), '^"|"$', ''), %(hogql_val_17)s))), or(and(greater(toTimeZone(events.timestamp, %(hogql_val_18)s), parseDateTime64BestEffort(%(hogql_val_19)s, 6, %(hogql_val_20)s)), less(toTimeZone(events.timestamp, %(hogql_val_21)s), parseDateTime64BestEffort(%(hogql_val_22)s, 6, %(hogql_val_23)s))), and(greater(toTimeZone(events.timestamp, %(hogql_val_24)s), toDateTime(%(hogql_val_25)s, %(hogql_val_26)s)), less(toTimeZone(events.timestamp, %(hogql_val_27)s), toDateTime64(%(hogql_val_28)s, 6, %(hogql_val_29)s))))))
GROUP BY if(not(empty(events__exception_issue_override.issue_id)), events__exception_issue_override.issue_id, accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_30)s), ''), 'null'), '^"|"$', ''), %(hogql_val_31)s))
ORDER BY occurrences DESC
@@ -75,12 +75,12 @@
'''
-- ClickHouse
WITH latest_events AS (
- SELECT events.distinct_id AS distinct_id, argMax(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', ''), toTimeZone(events.timestamp, %(hogql_val_1)s)) AS latest_os_version
- FROM events
- WHERE and(equals(events.team_id, 99999), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_2)s), ''), 'null'), '^"|"$', ''), %(hogql_val_3)s), 0), greaterOrEquals(events.timestamp, minus(now64(6, %(hogql_val_4)s), toIntervalDay(30))))
- GROUP BY events.distinct_id), major_versions AS (
- SELECT latest_events.distinct_id AS distinct_id, latest_events.latest_os_version AS latest_os_version, splitByChar(%(hogql_val_5)s, ifNull(latest_events.latest_os_version, %(hogql_val_6)s))[1] AS major_version
- FROM latest_events)
+ SELECT events.distinct_id AS distinct_id, argMax(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', ''), toTimeZone(events.timestamp, %(hogql_val_1)s)) AS latest_os_version
+ FROM events
+ WHERE and(equals(events.team_id, 99999), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_2)s), ''), 'null'), '^"|"$', ''), %(hogql_val_3)s), 0), greaterOrEquals(events.timestamp, minus(now64(6, %(hogql_val_4)s), toIntervalDay(30))))
+ GROUP BY events.distinct_id), major_versions AS (
+ SELECT latest_events.distinct_id AS distinct_id, latest_events.latest_os_version AS latest_os_version, splitByChar(%(hogql_val_5)s, ifNull(latest_events.latest_os_version, %(hogql_val_6)s))[1] AS major_version
+ FROM latest_events)
SELECT major_versions.major_version AS major_version, count() AS user_count, round(divide(multiply(100, count()), sum(count()) OVER ()), 2) AS percentage
FROM major_versions
WHERE in(major_versions.major_version, tuple(%(hogql_val_7)s, %(hogql_val_8)s, %(hogql_val_9)s))
@@ -91,12 +91,12 @@
-- HogQL
WITH latest_events AS (
- SELECT distinct_id, argMax(properties.$os_version, timestamp) AS latest_os_version
- FROM events
- WHERE and(equals(properties.$os, 'iOS'), greaterOrEquals(timestamp, minus(now(), toIntervalDay(30))))
- GROUP BY distinct_id), major_versions AS (
- SELECT distinct_id, latest_os_version, splitByChar('.', ifNull(latest_os_version, ''))[1] AS major_version
- FROM latest_events)
+ SELECT distinct_id, argMax(properties.$os_version, timestamp) AS latest_os_version
+ FROM events
+ WHERE and(equals(properties.$os, 'iOS'), greaterOrEquals(timestamp, minus(now(), toIntervalDay(30))))
+ GROUP BY distinct_id), major_versions AS (
+ SELECT distinct_id, latest_os_version, splitByChar('.', ifNull(latest_os_version, ''))[1] AS major_version
+ FROM latest_events)
SELECT major_version, count() AS user_count, round(divide(multiply(100, count()), sum(count()) OVER ()), 2) AS percentage
FROM major_versions
WHERE in(major_version, tuple('17', '18', '26'))
@@ -176,10 +176,10 @@
-- ClickHouse
SELECT sessions.session_id AS session_id, sessions.`$entry_current_url` AS `$entry_current_url`
FROM (
- SELECT toString(reinterpretAsUUID(bitOr(bitShiftLeft(raw_sessions.session_id_v7, 64), bitShiftRight(raw_sessions.session_id_v7, 64)))) AS session_id, nullIf(nullIf(argMinMerge(raw_sessions.entry_url), %(hogql_val_0)s), %(hogql_val_1)s) AS `$entry_current_url`, min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_2)s)) AS `$start_timestamp`, raw_sessions.session_id_v7 AS session_id_v7
- FROM raw_sessions
- WHERE and(equals(raw_sessions.team_id, 99999), lessOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), plus(toDateTime64('today', 6, 'UTC'), toIntervalDay(3))), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(toDateTime64('today', 6, 'UTC'), toIntervalDay(3))))
- GROUP BY raw_sessions.session_id_v7) AS sessions
+ SELECT toString(reinterpretAsUUID(bitOr(bitShiftLeft(raw_sessions.session_id_v7, 64), bitShiftRight(raw_sessions.session_id_v7, 64)))) AS session_id, nullIf(nullIf(argMinMerge(raw_sessions.entry_url), %(hogql_val_0)s), %(hogql_val_1)s) AS `$entry_current_url`, min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_2)s)) AS `$start_timestamp`, raw_sessions.session_id_v7 AS session_id_v7
+ FROM raw_sessions
+ WHERE and(equals(raw_sessions.team_id, 99999), lessOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), plus(toDateTime64('today', 6, 'UTC'), toIntervalDay(3))), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(toDateTime64('today', 6, 'UTC'), toIntervalDay(3))))
+ GROUP BY raw_sessions.session_id_v7) AS sessions
WHERE and(ifNull(less(sessions.`$start_timestamp`, toDateTime64('today', 6, 'UTC')), 0), ifNull(greaterOrEquals(sessions.`$start_timestamp`, toDateTime64('today', 6, 'UTC')), 0))
LIMIT 100
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0, transform_null_in=1, optimize_min_equality_disjunction_chain_length=4294967295, allow_experimental_join_condition=1, use_hive_partitioning=0
@@ -205,10 +205,10 @@
-- ClickHouse
SELECT sessions.session_id AS session_id, sessions.`$entry_current_url` AS `$entry_current_url`
FROM (
- SELECT toString(reinterpretAsUUID(bitOr(bitShiftLeft(raw_sessions.session_id_v7, 64), bitShiftRight(raw_sessions.session_id_v7, 64)))) AS session_id, nullIf(nullIf(argMinMerge(raw_sessions.entry_url), %(hogql_val_0)s), %(hogql_val_1)s) AS `$entry_current_url`, raw_sessions.session_id_v7 AS session_id_v7
- FROM raw_sessions
- WHERE equals(raw_sessions.team_id, 99999)
- GROUP BY raw_sessions.session_id_v7) AS sessions
+ SELECT toString(reinterpretAsUUID(bitOr(bitShiftLeft(raw_sessions.session_id_v7, 64), bitShiftRight(raw_sessions.session_id_v7, 64)))) AS session_id, nullIf(nullIf(argMinMerge(raw_sessions.entry_url), %(hogql_val_0)s), %(hogql_val_1)s) AS `$entry_current_url`, raw_sessions.session_id_v7 AS session_id_v7
+ FROM raw_sessions
+ WHERE equals(raw_sessions.team_id, 99999)
+ GROUP BY raw_sessions.session_id_v7) AS sessions
WHERE ifNull(equals(sessions.`$entry_current_url`, %(hogql_val_2)s), 0)
LIMIT 100
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0, transform_null_in=1, optimize_min_equality_disjunction_chain_length=4294967295, allow_experimental_join_condition=1, use_hive_partitioning=0
@@ -272,14 +272,14 @@
-- ClickHouse
SELECT e.event AS event, s.session_id AS session_id
FROM (
- SELECT e.event AS event, e.`$session_id` AS `$session_id`
- FROM events AS e
- WHERE and(equals(e.team_id, 99999), isNotNull(nullIf(nullIf(e.`$session_id`, ''), 'null')))
- LIMIT 10) AS e LEFT JOIN (
- SELECT session_replay_events.session_id AS session_id
- FROM session_replay_events
- WHERE equals(session_replay_events.team_id, 99999)
- GROUP BY session_replay_events.session_id) AS s ON equals(s.session_id, nullIf(nullIf(e.`$session_id`, ''), 'null'))
+ SELECT e.event AS event, e.`$session_id` AS `$session_id`
+ FROM events AS e
+ WHERE and(equals(e.team_id, 99999), isNotNull(nullIf(nullIf(e.`$session_id`, ''), 'null')))
+ LIMIT 10) AS e LEFT JOIN (
+ SELECT session_replay_events.session_id AS session_id
+ FROM session_replay_events
+ WHERE equals(session_replay_events.team_id, 99999)
+ GROUP BY session_replay_events.session_id) AS s ON equals(s.session_id, nullIf(nullIf(e.`$session_id`, ''), 'null'))
LIMIT 10
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0, transform_null_in=1, optimize_min_equality_disjunction_chain_length=4294967295, allow_experimental_join_condition=1, use_hive_partitioning=0
@@ -304,14 +304,14 @@
-- ClickHouse
SELECT e.event AS event, s.session_id AS session_id
FROM (
- SELECT e.event AS event, e.properties AS properties
- FROM events AS e
- WHERE and(equals(e.team_id, 99999), isNotNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(e.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '')))
- LIMIT 10) AS e LEFT JOIN (
- SELECT session_replay_events.session_id AS session_id
- FROM session_replay_events
- WHERE equals(session_replay_events.team_id, 99999)
- GROUP BY session_replay_events.session_id) AS s ON equals(s.session_id, replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(e.properties, %(hogql_val_1)s), ''), 'null'), '^"|"$', ''))
+ SELECT e.event AS event, e.properties AS properties
+ FROM events AS e
+ WHERE and(equals(e.team_id, 99999), isNotNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(e.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '')))
+ LIMIT 10) AS e LEFT JOIN (
+ SELECT session_replay_events.session_id AS session_id
+ FROM session_replay_events
+ WHERE equals(session_replay_events.team_id, 99999)
+ GROUP BY session_replay_events.session_id) AS s ON equals(s.session_id, replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(e.properties, %(hogql_val_1)s), ''), 'null'), '^"|"$', ''))
LIMIT 10
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0, transform_null_in=1, optimize_min_equality_disjunction_chain_length=4294967295, allow_experimental_join_condition=1, use_hive_partitioning=0
@@ -336,18 +336,18 @@
-- ClickHouse
SELECT events.event AS event, count() AS `count()`
FROM events LEFT OUTER JOIN (
- SELECT tupleElement(argMax(tuple(person_distinct_id_overrides.person_id), person_distinct_id_overrides.version), 1) AS person_id, person_distinct_id_overrides.distinct_id AS distinct_id
- FROM person_distinct_id_overrides
- WHERE equals(person_distinct_id_overrides.team_id, 99999)
- GROUP BY person_distinct_id_overrides.distinct_id
- HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id_overrides.is_deleted), person_distinct_id_overrides.version), 1), 0), 0)
- SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)
+ SELECT tupleElement(argMax(tuple(person_distinct_id_overrides.person_id), person_distinct_id_overrides.version), 1) AS person_id, person_distinct_id_overrides.distinct_id AS distinct_id
+ FROM person_distinct_id_overrides
+ WHERE equals(person_distinct_id_overrides.team_id, 99999)
+ GROUP BY person_distinct_id_overrides.distinct_id
+ HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id_overrides.is_deleted), person_distinct_id_overrides.version), 1), 0), 0)
+ SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)
WHERE and(equals(events.team_id, 99999), in(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), (
- SELECT cohortpeople.person_id AS person_id
- FROM cohortpeople
- WHERE and(equals(cohortpeople.team_id, 99999), equals(cohortpeople.cohort_id, XX))
- GROUP BY cohortpeople.person_id, cohortpeople.cohort_id, cohortpeople.version
- HAVING ifNull(greater(sum(cohortpeople.sign), 0), 0))))
+ SELECT cohortpeople.person_id AS person_id
+ FROM cohortpeople
+ WHERE and(equals(cohortpeople.team_id, 99999), equals(cohortpeople.cohort_id, XX))
+ GROUP BY cohortpeople.person_id, cohortpeople.cohort_id, cohortpeople.version
+ HAVING ifNull(greater(sum(cohortpeople.sign), 0), 0))))
GROUP BY events.event
LIMIT 100
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0, transform_null_in=1, optimize_min_equality_disjunction_chain_length=4294967295, allow_experimental_join_condition=1, use_hive_partitioning=0
@@ -356,11 +356,11 @@
SELECT event, count()
FROM events
WHERE in(person_id, (
- SELECT person_id
- FROM raw_cohort_people
- WHERE equals(cohort_id, XX)
- GROUP BY person_id, cohort_id, version
- HAVING greater(sum(sign), 0)))
+ SELECT person_id
+ FROM raw_cohort_people
+ WHERE equals(cohort_id, XX)
+ GROUP BY person_id, cohort_id, version
+ HAVING greater(sum(sign), 0)))
GROUP BY event
LIMIT 100
'''
@@ -371,11 +371,11 @@
SELECT events.event AS event, count(*) AS `count(*)`
FROM events
WHERE and(equals(events.team_id, 99999), in(events.person_id, (
- SELECT cohortpeople.person_id AS person_id
- FROM cohortpeople
- WHERE and(equals(cohortpeople.team_id, 99999), equals(cohortpeople.cohort_id, XX))
- GROUP BY cohortpeople.person_id, cohortpeople.cohort_id, cohortpeople.version
- HAVING ifNull(greater(sum(cohortpeople.sign), 0), 0))))
+ SELECT cohortpeople.person_id AS person_id
+ FROM cohortpeople
+ WHERE and(equals(cohortpeople.team_id, 99999), equals(cohortpeople.cohort_id, XX))
+ GROUP BY cohortpeople.person_id, cohortpeople.cohort_id, cohortpeople.version
+ HAVING ifNull(greater(sum(cohortpeople.sign), 0), 0))))
GROUP BY events.event
LIMIT 100
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0, transform_null_in=1, optimize_min_equality_disjunction_chain_length=4294967295, allow_experimental_join_condition=1, use_hive_partitioning=0
@@ -384,11 +384,11 @@
SELECT event, count(*)
FROM events
WHERE in(person_id, (
- SELECT person_id
- FROM raw_cohort_people
- WHERE equals(cohort_id, XX)
- GROUP BY person_id, cohort_id, version
- HAVING greater(sum(sign), 0)))
+ SELECT person_id
+ FROM raw_cohort_people
+ WHERE equals(cohort_id, XX)
+ GROUP BY person_id, cohort_id, version
+ HAVING greater(sum(sign), 0)))
GROUP BY event
LIMIT 100
'''
@@ -398,16 +398,16 @@
-- ClickHouse
SELECT events.event AS event, count() AS `count()`
FROM events LEFT OUTER JOIN (
- SELECT tupleElement(argMax(tuple(person_distinct_id_overrides.person_id), person_distinct_id_overrides.version), 1) AS person_id, person_distinct_id_overrides.distinct_id AS distinct_id
- FROM person_distinct_id_overrides
- WHERE equals(person_distinct_id_overrides.team_id, 99999)
- GROUP BY person_distinct_id_overrides.distinct_id
- HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id_overrides.is_deleted), person_distinct_id_overrides.version), 1), 0), 0)
- SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)
+ SELECT tupleElement(argMax(tuple(person_distinct_id_overrides.person_id), person_distinct_id_overrides.version), 1) AS person_id, person_distinct_id_overrides.distinct_id AS distinct_id
+ FROM person_distinct_id_overrides
+ WHERE equals(person_distinct_id_overrides.team_id, 99999)
+ GROUP BY person_distinct_id_overrides.distinct_id
+ HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id_overrides.is_deleted), person_distinct_id_overrides.version), 1), 0), 0)
+ SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)
WHERE and(equals(events.team_id, 99999), in(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), (
- SELECT person_static_cohort.person_id AS person_id
- FROM person_static_cohort
- WHERE and(equals(person_static_cohort.team_id, 99999), equals(person_static_cohort.cohort_id, XX)))))
+ SELECT person_static_cohort.person_id AS person_id
+ FROM person_static_cohort
+ WHERE and(equals(person_static_cohort.team_id, 99999), equals(person_static_cohort.cohort_id, XX)))))
GROUP BY events.event
LIMIT 100
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0, transform_null_in=1, optimize_min_equality_disjunction_chain_length=4294967295, allow_experimental_join_condition=1, use_hive_partitioning=0
@@ -416,9 +416,9 @@
SELECT event, count()
FROM events
WHERE in(person_id, (
- SELECT person_id
- FROM static_cohort_people
- WHERE equals(cohort_id, XX)))
+ SELECT person_id
+ FROM static_cohort_people
+ WHERE equals(cohort_id, XX)))
GROUP BY event
LIMIT 100
'''
@@ -429,9 +429,9 @@
SELECT events.event AS event, count(*) AS `count(*)`
FROM events
WHERE and(equals(events.team_id, 99999), in(events.person_id, (
- SELECT person_static_cohort.person_id AS person_id
- FROM person_static_cohort
- WHERE and(equals(person_static_cohort.team_id, 99999), equals(person_static_cohort.cohort_id, XX)))))
+ SELECT person_static_cohort.person_id AS person_id
+ FROM person_static_cohort
+ WHERE and(equals(person_static_cohort.team_id, 99999), equals(person_static_cohort.cohort_id, XX)))))
GROUP BY events.event
LIMIT 100
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0, transform_null_in=1, optimize_min_equality_disjunction_chain_length=4294967295, allow_experimental_join_condition=1, use_hive_partitioning=0
@@ -440,9 +440,9 @@
SELECT event, count(*)
FROM events
WHERE in(person_id, (
- SELECT person_id
- FROM static_cohort_people
- WHERE equals(cohort_id, XX)))
+ SELECT person_id
+ FROM static_cohort_people
+ WHERE equals(cohort_id, XX)))
GROUP BY event
LIMIT 100
'''
@@ -470,14 +470,14 @@
-- ClickHouse
SELECT DISTINCT persons.properties___sneaky_mail AS sneaky_mail
FROM (
- SELECT tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '')), person.version), 1) AS properties___sneaky_mail, tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_1)s), ''), 'null'), '^"|"$', '')), person.version), 1) AS properties___random_uuid, person.id AS id
- FROM person
- WHERE and(equals(person.team_id, 99999), in(id, (
- SELECT where_optimization.id AS id
- FROM person AS where_optimization
- WHERE and(equals(where_optimization.team_id, 99999), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, %(hogql_val_2)s), ''), 'null'), '^"|"$', ''), %(hogql_val_3)s), 0)))))
- GROUP BY person.id
- HAVING and(ifNull(equals(tupleElement(argMax(tuple(person.is_deleted), person.version), 1), 0), 0), ifNull(less(tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_4)s)), person.version), 1), plus(now64(6, %(hogql_val_5)s), toIntervalDay(1))), 0))) AS persons
+ SELECT tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '')), person.version), 1) AS properties___sneaky_mail, tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_1)s), ''), 'null'), '^"|"$', '')), person.version), 1) AS properties___random_uuid, person.id AS id
+ FROM person
+ WHERE and(equals(person.team_id, 99999), in(id, (
+ SELECT where_optimization.id AS id
+ FROM person AS where_optimization
+ WHERE and(equals(where_optimization.team_id, 99999), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, %(hogql_val_2)s), ''), 'null'), '^"|"$', ''), %(hogql_val_3)s), 0)))))
+ GROUP BY person.id
+ HAVING and(ifNull(equals(tupleElement(argMax(tuple(person.is_deleted), person.version), 1), 0), 0), ifNull(less(tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_4)s)), person.version), 1), plus(now64(6, %(hogql_val_5)s), toIntervalDay(1))), 0))) AS persons
WHERE ifNull(equals(persons.properties___random_uuid, %(hogql_val_6)s), 0)
LIMIT 100
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0, transform_null_in=1, optimize_min_equality_disjunction_chain_length=4294967295, allow_experimental_join_condition=1, use_hive_partitioning=0
@@ -494,12 +494,12 @@
-- ClickHouse
SELECT e.event AS event, toTimeZone(e.timestamp, %(hogql_val_0)s) AS timestamp, e__pdi.distinct_id AS distinct_id, e__pdi.person_id AS person_id
FROM events AS e INNER JOIN (
- SELECT tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS person_id, person_distinct_id2.distinct_id AS distinct_id
- FROM person_distinct_id2
- WHERE equals(person_distinct_id2.team_id, 99999)
- GROUP BY person_distinct_id2.distinct_id
- HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id2.is_deleted), person_distinct_id2.version), 1), 0), 0)
- SETTINGS optimize_aggregation_in_order=1) AS e__pdi ON equals(e.distinct_id, e__pdi.distinct_id)
+ SELECT tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS person_id, person_distinct_id2.distinct_id AS distinct_id
+ FROM person_distinct_id2
+ WHERE equals(person_distinct_id2.team_id, 99999)
+ GROUP BY person_distinct_id2.distinct_id
+ HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id2.is_deleted), person_distinct_id2.version), 1), 0), 0)
+ SETTINGS optimize_aggregation_in_order=1) AS e__pdi ON equals(e.distinct_id, e__pdi.distinct_id)
WHERE equals(e.team_id, 99999)
LIMIT 10
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0, transform_null_in=1, optimize_min_equality_disjunction_chain_length=4294967295, allow_experimental_join_condition=1, use_hive_partitioning=0
@@ -515,21 +515,21 @@
-- ClickHouse
SELECT events.event AS event, persons.properties___email AS email
FROM events LEFT OUTER JOIN (
- SELECT tupleElement(argMax(tuple(person_distinct_id_overrides.person_id), person_distinct_id_overrides.version), 1) AS person_id, person_distinct_id_overrides.distinct_id AS distinct_id
- FROM person_distinct_id_overrides
- WHERE equals(person_distinct_id_overrides.team_id, 99999)
- GROUP BY person_distinct_id_overrides.distinct_id
- HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id_overrides.is_deleted), person_distinct_id_overrides.version), 1), 0), 0)
- SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id) JOIN (
- SELECT person.id AS id, replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '') AS properties___email
- FROM person
- WHERE and(equals(person.team_id, 99999), in(tuple(person.id, person.version), (
- SELECT person.id AS id, max(person.version) AS version
- FROM person
- WHERE equals(person.team_id, 99999)
- GROUP BY person.id
- HAVING and(ifNull(equals(argMax(person.is_deleted, person.version), 0), 0), ifNull(less(argMax(toTimeZone(person.created_at, %(hogql_val_1)s), person.version), plus(now64(6, %(hogql_val_2)s), toIntervalDay(1))), 0)))))
- SETTINGS optimize_aggregation_in_order=1) AS persons ON equals(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), persons.id)
+ SELECT tupleElement(argMax(tuple(person_distinct_id_overrides.person_id), person_distinct_id_overrides.version), 1) AS person_id, person_distinct_id_overrides.distinct_id AS distinct_id
+ FROM person_distinct_id_overrides
+ WHERE equals(person_distinct_id_overrides.team_id, 99999)
+ GROUP BY person_distinct_id_overrides.distinct_id
+ HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id_overrides.is_deleted), person_distinct_id_overrides.version), 1), 0), 0)
+ SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id) JOIN (
+ SELECT person.id AS id, replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '') AS properties___email
+ FROM person
+ WHERE and(equals(person.team_id, 99999), in(tuple(person.id, person.version), (
+ SELECT person.id AS id, max(person.version) AS version
+ FROM person
+ WHERE equals(person.team_id, 99999)
+ GROUP BY person.id
+ HAVING and(ifNull(equals(argMax(person.is_deleted, person.version), 0), 0), ifNull(less(argMax(toTimeZone(person.created_at, %(hogql_val_1)s), person.version), plus(now64(6, %(hogql_val_2)s), toIntervalDay(1))), 0)))))
+ SETTINGS optimize_aggregation_in_order=1) AS persons ON equals(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), persons.id)
WHERE and(equals(events.team_id, 99999), equals(events.event, %(hogql_val_3)s))
LIMIT 100
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0, transform_null_in=1, optimize_min_equality_disjunction_chain_length=4294967295, allow_experimental_join_condition=1, use_hive_partitioning=0
@@ -546,12 +546,12 @@
-- ClickHouse
SELECT events.event AS event, toTimeZone(events.timestamp, %(hogql_val_0)s) AS timestamp, events__pdi.distinct_id AS distinct_id, events__pdi.person_id AS person_id
FROM events INNER JOIN (
- SELECT tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS person_id, person_distinct_id2.distinct_id AS distinct_id
- FROM person_distinct_id2
- WHERE equals(person_distinct_id2.team_id, 99999)
- GROUP BY person_distinct_id2.distinct_id
- HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id2.is_deleted), person_distinct_id2.version), 1), 0), 0)
- SETTINGS optimize_aggregation_in_order=1) AS events__pdi ON equals(events.distinct_id, events__pdi.distinct_id)
+ SELECT tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS person_id, person_distinct_id2.distinct_id AS distinct_id
+ FROM person_distinct_id2
+ WHERE equals(person_distinct_id2.team_id, 99999)
+ GROUP BY person_distinct_id2.distinct_id
+ HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id2.is_deleted), person_distinct_id2.version), 1), 0), 0)
+ SETTINGS optimize_aggregation_in_order=1) AS events__pdi ON equals(events.distinct_id, events__pdi.distinct_id)
WHERE equals(events.team_id, 99999)
LIMIT 10
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0, transform_null_in=1, optimize_min_equality_disjunction_chain_length=4294967295, allow_experimental_join_condition=1, use_hive_partitioning=0
@@ -567,21 +567,21 @@
-- ClickHouse
SELECT e.event AS event, toTimeZone(e.timestamp, %(hogql_val_3)s) AS timestamp, e__pdi.distinct_id AS distinct_id, e__pdi__person.properties___sneaky_mail AS sneaky_mail
FROM events AS e INNER JOIN (
- SELECT tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS e__pdi___person_id, tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS person_id, person_distinct_id2.distinct_id AS distinct_id
- FROM person_distinct_id2
- WHERE equals(person_distinct_id2.team_id, 99999)
- GROUP BY person_distinct_id2.distinct_id
- HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id2.is_deleted), person_distinct_id2.version), 1), 0), 0)
- SETTINGS optimize_aggregation_in_order=1) AS e__pdi ON equals(e.distinct_id, e__pdi.distinct_id) LEFT JOIN (
- SELECT person.id AS id, replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '') AS properties___sneaky_mail
- FROM person
- WHERE and(equals(person.team_id, 99999), in(tuple(person.id, person.version), (
- SELECT person.id AS id, max(person.version) AS version
- FROM person
- WHERE equals(person.team_id, 99999)
- GROUP BY person.id
- HAVING and(ifNull(equals(argMax(person.is_deleted, person.version), 0), 0), ifNull(less(argMax(toTimeZone(person.created_at, %(hogql_val_1)s), person.version), plus(now64(6, %(hogql_val_2)s), toIntervalDay(1))), 0)))))
- SETTINGS optimize_aggregation_in_order=1) AS e__pdi__person ON equals(e__pdi.e__pdi___person_id, e__pdi__person.id)
+ SELECT tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS e__pdi___person_id, tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS person_id, person_distinct_id2.distinct_id AS distinct_id
+ FROM person_distinct_id2
+ WHERE equals(person_distinct_id2.team_id, 99999)
+ GROUP BY person_distinct_id2.distinct_id
+ HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id2.is_deleted), person_distinct_id2.version), 1), 0), 0)
+ SETTINGS optimize_aggregation_in_order=1) AS e__pdi ON equals(e.distinct_id, e__pdi.distinct_id) LEFT JOIN (
+ SELECT person.id AS id, replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '') AS properties___sneaky_mail
+ FROM person
+ WHERE and(equals(person.team_id, 99999), in(tuple(person.id, person.version), (
+ SELECT person.id AS id, max(person.version) AS version
+ FROM person
+ WHERE equals(person.team_id, 99999)
+ GROUP BY person.id
+ HAVING and(ifNull(equals(argMax(person.is_deleted, person.version), 0), 0), ifNull(less(argMax(toTimeZone(person.created_at, %(hogql_val_1)s), person.version), plus(now64(6, %(hogql_val_2)s), toIntervalDay(1))), 0)))))
+ SETTINGS optimize_aggregation_in_order=1) AS e__pdi__person ON equals(e__pdi.e__pdi___person_id, e__pdi__person.id)
WHERE equals(e.team_id, 99999)
LIMIT 10
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0, transform_null_in=1, optimize_min_equality_disjunction_chain_length=4294967295, allow_experimental_join_condition=1, use_hive_partitioning=0
@@ -597,18 +597,18 @@
-- ClickHouse
SELECT events.event AS event, toTimeZone(events.timestamp, %(hogql_val_2)s) AS timestamp, events__pdi.distinct_id AS distinct_id, events__pdi__person.id AS id
FROM events INNER JOIN (
- SELECT tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS events__pdi___person_id, tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS person_id, person_distinct_id2.distinct_id AS distinct_id
- FROM person_distinct_id2
- WHERE equals(person_distinct_id2.team_id, 99999)
- GROUP BY person_distinct_id2.distinct_id
- HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id2.is_deleted), person_distinct_id2.version), 1), 0), 0)
- SETTINGS optimize_aggregation_in_order=1) AS events__pdi ON equals(events.distinct_id, events__pdi.distinct_id) LEFT JOIN (
- SELECT person.id AS id
- FROM person
- WHERE equals(person.team_id, 99999)
- GROUP BY person.id
- HAVING and(ifNull(equals(tupleElement(argMax(tuple(person.is_deleted), person.version), 1), 0), 0), ifNull(less(tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_0)s)), person.version), 1), plus(now64(6, %(hogql_val_1)s), toIntervalDay(1))), 0))
- SETTINGS optimize_aggregation_in_order=1) AS events__pdi__person ON equals(events__pdi.events__pdi___person_id, events__pdi__person.id)
+ SELECT tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS events__pdi___person_id, tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS person_id, person_distinct_id2.distinct_id AS distinct_id
+ FROM person_distinct_id2
+ WHERE equals(person_distinct_id2.team_id, 99999)
+ GROUP BY person_distinct_id2.distinct_id
+ HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id2.is_deleted), person_distinct_id2.version), 1), 0), 0)
+ SETTINGS optimize_aggregation_in_order=1) AS events__pdi ON equals(events.distinct_id, events__pdi.distinct_id) LEFT JOIN (
+ SELECT person.id AS id
+ FROM person
+ WHERE equals(person.team_id, 99999)
+ GROUP BY person.id
+ HAVING and(ifNull(equals(tupleElement(argMax(tuple(person.is_deleted), person.version), 1), 0), 0), ifNull(less(tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_0)s)), person.version), 1), plus(now64(6, %(hogql_val_1)s), toIntervalDay(1))), 0))
+ SETTINGS optimize_aggregation_in_order=1) AS events__pdi__person ON equals(events__pdi.events__pdi___person_id, events__pdi__person.id)
WHERE equals(events.team_id, 99999)
LIMIT 10
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0, transform_null_in=1, optimize_min_equality_disjunction_chain_length=4294967295, allow_experimental_join_condition=1, use_hive_partitioning=0
@@ -624,21 +624,21 @@
-- ClickHouse
SELECT events.event AS event, toTimeZone(events.timestamp, %(hogql_val_3)s) AS timestamp, events__pdi.distinct_id AS distinct_id, events__pdi__person.properties___sneaky_mail AS sneaky_mail
FROM events INNER JOIN (
- SELECT tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS events__pdi___person_id, tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS person_id, person_distinct_id2.distinct_id AS distinct_id
- FROM person_distinct_id2
- WHERE equals(person_distinct_id2.team_id, 99999)
- GROUP BY person_distinct_id2.distinct_id
- HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id2.is_deleted), person_distinct_id2.version), 1), 0), 0)
- SETTINGS optimize_aggregation_in_order=1) AS events__pdi ON equals(events.distinct_id, events__pdi.distinct_id) LEFT JOIN (
- SELECT person.id AS id, replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '') AS properties___sneaky_mail
- FROM person
- WHERE and(equals(person.team_id, 99999), in(tuple(person.id, person.version), (
- SELECT person.id AS id, max(person.version) AS version
- FROM person
- WHERE equals(person.team_id, 99999)
- GROUP BY person.id
- HAVING and(ifNull(equals(argMax(person.is_deleted, person.version), 0), 0), ifNull(less(argMax(toTimeZone(person.created_at, %(hogql_val_1)s), person.version), plus(now64(6, %(hogql_val_2)s), toIntervalDay(1))), 0)))))
- SETTINGS optimize_aggregation_in_order=1) AS events__pdi__person ON equals(events__pdi.events__pdi___person_id, events__pdi__person.id)
+ SELECT tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS events__pdi___person_id, tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS person_id, person_distinct_id2.distinct_id AS distinct_id
+ FROM person_distinct_id2
+ WHERE equals(person_distinct_id2.team_id, 99999)
+ GROUP BY person_distinct_id2.distinct_id
+ HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id2.is_deleted), person_distinct_id2.version), 1), 0), 0)
+ SETTINGS optimize_aggregation_in_order=1) AS events__pdi ON equals(events.distinct_id, events__pdi.distinct_id) LEFT JOIN (
+ SELECT person.id AS id, replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '') AS properties___sneaky_mail
+ FROM person
+ WHERE and(equals(person.team_id, 99999), in(tuple(person.id, person.version), (
+ SELECT person.id AS id, max(person.version) AS version
+ FROM person
+ WHERE equals(person.team_id, 99999)
+ GROUP BY person.id
+ HAVING and(ifNull(equals(argMax(person.is_deleted, person.version), 0), 0), ifNull(less(argMax(toTimeZone(person.created_at, %(hogql_val_1)s), person.version), plus(now64(6, %(hogql_val_2)s), toIntervalDay(1))), 0)))))
+ SETTINGS optimize_aggregation_in_order=1) AS events__pdi__person ON equals(events__pdi.events__pdi___person_id, events__pdi__person.id)
WHERE equals(events.team_id, 99999)
LIMIT 10
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0, transform_null_in=1, optimize_min_equality_disjunction_chain_length=4294967295, allow_experimental_join_condition=1, use_hive_partitioning=0
@@ -654,21 +654,21 @@
-- ClickHouse
SELECT e.event AS event, toTimeZone(e.timestamp, %(hogql_val_3)s) AS timestamp, e__pdi__person.properties___sneaky_mail AS sneaky_mail
FROM events AS e INNER JOIN (
- SELECT tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS e__pdi___person_id, tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS person_id, person_distinct_id2.distinct_id AS distinct_id
- FROM person_distinct_id2
- WHERE equals(person_distinct_id2.team_id, 99999)
- GROUP BY person_distinct_id2.distinct_id
- HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id2.is_deleted), person_distinct_id2.version), 1), 0), 0)
- SETTINGS optimize_aggregation_in_order=1) AS e__pdi ON equals(e.distinct_id, e__pdi.distinct_id) LEFT JOIN (
- SELECT person.id AS id, replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '') AS properties___sneaky_mail
- FROM person
- WHERE and(equals(person.team_id, 99999), in(tuple(person.id, person.version), (
- SELECT person.id AS id, max(person.version) AS version
- FROM person
- WHERE equals(person.team_id, 99999)
- GROUP BY person.id
- HAVING and(ifNull(equals(argMax(person.is_deleted, person.version), 0), 0), ifNull(less(argMax(toTimeZone(person.created_at, %(hogql_val_1)s), person.version), plus(now64(6, %(hogql_val_2)s), toIntervalDay(1))), 0)))))
- SETTINGS optimize_aggregation_in_order=1) AS e__pdi__person ON equals(e__pdi.e__pdi___person_id, e__pdi__person.id)
+ SELECT tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS e__pdi___person_id, tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS person_id, person_distinct_id2.distinct_id AS distinct_id
+ FROM person_distinct_id2
+ WHERE equals(person_distinct_id2.team_id, 99999)
+ GROUP BY person_distinct_id2.distinct_id
+ HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id2.is_deleted), person_distinct_id2.version), 1), 0), 0)
+ SETTINGS optimize_aggregation_in_order=1) AS e__pdi ON equals(e.distinct_id, e__pdi.distinct_id) LEFT JOIN (
+ SELECT person.id AS id, replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '') AS properties___sneaky_mail
+ FROM person
+ WHERE and(equals(person.team_id, 99999), in(tuple(person.id, person.version), (
+ SELECT person.id AS id, max(person.version) AS version
+ FROM person
+ WHERE equals(person.team_id, 99999)
+ GROUP BY person.id
+ HAVING and(ifNull(equals(argMax(person.is_deleted, person.version), 0), 0), ifNull(less(argMax(toTimeZone(person.created_at, %(hogql_val_1)s), person.version), plus(now64(6, %(hogql_val_2)s), toIntervalDay(1))), 0)))))
+ SETTINGS optimize_aggregation_in_order=1) AS e__pdi__person ON equals(e__pdi.e__pdi___person_id, e__pdi__person.id)
WHERE equals(e.team_id, 99999)
LIMIT 10
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0, transform_null_in=1, optimize_min_equality_disjunction_chain_length=4294967295, allow_experimental_join_condition=1, use_hive_partitioning=0
@@ -684,21 +684,21 @@
-- ClickHouse
SELECT s__pdi__person.properties___sneaky_mail AS sneaky_mail, count() AS `count()`
FROM events AS s INNER JOIN (
- SELECT tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS s__pdi___person_id, tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS person_id, person_distinct_id2.distinct_id AS distinct_id
- FROM person_distinct_id2
- WHERE equals(person_distinct_id2.team_id, 99999)
- GROUP BY person_distinct_id2.distinct_id
- HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id2.is_deleted), person_distinct_id2.version), 1), 0), 0)
- SETTINGS optimize_aggregation_in_order=1) AS s__pdi ON equals(s.distinct_id, s__pdi.distinct_id) LEFT JOIN (
- SELECT person.id AS id, replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '') AS properties___sneaky_mail
- FROM person
- WHERE and(equals(person.team_id, 99999), in(tuple(person.id, person.version), (
- SELECT person.id AS id, max(person.version) AS version
- FROM person
- WHERE equals(person.team_id, 99999)
- GROUP BY person.id
- HAVING and(ifNull(equals(argMax(person.is_deleted, person.version), 0), 0), ifNull(less(argMax(toTimeZone(person.created_at, %(hogql_val_1)s), person.version), plus(now64(6, %(hogql_val_2)s), toIntervalDay(1))), 0)))))
- SETTINGS optimize_aggregation_in_order=1) AS s__pdi__person ON equals(s__pdi.s__pdi___person_id, s__pdi__person.id)
+ SELECT tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS s__pdi___person_id, tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS person_id, person_distinct_id2.distinct_id AS distinct_id
+ FROM person_distinct_id2
+ WHERE equals(person_distinct_id2.team_id, 99999)
+ GROUP BY person_distinct_id2.distinct_id
+ HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id2.is_deleted), person_distinct_id2.version), 1), 0), 0)
+ SETTINGS optimize_aggregation_in_order=1) AS s__pdi ON equals(s.distinct_id, s__pdi.distinct_id) LEFT JOIN (
+ SELECT person.id AS id, replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '') AS properties___sneaky_mail
+ FROM person
+ WHERE and(equals(person.team_id, 99999), in(tuple(person.id, person.version), (
+ SELECT person.id AS id, max(person.version) AS version
+ FROM person
+ WHERE equals(person.team_id, 99999)
+ GROUP BY person.id
+ HAVING and(ifNull(equals(argMax(person.is_deleted, person.version), 0), 0), ifNull(less(argMax(toTimeZone(person.created_at, %(hogql_val_1)s), person.version), plus(now64(6, %(hogql_val_2)s), toIntervalDay(1))), 0)))))
+ SETTINGS optimize_aggregation_in_order=1) AS s__pdi__person ON equals(s__pdi.s__pdi___person_id, s__pdi__person.id)
WHERE equals(s.team_id, 99999)
GROUP BY s__pdi__person.properties___sneaky_mail
LIMIT 10
@@ -716,21 +716,21 @@
-- ClickHouse
SELECT e1.event, e2.event AS event
FROM events AS e1 LEFT OUTER JOIN (
- SELECT tupleElement(argMax(tuple(person_distinct_id_overrides.person_id), person_distinct_id_overrides.version), 1) AS person_id, person_distinct_id_overrides.distinct_id AS distinct_id
- FROM person_distinct_id_overrides
- WHERE equals(person_distinct_id_overrides.team_id, 99999)
- GROUP BY person_distinct_id_overrides.distinct_id
- HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id_overrides.is_deleted), person_distinct_id_overrides.version), 1), 0), 0)
- SETTINGS optimize_aggregation_in_order=1) AS e1__override ON equals(e1.distinct_id, e1__override.distinct_id) JOIN (
- SELECT e2.distinct_id AS distinct_id, e2.event AS event, e2.person_id AS event_person_id, if(not(empty(e2__override.distinct_id)), e2__override.person_id, e2.person_id) AS person_id
- FROM events AS e2 LEFT OUTER JOIN (
- SELECT tupleElement(argMax(tuple(person_distinct_id_overrides.person_id), person_distinct_id_overrides.version), 1) AS person_id, person_distinct_id_overrides.distinct_id AS distinct_id
- FROM person_distinct_id_overrides
- WHERE equals(person_distinct_id_overrides.team_id, 99999)
- GROUP BY person_distinct_id_overrides.distinct_id
- HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id_overrides.is_deleted), person_distinct_id_overrides.version), 1), 0), 0)
- SETTINGS optimize_aggregation_in_order=1) AS e2__override ON equals(e2.distinct_id, e2__override.distinct_id)
- WHERE equals(e2.team_id, 99999)) AS e2 ON equals(if(not(empty(e1__override.distinct_id)), e1__override.person_id, e1.person_id), e2.person_id)
+ SELECT tupleElement(argMax(tuple(person_distinct_id_overrides.person_id), person_distinct_id_overrides.version), 1) AS person_id, person_distinct_id_overrides.distinct_id AS distinct_id
+ FROM person_distinct_id_overrides
+ WHERE equals(person_distinct_id_overrides.team_id, 99999)
+ GROUP BY person_distinct_id_overrides.distinct_id
+ HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id_overrides.is_deleted), person_distinct_id_overrides.version), 1), 0), 0)
+ SETTINGS optimize_aggregation_in_order=1) AS e1__override ON equals(e1.distinct_id, e1__override.distinct_id) JOIN (
+ SELECT e2.distinct_id AS distinct_id, e2.event AS event, e2.person_id AS event_person_id, if(not(empty(e2__override.distinct_id)), e2__override.person_id, e2.person_id) AS person_id
+ FROM events AS e2 LEFT OUTER JOIN (
+ SELECT tupleElement(argMax(tuple(person_distinct_id_overrides.person_id), person_distinct_id_overrides.version), 1) AS person_id, person_distinct_id_overrides.distinct_id AS distinct_id
+ FROM person_distinct_id_overrides
+ WHERE equals(person_distinct_id_overrides.team_id, 99999)
+ GROUP BY person_distinct_id_overrides.distinct_id
+ HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id_overrides.is_deleted), person_distinct_id_overrides.version), 1), 0), 0)
+ SETTINGS optimize_aggregation_in_order=1) AS e2__override ON equals(e2.distinct_id, e2__override.distinct_id)
+ WHERE equals(e2.team_id, 99999)) AS e2 ON equals(if(not(empty(e1__override.distinct_id)), e1__override.person_id, e1.person_id), e2.person_id)
WHERE and(equals(e1.team_id, 99999), equals(e1.event, %(hogql_val_0)s), equals(e2.event, %(hogql_val_1)s))
LIMIT 100
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0, transform_null_in=1, optimize_min_equality_disjunction_chain_length=4294967295, allow_experimental_join_condition=1, use_hive_partitioning=0
@@ -747,11 +747,11 @@
-- ClickHouse
SELECT e.event AS event, toTimeZone(e.timestamp, %(hogql_val_0)s) AS timestamp, pdi.person_id AS person_id
FROM events AS e INNER JOIN (
- SELECT person_distinct_id2.distinct_id AS distinct_id, argMax(person_distinct_id2.person_id, person_distinct_id2.version) AS person_id
- FROM person_distinct_id2
- WHERE equals(person_distinct_id2.team_id, 99999)
- GROUP BY person_distinct_id2.distinct_id
- HAVING ifNull(equals(argMax(person_distinct_id2.is_deleted, person_distinct_id2.version), 0), 0)) AS pdi ON equals(e.distinct_id, pdi.distinct_id)
+ SELECT person_distinct_id2.distinct_id AS distinct_id, argMax(person_distinct_id2.person_id, person_distinct_id2.version) AS person_id
+ FROM person_distinct_id2
+ WHERE equals(person_distinct_id2.team_id, 99999)
+ GROUP BY person_distinct_id2.distinct_id
+ HAVING ifNull(equals(argMax(person_distinct_id2.is_deleted, person_distinct_id2.version), 0), 0)) AS pdi ON equals(e.distinct_id, pdi.distinct_id)
WHERE equals(e.team_id, 99999)
LIMIT 100
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0, transform_null_in=1, optimize_min_equality_disjunction_chain_length=4294967295, allow_experimental_join_condition=1, use_hive_partitioning=0
@@ -759,10 +759,10 @@
-- HogQL
SELECT event, timestamp, pdi.person_id
FROM events AS e INNER JOIN (
- SELECT distinct_id, argMax(person_id, version) AS person_id
- FROM raw_person_distinct_ids
- GROUP BY distinct_id
- HAVING equals(argMax(is_deleted, version), 0)) AS pdi ON equals(e.distinct_id, pdi.distinct_id)
+ SELECT distinct_id, argMax(person_id, version) AS person_id
+ FROM raw_person_distinct_ids
+ GROUP BY distinct_id
+ HAVING equals(argMax(is_deleted, version), 0)) AS pdi ON equals(e.distinct_id, pdi.distinct_id)
LIMIT 100
'''
# ---
@@ -771,21 +771,21 @@
-- ClickHouse
SELECT pdi.distinct_id AS distinct_id, pdi__person.properties___sneaky_mail AS sneaky_mail
FROM (
- SELECT tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS pdi___person_id, tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS person_id, person_distinct_id2.distinct_id AS distinct_id
- FROM person_distinct_id2
- WHERE equals(person_distinct_id2.team_id, 99999)
- GROUP BY person_distinct_id2.distinct_id
- HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id2.is_deleted), person_distinct_id2.version), 1), 0), 0)
- SETTINGS optimize_aggregation_in_order=1) AS pdi LEFT JOIN (
- SELECT person.id AS id, replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '') AS properties___sneaky_mail
- FROM person
- WHERE and(equals(person.team_id, 99999), in(tuple(person.id, person.version), (
- SELECT person.id AS id, max(person.version) AS version
- FROM person
- WHERE equals(person.team_id, 99999)
- GROUP BY person.id
- HAVING and(ifNull(equals(argMax(person.is_deleted, person.version), 0), 0), ifNull(less(argMax(toTimeZone(person.created_at, %(hogql_val_1)s), person.version), plus(now64(6, %(hogql_val_2)s), toIntervalDay(1))), 0)))))
- SETTINGS optimize_aggregation_in_order=1) AS pdi__person ON equals(pdi.pdi___person_id, pdi__person.id)
+ SELECT tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS pdi___person_id, tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS person_id, person_distinct_id2.distinct_id AS distinct_id
+ FROM person_distinct_id2
+ WHERE equals(person_distinct_id2.team_id, 99999)
+ GROUP BY person_distinct_id2.distinct_id
+ HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id2.is_deleted), person_distinct_id2.version), 1), 0), 0)
+ SETTINGS optimize_aggregation_in_order=1) AS pdi LEFT JOIN (
+ SELECT person.id AS id, replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '') AS properties___sneaky_mail
+ FROM person
+ WHERE and(equals(person.team_id, 99999), in(tuple(person.id, person.version), (
+ SELECT person.id AS id, max(person.version) AS version
+ FROM person
+ WHERE equals(person.team_id, 99999)
+ GROUP BY person.id
+ HAVING and(ifNull(equals(argMax(person.is_deleted, person.version), 0), 0), ifNull(less(argMax(toTimeZone(person.created_at, %(hogql_val_1)s), person.version), plus(now64(6, %(hogql_val_2)s), toIntervalDay(1))), 0)))))
+ SETTINGS optimize_aggregation_in_order=1) AS pdi__person ON equals(pdi.pdi___person_id, pdi__person.id)
LIMIT 10
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0, transform_null_in=1, optimize_min_equality_disjunction_chain_length=4294967295, allow_experimental_join_condition=1, use_hive_partitioning=0
@@ -800,18 +800,18 @@
-- ClickHouse
SELECT pdi.distinct_id AS distinct_id, pdi__person.created_at AS created_at
FROM (
- SELECT tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS pdi___person_id, tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS person_id, person_distinct_id2.distinct_id AS distinct_id
- FROM person_distinct_id2
- WHERE equals(person_distinct_id2.team_id, 99999)
- GROUP BY person_distinct_id2.distinct_id
- HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id2.is_deleted), person_distinct_id2.version), 1), 0), 0)
- SETTINGS optimize_aggregation_in_order=1) AS pdi LEFT JOIN (
- SELECT tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_0)s)), person.version), 1) AS created_at, person.id AS id
- FROM person
- WHERE equals(person.team_id, 99999)
- GROUP BY person.id
- HAVING and(ifNull(equals(tupleElement(argMax(tuple(person.is_deleted), person.version), 1), 0), 0), ifNull(less(tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_1)s)), person.version), 1), plus(now64(6, %(hogql_val_2)s), toIntervalDay(1))), 0))
- SETTINGS optimize_aggregation_in_order=1) AS pdi__person ON equals(pdi.pdi___person_id, pdi__person.id)
+ SELECT tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS pdi___person_id, tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS person_id, person_distinct_id2.distinct_id AS distinct_id
+ FROM person_distinct_id2
+ WHERE equals(person_distinct_id2.team_id, 99999)
+ GROUP BY person_distinct_id2.distinct_id
+ HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id2.is_deleted), person_distinct_id2.version), 1), 0), 0)
+ SETTINGS optimize_aggregation_in_order=1) AS pdi LEFT JOIN (
+ SELECT tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_0)s)), person.version), 1) AS created_at, person.id AS id
+ FROM person
+ WHERE equals(person.team_id, 99999)
+ GROUP BY person.id
+ HAVING and(ifNull(equals(tupleElement(argMax(tuple(person.is_deleted), person.version), 1), 0), 0), ifNull(less(tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_1)s)), person.version), 1), plus(now64(6, %(hogql_val_2)s), toIntervalDay(1))), 0))
+ SETTINGS optimize_aggregation_in_order=1) AS pdi__person ON equals(pdi.pdi___person_id, pdi__person.id)
LIMIT 10
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0, transform_null_in=1, optimize_min_equality_disjunction_chain_length=4294967295, allow_experimental_join_condition=1, use_hive_partitioning=0
@@ -826,21 +826,21 @@
-- ClickHouse
SELECT persons.id AS id, events.event AS event
FROM (
- SELECT person.id AS id
- FROM person
- WHERE equals(person.team_id, 99999)
- GROUP BY person.id
- HAVING and(ifNull(equals(tupleElement(argMax(tuple(person.is_deleted), person.version), 1), 0), 0), ifNull(less(tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_0)s)), person.version), 1), plus(now64(6, %(hogql_val_1)s), toIntervalDay(1))), 0))
- SETTINGS optimize_aggregation_in_order=1) AS persons JOIN (
- SELECT events.distinct_id AS distinct_id, events.event AS event, events.person_id AS event_person_id, if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS person_id
- FROM events LEFT OUTER JOIN (
- SELECT tupleElement(argMax(tuple(person_distinct_id_overrides.person_id), person_distinct_id_overrides.version), 1) AS person_id, person_distinct_id_overrides.distinct_id AS distinct_id
- FROM person_distinct_id_overrides
- WHERE equals(person_distinct_id_overrides.team_id, 99999)
- GROUP BY person_distinct_id_overrides.distinct_id
- HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id_overrides.is_deleted), person_distinct_id_overrides.version), 1), 0), 0)
- SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)
- WHERE equals(events.team_id, 99999)) AS events ON equals(persons.id, events.person_id)
+ SELECT person.id AS id
+ FROM person
+ WHERE equals(person.team_id, 99999)
+ GROUP BY person.id
+ HAVING and(ifNull(equals(tupleElement(argMax(tuple(person.is_deleted), person.version), 1), 0), 0), ifNull(less(tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_0)s)), person.version), 1), plus(now64(6, %(hogql_val_1)s), toIntervalDay(1))), 0))
+ SETTINGS optimize_aggregation_in_order=1) AS persons JOIN (
+ SELECT events.distinct_id AS distinct_id, events.event AS event, events.person_id AS event_person_id, if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS person_id
+ FROM events LEFT OUTER JOIN (
+ SELECT tupleElement(argMax(tuple(person_distinct_id_overrides.person_id), person_distinct_id_overrides.version), 1) AS person_id, person_distinct_id_overrides.distinct_id AS distinct_id
+ FROM person_distinct_id_overrides
+ WHERE equals(person_distinct_id_overrides.team_id, 99999)
+ GROUP BY person_distinct_id_overrides.distinct_id
+ HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id_overrides.is_deleted), person_distinct_id_overrides.version), 1), 0), 0)
+ SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)
+ WHERE equals(events.team_id, 99999)) AS events ON equals(persons.id, events.person_id)
LIMIT 10
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0, transform_null_in=1, optimize_min_equality_disjunction_chain_length=4294967295, allow_experimental_join_condition=1, use_hive_partitioning=0
@@ -855,21 +855,21 @@
-- ClickHouse
SELECT e.event AS event, toTimeZone(e.timestamp, %(hogql_val_3)s) AS timestamp, pdi.distinct_id AS distinct_id, p.id AS id, p.properties___sneaky_mail AS sneaky_mail
FROM events AS e LEFT JOIN (
- SELECT tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS person_id, person_distinct_id2.distinct_id AS distinct_id
- FROM person_distinct_id2
- WHERE equals(person_distinct_id2.team_id, 99999)
- GROUP BY person_distinct_id2.distinct_id
- HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id2.is_deleted), person_distinct_id2.version), 1), 0), 0)
- SETTINGS optimize_aggregation_in_order=1) AS pdi ON equals(pdi.distinct_id, e.distinct_id) LEFT JOIN (
- SELECT person.id AS id, replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '') AS properties___sneaky_mail
- FROM person
- WHERE and(equals(person.team_id, 99999), in(tuple(person.id, person.version), (
- SELECT person.id AS id, max(person.version) AS version
- FROM person
- WHERE equals(person.team_id, 99999)
- GROUP BY person.id
- HAVING and(ifNull(equals(argMax(person.is_deleted, person.version), 0), 0), ifNull(less(argMax(toTimeZone(person.created_at, %(hogql_val_1)s), person.version), plus(now64(6, %(hogql_val_2)s), toIntervalDay(1))), 0)))))
- SETTINGS optimize_aggregation_in_order=1) AS p ON equals(p.id, pdi.person_id)
+ SELECT tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS person_id, person_distinct_id2.distinct_id AS distinct_id
+ FROM person_distinct_id2
+ WHERE equals(person_distinct_id2.team_id, 99999)
+ GROUP BY person_distinct_id2.distinct_id
+ HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id2.is_deleted), person_distinct_id2.version), 1), 0), 0)
+ SETTINGS optimize_aggregation_in_order=1) AS pdi ON equals(pdi.distinct_id, e.distinct_id) LEFT JOIN (
+ SELECT person.id AS id, replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '') AS properties___sneaky_mail
+ FROM person
+ WHERE and(equals(person.team_id, 99999), in(tuple(person.id, person.version), (
+ SELECT person.id AS id, max(person.version) AS version
+ FROM person
+ WHERE equals(person.team_id, 99999)
+ GROUP BY person.id
+ HAVING and(ifNull(equals(argMax(person.is_deleted, person.version), 0), 0), ifNull(less(argMax(toTimeZone(person.created_at, %(hogql_val_1)s), person.version), plus(now64(6, %(hogql_val_2)s), toIntervalDay(1))), 0)))))
+ SETTINGS optimize_aggregation_in_order=1) AS p ON equals(p.id, pdi.person_id)
WHERE equals(e.team_id, 99999)
LIMIT 100
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0, transform_null_in=1, optimize_min_equality_disjunction_chain_length=4294967295, allow_experimental_join_condition=1, use_hive_partitioning=0
@@ -885,12 +885,12 @@
-- ClickHouse
SELECT DISTINCT person_distinct_ids.person_id AS person_id, person_distinct_ids.distinct_id AS distinct_id
FROM (
- SELECT tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS person_id, person_distinct_id2.distinct_id AS distinct_id
- FROM person_distinct_id2
- WHERE equals(person_distinct_id2.team_id, 99999)
- GROUP BY person_distinct_id2.distinct_id
- HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id2.is_deleted), person_distinct_id2.version), 1), 0), 0)
- SETTINGS optimize_aggregation_in_order=1) AS person_distinct_ids
+ SELECT tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS person_id, person_distinct_id2.distinct_id AS distinct_id
+ FROM person_distinct_id2
+ WHERE equals(person_distinct_id2.team_id, 99999)
+ GROUP BY person_distinct_id2.distinct_id
+ HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id2.is_deleted), person_distinct_id2.version), 1), 0), 0)
+ SETTINGS optimize_aggregation_in_order=1) AS person_distinct_ids
LIMIT 100
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0, transform_null_in=1, optimize_min_equality_disjunction_chain_length=4294967295, allow_experimental_join_condition=1, use_hive_partitioning=0
@@ -905,21 +905,21 @@
-- ClickHouse
SELECT events.event AS event, toTimeZone(events.timestamp, %(hogql_val_3)s) AS timestamp, events__person.id AS id, events__person.properties___sneaky_mail AS sneaky_mail
FROM events LEFT OUTER JOIN (
- SELECT tupleElement(argMax(tuple(person_distinct_id_overrides.person_id), person_distinct_id_overrides.version), 1) AS person_id, person_distinct_id_overrides.distinct_id AS distinct_id
- FROM person_distinct_id_overrides
- WHERE equals(person_distinct_id_overrides.team_id, 99999)
- GROUP BY person_distinct_id_overrides.distinct_id
- HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id_overrides.is_deleted), person_distinct_id_overrides.version), 1), 0), 0)
- SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id) LEFT JOIN (
- SELECT person.id AS id, replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '') AS properties___sneaky_mail
- FROM person
- WHERE and(equals(person.team_id, 99999), in(tuple(person.id, person.version), (
- SELECT person.id AS id, max(person.version) AS version
- FROM person
- WHERE equals(person.team_id, 99999)
- GROUP BY person.id
- HAVING and(ifNull(equals(argMax(person.is_deleted, person.version), 0), 0), ifNull(less(argMax(toTimeZone(person.created_at, %(hogql_val_1)s), person.version), plus(now64(6, %(hogql_val_2)s), toIntervalDay(1))), 0)))))
- SETTINGS optimize_aggregation_in_order=1) AS events__person ON equals(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), events__person.id)
+ SELECT tupleElement(argMax(tuple(person_distinct_id_overrides.person_id), person_distinct_id_overrides.version), 1) AS person_id, person_distinct_id_overrides.distinct_id AS distinct_id
+ FROM person_distinct_id_overrides
+ WHERE equals(person_distinct_id_overrides.team_id, 99999)
+ GROUP BY person_distinct_id_overrides.distinct_id
+ HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id_overrides.is_deleted), person_distinct_id_overrides.version), 1), 0), 0)
+ SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id) LEFT JOIN (
+ SELECT person.id AS id, replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '') AS properties___sneaky_mail
+ FROM person
+ WHERE and(equals(person.team_id, 99999), in(tuple(person.id, person.version), (
+ SELECT person.id AS id, max(person.version) AS version
+ FROM person
+ WHERE equals(person.team_id, 99999)
+ GROUP BY person.id
+ HAVING and(ifNull(equals(argMax(person.is_deleted, person.version), 0), 0), ifNull(less(argMax(toTimeZone(person.created_at, %(hogql_val_1)s), person.version), plus(now64(6, %(hogql_val_2)s), toIntervalDay(1))), 0)))))
+ SETTINGS optimize_aggregation_in_order=1) AS events__person ON equals(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), events__person.id)
WHERE equals(events.team_id, 99999)
LIMIT 10
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0, transform_null_in=1, optimize_min_equality_disjunction_chain_length=4294967295, allow_experimental_join_condition=1, use_hive_partitioning=0
@@ -967,10 +967,10 @@
-- ClickHouse
SELECT cnt AS cnt, event AS event
FROM (
- SELECT count() AS cnt, events.event AS event
- FROM events
- WHERE and(equals(events.team_id, 99999), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', ''), %(hogql_val_1)s), 0))
- GROUP BY events.event)
+ SELECT count() AS cnt, events.event AS event
+ FROM events
+ WHERE and(equals(events.team_id, 99999), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', ''), %(hogql_val_1)s), 0))
+ GROUP BY events.event)
GROUP BY cnt, event
LIMIT 100
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0, transform_null_in=1, optimize_min_equality_disjunction_chain_length=4294967295, allow_experimental_join_condition=1, use_hive_partitioning=0
@@ -978,10 +978,10 @@
-- HogQL
SELECT cnt, event
FROM (
- SELECT count() AS cnt, event
- FROM events
- WHERE equals(properties.random_uuid, 'RANDOM_TEST_ID::UUID')
- GROUP BY event)
+ SELECT count() AS cnt, event
+ FROM events
+ WHERE equals(properties.random_uuid, 'RANDOM_TEST_ID::UUID')
+ GROUP BY event)
GROUP BY cnt, event
LIMIT 100
'''
@@ -991,10 +991,10 @@
-- ClickHouse
SELECT c.cnt AS cnt, c.event AS event
FROM (
- SELECT count(*) AS cnt, events.event AS event
- FROM events
- WHERE and(equals(events.team_id, 99999), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', ''), %(hogql_val_1)s), 0))
- GROUP BY events.event) AS c
+ SELECT count(*) AS cnt, events.event AS event
+ FROM events
+ WHERE and(equals(events.team_id, 99999), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', ''), %(hogql_val_1)s), 0))
+ GROUP BY events.event) AS c
GROUP BY c.cnt, c.event
LIMIT 100
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0, transform_null_in=1, optimize_min_equality_disjunction_chain_length=4294967295, allow_experimental_join_condition=1, use_hive_partitioning=0
@@ -1002,10 +1002,10 @@
-- HogQL
SELECT cnt, event
FROM (
- SELECT count(*) AS cnt, event
- FROM events
- WHERE equals(properties.random_uuid, 'RANDOM_TEST_ID::UUID')
- GROUP BY event) AS c
+ SELECT count(*) AS cnt, event
+ FROM events
+ WHERE equals(properties.random_uuid, 'RANDOM_TEST_ID::UUID')
+ GROUP BY event) AS c
GROUP BY cnt, event
LIMIT 100
'''
@@ -1015,13 +1015,13 @@
-- ClickHouse
SELECT col_a AS col_a, arrayZip((sumMap((g).1, (g).2) AS x).1, x.2) AS r
FROM (
- SELECT col_a AS col_a, groupArray(tuple(col_b, col_c)) AS g
- FROM (
- SELECT replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '') AS col_a, events.event AS col_b, count() AS col_c
- FROM events
- WHERE equals(events.team_id, 99999)
- GROUP BY replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_1)s), ''), 'null'), '^"|"$', ''), events.event)
- GROUP BY col_a)
+ SELECT col_a AS col_a, groupArray(tuple(col_b, col_c)) AS g
+ FROM (
+ SELECT replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '') AS col_a, events.event AS col_b, count() AS col_c
+ FROM events
+ WHERE equals(events.team_id, 99999)
+ GROUP BY replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_1)s), ''), 'null'), '^"|"$', ''), events.event)
+ GROUP BY col_a)
GROUP BY col_a
ORDER BY col_a ASC
LIMIT 100
@@ -1030,12 +1030,12 @@
-- HogQL
SELECT col_a, arrayZip((sumMap((g).1, (g).2) AS x).1, x.2) AS r
FROM (
- SELECT col_a, groupArray(tuple(col_b, col_c)) AS g
- FROM (
- SELECT properties.index AS col_a, event AS col_b, count() AS col_c
- FROM events
- GROUP BY properties.index, event)
- GROUP BY col_a)
+ SELECT col_a, groupArray(tuple(col_b, col_c)) AS g
+ FROM (
+ SELECT properties.index AS col_a, event AS col_b, count() AS col_c
+ FROM events
+ GROUP BY properties.index, event)
+ GROUP BY col_a)
GROUP BY col_a
ORDER BY col_a ASC
LIMIT 100
@@ -1045,16 +1045,16 @@
'''
-- ClickHouse
WITH PIVOT_TABLE_COL_ABC AS (
- SELECT replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '') AS col_a, events.event AS col_b, count() AS col_c
- FROM events
- WHERE equals(events.team_id, 99999)
- GROUP BY replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_1)s), ''), 'null'), '^"|"$', ''), events.event), PIVOT_FUNCTION_1 AS (
- SELECT PIVOT_TABLE_COL_ABC.col_a AS col_a, groupArray(tuple(PIVOT_TABLE_COL_ABC.col_b, PIVOT_TABLE_COL_ABC.col_c)) AS g
- FROM PIVOT_TABLE_COL_ABC
- GROUP BY PIVOT_TABLE_COL_ABC.col_a), PIVOT_FUNCTION_2 AS (
- SELECT PIVOT_FUNCTION_1.col_a AS col_a, arrayZip((sumMap((PIVOT_FUNCTION_1.g).1, (PIVOT_FUNCTION_1.g).2) AS x).1, x.2) AS r
- FROM PIVOT_FUNCTION_1
- GROUP BY PIVOT_FUNCTION_1.col_a)
+ SELECT replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '') AS col_a, events.event AS col_b, count() AS col_c
+ FROM events
+ WHERE equals(events.team_id, 99999)
+ GROUP BY replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_1)s), ''), 'null'), '^"|"$', ''), events.event), PIVOT_FUNCTION_1 AS (
+ SELECT PIVOT_TABLE_COL_ABC.col_a AS col_a, groupArray(tuple(PIVOT_TABLE_COL_ABC.col_b, PIVOT_TABLE_COL_ABC.col_c)) AS g
+ FROM PIVOT_TABLE_COL_ABC
+ GROUP BY PIVOT_TABLE_COL_ABC.col_a), PIVOT_FUNCTION_2 AS (
+ SELECT PIVOT_FUNCTION_1.col_a AS col_a, arrayZip((sumMap((PIVOT_FUNCTION_1.g).1, (PIVOT_FUNCTION_1.g).2) AS x).1, x.2) AS r
+ FROM PIVOT_FUNCTION_1
+ GROUP BY PIVOT_FUNCTION_1.col_a)
SELECT PIVOT_FUNCTION_2.col_a AS col_a, PIVOT_FUNCTION_2.r AS r
FROM PIVOT_FUNCTION_2
ORDER BY PIVOT_FUNCTION_2.col_a ASC
@@ -1063,15 +1063,15 @@
-- HogQL
WITH PIVOT_TABLE_COL_ABC AS (
- SELECT properties.index AS col_a, event AS col_b, count() AS col_c
- FROM events
- GROUP BY properties.index, event), PIVOT_FUNCTION_1 AS (
- SELECT col_a, groupArray(tuple(col_b, col_c)) AS g
- FROM PIVOT_TABLE_COL_ABC
- GROUP BY col_a), PIVOT_FUNCTION_2 AS (
- SELECT col_a, arrayZip((sumMap((g).1, (g).2) AS x).1, x.2) AS r
- FROM PIVOT_FUNCTION_1
- GROUP BY col_a)
+ SELECT properties.index AS col_a, event AS col_b, count() AS col_c
+ FROM events
+ GROUP BY properties.index, event), PIVOT_FUNCTION_1 AS (
+ SELECT col_a, groupArray(tuple(col_b, col_c)) AS g
+ FROM PIVOT_TABLE_COL_ABC
+ GROUP BY col_a), PIVOT_FUNCTION_2 AS (
+ SELECT col_a, arrayZip((sumMap((g).1, (g).2) AS x).1, x.2) AS r
+ FROM PIVOT_FUNCTION_1
+ GROUP BY col_a)
SELECT col_a, r
FROM PIVOT_FUNCTION_2
ORDER BY col_a ASC
@@ -1082,18 +1082,18 @@
'''
-- ClickHouse
WITH PIVOT_TABLE_COL_ABC AS (
- SELECT replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '') AS col_a, events.event AS col_b, count() AS col_c
- FROM events
- WHERE equals(events.team_id, 99999)
- GROUP BY replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_1)s), ''), 'null'), '^"|"$', ''), events.event), PIVOT_FUNCTION_1 AS (
- SELECT PIVOT_TABLE_COL_ABC.col_a AS col_a, groupArray(tuple(PIVOT_TABLE_COL_ABC.col_b, PIVOT_TABLE_COL_ABC.col_c)) AS g
- FROM PIVOT_TABLE_COL_ABC
- GROUP BY PIVOT_TABLE_COL_ABC.col_a), PIVOT_FUNCTION_2 AS (
- SELECT PIVOT_FUNCTION_1.col_a AS col_a, arrayZip((sumMap((PIVOT_FUNCTION_1.g).1, (PIVOT_FUNCTION_1.g).2) AS x).1, x.2) AS r
- FROM PIVOT_FUNCTION_1
- GROUP BY PIVOT_FUNCTION_1.col_a), final AS (
- SELECT PIVOT_FUNCTION_2.col_a AS col_a, PIVOT_FUNCTION_2.r AS r
- FROM PIVOT_FUNCTION_2)
+ SELECT replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '') AS col_a, events.event AS col_b, count() AS col_c
+ FROM events
+ WHERE equals(events.team_id, 99999)
+ GROUP BY replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_1)s), ''), 'null'), '^"|"$', ''), events.event), PIVOT_FUNCTION_1 AS (
+ SELECT PIVOT_TABLE_COL_ABC.col_a AS col_a, groupArray(tuple(PIVOT_TABLE_COL_ABC.col_b, PIVOT_TABLE_COL_ABC.col_c)) AS g
+ FROM PIVOT_TABLE_COL_ABC
+ GROUP BY PIVOT_TABLE_COL_ABC.col_a), PIVOT_FUNCTION_2 AS (
+ SELECT PIVOT_FUNCTION_1.col_a AS col_a, arrayZip((sumMap((PIVOT_FUNCTION_1.g).1, (PIVOT_FUNCTION_1.g).2) AS x).1, x.2) AS r
+ FROM PIVOT_FUNCTION_1
+ GROUP BY PIVOT_FUNCTION_1.col_a), final AS (
+ SELECT PIVOT_FUNCTION_2.col_a AS col_a, PIVOT_FUNCTION_2.r AS r
+ FROM PIVOT_FUNCTION_2)
SELECT final.col_a AS col_a, final.r AS r
FROM final
ORDER BY final.col_a ASC
@@ -1102,17 +1102,17 @@
-- HogQL
WITH PIVOT_TABLE_COL_ABC AS (
- SELECT properties.index AS col_a, event AS col_b, count() AS col_c
- FROM events
- GROUP BY properties.index, event), PIVOT_FUNCTION_1 AS (
- SELECT col_a, groupArray(tuple(col_b, col_c)) AS g
- FROM PIVOT_TABLE_COL_ABC
- GROUP BY col_a), PIVOT_FUNCTION_2 AS (
- SELECT col_a, arrayZip((sumMap((g).1, (g).2) AS x).1, x.2) AS r
- FROM PIVOT_FUNCTION_1
- GROUP BY col_a), final AS (
- SELECT col_a, r
- FROM PIVOT_FUNCTION_2)
+ SELECT properties.index AS col_a, event AS col_b, count() AS col_c
+ FROM events
+ GROUP BY properties.index, event), PIVOT_FUNCTION_1 AS (
+ SELECT col_a, groupArray(tuple(col_b, col_c)) AS g
+ FROM PIVOT_TABLE_COL_ABC
+ GROUP BY col_a), PIVOT_FUNCTION_2 AS (
+ SELECT col_a, arrayZip((sumMap((g).1, (g).2) AS x).1, x.2) AS r
+ FROM PIVOT_FUNCTION_1
+ GROUP BY col_a), final AS (
+ SELECT col_a, r
+ FROM PIVOT_FUNCTION_2)
SELECT col_a, r
FROM final
ORDER BY col_a ASC
diff --git a/posthog/hogql/test/test_utils.py b/posthog/hogql/test/test_utils.py
index cd24dd321b18..068b5a86d77b 100644
--- a/posthog/hogql/test/test_utils.py
+++ b/posthog/hogql/test/test_utils.py
@@ -556,7 +556,7 @@ class TestPrettyPrintInTests(TestCase):
(
"keyword_already_at_line_start_not_doubled",
"SELECT a\n FROM t\n WHERE b\n ORDER BY c",
- ["\n FROM t", "\n WHERE b", "\n ORDER BY c"],
+ ["\nFROM t", "\nWHERE b", "\nORDER BY c"],
["\n \nFROM", "\n \nWHERE", "\n \nORDER", "\n\nFROM"],
),
(
@@ -574,6 +574,59 @@ class TestPrettyPrintInTests(TestCase):
for fragment in absent:
self.assertNotIn(fragment, result)
+ @parameterized.expand(
+ [
+ (
+ "nested_subquery_indents_deeper_than_outer",
+ "FROM (SELECT x FROM (SELECT y FROM z) AS inner) AS outer",
+ ["\nFROM (", "\n SELECT x", "\n FROM (", "\n SELECT y", "\n FROM z) AS inner) AS outer"],
+ ["\n SELECT y", "\nSELECT x"],
+ ),
+ (
+ "line_starting_with_closing_bracket_dedents",
+ "FROM (SELECT b\n) AS e LIMIT 1",
+ ["\nFROM (", "\n SELECT b", "\n) AS e", "\nLIMIT 1"],
+ ["\n ) AS e", "\n SELECT b"],
+ ),
+ (
+ "sibling_subqueries_each_indent_under_their_own_bracket",
+ "FROM (SELECT a FROM x) AS p, (SELECT b FROM y) AS q",
+ ["\nFROM (", "\n SELECT a", "\n FROM x) AS p, (", "\n SELECT b", "\n FROM y) AS q"],
+ ["\n SELECT a", "\n SELECT b"],
+ ),
+ (
+ "brackets_inside_string_literal_do_not_shift_depth",
+ "SELECT a FROM events WHERE x = '(' GROUP BY a",
+ ["\nSELECT a", "\nFROM events", "\nWHERE x = '('", "\nGROUP BY a"],
+ ["\n GROUP BY a", "\n WHERE"],
+ ),
+ (
+ "continuation_lines_indent_under_their_clause_keyword",
+ "-- ClickHouse\nSELECT\n e.event AS event\nFROM\n events AS e",
+ ["-- ClickHouse", "\nSELECT", "\n e.event AS event", "\nFROM", "\n events AS e"],
+ ["\n SELECT", "\n FROM", "\nevents AS e", " -- ClickHouse"],
+ ),
+ (
+ "with_clause_anchors_at_column_zero_and_body_indents",
+ "WITH a AS (SELECT 1) SELECT b FROM c",
+ ["WITH a AS (", "\n SELECT 1)", "\nSELECT b", "\nFROM c"],
+ ["\n WITH", "\n SELECT b"],
+ ),
+ (
+ "bracket_started_line_is_not_indented_as_a_continuation",
+ "[['a', (0, None)], ['b', (1, None)]]",
+ ["[['a', (0, None)], ['b', (1, None)]]"],
+ [" [['a'"],
+ ),
+ ]
+ )
+ def test_bracket_depth_indentation(self, _name: str, query: str, present: list[str], absent: list[str]) -> None:
+ result = pretty_print_in_tests(query, 1)
+ for fragment in present:
+ self.assertIn(fragment, result)
+ for fragment in absent:
+ self.assertNotIn(fragment, result)
+
def test_normalizes_team_id(self) -> None:
result = pretty_print_in_tests("WHERE equals(events.team_id, 99999)", 99999)
self.assertIn("team_id, 420)", result)
diff --git a/posthog/hogql/test/utils.py b/posthog/hogql/test/utils.py
index 37ee44bd43f0..224d1b94c7df 100644
--- a/posthog/hogql/test/utils.py
+++ b/posthog/hogql/test/utils.py
@@ -42,7 +42,61 @@ def pretty_print_in_tests(query: str | None, team_id: int) -> str:
query = re.sub(r"in_cohort__[0-9]+", "in_cohort__XX", query)
query = re.sub(r"cohort_id, [0-9]+", "cohort_id, XX", query)
query = re.sub(r"RANDOM_TEST_ID::[a-f0-9\-]+", "RANDOM_TEST_ID::UUID", query)
- return query
+ return _indent_nested_sql(query)
+
+
+# A line that opens a clause: the keywords we split on, plus WITH (a clause too, just never split before).
+_CLAUSE_LINE = re.compile(
+ r"(?:WITH|SELECT|FROM|PREWHERE|WHERE|GROUP|HAVING|QUALIFY|WINDOW|ORDER|LIMIT|OFFSET|SETTINGS)\b"
+)
+
+
+def _indent_nested_sql(query: str) -> str:
+ # Indent each already-split line by its bracket-nesting depth so nested subqueries read as nested rather
+ # than as one flat sequence, and give continuation lines (a clause body the production printer wrapped
+ # onto its own line) one extra level so they sit under their keyword. Naive about brackets inside string
+ # literals other than single-quoted ones, which we skip; good enough for test snapshots, no real tokenizer.
+ openers, closers = "([{", ")]}"
+ depth = 0
+ indented_lines: list[str] = []
+ for line in query.split("\n"):
+ content = line.lstrip(" \t")
+ if not content:
+ indented_lines.append("")
+ continue
+
+ # A line that starts by closing brackets lines up with its opener, so dedent for those leading closers.
+ leading_closers = 0
+ for char in content:
+ if char in closers:
+ leading_closers += 1
+ else:
+ break
+ indent = max(depth - leading_closers, 0)
+ # Continuation lines — a clause body the printer wrapped onto its own line — nest one deeper than the
+ # keyword above them. Lines that open a clause, a comment header, or a bracket (structural, or a data
+ # repr passed through this helper) stay at the bracket-depth indent.
+ if not (content.startswith("--") or content[0] in openers + closers or _CLAUSE_LINE.match(content)):
+ indent += 1
+ indented_lines.append(" " * indent + content)
+
+ # Carry the running depth forward by the net brackets on this line, ignoring single-quoted strings.
+ opens = shuts = 0
+ in_string = escaped = False
+ for char in content:
+ if escaped:
+ escaped = False
+ elif char == "\\":
+ escaped = True
+ elif char == "'":
+ in_string = not in_string
+ elif not in_string and char in openers:
+ opens += 1
+ elif not in_string and char in closers:
+ shuts += 1
+ depth = max(depth + opens - shuts, 0)
+
+ return "\n".join(indented_lines)
def pretty_print_response_in_tests(response: Any, team_id: int) -> str:
diff --git a/posthog/hogql/transforms/test/__snapshots__/test_events_predicate_pushdown.ambr b/posthog/hogql/transforms/test/__snapshots__/test_events_predicate_pushdown.ambr
index bd96ea4787a0..679de522ff37 100644
--- a/posthog/hogql/transforms/test/__snapshots__/test_events_predicate_pushdown.ambr
+++ b/posthog/hogql/transforms/test/__snapshots__/test_events_predicate_pushdown.ambr
@@ -4,14 +4,14 @@
SELECT e.event AS event, e__session.`$session_duration` AS `$session_duration`
FROM (
- SELECT e.`$session_id` AS `$session_id`, e.event AS event, toTimeZone(e.timestamp, %(hogql_val_0)s) AS timestamp
- FROM events AS e
- WHERE and(equals(e.team_id, 420), greaterOrEquals(e.timestamp, toDateTime64(%(hogql_val_1)s, 6, %(hogql_val_2)s)), less(e.timestamp, toDateTime64(%(hogql_val_3)s, 6, %(hogql_val_4)s)))
- LIMIT 50) AS e LEFT JOIN (
- SELECT dateDiff(%(hogql_val_5)s, min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_6)s)), max(toTimeZone(raw_sessions.max_timestamp, %(hogql_val_7)s))) AS `$session_duration`, raw_sessions.session_id_v7 AS session_id_v7
- FROM raw_sessions
- WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(%(hogql_val_8)s, toIntervalDay(3))), lessOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), plus(%(hogql_val_9)s, toIntervalDay(3))))
- GROUP BY raw_sessions.session_id_v7) AS e__session ON equals(toUInt128(accurateCastOrNull(e.`$session_id`, %(hogql_val_10)s)), e__session.session_id_v7)
+ SELECT e.`$session_id` AS `$session_id`, e.event AS event, toTimeZone(e.timestamp, %(hogql_val_0)s) AS timestamp
+ FROM events AS e
+ WHERE and(equals(e.team_id, 420), greaterOrEquals(e.timestamp, toDateTime64(%(hogql_val_1)s, 6, %(hogql_val_2)s)), less(e.timestamp, toDateTime64(%(hogql_val_3)s, 6, %(hogql_val_4)s)))
+ LIMIT 50) AS e LEFT JOIN (
+ SELECT dateDiff(%(hogql_val_5)s, min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_6)s)), max(toTimeZone(raw_sessions.max_timestamp, %(hogql_val_7)s))) AS `$session_duration`, raw_sessions.session_id_v7 AS session_id_v7
+ FROM raw_sessions
+ WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(%(hogql_val_8)s, toIntervalDay(3))), lessOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), plus(%(hogql_val_9)s, toIntervalDay(3))))
+ GROUP BY raw_sessions.session_id_v7) AS e__session ON equals(toUInt128(accurateCastOrNull(e.`$session_id`, %(hogql_val_10)s)), e__session.session_id_v7)
LIMIT 50
'''
# ---
@@ -20,14 +20,14 @@
SELECT events.event AS event, events__session.`$session_duration` AS `$session_duration`
FROM (
- SELECT events.`$session_id` AS `$session_id`, events.event AS event, toTimeZone(events.timestamp, %(hogql_val_0)s) AS timestamp
- FROM events
- WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_1)s, 6, %(hogql_val_2)s)), less(events.timestamp, toDateTime64(%(hogql_val_3)s, 6, %(hogql_val_4)s)))
- LIMIT 50) AS events LEFT JOIN (
- SELECT dateDiff(%(hogql_val_5)s, min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_6)s)), max(toTimeZone(raw_sessions.max_timestamp, %(hogql_val_7)s))) AS `$session_duration`, raw_sessions.session_id_v7 AS session_id_v7
- FROM raw_sessions
- WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(%(hogql_val_8)s, toIntervalDay(3))), lessOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), plus(%(hogql_val_9)s, toIntervalDay(3))))
- GROUP BY raw_sessions.session_id_v7) AS events__session ON equals(toUInt128(accurateCastOrNull(events.`$session_id`, %(hogql_val_10)s)), events__session.session_id_v7)
+ SELECT events.`$session_id` AS `$session_id`, events.event AS event, toTimeZone(events.timestamp, %(hogql_val_0)s) AS timestamp
+ FROM events
+ WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_1)s, 6, %(hogql_val_2)s)), less(events.timestamp, toDateTime64(%(hogql_val_3)s, 6, %(hogql_val_4)s)))
+ LIMIT 50) AS events LEFT JOIN (
+ SELECT dateDiff(%(hogql_val_5)s, min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_6)s)), max(toTimeZone(raw_sessions.max_timestamp, %(hogql_val_7)s))) AS `$session_duration`, raw_sessions.session_id_v7 AS session_id_v7
+ FROM raw_sessions
+ WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(%(hogql_val_8)s, toIntervalDay(3))), lessOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), plus(%(hogql_val_9)s, toIntervalDay(3))))
+ GROUP BY raw_sessions.session_id_v7) AS events__session ON equals(toUInt128(accurateCastOrNull(events.`$session_id`, %(hogql_val_10)s)), events__session.session_id_v7)
LIMIT 50
'''
# ---
@@ -36,14 +36,14 @@
SELECT events.event AS event, toTimeZone(toTimeZone(events.timestamp, %(hogql_val_10)s), %(hogql_val_11)s) AS timestamp, events__session.`$session_duration` AS `$session_duration`
FROM (
- SELECT events.`$session_id` AS `$session_id`, events.event AS event, toTimeZone(events.timestamp, %(hogql_val_0)s) AS timestamp
- FROM events
- WHERE and(equals(events.team_id, 420), greaterOrEquals(toTimeZone(events.timestamp, %(hogql_val_1)s), toDateTime64(%(hogql_val_2)s, 6, %(hogql_val_3)s)), lessOrEquals(toTimeZone(events.timestamp, %(hogql_val_4)s), today()))
- LIMIT 50000) AS events LEFT JOIN (
- SELECT dateDiff(%(hogql_val_5)s, min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_6)s)), max(toTimeZone(raw_sessions.max_timestamp, %(hogql_val_7)s))) AS `$session_duration`, raw_sessions.session_id_v7 AS session_id_v7
- FROM raw_sessions
- WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(%(hogql_val_8)s, toIntervalDay(3))), lessOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), plus(today(), toIntervalDay(3))))
- GROUP BY raw_sessions.session_id_v7) AS events__session ON equals(toUInt128(accurateCastOrNull(events.`$session_id`, %(hogql_val_9)s)), events__session.session_id_v7)
+ SELECT events.`$session_id` AS `$session_id`, events.event AS event, toTimeZone(events.timestamp, %(hogql_val_0)s) AS timestamp
+ FROM events
+ WHERE and(equals(events.team_id, 420), greaterOrEquals(toTimeZone(events.timestamp, %(hogql_val_1)s), toDateTime64(%(hogql_val_2)s, 6, %(hogql_val_3)s)), lessOrEquals(toTimeZone(events.timestamp, %(hogql_val_4)s), today()))
+ LIMIT 50000) AS events LEFT JOIN (
+ SELECT dateDiff(%(hogql_val_5)s, min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_6)s)), max(toTimeZone(raw_sessions.max_timestamp, %(hogql_val_7)s))) AS `$session_duration`, raw_sessions.session_id_v7 AS session_id_v7
+ FROM raw_sessions
+ WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(%(hogql_val_8)s, toIntervalDay(3))), lessOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), plus(today(), toIntervalDay(3))))
+ GROUP BY raw_sessions.session_id_v7) AS events__session ON equals(toUInt128(accurateCastOrNull(events.`$session_id`, %(hogql_val_9)s)), events__session.session_id_v7)
LIMIT 50000
'''
# ---
@@ -52,14 +52,14 @@
SELECT e.event AS event, e__session.`$session_duration` AS `$session_duration`
FROM (
- SELECT e.`$session_id` AS `$session_id`, e.event AS event, toTimeZone(e.timestamp, %(hogql_val_0)s) AS timestamp
- FROM events AS e
- WHERE and(equals(e.team_id, 420), greaterOrEquals(e.timestamp, toDateTime64(%(hogql_val_1)s, 6, %(hogql_val_2)s)))
- LIMIT 50000) AS e LEFT JOIN (
- SELECT dateDiff(%(hogql_val_3)s, min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_4)s)), max(toTimeZone(raw_sessions.max_timestamp, %(hogql_val_5)s))) AS `$session_duration`, raw_sessions.session_id_v7 AS session_id_v7
- FROM raw_sessions
- WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(%(hogql_val_6)s, toIntervalDay(3))))
- GROUP BY raw_sessions.session_id_v7) AS e__session ON equals(toUInt128(accurateCastOrNull(e.`$session_id`, %(hogql_val_7)s)), e__session.session_id_v7)
+ SELECT e.`$session_id` AS `$session_id`, e.event AS event, toTimeZone(e.timestamp, %(hogql_val_0)s) AS timestamp
+ FROM events AS e
+ WHERE and(equals(e.team_id, 420), greaterOrEquals(e.timestamp, toDateTime64(%(hogql_val_1)s, 6, %(hogql_val_2)s)))
+ LIMIT 50000) AS e LEFT JOIN (
+ SELECT dateDiff(%(hogql_val_3)s, min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_4)s)), max(toTimeZone(raw_sessions.max_timestamp, %(hogql_val_5)s))) AS `$session_duration`, raw_sessions.session_id_v7 AS session_id_v7
+ FROM raw_sessions
+ WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(%(hogql_val_6)s, toIntervalDay(3))))
+ GROUP BY raw_sessions.session_id_v7) AS e__session ON equals(toUInt128(accurateCastOrNull(e.`$session_id`, %(hogql_val_7)s)), e__session.session_id_v7)
LIMIT 50000
'''
# ---
@@ -68,14 +68,14 @@
SELECT events.event AS event, events__session.`$session_duration` AS `$session_duration`
FROM (
- SELECT events.`$session_id` AS `$session_id`, events.event AS event, toTimeZone(events.timestamp, %(hogql_val_0)s) AS timestamp
- FROM events
- WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_1)s, 6, %(hogql_val_2)s)))
- LIMIT 50000) AS events LEFT JOIN (
- SELECT dateDiff(%(hogql_val_3)s, min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_4)s)), max(toTimeZone(raw_sessions.max_timestamp, %(hogql_val_5)s))) AS `$session_duration`, raw_sessions.session_id_v7 AS session_id_v7
- FROM raw_sessions
- WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(%(hogql_val_6)s, toIntervalDay(3))))
- 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)
+ SELECT events.`$session_id` AS `$session_id`, events.event AS event, toTimeZone(events.timestamp, %(hogql_val_0)s) AS timestamp
+ FROM events
+ WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_1)s, 6, %(hogql_val_2)s)))
+ LIMIT 50000) AS events LEFT JOIN (
+ SELECT dateDiff(%(hogql_val_3)s, min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_4)s)), max(toTimeZone(raw_sessions.max_timestamp, %(hogql_val_5)s))) AS `$session_duration`, raw_sessions.session_id_v7 AS session_id_v7
+ FROM raw_sessions
+ WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(%(hogql_val_6)s, toIntervalDay(3))))
+ 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)
LIMIT 50000
'''
# ---
@@ -93,10 +93,10 @@
SELECT events.event AS event, events__session.`$session_duration` AS `$session_duration`
FROM events LEFT JOIN (
- SELECT dateDiff(%(hogql_val_0)s, min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_1)s)), max(toTimeZone(raw_sessions.max_timestamp, %(hogql_val_2)s))) AS `$session_duration`, raw_sessions.session_id_v7 AS session_id_v7
- FROM raw_sessions
- WHERE equals(raw_sessions.team_id, 420)
- GROUP BY raw_sessions.session_id_v7) AS events__session ON equals(toUInt128(accurateCastOrNull(events.`$session_id`, %(hogql_val_3)s)), events__session.session_id_v7)
+ SELECT dateDiff(%(hogql_val_0)s, min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_1)s)), max(toTimeZone(raw_sessions.max_timestamp, %(hogql_val_2)s))) AS `$session_duration`, raw_sessions.session_id_v7 AS session_id_v7
+ FROM raw_sessions
+ WHERE equals(raw_sessions.team_id, 420)
+ GROUP BY raw_sessions.session_id_v7) AS events__session ON equals(toUInt128(accurateCastOrNull(events.`$session_id`, %(hogql_val_3)s)), events__session.session_id_v7)
WHERE equals(events.team_id, 420)
LIMIT 50000
'''
@@ -106,14 +106,14 @@
SELECT events.event AS event, events.person_id AS id, events.person_properties AS properties, toTimeZone(events.person_created_at, %(hogql_val_9)s) AS created_at, events__session.`$session_duration` AS `$session_duration`
FROM (
- SELECT events.`$session_id` AS `$session_id`, events.event AS event, toTimeZone(events.person_created_at, %(hogql_val_0)s) AS person_created_at, events.person_id AS person_id, events.person_properties AS person_properties, toTimeZone(events.timestamp, %(hogql_val_1)s) AS timestamp
- FROM events
- WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_2)s, 6, %(hogql_val_3)s)))
- LIMIT 50000) AS events LEFT JOIN (
- SELECT dateDiff(%(hogql_val_4)s, min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_5)s)), max(toTimeZone(raw_sessions.max_timestamp, %(hogql_val_6)s))) AS `$session_duration`, raw_sessions.session_id_v7 AS session_id_v7
- FROM raw_sessions
- WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(%(hogql_val_7)s, toIntervalDay(3))))
- GROUP BY raw_sessions.session_id_v7) AS events__session ON equals(toUInt128(accurateCastOrNull(events.`$session_id`, %(hogql_val_8)s)), events__session.session_id_v7)
+ SELECT events.`$session_id` AS `$session_id`, events.event AS event, toTimeZone(events.person_created_at, %(hogql_val_0)s) AS person_created_at, events.person_id AS person_id, events.person_properties AS person_properties, toTimeZone(events.timestamp, %(hogql_val_1)s) AS timestamp
+ FROM events
+ WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_2)s, 6, %(hogql_val_3)s)))
+ LIMIT 50000) AS events LEFT JOIN (
+ SELECT dateDiff(%(hogql_val_4)s, min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_5)s)), max(toTimeZone(raw_sessions.max_timestamp, %(hogql_val_6)s))) AS `$session_duration`, raw_sessions.session_id_v7 AS session_id_v7
+ FROM raw_sessions
+ WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(%(hogql_val_7)s, toIntervalDay(3))))
+ GROUP BY raw_sessions.session_id_v7) AS events__session ON equals(toUInt128(accurateCastOrNull(events.`$session_id`, %(hogql_val_8)s)), events__session.session_id_v7)
LIMIT 50000
'''
# ---
@@ -122,14 +122,14 @@
SELECT events.event AS event, events__session.`$session_duration` AS `$session_duration`
FROM (
- SELECT events.`$session_id` AS `$session_id`, events.event AS event, toTimeZone(events.timestamp, %(hogql_val_0)s) AS timestamp
- FROM events
- WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_1)s, 6, %(hogql_val_2)s)), equals(events.event, %(hogql_val_3)s))
- LIMIT 50000) AS events LEFT JOIN (
- SELECT dateDiff(%(hogql_val_4)s, min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_5)s)), max(toTimeZone(raw_sessions.max_timestamp, %(hogql_val_6)s))) AS `$session_duration`, raw_sessions.session_id_v7 AS session_id_v7
- FROM raw_sessions
- WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(%(hogql_val_7)s, toIntervalDay(3))))
- GROUP BY raw_sessions.session_id_v7) AS events__session ON equals(toUInt128(accurateCastOrNull(events.`$session_id`, %(hogql_val_8)s)), events__session.session_id_v7)
+ SELECT events.`$session_id` AS `$session_id`, events.event AS event, toTimeZone(events.timestamp, %(hogql_val_0)s) AS timestamp
+ FROM events
+ WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_1)s, 6, %(hogql_val_2)s)), equals(events.event, %(hogql_val_3)s))
+ LIMIT 50000) AS events LEFT JOIN (
+ SELECT dateDiff(%(hogql_val_4)s, min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_5)s)), max(toTimeZone(raw_sessions.max_timestamp, %(hogql_val_6)s))) AS `$session_duration`, raw_sessions.session_id_v7 AS session_id_v7
+ FROM raw_sessions
+ WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(%(hogql_val_7)s, toIntervalDay(3))))
+ GROUP BY raw_sessions.session_id_v7) AS events__session ON equals(toUInt128(accurateCastOrNull(events.`$session_id`, %(hogql_val_8)s)), events__session.session_id_v7)
LIMIT 50000
'''
# ---
@@ -138,10 +138,10 @@
SELECT sessions.session_id AS session_id, events.uuid AS uuid
FROM events JOIN (
- SELECT toString(reinterpretAsUUID(bitOr(bitShiftLeft(raw_sessions.session_id_v7, 64), bitShiftRight(raw_sessions.session_id_v7, 64)))) AS session_id, raw_sessions.session_id_v7 AS session_id_v7
- FROM raw_sessions
- WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(%(hogql_val_0)s, toIntervalDay(3))))
- GROUP BY raw_sessions.session_id_v7) AS sessions ON equals(events.`$session_id`, sessions.session_id)
+ SELECT toString(reinterpretAsUUID(bitOr(bitShiftLeft(raw_sessions.session_id_v7, 64), bitShiftRight(raw_sessions.session_id_v7, 64)))) AS session_id, raw_sessions.session_id_v7 AS session_id_v7
+ FROM raw_sessions
+ WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(%(hogql_val_0)s, toIntervalDay(3))))
+ GROUP BY raw_sessions.session_id_v7) AS sessions ON equals(events.`$session_id`, sessions.session_id)
WHERE and(equals(events.team_id, 420), greater(events.timestamp, toDateTime64(%(hogql_val_1)s, 6, %(hogql_val_2)s)))
LIMIT 50000
'''
@@ -151,10 +151,10 @@
SELECT sessions.session_id AS session_id, events.uuid AS uuid
FROM events INNER JOIN (
- SELECT toString(reinterpretAsUUID(bitOr(bitShiftLeft(raw_sessions.session_id_v7, 64), bitShiftRight(raw_sessions.session_id_v7, 64)))) AS session_id, raw_sessions.session_id_v7 AS session_id_v7
- FROM raw_sessions
- WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(%(hogql_val_0)s, toIntervalDay(3))))
- GROUP BY raw_sessions.session_id_v7) AS sessions ON equals(events.`$session_id`, sessions.session_id)
+ SELECT toString(reinterpretAsUUID(bitOr(bitShiftLeft(raw_sessions.session_id_v7, 64), bitShiftRight(raw_sessions.session_id_v7, 64)))) AS session_id, raw_sessions.session_id_v7 AS session_id_v7
+ FROM raw_sessions
+ WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(%(hogql_val_0)s, toIntervalDay(3))))
+ GROUP BY raw_sessions.session_id_v7) AS sessions ON equals(events.`$session_id`, sessions.session_id)
WHERE and(equals(events.team_id, 420), greater(events.timestamp, toDateTime64(%(hogql_val_1)s, 6, %(hogql_val_2)s)))
LIMIT 50000
'''
@@ -164,10 +164,10 @@
SELECT sessions.session_id AS session_id, events.uuid AS uuid
FROM events CROSS JOIN (
- SELECT toString(reinterpretAsUUID(bitOr(bitShiftLeft(raw_sessions.session_id_v7, 64), bitShiftRight(raw_sessions.session_id_v7, 64)))) AS session_id, raw_sessions.session_id_v7 AS session_id_v7
- FROM raw_sessions
- WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(%(hogql_val_0)s, toIntervalDay(3))))
- GROUP BY raw_sessions.session_id_v7) AS sessions
+ SELECT toString(reinterpretAsUUID(bitOr(bitShiftLeft(raw_sessions.session_id_v7, 64), bitShiftRight(raw_sessions.session_id_v7, 64)))) AS session_id, raw_sessions.session_id_v7 AS session_id_v7
+ FROM raw_sessions
+ WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(%(hogql_val_0)s, toIntervalDay(3))))
+ GROUP BY raw_sessions.session_id_v7) AS sessions
WHERE and(equals(events.team_id, 420), greater(events.timestamp, toDateTime64(%(hogql_val_1)s, 6, %(hogql_val_2)s)))
LIMIT 50000
'''
@@ -177,10 +177,10 @@
SELECT sessions.session_id AS session_id, events.uuid AS uuid
FROM events RIGHT JOIN (
- SELECT toString(reinterpretAsUUID(bitOr(bitShiftLeft(raw_sessions.session_id_v7, 64), bitShiftRight(raw_sessions.session_id_v7, 64)))) AS session_id, raw_sessions.session_id_v7 AS session_id_v7
- FROM raw_sessions
- WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(%(hogql_val_0)s, toIntervalDay(3))))
- GROUP BY raw_sessions.session_id_v7) AS sessions ON equals(events.`$session_id`, sessions.session_id)
+ SELECT toString(reinterpretAsUUID(bitOr(bitShiftLeft(raw_sessions.session_id_v7, 64), bitShiftRight(raw_sessions.session_id_v7, 64)))) AS session_id, raw_sessions.session_id_v7 AS session_id_v7
+ FROM raw_sessions
+ WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(%(hogql_val_0)s, toIntervalDay(3))))
+ GROUP BY raw_sessions.session_id_v7) AS sessions ON equals(events.`$session_id`, sessions.session_id)
WHERE and(equals(events.team_id, 420), greater(events.timestamp, toDateTime64(%(hogql_val_1)s, 6, %(hogql_val_2)s)))
LIMIT 50000
'''
@@ -190,10 +190,10 @@
SELECT sessions.session_id AS session_id, events.uuid AS uuid
FROM events RIGHT OUTER JOIN (
- SELECT toString(reinterpretAsUUID(bitOr(bitShiftLeft(raw_sessions.session_id_v7, 64), bitShiftRight(raw_sessions.session_id_v7, 64)))) AS session_id, raw_sessions.session_id_v7 AS session_id_v7
- FROM raw_sessions
- WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(%(hogql_val_0)s, toIntervalDay(3))))
- GROUP BY raw_sessions.session_id_v7) AS sessions ON equals(events.`$session_id`, sessions.session_id)
+ SELECT toString(reinterpretAsUUID(bitOr(bitShiftLeft(raw_sessions.session_id_v7, 64), bitShiftRight(raw_sessions.session_id_v7, 64)))) AS session_id, raw_sessions.session_id_v7 AS session_id_v7
+ FROM raw_sessions
+ WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(%(hogql_val_0)s, toIntervalDay(3))))
+ GROUP BY raw_sessions.session_id_v7) AS sessions ON equals(events.`$session_id`, sessions.session_id)
WHERE and(equals(events.team_id, 420), greater(events.timestamp, toDateTime64(%(hogql_val_1)s, 6, %(hogql_val_2)s)))
LIMIT 50000
'''
@@ -203,10 +203,10 @@
SELECT sessions.session_id AS session_id, events.uuid AS uuid
FROM events FULL OUTER JOIN (
- SELECT toString(reinterpretAsUUID(bitOr(bitShiftLeft(raw_sessions.session_id_v7, 64), bitShiftRight(raw_sessions.session_id_v7, 64)))) AS session_id, raw_sessions.session_id_v7 AS session_id_v7
- FROM raw_sessions
- WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(%(hogql_val_0)s, toIntervalDay(3))))
- GROUP BY raw_sessions.session_id_v7) AS sessions ON equals(events.`$session_id`, sessions.session_id)
+ SELECT toString(reinterpretAsUUID(bitOr(bitShiftLeft(raw_sessions.session_id_v7, 64), bitShiftRight(raw_sessions.session_id_v7, 64)))) AS session_id, raw_sessions.session_id_v7 AS session_id_v7
+ FROM raw_sessions
+ WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(%(hogql_val_0)s, toIntervalDay(3))))
+ GROUP BY raw_sessions.session_id_v7) AS sessions ON equals(events.`$session_id`, sessions.session_id)
WHERE and(equals(events.team_id, 420), greater(events.timestamp, toDateTime64(%(hogql_val_1)s, 6, %(hogql_val_2)s)))
LIMIT 50000
'''
@@ -216,10 +216,10 @@
SELECT sessions.session_id AS session_id, events.uuid AS uuid
FROM events FULL JOIN (
- SELECT toString(reinterpretAsUUID(bitOr(bitShiftLeft(raw_sessions.session_id_v7, 64), bitShiftRight(raw_sessions.session_id_v7, 64)))) AS session_id, raw_sessions.session_id_v7 AS session_id_v7
- FROM raw_sessions
- WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(%(hogql_val_0)s, toIntervalDay(3))))
- GROUP BY raw_sessions.session_id_v7) AS sessions ON equals(events.`$session_id`, sessions.session_id)
+ SELECT toString(reinterpretAsUUID(bitOr(bitShiftLeft(raw_sessions.session_id_v7, 64), bitShiftRight(raw_sessions.session_id_v7, 64)))) AS session_id, raw_sessions.session_id_v7 AS session_id_v7
+ FROM raw_sessions
+ WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(%(hogql_val_0)s, toIntervalDay(3))))
+ GROUP BY raw_sessions.session_id_v7) AS sessions ON equals(events.`$session_id`, sessions.session_id)
WHERE and(equals(events.team_id, 420), greater(events.timestamp, toDateTime64(%(hogql_val_1)s, 6, %(hogql_val_2)s)))
LIMIT 50000
'''
@@ -229,14 +229,14 @@
SELECT events.event AS event, toTimeZone(events.person_created_at, %(hogql_val_9)s) AS created_at, events__session.`$session_duration` AS `$session_duration`
FROM (
- SELECT events.`$session_id` AS `$session_id`, events.event AS event, toTimeZone(events.person_created_at, %(hogql_val_0)s) AS person_created_at, toTimeZone(events.timestamp, %(hogql_val_1)s) AS timestamp
- FROM events
- WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_2)s, 6, %(hogql_val_3)s)))
- LIMIT 50000) AS events LEFT JOIN (
- SELECT dateDiff(%(hogql_val_4)s, min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_5)s)), max(toTimeZone(raw_sessions.max_timestamp, %(hogql_val_6)s))) AS `$session_duration`, raw_sessions.session_id_v7 AS session_id_v7
- FROM raw_sessions
- WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(%(hogql_val_7)s, toIntervalDay(3))))
- GROUP BY raw_sessions.session_id_v7) AS events__session ON equals(toUInt128(accurateCastOrNull(events.`$session_id`, %(hogql_val_8)s)), events__session.session_id_v7)
+ SELECT events.`$session_id` AS `$session_id`, events.event AS event, toTimeZone(events.person_created_at, %(hogql_val_0)s) AS person_created_at, toTimeZone(events.timestamp, %(hogql_val_1)s) AS timestamp
+ FROM events
+ WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_2)s, 6, %(hogql_val_3)s)))
+ LIMIT 50000) AS events LEFT JOIN (
+ SELECT dateDiff(%(hogql_val_4)s, min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_5)s)), max(toTimeZone(raw_sessions.max_timestamp, %(hogql_val_6)s))) AS `$session_duration`, raw_sessions.session_id_v7 AS session_id_v7
+ FROM raw_sessions
+ WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(%(hogql_val_7)s, toIntervalDay(3))))
+ GROUP BY raw_sessions.session_id_v7) AS events__session ON equals(toUInt128(accurateCastOrNull(events.`$session_id`, %(hogql_val_8)s)), events__session.session_id_v7)
LIMIT 50000
'''
# ---
@@ -245,14 +245,14 @@
SELECT events.person_id AS id, events__session.`$session_duration` AS `$session_duration`
FROM (
- SELECT events.`$session_id` AS `$session_id`, events.person_id AS person_id, toTimeZone(events.timestamp, %(hogql_val_0)s) AS timestamp
- FROM events
- WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_1)s, 6, %(hogql_val_2)s)))
- LIMIT 50000) AS events LEFT JOIN (
- SELECT dateDiff(%(hogql_val_3)s, min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_4)s)), max(toTimeZone(raw_sessions.max_timestamp, %(hogql_val_5)s))) AS `$session_duration`, raw_sessions.session_id_v7 AS session_id_v7
- FROM raw_sessions
- WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(%(hogql_val_6)s, toIntervalDay(3))))
- 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)
+ SELECT events.`$session_id` AS `$session_id`, events.person_id AS person_id, toTimeZone(events.timestamp, %(hogql_val_0)s) AS timestamp
+ FROM events
+ WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_1)s, 6, %(hogql_val_2)s)))
+ LIMIT 50000) AS events LEFT JOIN (
+ SELECT dateDiff(%(hogql_val_3)s, min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_4)s)), max(toTimeZone(raw_sessions.max_timestamp, %(hogql_val_5)s))) AS `$session_duration`, raw_sessions.session_id_v7 AS session_id_v7
+ FROM raw_sessions
+ WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(%(hogql_val_6)s, toIntervalDay(3))))
+ 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)
LIMIT 50000
'''
# ---
@@ -261,14 +261,14 @@
SELECT events.event AS event, events.person_properties AS properties, events__session.`$session_duration` AS `$session_duration`
FROM (
- SELECT events.`$session_id` AS `$session_id`, events.event AS event, events.person_properties AS person_properties, toTimeZone(events.timestamp, %(hogql_val_0)s) AS timestamp
- FROM events
- WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_1)s, 6, %(hogql_val_2)s)))
- LIMIT 50000) AS events LEFT JOIN (
- SELECT dateDiff(%(hogql_val_3)s, min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_4)s)), max(toTimeZone(raw_sessions.max_timestamp, %(hogql_val_5)s))) AS `$session_duration`, raw_sessions.session_id_v7 AS session_id_v7
- FROM raw_sessions
- WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(%(hogql_val_6)s, toIntervalDay(3))))
- 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)
+ SELECT events.`$session_id` AS `$session_id`, events.event AS event, events.person_properties AS person_properties, toTimeZone(events.timestamp, %(hogql_val_0)s) AS timestamp
+ FROM events
+ WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_1)s, 6, %(hogql_val_2)s)))
+ LIMIT 50000) AS events LEFT JOIN (
+ SELECT dateDiff(%(hogql_val_3)s, min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_4)s)), max(toTimeZone(raw_sessions.max_timestamp, %(hogql_val_5)s))) AS `$session_duration`, raw_sessions.session_id_v7 AS session_id_v7
+ FROM raw_sessions
+ WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(%(hogql_val_6)s, toIntervalDay(3))))
+ 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)
LIMIT 50000
'''
# ---
@@ -277,14 +277,14 @@
SELECT sessions.session_id AS session_id, events.uuid AS uuid
FROM (
- SELECT events.`$session_id` AS `$session_id`, toTimeZone(events.timestamp, %(hogql_val_0)s) AS timestamp, events.uuid AS uuid
- FROM events
- WHERE and(equals(events.team_id, 420), greater(events.timestamp, toDateTime64(%(hogql_val_1)s, 6, %(hogql_val_2)s)))
- LIMIT 50000) AS events LEFT JOIN (
- SELECT toString(reinterpretAsUUID(bitOr(bitShiftLeft(raw_sessions.session_id_v7, 64), bitShiftRight(raw_sessions.session_id_v7, 64)))) AS session_id, raw_sessions.session_id_v7 AS session_id_v7
- FROM raw_sessions
- WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(%(hogql_val_3)s, toIntervalDay(3))))
- GROUP BY raw_sessions.session_id_v7) AS sessions ON equals(events.`$session_id`, sessions.session_id)
+ SELECT events.`$session_id` AS `$session_id`, toTimeZone(events.timestamp, %(hogql_val_0)s) AS timestamp, events.uuid AS uuid
+ FROM events
+ WHERE and(equals(events.team_id, 420), greater(events.timestamp, toDateTime64(%(hogql_val_1)s, 6, %(hogql_val_2)s)))
+ LIMIT 50000) AS events LEFT JOIN (
+ SELECT toString(reinterpretAsUUID(bitOr(bitShiftLeft(raw_sessions.session_id_v7, 64), bitShiftRight(raw_sessions.session_id_v7, 64)))) AS session_id, raw_sessions.session_id_v7 AS session_id_v7
+ FROM raw_sessions
+ WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(%(hogql_val_3)s, toIntervalDay(3))))
+ GROUP BY raw_sessions.session_id_v7) AS sessions ON equals(events.`$session_id`, sessions.session_id)
LIMIT 50000
'''
# ---
@@ -293,14 +293,14 @@
SELECT sessions.session_id AS session_id, events.uuid AS uuid
FROM (
- SELECT events.`$session_id` AS `$session_id`, toTimeZone(events.timestamp, %(hogql_val_0)s) AS timestamp, events.uuid AS uuid
- FROM events
- WHERE and(equals(events.team_id, 420), greater(events.timestamp, toDateTime64(%(hogql_val_1)s, 6, %(hogql_val_2)s)))
- LIMIT 50000) AS events LEFT OUTER JOIN (
- SELECT toString(reinterpretAsUUID(bitOr(bitShiftLeft(raw_sessions.session_id_v7, 64), bitShiftRight(raw_sessions.session_id_v7, 64)))) AS session_id, raw_sessions.session_id_v7 AS session_id_v7
- FROM raw_sessions
- WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(%(hogql_val_3)s, toIntervalDay(3))))
- GROUP BY raw_sessions.session_id_v7) AS sessions ON equals(events.`$session_id`, sessions.session_id)
+ SELECT events.`$session_id` AS `$session_id`, toTimeZone(events.timestamp, %(hogql_val_0)s) AS timestamp, events.uuid AS uuid
+ FROM events
+ WHERE and(equals(events.team_id, 420), greater(events.timestamp, toDateTime64(%(hogql_val_1)s, 6, %(hogql_val_2)s)))
+ LIMIT 50000) AS events LEFT OUTER JOIN (
+ SELECT toString(reinterpretAsUUID(bitOr(bitShiftLeft(raw_sessions.session_id_v7, 64), bitShiftRight(raw_sessions.session_id_v7, 64)))) AS session_id, raw_sessions.session_id_v7 AS session_id_v7
+ FROM raw_sessions
+ WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(%(hogql_val_3)s, toIntervalDay(3))))
+ GROUP BY raw_sessions.session_id_v7) AS sessions ON equals(events.`$session_id`, sessions.session_id)
LIMIT 50000
'''
# ---
@@ -309,10 +309,10 @@
SELECT events.event AS event, events__session.`$session_duration` AS `$session_duration`
FROM events LEFT JOIN (
- SELECT dateDiff(%(hogql_val_0)s, min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_1)s)), max(toTimeZone(raw_sessions.max_timestamp, %(hogql_val_2)s))) AS `$session_duration`, raw_sessions.session_id_v7 AS session_id_v7
- FROM raw_sessions
- WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(%(hogql_val_3)s, toIntervalDay(3))))
- GROUP BY raw_sessions.session_id_v7) AS events__session ON equals(toUInt128(accurateCastOrNull(events.`$session_id`, %(hogql_val_4)s)), events__session.session_id_v7)
+ SELECT dateDiff(%(hogql_val_0)s, min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_1)s)), max(toTimeZone(raw_sessions.max_timestamp, %(hogql_val_2)s))) AS `$session_duration`, raw_sessions.session_id_v7 AS session_id_v7
+ FROM raw_sessions
+ WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(%(hogql_val_3)s, toIntervalDay(3))))
+ GROUP BY raw_sessions.session_id_v7) AS events__session ON equals(toUInt128(accurateCastOrNull(events.`$session_id`, %(hogql_val_4)s)), events__session.session_id_v7)
WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_5)s, 6, %(hogql_val_6)s)), ifNull(greater(events__session.`$session_duration`, 0), 0))
LIMIT 50000
'''
@@ -322,22 +322,22 @@
SELECT events.event AS event, events__person.id AS id
FROM (
- SELECT events.distinct_id AS distinct_id, events.event AS event, events.person_id AS person_id, toTimeZone(events.timestamp, %(hogql_val_0)s) AS timestamp
- FROM events
- WHERE and(equals(events.team_id, 420), greater(events.timestamp, toDateTime64(%(hogql_val_1)s, 6, %(hogql_val_2)s)))
- LIMIT 50000) AS events LEFT OUTER JOIN (
- SELECT tupleElement(argMax(tuple(person_distinct_id_overrides.person_id), person_distinct_id_overrides.version), 1) AS person_id, person_distinct_id_overrides.distinct_id AS distinct_id
- FROM person_distinct_id_overrides
- WHERE equals(person_distinct_id_overrides.team_id, 420)
- GROUP BY person_distinct_id_overrides.distinct_id
- HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id_overrides.is_deleted), person_distinct_id_overrides.version), 1), 0), 0)
- SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id) LEFT JOIN (
- SELECT person.id AS id
- FROM person
- WHERE equals(person.team_id, 420)
- GROUP BY person.id
- HAVING and(ifNull(equals(tupleElement(argMax(tuple(person.is_deleted), person.version), 1), 0), 0), ifNull(less(tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_3)s)), person.version), 1), plus(now64(6, %(hogql_val_4)s), toIntervalDay(1))), 0))
- SETTINGS optimize_aggregation_in_order=1) AS events__person ON equals(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), events__person.id)
+ SELECT events.distinct_id AS distinct_id, events.event AS event, events.person_id AS person_id, toTimeZone(events.timestamp, %(hogql_val_0)s) AS timestamp
+ FROM events
+ WHERE and(equals(events.team_id, 420), greater(events.timestamp, toDateTime64(%(hogql_val_1)s, 6, %(hogql_val_2)s)))
+ LIMIT 50000) AS events LEFT OUTER JOIN (
+ SELECT tupleElement(argMax(tuple(person_distinct_id_overrides.person_id), person_distinct_id_overrides.version), 1) AS person_id, person_distinct_id_overrides.distinct_id AS distinct_id
+ FROM person_distinct_id_overrides
+ WHERE equals(person_distinct_id_overrides.team_id, 420)
+ GROUP BY person_distinct_id_overrides.distinct_id
+ HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id_overrides.is_deleted), person_distinct_id_overrides.version), 1), 0), 0)
+ SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id) LEFT JOIN (
+ SELECT person.id AS id
+ FROM person
+ WHERE equals(person.team_id, 420)
+ GROUP BY person.id
+ HAVING and(ifNull(equals(tupleElement(argMax(tuple(person.is_deleted), person.version), 1), 0), 0), ifNull(less(tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_3)s)), person.version), 1), plus(now64(6, %(hogql_val_4)s), toIntervalDay(1))), 0))
+ SETTINGS optimize_aggregation_in_order=1) AS events__person ON equals(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), events__person.id)
LIMIT 50000
'''
# ---
@@ -346,17 +346,17 @@
SELECT event AS event, avg(`$session_duration`) AS `avg($session_duration)`
FROM (
- SELECT events.event AS event, events__session.`$session_duration` AS `$session_duration`
- FROM (
- SELECT events.`$session_id` AS `$session_id`, events.event AS event, toTimeZone(events.timestamp, %(hogql_val_0)s) AS timestamp
- FROM events
- WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_1)s, 6, %(hogql_val_2)s)), or(equals(events.event, %(hogql_val_3)s), equals(events.event, %(hogql_val_4)s)))
- LIMIT 100) AS events LEFT JOIN (
- SELECT dateDiff(%(hogql_val_5)s, min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_6)s)), max(toTimeZone(raw_sessions.max_timestamp, %(hogql_val_7)s))) AS `$session_duration`, raw_sessions.session_id_v7 AS session_id_v7
- FROM raw_sessions
- WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(%(hogql_val_8)s, toIntervalDay(3))))
- GROUP BY raw_sessions.session_id_v7) AS events__session ON equals(toUInt128(accurateCastOrNull(events.`$session_id`, %(hogql_val_9)s)), events__session.session_id_v7)
- LIMIT 100)
+ SELECT events.event AS event, events__session.`$session_duration` AS `$session_duration`
+ FROM (
+ SELECT events.`$session_id` AS `$session_id`, events.event AS event, toTimeZone(events.timestamp, %(hogql_val_0)s) AS timestamp
+ FROM events
+ WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_1)s, 6, %(hogql_val_2)s)), or(equals(events.event, %(hogql_val_3)s), equals(events.event, %(hogql_val_4)s)))
+ LIMIT 100) AS events LEFT JOIN (
+ SELECT dateDiff(%(hogql_val_5)s, min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_6)s)), max(toTimeZone(raw_sessions.max_timestamp, %(hogql_val_7)s))) AS `$session_duration`, raw_sessions.session_id_v7 AS session_id_v7
+ FROM raw_sessions
+ WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(%(hogql_val_8)s, toIntervalDay(3))))
+ GROUP BY raw_sessions.session_id_v7) AS events__session ON equals(toUInt128(accurateCastOrNull(events.`$session_id`, %(hogql_val_9)s)), events__session.session_id_v7)
+ LIMIT 100)
GROUP BY event
LIMIT 50000
'''
diff --git a/posthog/hogql/transforms/test/__snapshots__/test_in_cohort.ambr b/posthog/hogql/transforms/test/__snapshots__/test_in_cohort.ambr
index 4d91c886d1ff..01039b1ca300 100644
--- a/posthog/hogql/transforms/test/__snapshots__/test_in_cohort.ambr
+++ b/posthog/hogql/transforms/test/__snapshots__/test_in_cohort.ambr
@@ -4,9 +4,9 @@
-- ClickHouse
SELECT events.event AS event
FROM events LEFT JOIN (
- SELECT cohortpeople.person_id AS cohort_person_id, 1 AS matched, cohortpeople.cohort_id AS cohort_id
- FROM cohortpeople
- WHERE and(equals(cohortpeople.team_id, 99999), equals(cohortpeople.cohort_id, XX), equals(cohortpeople.version, 0))) AS __in_cohort ON equals(__in_cohort.cohort_person_id, events.person_id)
+ SELECT cohortpeople.person_id AS cohort_person_id, 1 AS matched, cohortpeople.cohort_id AS cohort_id
+ FROM cohortpeople
+ WHERE and(equals(cohortpeople.team_id, 99999), equals(cohortpeople.cohort_id, XX), equals(cohortpeople.version, 0))) AS __in_cohort ON equals(__in_cohort.cohort_person_id, events.person_id)
WHERE and(equals(events.team_id, 99999), equals(events.event, %(hogql_val_0)s), ifNull(equals(__in_cohort.matched, 1), 0))
LIMIT 100
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0, transform_null_in=1, optimize_min_equality_disjunction_chain_length=4294967295, allow_experimental_join_condition=1, use_hive_partitioning=0
@@ -14,9 +14,9 @@
-- HogQL
SELECT event
FROM events LEFT JOIN (
- SELECT person_id AS cohort_person_id, 1 AS matched, cohort_id
- FROM raw_cohort_people
- WHERE and(equals(cohort_id, XX), equals(version, 0))) AS __in_cohort ON equals(__in_cohort.cohort_person_id, person_id)
+ SELECT person_id AS cohort_person_id, 1 AS matched, cohort_id
+ FROM raw_cohort_people
+ WHERE and(equals(cohort_id, XX), equals(version, 0))) AS __in_cohort ON equals(__in_cohort.cohort_person_id, person_id)
WHERE and(and(equals(1, 1), equals(event, 'RANDOM_TEST_ID::UUID')), equals(__in_cohort.matched, 1))
LIMIT 100
'''
@@ -26,9 +26,9 @@
-- ClickHouse
SELECT events.event AS event
FROM events LEFT JOIN (
- SELECT person_static_cohort.person_id AS cohort_person_id, 1 AS matched, person_static_cohort.cohort_id AS cohort_id
- FROM person_static_cohort
- WHERE and(equals(person_static_cohort.team_id, 99999), in(person_static_cohort.cohort_id, [1, 2, 3, 4, 5 /* ... */]))) AS __in_cohort ON equals(__in_cohort.cohort_person_id, events.person_id)
+ SELECT person_static_cohort.person_id AS cohort_person_id, 1 AS matched, person_static_cohort.cohort_id AS cohort_id
+ FROM person_static_cohort
+ WHERE and(equals(person_static_cohort.team_id, 99999), in(person_static_cohort.cohort_id, [1, 2, 3, 4, 5 /* ... */]))) AS __in_cohort ON equals(__in_cohort.cohort_person_id, events.person_id)
WHERE and(equals(events.team_id, 99999), ifNull(equals(__in_cohort.matched, 1), 0))
LIMIT 100
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0, transform_null_in=1, optimize_min_equality_disjunction_chain_length=4294967295, allow_experimental_join_condition=1, use_hive_partitioning=0
@@ -36,9 +36,9 @@
-- HogQL
SELECT event
FROM events LEFT JOIN (
- SELECT person_id AS cohort_person_id, 1 AS matched, cohort_id
- FROM static_cohort_people
- WHERE in(cohort_id, [1, 2, 3, 4, 5 /* ... */])) AS __in_cohort ON equals(__in_cohort.cohort_person_id, person_id)
+ SELECT person_id AS cohort_person_id, 1 AS matched, cohort_id
+ FROM static_cohort_people
+ WHERE in(cohort_id, [1, 2, 3, 4, 5 /* ... */])) AS __in_cohort ON equals(__in_cohort.cohort_person_id, person_id)
WHERE and(equals(1, 1), equals(__in_cohort.matched, 1))
LIMIT 100
'''
@@ -48,9 +48,9 @@
-- ClickHouse
SELECT events.event AS event
FROM events LEFT JOIN (
- SELECT person_static_cohort.person_id AS cohort_person_id, 1 AS matched, person_static_cohort.cohort_id AS cohort_id
- FROM person_static_cohort
- WHERE and(equals(person_static_cohort.team_id, 99999), in(person_static_cohort.cohort_id, [1, 2, 3, 4, 5 /* ... */]))) AS __in_cohort ON equals(__in_cohort.cohort_person_id, events.person_id)
+ SELECT person_static_cohort.person_id AS cohort_person_id, 1 AS matched, person_static_cohort.cohort_id AS cohort_id
+ FROM person_static_cohort
+ WHERE and(equals(person_static_cohort.team_id, 99999), in(person_static_cohort.cohort_id, [1, 2, 3, 4, 5 /* ... */]))) AS __in_cohort ON equals(__in_cohort.cohort_person_id, events.person_id)
WHERE and(equals(events.team_id, 99999), ifNull(equals(__in_cohort.matched, 1), 0))
LIMIT 100
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0, transform_null_in=1, optimize_min_equality_disjunction_chain_length=4294967295, allow_experimental_join_condition=1, use_hive_partitioning=0
@@ -58,9 +58,9 @@
-- HogQL
SELECT event
FROM events LEFT JOIN (
- SELECT person_id AS cohort_person_id, 1 AS matched, cohort_id
- FROM static_cohort_people
- WHERE in(cohort_id, [1, 2, 3, 4, 5 /* ... */])) AS __in_cohort ON equals(__in_cohort.cohort_person_id, person_id)
+ SELECT person_id AS cohort_person_id, 1 AS matched, cohort_id
+ FROM static_cohort_people
+ WHERE in(cohort_id, [1, 2, 3, 4, 5 /* ... */])) AS __in_cohort ON equals(__in_cohort.cohort_person_id, person_id)
WHERE and(equals(1, 1), equals(__in_cohort.matched, 1))
LIMIT 100
'''
@@ -70,9 +70,9 @@
-- ClickHouse
SELECT events.event AS event
FROM events LEFT JOIN (
- SELECT person_static_cohort.person_id AS person_id, 1 AS matched
- FROM person_static_cohort
- WHERE and(equals(person_static_cohort.team_id, 99999), equals(person_static_cohort.cohort_id, XX))) AS in_cohort__XX ON equals(in_cohort__XX.person_id, events.person_id)
+ SELECT person_static_cohort.person_id AS person_id, 1 AS matched
+ FROM person_static_cohort
+ WHERE and(equals(person_static_cohort.team_id, 99999), equals(person_static_cohort.cohort_id, XX))) AS in_cohort__XX ON equals(in_cohort__XX.person_id, events.person_id)
WHERE and(equals(events.team_id, 99999), ifNull(equals(in_cohort__XX.matched, 1), 0))
LIMIT 100
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0, transform_null_in=1, optimize_min_equality_disjunction_chain_length=4294967295, allow_experimental_join_condition=1, use_hive_partitioning=0
@@ -80,9 +80,9 @@
-- HogQL
SELECT event
FROM events LEFT JOIN (
- SELECT person_id, 1 AS matched
- FROM static_cohort_people
- WHERE equals(cohort_id, XX)) AS in_cohort__XX ON equals(in_cohort__XX.person_id, person_id)
+ SELECT person_id, 1 AS matched
+ FROM static_cohort_people
+ WHERE equals(cohort_id, XX)) AS in_cohort__XX ON equals(in_cohort__XX.person_id, person_id)
WHERE equals(in_cohort__XX.matched, 1)
LIMIT 100
'''
@@ -92,11 +92,11 @@
-- ClickHouse
SELECT events.event AS event
FROM events LEFT JOIN (
- SELECT cohortpeople.person_id AS person_id, 1 AS matched
- FROM cohortpeople
- WHERE and(equals(cohortpeople.team_id, 99999), equals(cohortpeople.cohort_id, XX))
- GROUP BY cohortpeople.person_id, cohortpeople.cohort_id, cohortpeople.version
- HAVING ifNull(greater(sum(cohortpeople.sign), 0), 0)) AS in_cohort__XX ON equals(in_cohort__XX.person_id, events.person_id)
+ SELECT cohortpeople.person_id AS person_id, 1 AS matched
+ FROM cohortpeople
+ WHERE and(equals(cohortpeople.team_id, 99999), equals(cohortpeople.cohort_id, XX))
+ GROUP BY cohortpeople.person_id, cohortpeople.cohort_id, cohortpeople.version
+ HAVING ifNull(greater(sum(cohortpeople.sign), 0), 0)) AS in_cohort__XX ON equals(in_cohort__XX.person_id, events.person_id)
WHERE and(equals(events.team_id, 99999), ifNull(equals(in_cohort__XX.matched, 1), 0), equals(events.event, %(hogql_val_0)s))
LIMIT 100
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0, transform_null_in=1, optimize_min_equality_disjunction_chain_length=4294967295, allow_experimental_join_condition=1, use_hive_partitioning=0
@@ -104,11 +104,11 @@
-- HogQL
SELECT event
FROM events LEFT JOIN (
- SELECT person_id, 1 AS matched
- FROM raw_cohort_people
- WHERE equals(cohort_id, XX)
- GROUP BY person_id, cohort_id, version
- HAVING greater(sum(sign), 0)) AS in_cohort__XX ON equals(in_cohort__XX.person_id, person_id)
+ SELECT person_id, 1 AS matched
+ FROM raw_cohort_people
+ WHERE equals(cohort_id, XX)
+ GROUP BY person_id, cohort_id, version
+ HAVING greater(sum(sign), 0)) AS in_cohort__XX ON equals(in_cohort__XX.person_id, person_id)
WHERE and(equals(in_cohort__XX.matched, 1), equals(event, 'RANDOM_TEST_ID::UUID'))
LIMIT 100
'''
@@ -118,9 +118,9 @@
-- ClickHouse
SELECT events.event AS event
FROM events LEFT JOIN (
- SELECT person_static_cohort.person_id AS person_id, 1 AS matched
- FROM person_static_cohort
- WHERE and(equals(person_static_cohort.team_id, 99999), equals(person_static_cohort.cohort_id, XX))) AS in_cohort__XX ON equals(in_cohort__XX.person_id, events.person_id)
+ SELECT person_static_cohort.person_id AS person_id, 1 AS matched
+ FROM person_static_cohort
+ WHERE and(equals(person_static_cohort.team_id, 99999), equals(person_static_cohort.cohort_id, XX))) AS in_cohort__XX ON equals(in_cohort__XX.person_id, events.person_id)
WHERE and(equals(events.team_id, 99999), ifNull(equals(in_cohort__XX.matched, 1), 0))
LIMIT 100
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0, transform_null_in=1, optimize_min_equality_disjunction_chain_length=4294967295, allow_experimental_join_condition=1, use_hive_partitioning=0
@@ -128,9 +128,9 @@
-- HogQL
SELECT event
FROM events LEFT JOIN (
- SELECT person_id, 1 AS matched
- FROM static_cohort_people
- WHERE equals(cohort_id, XX)) AS in_cohort__XX ON equals(in_cohort__XX.person_id, person_id)
+ SELECT person_id, 1 AS matched
+ FROM static_cohort_people
+ WHERE equals(cohort_id, XX)) AS in_cohort__XX ON equals(in_cohort__XX.person_id, person_id)
WHERE equals(in_cohort__XX.matched, 1)
LIMIT 100
'''
@@ -140,9 +140,9 @@
-- ClickHouse
SELECT events.event AS event
FROM events LEFT JOIN (
- SELECT person_static_cohort.person_id AS person_id, 1 AS matched
- FROM person_static_cohort
- WHERE and(equals(person_static_cohort.team_id, 99999), equals(person_static_cohort.cohort_id, XX))) AS in_cohort__XX ON equals(in_cohort__XX.person_id, events.person_id)
+ SELECT person_static_cohort.person_id AS person_id, 1 AS matched
+ FROM person_static_cohort
+ WHERE and(equals(person_static_cohort.team_id, 99999), equals(person_static_cohort.cohort_id, XX))) AS in_cohort__XX ON equals(in_cohort__XX.person_id, events.person_id)
WHERE and(equals(events.team_id, 99999), ifNull(equals(in_cohort__XX.matched, 1), 0))
LIMIT 100
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0, transform_null_in=1, optimize_min_equality_disjunction_chain_length=4294967295, allow_experimental_join_condition=1, use_hive_partitioning=0
@@ -150,9 +150,9 @@
-- HogQL
SELECT event
FROM events LEFT JOIN (
- SELECT person_id, 1 AS matched
- FROM static_cohort_people
- WHERE equals(cohort_id, XX)) AS in_cohort__XX ON equals(in_cohort__XX.person_id, person_id)
+ SELECT person_id, 1 AS matched
+ FROM static_cohort_people
+ WHERE equals(cohort_id, XX)) AS in_cohort__XX ON equals(in_cohort__XX.person_id, person_id)
WHERE equals(in_cohort__XX.matched, 1)
LIMIT 100
'''
@@ -162,24 +162,24 @@
-- ClickHouse
SELECT events.event AS event
FROM events LEFT JOIN (
- SELECT person_static_cohort.person_id AS cohort_person_id, 1 AS matched, person_static_cohort.cohort_id AS cohort_id
- FROM person_static_cohort
- WHERE and(equals(person_static_cohort.team_id, 99999), in(person_static_cohort.cohort_id, [1, 2, 3, 4, 5 /* ... */])) UNION ALL
- SELECT id AS cohort_person_id, 1 AS matched, 99999 AS cohort_id
- FROM ((
- SELECT persons.id AS id
- FROM (
- SELECT tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '')), person.version), 1) AS properties___test_prop, person.id AS id
- FROM person
- WHERE and(equals(person.team_id, 99999), in(id, (
- SELECT where_optimization.id AS id
- FROM person AS where_optimization
- WHERE and(equals(where_optimization.team_id, 99999), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, %(hogql_val_1)s), ''), 'null'), '^"|"$', ''), %(hogql_val_2)s), 0)))))
- GROUP BY person.id
- HAVING and(ifNull(equals(tupleElement(argMax(tuple(person.is_deleted), person.version), 1), 0), 0), ifNull(less(tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_3)s)), person.version), 1), plus(now64(6, %(hogql_val_4)s), toIntervalDay(1))), 0))) AS persons
- WHERE ifNull(equals(persons.properties___test_prop, %(hogql_val_5)s), 0)
- ORDER BY persons.id ASC
- SETTINGS optimize_aggregation_in_order=1, join_algorithm='auto'))) AS __in_cohort ON equals(__in_cohort.cohort_person_id, events.person_id)
+ SELECT person_static_cohort.person_id AS cohort_person_id, 1 AS matched, person_static_cohort.cohort_id AS cohort_id
+ FROM person_static_cohort
+ WHERE and(equals(person_static_cohort.team_id, 99999), in(person_static_cohort.cohort_id, [1, 2, 3, 4, 5 /* ... */])) UNION ALL
+ SELECT id AS cohort_person_id, 1 AS matched, 99999 AS cohort_id
+ FROM ((
+ SELECT persons.id AS id
+ FROM (
+ SELECT tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '')), person.version), 1) AS properties___test_prop, person.id AS id
+ FROM person
+ WHERE and(equals(person.team_id, 99999), in(id, (
+ SELECT where_optimization.id AS id
+ FROM person AS where_optimization
+ WHERE and(equals(where_optimization.team_id, 99999), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, %(hogql_val_1)s), ''), 'null'), '^"|"$', ''), %(hogql_val_2)s), 0)))))
+ GROUP BY person.id
+ HAVING and(ifNull(equals(tupleElement(argMax(tuple(person.is_deleted), person.version), 1), 0), 0), ifNull(less(tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_3)s)), person.version), 1), plus(now64(6, %(hogql_val_4)s), toIntervalDay(1))), 0))) AS persons
+ WHERE ifNull(equals(persons.properties___test_prop, %(hogql_val_5)s), 0)
+ ORDER BY persons.id ASC
+ SETTINGS optimize_aggregation_in_order=1, join_algorithm='auto'))) AS __in_cohort ON equals(__in_cohort.cohort_person_id, events.person_id)
WHERE and(equals(events.team_id, 99999), equals(events.event, %(hogql_val_6)s), ifNull(equals(__in_cohort.matched, 1), 0))
LIMIT 100
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0, transform_null_in=1, optimize_min_equality_disjunction_chain_length=4294967295, allow_experimental_join_condition=1, use_hive_partitioning=0
@@ -187,15 +187,15 @@
-- HogQL
SELECT event
FROM events LEFT JOIN (
- SELECT person_id AS cohort_person_id, 1 AS matched, cohort_id
- FROM static_cohort_people
- WHERE in(cohort_id, [1, 2, 3, 4, 5 /* ... */]) UNION ALL
- SELECT id AS cohort_person_id, 1 AS matched, 99999 AS cohort_id
- FROM ((
- SELECT id
- FROM persons
- WHERE equals(properties.test_prop, 'RANDOM_TEST_ID::UUID')
- ORDER BY id ASC))) AS __in_cohort ON equals(__in_cohort.cohort_person_id, person_id)
+ SELECT person_id AS cohort_person_id, 1 AS matched, cohort_id
+ FROM static_cohort_people
+ WHERE in(cohort_id, [1, 2, 3, 4, 5 /* ... */]) UNION ALL
+ SELECT id AS cohort_person_id, 1 AS matched, 99999 AS cohort_id
+ FROM ((
+ SELECT id
+ FROM persons
+ WHERE equals(properties.test_prop, 'RANDOM_TEST_ID::UUID')
+ ORDER BY id ASC))) AS __in_cohort ON equals(__in_cohort.cohort_person_id, person_id)
WHERE and(and(or(equals(1, 1), equals(1, 1)), equals(event, 'RANDOM_TEST_ID::UUID')), equals(__in_cohort.matched, 1))
LIMIT 100
'''
@@ -205,9 +205,9 @@
-- ClickHouse
SELECT events.event AS event
FROM events LEFT JOIN (
- SELECT cohortpeople.person_id AS cohort_person_id, 1 AS matched, cohortpeople.cohort_id AS cohort_id
- FROM cohortpeople
- WHERE and(equals(cohortpeople.team_id, 99999), or(and(equals(cohortpeople.cohort_id, XX), equals(cohortpeople.version, 0)), and(equals(cohortpeople.cohort_id, XX), equals(cohortpeople.version, 0))))) AS __in_cohort ON equals(__in_cohort.cohort_person_id, events.person_id)
+ SELECT cohortpeople.person_id AS cohort_person_id, 1 AS matched, cohortpeople.cohort_id AS cohort_id
+ FROM cohortpeople
+ WHERE and(equals(cohortpeople.team_id, 99999), or(and(equals(cohortpeople.cohort_id, XX), equals(cohortpeople.version, 0)), and(equals(cohortpeople.cohort_id, XX), equals(cohortpeople.version, 0))))) AS __in_cohort ON equals(__in_cohort.cohort_person_id, events.person_id)
WHERE and(equals(events.team_id, 99999), equals(events.event, %(hogql_val_0)s), ifNull(equals(__in_cohort.matched, 1), 0))
LIMIT 100
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0, transform_null_in=1, optimize_min_equality_disjunction_chain_length=4294967295, allow_experimental_join_condition=1, use_hive_partitioning=0
@@ -215,9 +215,9 @@
-- HogQL
SELECT event
FROM events LEFT JOIN (
- SELECT person_id AS cohort_person_id, 1 AS matched, cohort_id
- FROM raw_cohort_people
- WHERE or(and(equals(cohort_id, XX), equals(version, 0)), and(equals(cohort_id, XX), equals(version, 0)))) AS __in_cohort ON equals(__in_cohort.cohort_person_id, person_id)
+ SELECT person_id AS cohort_person_id, 1 AS matched, cohort_id
+ FROM raw_cohort_people
+ WHERE or(and(equals(cohort_id, XX), equals(version, 0)), and(equals(cohort_id, XX), equals(version, 0)))) AS __in_cohort ON equals(__in_cohort.cohort_person_id, person_id)
WHERE and(and(or(equals(1, 1), equals(1, 1)), equals(event, 'RANDOM_TEST_ID::UUID')), equals(__in_cohort.matched, 1))
LIMIT 100
'''
@@ -227,36 +227,36 @@
-- ClickHouse
SELECT events.event AS event
FROM events LEFT JOIN (
- SELECT id AS cohort_person_id, 1 AS matched, 99999 AS cohort_id
- FROM ((
- SELECT persons.id AS id
- FROM (
- SELECT tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '')), person.version), 1) AS properties___test_prop, person.id AS id
- FROM person
- WHERE and(equals(person.team_id, 99999), in(id, (
- SELECT where_optimization.id AS id
- FROM person AS where_optimization
- WHERE and(equals(where_optimization.team_id, 99999), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, %(hogql_val_1)s), ''), 'null'), '^"|"$', ''), %(hogql_val_2)s), 0)))))
- GROUP BY person.id
- HAVING and(ifNull(equals(tupleElement(argMax(tuple(person.is_deleted), person.version), 1), 0), 0), ifNull(less(tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_3)s)), person.version), 1), plus(now64(6, %(hogql_val_4)s), toIntervalDay(1))), 0))) AS persons
- WHERE ifNull(equals(persons.properties___test_prop, %(hogql_val_5)s), 0)
- ORDER BY persons.id ASC
- SETTINGS optimize_aggregation_in_order=1, join_algorithm='auto')) UNION ALL
- SELECT id AS cohort_person_id, 1 AS matched, 99999 AS cohort_id
- FROM ((
- SELECT persons.id AS id
- FROM (
- SELECT tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_6)s), ''), 'null'), '^"|"$', '')), person.version), 1) AS properties___test_prop, person.id AS id
- FROM person
- WHERE and(equals(person.team_id, 99999), in(id, (
- SELECT where_optimization.id AS id
- FROM person AS where_optimization
- WHERE and(equals(where_optimization.team_id, 99999), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, %(hogql_val_7)s), ''), 'null'), '^"|"$', ''), %(hogql_val_8)s), 0)))))
- GROUP BY person.id
- HAVING and(ifNull(equals(tupleElement(argMax(tuple(person.is_deleted), person.version), 1), 0), 0), ifNull(less(tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_9)s)), person.version), 1), plus(now64(6, %(hogql_val_10)s), toIntervalDay(1))), 0))) AS persons
- WHERE ifNull(equals(persons.properties___test_prop, %(hogql_val_11)s), 0)
- ORDER BY persons.id ASC
- SETTINGS optimize_aggregation_in_order=1, join_algorithm='auto'))) AS __in_cohort ON equals(__in_cohort.cohort_person_id, events.person_id)
+ SELECT id AS cohort_person_id, 1 AS matched, 99999 AS cohort_id
+ FROM ((
+ SELECT persons.id AS id
+ FROM (
+ SELECT tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '')), person.version), 1) AS properties___test_prop, person.id AS id
+ FROM person
+ WHERE and(equals(person.team_id, 99999), in(id, (
+ SELECT where_optimization.id AS id
+ FROM person AS where_optimization
+ WHERE and(equals(where_optimization.team_id, 99999), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, %(hogql_val_1)s), ''), 'null'), '^"|"$', ''), %(hogql_val_2)s), 0)))))
+ GROUP BY person.id
+ HAVING and(ifNull(equals(tupleElement(argMax(tuple(person.is_deleted), person.version), 1), 0), 0), ifNull(less(tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_3)s)), person.version), 1), plus(now64(6, %(hogql_val_4)s), toIntervalDay(1))), 0))) AS persons
+ WHERE ifNull(equals(persons.properties___test_prop, %(hogql_val_5)s), 0)
+ ORDER BY persons.id ASC
+ SETTINGS optimize_aggregation_in_order=1, join_algorithm='auto')) UNION ALL
+ SELECT id AS cohort_person_id, 1 AS matched, 99999 AS cohort_id
+ FROM ((
+ SELECT persons.id AS id
+ FROM (
+ SELECT tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_6)s), ''), 'null'), '^"|"$', '')), person.version), 1) AS properties___test_prop, person.id AS id
+ FROM person
+ WHERE and(equals(person.team_id, 99999), in(id, (
+ SELECT where_optimization.id AS id
+ FROM person AS where_optimization
+ WHERE and(equals(where_optimization.team_id, 99999), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, %(hogql_val_7)s), ''), 'null'), '^"|"$', ''), %(hogql_val_8)s), 0)))))
+ GROUP BY person.id
+ HAVING and(ifNull(equals(tupleElement(argMax(tuple(person.is_deleted), person.version), 1), 0), 0), ifNull(less(tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_9)s)), person.version), 1), plus(now64(6, %(hogql_val_10)s), toIntervalDay(1))), 0))) AS persons
+ WHERE ifNull(equals(persons.properties___test_prop, %(hogql_val_11)s), 0)
+ ORDER BY persons.id ASC
+ SETTINGS optimize_aggregation_in_order=1, join_algorithm='auto'))) AS __in_cohort ON equals(__in_cohort.cohort_person_id, events.person_id)
WHERE and(equals(events.team_id, 99999), equals(events.event, %(hogql_val_12)s), ifNull(equals(__in_cohort.matched, 1), 0))
LIMIT 100
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0, transform_null_in=1, optimize_min_equality_disjunction_chain_length=4294967295, allow_experimental_join_condition=1, use_hive_partitioning=0
@@ -264,18 +264,18 @@
-- HogQL
SELECT event
FROM events LEFT JOIN (
- SELECT id AS cohort_person_id, 1 AS matched, 99999 AS cohort_id
- FROM ((
- SELECT id
- FROM persons
- WHERE equals(properties.test_prop, 'RANDOM_TEST_ID::UUID')
- ORDER BY id ASC)) UNION ALL
- SELECT id AS cohort_person_id, 1 AS matched, 99999 AS cohort_id
- FROM ((
- SELECT id
- FROM persons
- WHERE equals(properties.test_prop, 'RANDOM_TEST_ID::UUID')
- ORDER BY id ASC))) AS __in_cohort ON equals(__in_cohort.cohort_person_id, person_id)
+ SELECT id AS cohort_person_id, 1 AS matched, 99999 AS cohort_id
+ FROM ((
+ SELECT id
+ FROM persons
+ WHERE equals(properties.test_prop, 'RANDOM_TEST_ID::UUID')
+ ORDER BY id ASC)) UNION ALL
+ SELECT id AS cohort_person_id, 1 AS matched, 99999 AS cohort_id
+ FROM ((
+ SELECT id
+ FROM persons
+ WHERE equals(properties.test_prop, 'RANDOM_TEST_ID::UUID')
+ ORDER BY id ASC))) AS __in_cohort ON equals(__in_cohort.cohort_person_id, person_id)
WHERE and(and(or(equals(1, 1), equals(1, 1)), equals(event, 'RANDOM_TEST_ID::UUID')), equals(__in_cohort.matched, 1))
LIMIT 100
'''
@@ -285,11 +285,11 @@
-- ClickHouse
SELECT events.event AS event
FROM events LEFT JOIN (
- SELECT cohortpeople.person_id AS person_id, 1 AS matched
- FROM cohortpeople
- WHERE and(equals(cohortpeople.team_id, 99999), equals(cohortpeople.cohort_id, XX))
- GROUP BY cohortpeople.person_id, cohortpeople.cohort_id, cohortpeople.version
- HAVING ifNull(greater(sum(cohortpeople.sign), 0), 0)) AS in_cohort__XX ON equals(in_cohort__XX.person_id, events.person_id)
+ SELECT cohortpeople.person_id AS person_id, 1 AS matched
+ FROM cohortpeople
+ WHERE and(equals(cohortpeople.team_id, 99999), equals(cohortpeople.cohort_id, XX))
+ GROUP BY cohortpeople.person_id, cohortpeople.cohort_id, cohortpeople.version
+ HAVING ifNull(greater(sum(cohortpeople.sign), 0), 0)) AS in_cohort__XX ON equals(in_cohort__XX.person_id, events.person_id)
WHERE and(equals(events.team_id, 99999), ifNull(equals(in_cohort__XX.matched, 1), 0), equals(events.event, %(hogql_val_0)s))
LIMIT 100
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0, transform_null_in=1, optimize_min_equality_disjunction_chain_length=4294967295, allow_experimental_join_condition=1, use_hive_partitioning=0
@@ -297,11 +297,11 @@
-- HogQL
SELECT event
FROM events LEFT JOIN (
- SELECT person_id, 1 AS matched
- FROM raw_cohort_people
- WHERE equals(cohort_id, XX)
- GROUP BY person_id, cohort_id, version
- HAVING greater(sum(sign), 0)) AS in_cohort__XX ON equals(in_cohort__XX.person_id, person_id)
+ SELECT person_id, 1 AS matched
+ FROM raw_cohort_people
+ WHERE equals(cohort_id, XX)
+ GROUP BY person_id, cohort_id, version
+ HAVING greater(sum(sign), 0)) AS in_cohort__XX ON equals(in_cohort__XX.person_id, person_id)
WHERE and(equals(in_cohort__XX.matched, 1), equals(event, 'RANDOM_TEST_ID::UUID'))
LIMIT 100
'''
@@ -311,21 +311,21 @@
-- ClickHouse
SELECT events.event AS event
FROM events LEFT JOIN (
- SELECT id AS person_id, 1 AS matched
- FROM ((
- SELECT persons.id AS id
- FROM (
- SELECT tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '')), person.version), 1) AS properties___test_prop, person.id AS id
- FROM person
- WHERE and(equals(person.team_id, 99999), in(id, (
- SELECT where_optimization.id AS id
- FROM person AS where_optimization
- WHERE and(equals(where_optimization.team_id, 99999), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, %(hogql_val_1)s), ''), 'null'), '^"|"$', ''), %(hogql_val_2)s), 0)))))
- GROUP BY person.id
- HAVING and(ifNull(equals(tupleElement(argMax(tuple(person.is_deleted), person.version), 1), 0), 0), ifNull(less(tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_3)s)), person.version), 1), plus(now64(6, %(hogql_val_4)s), toIntervalDay(1))), 0))) AS persons
- WHERE ifNull(equals(persons.properties___test_prop, %(hogql_val_5)s), 0)
- ORDER BY persons.id ASC
- SETTINGS optimize_aggregation_in_order=1, join_algorithm='auto'))) AS in_cohort__XX ON equals(in_cohort__XX.person_id, events.person_id)
+ SELECT id AS person_id, 1 AS matched
+ FROM ((
+ SELECT persons.id AS id
+ FROM (
+ SELECT tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '')), person.version), 1) AS properties___test_prop, person.id AS id
+ FROM person
+ WHERE and(equals(person.team_id, 99999), in(id, (
+ SELECT where_optimization.id AS id
+ FROM person AS where_optimization
+ WHERE and(equals(where_optimization.team_id, 99999), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, %(hogql_val_1)s), ''), 'null'), '^"|"$', ''), %(hogql_val_2)s), 0)))))
+ GROUP BY person.id
+ HAVING and(ifNull(equals(tupleElement(argMax(tuple(person.is_deleted), person.version), 1), 0), 0), ifNull(less(tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_3)s)), person.version), 1), plus(now64(6, %(hogql_val_4)s), toIntervalDay(1))), 0))) AS persons
+ WHERE ifNull(equals(persons.properties___test_prop, %(hogql_val_5)s), 0)
+ ORDER BY persons.id ASC
+ SETTINGS optimize_aggregation_in_order=1, join_algorithm='auto'))) AS in_cohort__XX ON equals(in_cohort__XX.person_id, events.person_id)
WHERE and(equals(events.team_id, 99999), ifNull(equals(in_cohort__XX.matched, 1), 0), equals(events.event, %(hogql_val_6)s))
LIMIT 100
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0, transform_null_in=1, optimize_min_equality_disjunction_chain_length=4294967295, allow_experimental_join_condition=1, use_hive_partitioning=0
@@ -333,20 +333,20 @@
-- HogQL
SELECT event
FROM events LEFT JOIN (
- SELECT id AS person_id, 1 AS matched
- FROM ((
- SELECT id
- FROM (
- SELECT tupleElement(argMax(tuple(raw_persons.properties.test_prop), raw_persons.version), 1) AS properties___test_prop, raw_persons.id AS id
- FROM raw_persons
- WHERE in(id, (
- SELECT id
- FROM raw_persons AS where_optimization
- WHERE equals(properties.test_prop, 'RANDOM_TEST_ID::UUID')))
- GROUP BY raw_persons.id
- HAVING and(equals(tupleElement(argMax(tuple(raw_persons.is_deleted), raw_persons.version), 1), 0), less(tupleElement(argMax(tuple(raw_persons.created_at), raw_persons.version), 1), plus(now(), toIntervalDay(1))))) AS persons
- WHERE equals(properties.test_prop, 'RANDOM_TEST_ID::UUID')
- ORDER BY id ASC))) AS in_cohort__XX ON equals(in_cohort__XX.person_id, person_id)
+ SELECT id AS person_id, 1 AS matched
+ FROM ((
+ SELECT id
+ FROM (
+ SELECT tupleElement(argMax(tuple(raw_persons.properties.test_prop), raw_persons.version), 1) AS properties___test_prop, raw_persons.id AS id
+ FROM raw_persons
+ WHERE in(id, (
+ SELECT id
+ FROM raw_persons AS where_optimization
+ WHERE equals(properties.test_prop, 'RANDOM_TEST_ID::UUID')))
+ GROUP BY raw_persons.id
+ HAVING and(equals(tupleElement(argMax(tuple(raw_persons.is_deleted), raw_persons.version), 1), 0), less(tupleElement(argMax(tuple(raw_persons.created_at), raw_persons.version), 1), plus(now(), toIntervalDay(1))))) AS persons
+ WHERE equals(properties.test_prop, 'RANDOM_TEST_ID::UUID')
+ ORDER BY id ASC))) AS in_cohort__XX ON equals(in_cohort__XX.person_id, person_id)
WHERE and(equals(in_cohort__XX.matched, 1), equals(event, 'RANDOM_TEST_ID::UUID'))
LIMIT 100
'''
@@ -356,9 +356,9 @@
-- ClickHouse
SELECT events.event AS event
FROM events LEFT JOIN (
- SELECT person_static_cohort.person_id AS person_id, 1 AS matched
- FROM person_static_cohort
- WHERE and(equals(person_static_cohort.team_id, 99999), equals(person_static_cohort.cohort_id, XX))) AS in_cohort__XX ON equals(in_cohort__XX.person_id, events.person_id)
+ SELECT person_static_cohort.person_id AS person_id, 1 AS matched
+ FROM person_static_cohort
+ WHERE and(equals(person_static_cohort.team_id, 99999), equals(person_static_cohort.cohort_id, XX))) AS in_cohort__XX ON equals(in_cohort__XX.person_id, events.person_id)
WHERE and(equals(events.team_id, 99999), ifNull(equals(in_cohort__XX.matched, 1), 0), equals(events.event, %(hogql_val_0)s))
LIMIT 100
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0, transform_null_in=1, optimize_min_equality_disjunction_chain_length=4294967295, allow_experimental_join_condition=1, use_hive_partitioning=0
@@ -366,9 +366,9 @@
-- HogQL
SELECT event
FROM events LEFT JOIN (
- SELECT person_id, 1 AS matched
- FROM static_cohort_people
- WHERE equals(cohort_id, XX)) AS in_cohort__XX ON equals(in_cohort__XX.person_id, person_id)
+ SELECT person_id, 1 AS matched
+ FROM static_cohort_people
+ WHERE equals(cohort_id, XX)) AS in_cohort__XX ON equals(in_cohort__XX.person_id, person_id)
WHERE and(equals(in_cohort__XX.matched, 1), equals(event, 'RANDOM_TEST_ID::UUID'))
LIMIT 100
'''
diff --git a/posthog/hogql/transforms/test/__snapshots__/test_lazy_tables.ambr b/posthog/hogql/transforms/test/__snapshots__/test_lazy_tables.ambr
index 071fb5332be2..2a4c940c17e3 100644
--- a/posthog/hogql/transforms/test/__snapshots__/test_lazy_tables.ambr
+++ b/posthog/hogql/transforms/test/__snapshots__/test_lazy_tables.ambr
@@ -4,14 +4,14 @@
SELECT events.event AS event, events__session.`$session_duration` AS `$session_duration`
FROM (
- SELECT events.`$session_id` AS `$session_id`, events.event AS event, toTimeZone(events.timestamp, %(hogql_val_0)s) AS timestamp
- FROM events
- WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_1)s, 6, %(hogql_val_2)s)), less(events.timestamp, toDateTime64(%(hogql_val_3)s, 6, %(hogql_val_4)s)), equals(events.event, %(hogql_val_5)s), isNotNull(events.`$session_id`))
- LIMIT 50000) AS events LEFT JOIN (
- SELECT dateDiff(%(hogql_val_6)s, min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_7)s)), max(toTimeZone(raw_sessions.max_timestamp, %(hogql_val_8)s))) AS `$session_duration`, raw_sessions.session_id_v7 AS session_id_v7
- FROM raw_sessions
- WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(%(hogql_val_9)s, toIntervalDay(3))), lessOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), plus(%(hogql_val_10)s, toIntervalDay(3))))
- GROUP BY raw_sessions.session_id_v7) AS events__session ON equals(toUInt128(accurateCastOrNull(events.`$session_id`, %(hogql_val_11)s)), events__session.session_id_v7)
+ SELECT events.`$session_id` AS `$session_id`, events.event AS event, toTimeZone(events.timestamp, %(hogql_val_0)s) AS timestamp
+ FROM events
+ WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_1)s, 6, %(hogql_val_2)s)), less(events.timestamp, toDateTime64(%(hogql_val_3)s, 6, %(hogql_val_4)s)), equals(events.event, %(hogql_val_5)s), isNotNull(events.`$session_id`))
+ LIMIT 50000) AS events LEFT JOIN (
+ SELECT dateDiff(%(hogql_val_6)s, min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_7)s)), max(toTimeZone(raw_sessions.max_timestamp, %(hogql_val_8)s))) AS `$session_duration`, raw_sessions.session_id_v7 AS session_id_v7
+ FROM raw_sessions
+ WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(%(hogql_val_9)s, toIntervalDay(3))), lessOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), plus(%(hogql_val_10)s, toIntervalDay(3))))
+ GROUP BY raw_sessions.session_id_v7) AS events__session ON equals(toUInt128(accurateCastOrNull(events.`$session_id`, %(hogql_val_11)s)), events__session.session_id_v7)
LIMIT 50000
'''
# ---
@@ -20,10 +20,10 @@
SELECT events.event AS event, events__session.`$session_duration` AS `$session_duration`
FROM events LEFT JOIN (
- SELECT dateDiff(%(hogql_val_0)s, min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_1)s)), max(toTimeZone(raw_sessions.max_timestamp, %(hogql_val_2)s))) AS `$session_duration`, raw_sessions.session_id_v7 AS session_id_v7
- FROM raw_sessions
- WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(%(hogql_val_3)s, toIntervalDay(3))))
- GROUP BY raw_sessions.session_id_v7) AS events__session ON equals(toUInt128(accurateCastOrNull(events.`$session_id`, %(hogql_val_4)s)), events__session.session_id_v7)
+ SELECT dateDiff(%(hogql_val_0)s, min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_1)s)), max(toTimeZone(raw_sessions.max_timestamp, %(hogql_val_2)s))) AS `$session_duration`, raw_sessions.session_id_v7 AS session_id_v7
+ FROM raw_sessions
+ WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(%(hogql_val_3)s, toIntervalDay(3))))
+ GROUP BY raw_sessions.session_id_v7) AS events__session ON equals(toUInt128(accurateCastOrNull(events.`$session_id`, %(hogql_val_4)s)), events__session.session_id_v7)
WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_5)s, 6, %(hogql_val_6)s)), ifNull(greater(events__session.`$session_duration`, 0), 0))
LIMIT 50000
'''
@@ -33,14 +33,14 @@
SELECT events.event AS event, events__session.`$session_duration` AS `$session_duration`
FROM (
- SELECT events.`$session_id` AS `$session_id`, events.event AS event, toTimeZone(events.timestamp, %(hogql_val_0)s) AS timestamp
- FROM events
- WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_1)s, 6, %(hogql_val_2)s)), less(events.timestamp, toDateTime64(%(hogql_val_3)s, 6, %(hogql_val_4)s)))
- LIMIT 50000) AS events LEFT JOIN (
- SELECT dateDiff(%(hogql_val_5)s, min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_6)s)), max(toTimeZone(raw_sessions.max_timestamp, %(hogql_val_7)s))) AS `$session_duration`, raw_sessions.session_id_v7 AS session_id_v7
- FROM raw_sessions
- WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(%(hogql_val_8)s, toIntervalDay(3))), lessOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), plus(%(hogql_val_9)s, toIntervalDay(3))))
- GROUP BY raw_sessions.session_id_v7) AS events__session ON equals(toUInt128(accurateCastOrNull(events.`$session_id`, %(hogql_val_10)s)), events__session.session_id_v7)
+ SELECT events.`$session_id` AS `$session_id`, events.event AS event, toTimeZone(events.timestamp, %(hogql_val_0)s) AS timestamp
+ FROM events
+ WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_1)s, 6, %(hogql_val_2)s)), less(events.timestamp, toDateTime64(%(hogql_val_3)s, 6, %(hogql_val_4)s)))
+ LIMIT 50000) AS events LEFT JOIN (
+ SELECT dateDiff(%(hogql_val_5)s, min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_6)s)), max(toTimeZone(raw_sessions.max_timestamp, %(hogql_val_7)s))) AS `$session_duration`, raw_sessions.session_id_v7 AS session_id_v7
+ FROM raw_sessions
+ WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(%(hogql_val_8)s, toIntervalDay(3))), lessOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), plus(%(hogql_val_9)s, toIntervalDay(3))))
+ GROUP BY raw_sessions.session_id_v7) AS events__session ON equals(toUInt128(accurateCastOrNull(events.`$session_id`, %(hogql_val_10)s)), events__session.session_id_v7)
LIMIT 50000
'''
# ---
@@ -49,14 +49,14 @@
SELECT e.event AS event, e__session.`$session_duration` AS `$session_duration`
FROM (
- SELECT e.`$session_id` AS `$session_id`, e.event AS event, toTimeZone(e.timestamp, %(hogql_val_0)s) AS timestamp
- FROM events AS e
- WHERE and(equals(e.team_id, 420), greaterOrEquals(e.timestamp, toDateTime64(%(hogql_val_1)s, 6, %(hogql_val_2)s)), equals(e.event, %(hogql_val_3)s))
- LIMIT 50000) AS e LEFT JOIN (
- SELECT dateDiff(%(hogql_val_4)s, min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_5)s)), max(toTimeZone(raw_sessions.max_timestamp, %(hogql_val_6)s))) AS `$session_duration`, raw_sessions.session_id_v7 AS session_id_v7
- FROM raw_sessions
- WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(%(hogql_val_7)s, toIntervalDay(3))))
- GROUP BY raw_sessions.session_id_v7) AS e__session ON equals(toUInt128(accurateCastOrNull(e.`$session_id`, %(hogql_val_8)s)), e__session.session_id_v7)
+ SELECT e.`$session_id` AS `$session_id`, e.event AS event, toTimeZone(e.timestamp, %(hogql_val_0)s) AS timestamp
+ FROM events AS e
+ WHERE and(equals(e.team_id, 420), greaterOrEquals(e.timestamp, toDateTime64(%(hogql_val_1)s, 6, %(hogql_val_2)s)), equals(e.event, %(hogql_val_3)s))
+ LIMIT 50000) AS e LEFT JOIN (
+ SELECT dateDiff(%(hogql_val_4)s, min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_5)s)), max(toTimeZone(raw_sessions.max_timestamp, %(hogql_val_6)s))) AS `$session_duration`, raw_sessions.session_id_v7 AS session_id_v7
+ FROM raw_sessions
+ WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(%(hogql_val_7)s, toIntervalDay(3))))
+ GROUP BY raw_sessions.session_id_v7) AS e__session ON equals(toUInt128(accurateCastOrNull(e.`$session_id`, %(hogql_val_8)s)), e__session.session_id_v7)
LIMIT 50000
'''
# ---
@@ -65,17 +65,17 @@
SELECT cohort_people__new_person.id AS id
FROM (
- SELECT DISTINCT cohortpeople.person_id AS cohort_people___person_id, cohortpeople.person_id AS person_id, cohortpeople.cohort_id AS cohort_id
- FROM cohortpeople
- WHERE 0) AS cohort_people LEFT JOIN (
- SELECT persons.id AS id, id AS cohort_people__new_person___id
- FROM (
- SELECT person.id AS id
- FROM person
- WHERE equals(person.team_id, 420)
- GROUP BY person.id
- HAVING and(ifNull(equals(tupleElement(argMax(tuple(person.is_deleted), person.version), 1), 0), 0), ifNull(less(tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_0)s)), person.version), 1), plus(now64(6, %(hogql_val_1)s), toIntervalDay(1))), 0))
- SETTINGS optimize_aggregation_in_order=1) AS persons) AS cohort_people__new_person ON equals(cohort_people.cohort_people___person_id, cohort_people__new_person.cohort_people__new_person___id)
+ SELECT DISTINCT cohortpeople.person_id AS cohort_people___person_id, cohortpeople.person_id AS person_id, cohortpeople.cohort_id AS cohort_id
+ FROM cohortpeople
+ WHERE 0) AS cohort_people LEFT JOIN (
+ SELECT persons.id AS id, id AS cohort_people__new_person___id
+ FROM (
+ SELECT person.id AS id
+ FROM person
+ WHERE equals(person.team_id, 420)
+ GROUP BY person.id
+ HAVING and(ifNull(equals(tupleElement(argMax(tuple(person.is_deleted), person.version), 1), 0), 0), ifNull(less(tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_0)s)), person.version), 1), plus(now64(6, %(hogql_val_1)s), toIntervalDay(1))), 0))
+ SETTINGS optimize_aggregation_in_order=1) AS persons) AS cohort_people__new_person ON equals(cohort_people.cohort_people___person_id, cohort_people__new_person.cohort_people__new_person___id)
LIMIT 50000
'''
# ---
@@ -84,15 +84,15 @@
SELECT persons__events.event AS event
FROM (
- SELECT tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '')), person.version), 1) AS persons___properties___email, person.id AS id
- FROM person
- WHERE equals(person.team_id, 420)
- GROUP BY person.id
- HAVING and(ifNull(equals(tupleElement(argMax(tuple(person.is_deleted), person.version), 1), 0), 0), ifNull(less(tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_1)s)), person.version), 1), plus(now64(6, %(hogql_val_2)s), toIntervalDay(1))), 0))
- SETTINGS optimize_aggregation_in_order=1) AS persons LEFT JOIN (
- SELECT events.event AS event, event AS persons__events___event
- FROM events
- WHERE equals(events.team_id, 420)) AS persons__events ON equals(persons.persons___properties___email, persons__events.persons__events___event)
+ SELECT tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '')), person.version), 1) AS persons___properties___email, person.id AS id
+ FROM person
+ WHERE equals(person.team_id, 420)
+ GROUP BY person.id
+ HAVING and(ifNull(equals(tupleElement(argMax(tuple(person.is_deleted), person.version), 1), 0), 0), ifNull(less(tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_1)s)), person.version), 1), plus(now64(6, %(hogql_val_2)s), toIntervalDay(1))), 0))
+ SETTINGS optimize_aggregation_in_order=1) AS persons LEFT JOIN (
+ SELECT events.event AS event, event AS persons__events___event
+ FROM events
+ WHERE equals(events.team_id, 420)) AS persons__events ON equals(persons.persons___properties___email, persons__events.persons__events___event)
LIMIT 50000
'''
# ---
@@ -101,17 +101,17 @@
SELECT cohort_people__new_person.id AS id
FROM (
- SELECT DISTINCT cohortpeople.person_id AS cohort_people___person_id, cohortpeople.person_id AS person_id, cohortpeople.cohort_id AS cohort_id
- FROM cohortpeople
- WHERE 0) AS cohort_people LEFT JOIN (
- SELECT persons.id AS id, persons.properties___email AS cohort_people__new_person___properties___email
- FROM (
- SELECT tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '')), person.version), 1) AS properties___email, person.id AS id
- FROM person
- WHERE equals(person.team_id, 420)
- GROUP BY person.id
- HAVING and(ifNull(equals(tupleElement(argMax(tuple(person.is_deleted), person.version), 1), 0), 0), ifNull(less(tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_1)s)), person.version), 1), plus(now64(6, %(hogql_val_2)s), toIntervalDay(1))), 0))
- SETTINGS optimize_aggregation_in_order=1) AS persons) AS cohort_people__new_person ON equals(cohort_people.cohort_people___person_id, cohort_people__new_person.cohort_people__new_person___properties___email)
+ SELECT DISTINCT cohortpeople.person_id AS cohort_people___person_id, cohortpeople.person_id AS person_id, cohortpeople.cohort_id AS cohort_id
+ FROM cohortpeople
+ WHERE 0) AS cohort_people LEFT JOIN (
+ SELECT persons.id AS id, persons.properties___email AS cohort_people__new_person___properties___email
+ FROM (
+ SELECT tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '')), person.version), 1) AS properties___email, person.id AS id
+ FROM person
+ WHERE equals(person.team_id, 420)
+ GROUP BY person.id
+ HAVING and(ifNull(equals(tupleElement(argMax(tuple(person.is_deleted), person.version), 1), 0), 0), ifNull(less(tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_1)s)), person.version), 1), plus(now64(6, %(hogql_val_2)s), toIntervalDay(1))), 0))
+ SETTINGS optimize_aggregation_in_order=1) AS persons) AS cohort_people__new_person ON equals(cohort_people.cohort_people___person_id, cohort_people__new_person.cohort_people__new_person___properties___email)
LIMIT 50000
'''
# ---
@@ -120,12 +120,12 @@
SELECT persons.id AS id, persons.properties___email AS email, persons.`properties___$browser` AS `$browser`
FROM (
- SELECT tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '')), person.version), 1) AS properties___email, tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_1)s), ''), 'null'), '^"|"$', '')), person.version), 1) AS `properties___$browser`, person.id AS id
- FROM person
- WHERE equals(person.team_id, 420)
- GROUP BY person.id
- HAVING and(ifNull(equals(tupleElement(argMax(tuple(person.is_deleted), person.version), 1), 0), 0), ifNull(less(tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_2)s)), person.version), 1), plus(now64(6, %(hogql_val_3)s), toIntervalDay(1))), 0))
- SETTINGS optimize_aggregation_in_order=1) AS persons
+ SELECT tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '')), person.version), 1) AS properties___email, tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_1)s), ''), 'null'), '^"|"$', '')), person.version), 1) AS `properties___$browser`, person.id AS id
+ FROM person
+ WHERE equals(person.team_id, 420)
+ GROUP BY person.id
+ HAVING and(ifNull(equals(tupleElement(argMax(tuple(person.is_deleted), person.version), 1), 0), 0), ifNull(less(tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_2)s)), person.version), 1), plus(now64(6, %(hogql_val_3)s), toIntervalDay(1))), 0))
+ SETTINGS optimize_aggregation_in_order=1) AS persons
LIMIT 50000
'''
# ---
@@ -134,18 +134,18 @@
SELECT events.event AS event, events.distinct_id AS distinct_id, if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS person_id, persons.properties___email AS email
FROM events LEFT OUTER JOIN (
- SELECT tupleElement(argMax(tuple(person_distinct_id_overrides.person_id), person_distinct_id_overrides.version), 1) AS person_id, person_distinct_id_overrides.distinct_id AS distinct_id
- FROM person_distinct_id_overrides
- WHERE equals(person_distinct_id_overrides.team_id, 420)
- GROUP BY person_distinct_id_overrides.distinct_id
- HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id_overrides.is_deleted), person_distinct_id_overrides.version), 1), 0), 0)
- SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id) LEFT JOIN (
- SELECT tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '')), person.version), 1) AS properties___email, person.id AS id
- FROM person
- WHERE equals(person.team_id, 420)
- GROUP BY person.id
- HAVING and(ifNull(equals(tupleElement(argMax(tuple(person.is_deleted), person.version), 1), 0), 0), ifNull(less(tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_1)s)), person.version), 1), plus(now64(6, %(hogql_val_2)s), toIntervalDay(1))), 0))
- SETTINGS optimize_aggregation_in_order=1) AS persons ON equals(persons.id, if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id))
+ SELECT tupleElement(argMax(tuple(person_distinct_id_overrides.person_id), person_distinct_id_overrides.version), 1) AS person_id, person_distinct_id_overrides.distinct_id AS distinct_id
+ FROM person_distinct_id_overrides
+ WHERE equals(person_distinct_id_overrides.team_id, 420)
+ GROUP BY person_distinct_id_overrides.distinct_id
+ HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id_overrides.is_deleted), person_distinct_id_overrides.version), 1), 0), 0)
+ SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id) LEFT JOIN (
+ SELECT tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '')), person.version), 1) AS properties___email, person.id AS id
+ FROM person
+ WHERE equals(person.team_id, 420)
+ GROUP BY person.id
+ HAVING and(ifNull(equals(tupleElement(argMax(tuple(person.is_deleted), person.version), 1), 0), 0), ifNull(less(tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_1)s)), person.version), 1), plus(now64(6, %(hogql_val_2)s), toIntervalDay(1))), 0))
+ SETTINGS optimize_aggregation_in_order=1) AS persons ON equals(persons.id, if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id))
WHERE equals(events.team_id, 420)
LIMIT 10
'''
@@ -155,18 +155,18 @@
SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS person_id, events__person.properties AS properties
FROM events LEFT OUTER JOIN (
- SELECT tupleElement(argMax(tuple(person_distinct_id_overrides.person_id), person_distinct_id_overrides.version), 1) AS person_id, person_distinct_id_overrides.distinct_id AS distinct_id
- FROM person_distinct_id_overrides
- WHERE equals(person_distinct_id_overrides.team_id, 420)
- GROUP BY person_distinct_id_overrides.distinct_id
- HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id_overrides.is_deleted), person_distinct_id_overrides.version), 1), 0), 0)
- SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id) LEFT JOIN (
- SELECT tupleElement(argMax(tuple(person.properties), person.version), 1) AS properties, person.id AS id
- FROM person
- WHERE equals(person.team_id, 420)
- GROUP BY person.id
- HAVING and(ifNull(equals(tupleElement(argMax(tuple(person.is_deleted), person.version), 1), 0), 0), ifNull(less(tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_0)s)), person.version), 1), plus(now64(6, %(hogql_val_1)s), toIntervalDay(1))), 0))
- SETTINGS optimize_aggregation_in_order=1) AS events__person ON equals(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), events__person.id)
+ SELECT tupleElement(argMax(tuple(person_distinct_id_overrides.person_id), person_distinct_id_overrides.version), 1) AS person_id, person_distinct_id_overrides.distinct_id AS distinct_id
+ FROM person_distinct_id_overrides
+ WHERE equals(person_distinct_id_overrides.team_id, 420)
+ GROUP BY person_distinct_id_overrides.distinct_id
+ HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id_overrides.is_deleted), person_distinct_id_overrides.version), 1), 0), 0)
+ SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id) LEFT JOIN (
+ SELECT tupleElement(argMax(tuple(person.properties), person.version), 1) AS properties, person.id AS id
+ FROM person
+ WHERE equals(person.team_id, 420)
+ GROUP BY person.id
+ HAVING and(ifNull(equals(tupleElement(argMax(tuple(person.is_deleted), person.version), 1), 0), 0), ifNull(less(tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_0)s)), person.version), 1), plus(now64(6, %(hogql_val_1)s), toIntervalDay(1))), 0))
+ SETTINGS optimize_aggregation_in_order=1) AS events__person ON equals(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), events__person.id)
WHERE equals(events.team_id, 420)
LIMIT 50000
'''
@@ -176,18 +176,18 @@
SELECT events__person.id AS id
FROM events LEFT OUTER JOIN (
- SELECT tupleElement(argMax(tuple(person_distinct_id_overrides.person_id), person_distinct_id_overrides.version), 1) AS person_id, person_distinct_id_overrides.distinct_id AS distinct_id
- FROM person_distinct_id_overrides
- WHERE equals(person_distinct_id_overrides.team_id, 420)
- GROUP BY person_distinct_id_overrides.distinct_id
- HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id_overrides.is_deleted), person_distinct_id_overrides.version), 1), 0), 0)
- SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id) LEFT JOIN (
- SELECT person.id AS id
- FROM person
- WHERE equals(person.team_id, 420)
- GROUP BY person.id
- HAVING and(ifNull(equals(tupleElement(argMax(tuple(person.is_deleted), person.version), 1), 0), 0), ifNull(less(tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_0)s)), person.version), 1), plus(now64(6, %(hogql_val_1)s), toIntervalDay(1))), 0))
- SETTINGS optimize_aggregation_in_order=1) AS events__person ON equals(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), events__person.id)
+ SELECT tupleElement(argMax(tuple(person_distinct_id_overrides.person_id), person_distinct_id_overrides.version), 1) AS person_id, person_distinct_id_overrides.distinct_id AS distinct_id
+ FROM person_distinct_id_overrides
+ WHERE equals(person_distinct_id_overrides.team_id, 420)
+ GROUP BY person_distinct_id_overrides.distinct_id
+ HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id_overrides.is_deleted), person_distinct_id_overrides.version), 1), 0), 0)
+ SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id) LEFT JOIN (
+ SELECT person.id AS id
+ FROM person
+ WHERE equals(person.team_id, 420)
+ GROUP BY person.id
+ HAVING and(ifNull(equals(tupleElement(argMax(tuple(person.is_deleted), person.version), 1), 0), 0), ifNull(less(tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_0)s)), person.version), 1), plus(now64(6, %(hogql_val_1)s), toIntervalDay(1))), 0))
+ SETTINGS optimize_aggregation_in_order=1) AS events__person ON equals(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), events__person.id)
WHERE equals(events.team_id, 420)
LIMIT 50000
'''
@@ -197,12 +197,12 @@
SELECT events.event AS event, events__pdi.person_id AS person_id
FROM events INNER JOIN (
- SELECT tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS person_id, person_distinct_id2.distinct_id AS distinct_id
- FROM person_distinct_id2
- WHERE equals(person_distinct_id2.team_id, 420)
- GROUP BY person_distinct_id2.distinct_id
- HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id2.is_deleted), person_distinct_id2.version), 1), 0), 0)
- SETTINGS optimize_aggregation_in_order=1) AS events__pdi ON equals(events.distinct_id, events__pdi.distinct_id)
+ SELECT tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS person_id, person_distinct_id2.distinct_id AS distinct_id
+ FROM person_distinct_id2
+ WHERE equals(person_distinct_id2.team_id, 420)
+ GROUP BY person_distinct_id2.distinct_id
+ HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id2.is_deleted), person_distinct_id2.version), 1), 0), 0)
+ SETTINGS optimize_aggregation_in_order=1) AS events__pdi ON equals(events.distinct_id, events__pdi.distinct_id)
WHERE equals(events.team_id, 420)
LIMIT 50000
'''
@@ -212,18 +212,18 @@
SELECT person_distinct_ids__person.`properties___$browser` AS `$browser`
FROM (
- SELECT tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS person_distinct_ids___person_id, tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS person_id, person_distinct_id2.distinct_id AS distinct_id
- FROM person_distinct_id2
- WHERE equals(person_distinct_id2.team_id, 420)
- GROUP BY person_distinct_id2.distinct_id
- HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id2.is_deleted), person_distinct_id2.version), 1), 0), 0)
- SETTINGS optimize_aggregation_in_order=1) AS person_distinct_ids LEFT JOIN (
- SELECT tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '')), person.version), 1) AS `properties___$browser`, person.id AS id
- FROM person
- WHERE equals(person.team_id, 420)
- GROUP BY person.id
- HAVING and(ifNull(equals(tupleElement(argMax(tuple(person.is_deleted), person.version), 1), 0), 0), ifNull(less(tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_1)s)), person.version), 1), plus(now64(6, %(hogql_val_2)s), toIntervalDay(1))), 0))
- SETTINGS optimize_aggregation_in_order=1) AS person_distinct_ids__person ON equals(person_distinct_ids.person_distinct_ids___person_id, person_distinct_ids__person.id)
+ SELECT tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS person_distinct_ids___person_id, tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS person_id, person_distinct_id2.distinct_id AS distinct_id
+ FROM person_distinct_id2
+ WHERE equals(person_distinct_id2.team_id, 420)
+ GROUP BY person_distinct_id2.distinct_id
+ HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id2.is_deleted), person_distinct_id2.version), 1), 0), 0)
+ SETTINGS optimize_aggregation_in_order=1) AS person_distinct_ids LEFT JOIN (
+ SELECT tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '')), person.version), 1) AS `properties___$browser`, person.id AS id
+ FROM person
+ WHERE equals(person.team_id, 420)
+ GROUP BY person.id
+ HAVING and(ifNull(equals(tupleElement(argMax(tuple(person.is_deleted), person.version), 1), 0), 0), ifNull(less(tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_1)s)), person.version), 1), plus(now64(6, %(hogql_val_2)s), toIntervalDay(1))), 0))
+ SETTINGS optimize_aggregation_in_order=1) AS person_distinct_ids__person ON equals(person_distinct_ids.person_distinct_ids___person_id, person_distinct_ids__person.id)
LIMIT 50000
'''
# ---
@@ -232,18 +232,18 @@
SELECT person_distinct_ids__person.`properties___$browser___in___json` AS `$browser__in__json`
FROM (
- SELECT tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS person_distinct_ids___person_id, tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS person_id, person_distinct_id2.distinct_id AS distinct_id
- FROM person_distinct_id2
- WHERE equals(person_distinct_id2.team_id, 420)
- GROUP BY person_distinct_id2.distinct_id
- HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id2.is_deleted), person_distinct_id2.version), 1), 0), 0)
- SETTINGS optimize_aggregation_in_order=1) AS person_distinct_ids LEFT JOIN (
- SELECT tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_0)s, %(hogql_val_1)s, %(hogql_val_2)s), ''), 'null'), '^"|"$', '')), person.version), 1) AS `properties___$browser___in___json`, person.id AS id
- FROM person
- WHERE equals(person.team_id, 420)
- GROUP BY person.id
- HAVING and(ifNull(equals(tupleElement(argMax(tuple(person.is_deleted), person.version), 1), 0), 0), ifNull(less(tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_3)s)), person.version), 1), plus(now64(6, %(hogql_val_4)s), toIntervalDay(1))), 0))
- SETTINGS optimize_aggregation_in_order=1) AS person_distinct_ids__person ON equals(person_distinct_ids.person_distinct_ids___person_id, person_distinct_ids__person.id)
+ SELECT tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS person_distinct_ids___person_id, tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS person_id, person_distinct_id2.distinct_id AS distinct_id
+ FROM person_distinct_id2
+ WHERE equals(person_distinct_id2.team_id, 420)
+ GROUP BY person_distinct_id2.distinct_id
+ HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id2.is_deleted), person_distinct_id2.version), 1), 0), 0)
+ SETTINGS optimize_aggregation_in_order=1) AS person_distinct_ids LEFT JOIN (
+ SELECT tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_0)s, %(hogql_val_1)s, %(hogql_val_2)s), ''), 'null'), '^"|"$', '')), person.version), 1) AS `properties___$browser___in___json`, person.id AS id
+ FROM person
+ WHERE equals(person.team_id, 420)
+ GROUP BY person.id
+ HAVING and(ifNull(equals(tupleElement(argMax(tuple(person.is_deleted), person.version), 1), 0), 0), ifNull(less(tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_3)s)), person.version), 1), plus(now64(6, %(hogql_val_4)s), toIntervalDay(1))), 0))
+ SETTINGS optimize_aggregation_in_order=1) AS person_distinct_ids__person ON equals(person_distinct_ids.person_distinct_ids___person_id, person_distinct_ids__person.id)
LIMIT 50000
'''
# ---
@@ -252,12 +252,12 @@
SELECT events.event AS event, if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS person_id
FROM events LEFT OUTER JOIN (
- SELECT tupleElement(argMax(tuple(person_distinct_id_overrides.person_id), person_distinct_id_overrides.version), 1) AS person_id, person_distinct_id_overrides.distinct_id AS distinct_id
- FROM person_distinct_id_overrides
- WHERE equals(person_distinct_id_overrides.team_id, 420)
- GROUP BY person_distinct_id_overrides.distinct_id
- HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id_overrides.is_deleted), person_distinct_id_overrides.version), 1), 0), 0)
- SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)
+ SELECT tupleElement(argMax(tuple(person_distinct_id_overrides.person_id), person_distinct_id_overrides.version), 1) AS person_id, person_distinct_id_overrides.distinct_id AS distinct_id
+ FROM person_distinct_id_overrides
+ WHERE equals(person_distinct_id_overrides.team_id, 420)
+ GROUP BY person_distinct_id_overrides.distinct_id
+ HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id_overrides.is_deleted), person_distinct_id_overrides.version), 1), 0), 0)
+ SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)
WHERE equals(events.team_id, 420)
LIMIT 50000
'''
@@ -267,18 +267,18 @@
SELECT events.event AS event, events__pdi__person.id AS id
FROM events INNER JOIN (
- SELECT tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS events__pdi___person_id, tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS person_id, person_distinct_id2.distinct_id AS distinct_id
- FROM person_distinct_id2
- WHERE equals(person_distinct_id2.team_id, 420)
- GROUP BY person_distinct_id2.distinct_id
- HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id2.is_deleted), person_distinct_id2.version), 1), 0), 0)
- SETTINGS optimize_aggregation_in_order=1) AS events__pdi ON equals(events.distinct_id, events__pdi.distinct_id) LEFT JOIN (
- SELECT person.id AS id
- FROM person
- WHERE equals(person.team_id, 420)
- GROUP BY person.id
- HAVING and(ifNull(equals(tupleElement(argMax(tuple(person.is_deleted), person.version), 1), 0), 0), ifNull(less(tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_0)s)), person.version), 1), plus(now64(6, %(hogql_val_1)s), toIntervalDay(1))), 0))
- SETTINGS optimize_aggregation_in_order=1) AS events__pdi__person ON equals(events__pdi.events__pdi___person_id, events__pdi__person.id)
+ SELECT tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS events__pdi___person_id, tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS person_id, person_distinct_id2.distinct_id AS distinct_id
+ FROM person_distinct_id2
+ WHERE equals(person_distinct_id2.team_id, 420)
+ GROUP BY person_distinct_id2.distinct_id
+ HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id2.is_deleted), person_distinct_id2.version), 1), 0), 0)
+ SETTINGS optimize_aggregation_in_order=1) AS events__pdi ON equals(events.distinct_id, events__pdi.distinct_id) LEFT JOIN (
+ SELECT person.id AS id
+ FROM person
+ WHERE equals(person.team_id, 420)
+ GROUP BY person.id
+ HAVING and(ifNull(equals(tupleElement(argMax(tuple(person.is_deleted), person.version), 1), 0), 0), ifNull(less(tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_0)s)), person.version), 1), plus(now64(6, %(hogql_val_1)s), toIntervalDay(1))), 0))
+ SETTINGS optimize_aggregation_in_order=1) AS events__pdi__person ON equals(events__pdi.events__pdi___person_id, events__pdi__person.id)
WHERE equals(events.team_id, 420)
LIMIT 50000
'''
@@ -288,18 +288,18 @@
SELECT events.event AS event, events__pdi__person.`properties___$browser` AS `$browser`
FROM events INNER JOIN (
- SELECT tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS events__pdi___person_id, tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS person_id, person_distinct_id2.distinct_id AS distinct_id
- FROM person_distinct_id2
- WHERE equals(person_distinct_id2.team_id, 420)
- GROUP BY person_distinct_id2.distinct_id
- HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id2.is_deleted), person_distinct_id2.version), 1), 0), 0)
- SETTINGS optimize_aggregation_in_order=1) AS events__pdi ON equals(events.distinct_id, events__pdi.distinct_id) LEFT JOIN (
- SELECT tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '')), person.version), 1) AS `properties___$browser`, person.id AS id
- FROM person
- WHERE equals(person.team_id, 420)
- GROUP BY person.id
- HAVING and(ifNull(equals(tupleElement(argMax(tuple(person.is_deleted), person.version), 1), 0), 0), ifNull(less(tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_1)s)), person.version), 1), plus(now64(6, %(hogql_val_2)s), toIntervalDay(1))), 0))
- SETTINGS optimize_aggregation_in_order=1) AS events__pdi__person ON equals(events__pdi.events__pdi___person_id, events__pdi__person.id)
+ SELECT tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS events__pdi___person_id, tupleElement(argMax(tuple(person_distinct_id2.person_id), person_distinct_id2.version), 1) AS person_id, person_distinct_id2.distinct_id AS distinct_id
+ FROM person_distinct_id2
+ WHERE equals(person_distinct_id2.team_id, 420)
+ GROUP BY person_distinct_id2.distinct_id
+ HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id2.is_deleted), person_distinct_id2.version), 1), 0), 0)
+ SETTINGS optimize_aggregation_in_order=1) AS events__pdi ON equals(events.distinct_id, events__pdi.distinct_id) LEFT JOIN (
+ SELECT tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '')), person.version), 1) AS `properties___$browser`, person.id AS id
+ FROM person
+ WHERE equals(person.team_id, 420)
+ GROUP BY person.id
+ HAVING and(ifNull(equals(tupleElement(argMax(tuple(person.is_deleted), person.version), 1), 0), 0), ifNull(less(tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_1)s)), person.version), 1), plus(now64(6, %(hogql_val_2)s), toIntervalDay(1))), 0))
+ SETTINGS optimize_aggregation_in_order=1) AS events__pdi__person ON equals(events__pdi.events__pdi___person_id, events__pdi__person.id)
WHERE equals(events.team_id, 420)
LIMIT 50000
'''
@@ -309,18 +309,18 @@
SELECT events.event AS event, events__person.properties AS properties, events__person.properties___name AS name
FROM events LEFT OUTER JOIN (
- SELECT tupleElement(argMax(tuple(person_distinct_id_overrides.person_id), person_distinct_id_overrides.version), 1) AS person_id, person_distinct_id_overrides.distinct_id AS distinct_id
- FROM person_distinct_id_overrides
- WHERE equals(person_distinct_id_overrides.team_id, 420)
- GROUP BY person_distinct_id_overrides.distinct_id
- HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id_overrides.is_deleted), person_distinct_id_overrides.version), 1), 0), 0)
- SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id) LEFT JOIN (
- SELECT tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '')), person.version), 1) AS properties___name, tupleElement(argMax(tuple(person.properties), person.version), 1) AS properties, person.id AS id
- FROM person
- WHERE equals(person.team_id, 420)
- GROUP BY person.id
- HAVING and(ifNull(equals(tupleElement(argMax(tuple(person.is_deleted), person.version), 1), 0), 0), ifNull(less(tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_1)s)), person.version), 1), plus(now64(6, %(hogql_val_2)s), toIntervalDay(1))), 0))
- SETTINGS optimize_aggregation_in_order=1) AS events__person ON equals(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), events__person.id)
+ SELECT tupleElement(argMax(tuple(person_distinct_id_overrides.person_id), person_distinct_id_overrides.version), 1) AS person_id, person_distinct_id_overrides.distinct_id AS distinct_id
+ FROM person_distinct_id_overrides
+ WHERE equals(person_distinct_id_overrides.team_id, 420)
+ GROUP BY person_distinct_id_overrides.distinct_id
+ HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id_overrides.is_deleted), person_distinct_id_overrides.version), 1), 0), 0)
+ SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id) LEFT JOIN (
+ SELECT tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '')), person.version), 1) AS properties___name, tupleElement(argMax(tuple(person.properties), person.version), 1) AS properties, person.id AS id
+ FROM person
+ WHERE equals(person.team_id, 420)
+ GROUP BY person.id
+ HAVING and(ifNull(equals(tupleElement(argMax(tuple(person.is_deleted), person.version), 1), 0), 0), ifNull(less(tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_1)s)), person.version), 1), plus(now64(6, %(hogql_val_2)s), toIntervalDay(1))), 0))
+ SETTINGS optimize_aggregation_in_order=1) AS events__person ON equals(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), events__person.id)
WHERE equals(events.team_id, 420)
LIMIT 50000
'''
@@ -330,18 +330,18 @@
SELECT events.event AS event, events__person.id AS id
FROM events LEFT OUTER JOIN (
- SELECT tupleElement(argMax(tuple(person_distinct_id_overrides.person_id), person_distinct_id_overrides.version), 1) AS person_id, person_distinct_id_overrides.distinct_id AS distinct_id
- FROM person_distinct_id_overrides
- WHERE equals(person_distinct_id_overrides.team_id, 420)
- GROUP BY person_distinct_id_overrides.distinct_id
- HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id_overrides.is_deleted), person_distinct_id_overrides.version), 1), 0), 0)
- SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id) LEFT JOIN (
- SELECT person.id AS id
- FROM person
- WHERE equals(person.team_id, 420)
- GROUP BY person.id
- HAVING and(ifNull(equals(tupleElement(argMax(tuple(person.is_deleted), person.version), 1), 0), 0), ifNull(less(tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_0)s)), person.version), 1), plus(now64(6, %(hogql_val_1)s), toIntervalDay(1))), 0))
- SETTINGS optimize_aggregation_in_order=1) AS events__person ON equals(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), events__person.id)
+ SELECT tupleElement(argMax(tuple(person_distinct_id_overrides.person_id), person_distinct_id_overrides.version), 1) AS person_id, person_distinct_id_overrides.distinct_id AS distinct_id
+ FROM person_distinct_id_overrides
+ WHERE equals(person_distinct_id_overrides.team_id, 420)
+ GROUP BY person_distinct_id_overrides.distinct_id
+ HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id_overrides.is_deleted), person_distinct_id_overrides.version), 1), 0), 0)
+ SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id) LEFT JOIN (
+ SELECT person.id AS id
+ FROM person
+ WHERE equals(person.team_id, 420)
+ GROUP BY person.id
+ HAVING and(ifNull(equals(tupleElement(argMax(tuple(person.is_deleted), person.version), 1), 0), 0), ifNull(less(tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_0)s)), person.version), 1), plus(now64(6, %(hogql_val_1)s), toIntervalDay(1))), 0))
+ SETTINGS optimize_aggregation_in_order=1) AS events__person ON equals(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), events__person.id)
WHERE equals(events.team_id, 420)
LIMIT 50000
'''
@@ -351,12 +351,12 @@
SELECT count() AS `count()`
FROM (
- SELECT person.id AS id
- FROM person
- WHERE equals(person.team_id, 420)
- GROUP BY person.id
- HAVING and(ifNull(equals(tupleElement(argMax(tuple(person.is_deleted), person.version), 1), 0), 0), ifNull(less(tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_0)s)), person.version), 1), plus(now64(6, %(hogql_val_1)s), toIntervalDay(1))), 0))
- SETTINGS optimize_aggregation_in_order=1) AS persons
+ SELECT person.id AS id
+ FROM person
+ WHERE equals(person.team_id, 420)
+ GROUP BY person.id
+ HAVING and(ifNull(equals(tupleElement(argMax(tuple(person.is_deleted), person.version), 1), 0), 0), ifNull(less(tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_0)s)), person.version), 1), plus(now64(6, %(hogql_val_1)s), toIntervalDay(1))), 0))
+ SETTINGS optimize_aggregation_in_order=1) AS persons
LIMIT 50000
'''
# ---
diff --git a/posthog/hogql/transforms/test/__snapshots__/test_property_types.ambr b/posthog/hogql/transforms/test/__snapshots__/test_property_types.ambr
index 46a1d88fb633..dfc12abe9938 100644
--- a/posthog/hogql/transforms/test/__snapshots__/test_property_types.ambr
+++ b/posthog/hogql/transforms/test/__snapshots__/test_property_types.ambr
@@ -4,14 +4,14 @@
SELECT persons__extended_properties.string_prop AS string_prop, persons__extended_properties.int_prop AS int_prop, toBool(transform(toString(persons__extended_properties.bool_prop), %(hogql_val_8)s, %(hogql_val_9)s, NULL)) AS bool_prop
FROM (
- SELECT tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '')), person.version), 1) AS persons___properties___email, person.id AS id
- FROM person
- WHERE equals(person.team_id, 420)
- GROUP BY person.id
- HAVING and(ifNull(equals(tupleElement(argMax(tuple(person.is_deleted), person.version), 1), 0), 0), ifNull(less(tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_1)s)), person.version), 1), plus(now64(6, %(hogql_val_2)s), toIntervalDay(1))), 0))
- SETTINGS optimize_aggregation_in_order=1) AS persons LEFT JOIN (
- SELECT extended_properties.string_prop AS string_prop, extended_properties.int_prop AS int_prop, extended_properties.bool_prop AS bool_prop, string_prop AS persons__extended_properties___string_prop
- FROM s3(%(hogql_val_3_sensitive)s, %(hogql_val_6_sensitive)s, %(hogql_val_7_sensitive)s, %(hogql_val_4)s, %(hogql_val_5)s) AS extended_properties) AS persons__extended_properties ON equals(persons.persons___properties___email, persons__extended_properties.persons__extended_properties___string_prop)
+ SELECT tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '')), person.version), 1) AS persons___properties___email, person.id AS id
+ FROM person
+ WHERE equals(person.team_id, 420)
+ GROUP BY person.id
+ HAVING and(ifNull(equals(tupleElement(argMax(tuple(person.is_deleted), person.version), 1), 0), 0), ifNull(less(tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_1)s)), person.version), 1), plus(now64(6, %(hogql_val_2)s), toIntervalDay(1))), 0))
+ SETTINGS optimize_aggregation_in_order=1) AS persons LEFT JOIN (
+ SELECT extended_properties.string_prop AS string_prop, extended_properties.int_prop AS int_prop, extended_properties.bool_prop AS bool_prop, string_prop AS persons__extended_properties___string_prop
+ FROM s3(%(hogql_val_3_sensitive)s, %(hogql_val_6_sensitive)s, %(hogql_val_7_sensitive)s, %(hogql_val_4)s, %(hogql_val_5)s) AS extended_properties) AS persons__extended_properties ON equals(persons.persons___properties___email, persons__extended_properties.persons__extended_properties___string_prop)
WHERE ifNull(equals(bool_prop, 1), 0)
LIMIT 50000
'''
@@ -21,10 +21,10 @@
SELECT ifNull(equals(toBool(transform(toString(events__group_0.properties___group_boolean), %(hogql_val_2)s, %(hogql_val_3)s, NULL)), 1), 0), ifNull(equals(toBool(transform(toString(events__group_0.properties___group_boolean), %(hogql_val_4)s, %(hogql_val_5)s, NULL)), 0), 0), isNull(toBool(transform(toString(events__group_0.properties___group_boolean), %(hogql_val_6)s, %(hogql_val_7)s, NULL)))
FROM events LEFT JOIN (
- SELECT tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(groups.group_properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '')), toTimeZone(groups._timestamp, %(hogql_val_1)s)), 1) AS properties___group_boolean, groups.group_type_index AS index, groups.group_key AS key
- FROM groups
- WHERE and(equals(groups.team_id, 420), equals(index, 0))
- GROUP BY groups.group_type_index, groups.group_key) AS events__group_0 ON equals(events.`$group_0`, events__group_0.key)
+ SELECT tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(groups.group_properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '')), toTimeZone(groups._timestamp, %(hogql_val_1)s)), 1) AS properties___group_boolean, groups.group_type_index AS index, groups.group_key AS key
+ FROM groups
+ WHERE and(equals(groups.team_id, 420), equals(index, 0))
+ GROUP BY groups.group_type_index, groups.group_key) AS events__group_0 ON equals(events.`$group_0`, events__group_0.key)
WHERE equals(events.team_id, 420)
LIMIT 50000
'''
@@ -34,10 +34,10 @@
SELECT accurateCastOrNull(events__group_0.properties___inty, %(hogql_val_2)s) AS inty
FROM events LEFT JOIN (
- SELECT tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(groups.group_properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '')), toTimeZone(groups._timestamp, %(hogql_val_1)s)), 1) AS properties___inty, groups.group_type_index AS index, groups.group_key AS key
- FROM groups
- WHERE and(equals(groups.team_id, 420), equals(index, 0))
- GROUP BY groups.group_type_index, groups.group_key) AS events__group_0 ON equals(events.`$group_0`, events__group_0.key)
+ SELECT tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(groups.group_properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '')), toTimeZone(groups._timestamp, %(hogql_val_1)s)), 1) AS properties___inty, groups.group_type_index AS index, groups.group_key AS key
+ FROM groups
+ WHERE and(equals(groups.team_id, 420), equals(index, 0))
+ GROUP BY groups.group_type_index, groups.group_key) AS events__group_0 ON equals(events.`$group_0`, events__group_0.key)
WHERE equals(events.team_id, 420)
LIMIT 50000
'''
@@ -47,18 +47,18 @@
SELECT multiply(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_4)s), ''), 'null'), '^"|"$', ''), %(hogql_val_5)s), events__person.properties___tickets)
FROM events LEFT OUTER JOIN (
- SELECT tupleElement(argMax(tuple(person_distinct_id_overrides.person_id), person_distinct_id_overrides.version), 1) AS person_id, person_distinct_id_overrides.distinct_id AS distinct_id
- FROM person_distinct_id_overrides
- WHERE equals(person_distinct_id_overrides.team_id, 420)
- GROUP BY person_distinct_id_overrides.distinct_id
- HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id_overrides.is_deleted), person_distinct_id_overrides.version), 1), 0), 0)
- SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id) LEFT JOIN (
- SELECT tupleElement(argMax(tuple(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', ''), %(hogql_val_1)s)), person.version), 1) AS properties___tickets, person.id AS id
- FROM person
- WHERE equals(person.team_id, 420)
- GROUP BY person.id
- HAVING and(ifNull(equals(tupleElement(argMax(tuple(person.is_deleted), person.version), 1), 0), 0), ifNull(less(tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_2)s)), person.version), 1), plus(now64(6, %(hogql_val_3)s), toIntervalDay(1))), 0))
- SETTINGS optimize_aggregation_in_order=1) AS events__person ON equals(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), events__person.id)
+ SELECT tupleElement(argMax(tuple(person_distinct_id_overrides.person_id), person_distinct_id_overrides.version), 1) AS person_id, person_distinct_id_overrides.distinct_id AS distinct_id
+ FROM person_distinct_id_overrides
+ WHERE equals(person_distinct_id_overrides.team_id, 420)
+ GROUP BY person_distinct_id_overrides.distinct_id
+ HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id_overrides.is_deleted), person_distinct_id_overrides.version), 1), 0), 0)
+ SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id) LEFT JOIN (
+ SELECT tupleElement(argMax(tuple(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', ''), %(hogql_val_1)s)), person.version), 1) AS properties___tickets, person.id AS id
+ FROM person
+ WHERE equals(person.team_id, 420)
+ GROUP BY person.id
+ HAVING and(ifNull(equals(tupleElement(argMax(tuple(person.is_deleted), person.version), 1), 0), 0), ifNull(less(tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_2)s)), person.version), 1), plus(now64(6, %(hogql_val_3)s), toIntervalDay(1))), 0))
+ SETTINGS optimize_aggregation_in_order=1) AS events__person ON equals(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), events__person.id)
WHERE equals(events.team_id, 420)
LIMIT 50000
'''
@@ -77,18 +77,18 @@
SELECT events__person.properties___provided_timestamp AS provided_timestamp
FROM events LEFT OUTER JOIN (
- SELECT tupleElement(argMax(tuple(person_distinct_id_overrides.person_id), person_distinct_id_overrides.version), 1) AS person_id, person_distinct_id_overrides.distinct_id AS distinct_id
- FROM person_distinct_id_overrides
- WHERE equals(person_distinct_id_overrides.team_id, 420)
- GROUP BY person_distinct_id_overrides.distinct_id
- HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id_overrides.is_deleted), person_distinct_id_overrides.version), 1), 0), 0)
- SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id) LEFT JOIN (
- SELECT tupleElement(argMax(tuple(parseDateTime64BestEffortOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', ''), 6, %(hogql_val_1)s)), person.version), 1) AS properties___provided_timestamp, person.id AS id
- FROM person
- WHERE equals(person.team_id, 420)
- GROUP BY person.id
- HAVING and(ifNull(equals(tupleElement(argMax(tuple(person.is_deleted), person.version), 1), 0), 0), ifNull(less(tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_2)s)), person.version), 1), plus(now64(6, %(hogql_val_3)s), toIntervalDay(1))), 0))
- SETTINGS optimize_aggregation_in_order=1) AS events__person ON equals(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), events__person.id)
+ SELECT tupleElement(argMax(tuple(person_distinct_id_overrides.person_id), person_distinct_id_overrides.version), 1) AS person_id, person_distinct_id_overrides.distinct_id AS distinct_id
+ FROM person_distinct_id_overrides
+ WHERE equals(person_distinct_id_overrides.team_id, 420)
+ GROUP BY person_distinct_id_overrides.distinct_id
+ HAVING ifNull(equals(tupleElement(argMax(tuple(person_distinct_id_overrides.is_deleted), person_distinct_id_overrides.version), 1), 0), 0)
+ SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id) LEFT JOIN (
+ SELECT tupleElement(argMax(tuple(parseDateTime64BestEffortOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', ''), 6, %(hogql_val_1)s)), person.version), 1) AS properties___provided_timestamp, person.id AS id
+ FROM person
+ WHERE equals(person.team_id, 420)
+ GROUP BY person.id
+ HAVING and(ifNull(equals(tupleElement(argMax(tuple(person.is_deleted), person.version), 1), 0), 0), ifNull(less(tupleElement(argMax(tuple(toTimeZone(person.created_at, %(hogql_val_2)s)), person.version), 1), plus(now64(6, %(hogql_val_3)s), toIntervalDay(1))), 0))
+ SETTINGS optimize_aggregation_in_order=1) AS events__person ON equals(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), events__person.id)
WHERE equals(events.team_id, 420)
LIMIT 50000
'''
diff --git a/posthog/hogql/transforms/test/__snapshots__/test_state_aggregations.ambr b/posthog/hogql/transforms/test/__snapshots__/test_state_aggregations.ambr
index bb489bd30bf4..47592059b475 100644
--- a/posthog/hogql/transforms/test/__snapshots__/test_state_aggregations.ambr
+++ b/posthog/hogql/transforms/test/__snapshots__/test_state_aggregations.ambr
@@ -4,21 +4,21 @@
SELECT tuple(uniqMerge(tupleElement(cohort_metrics, 1)), countMerge(tupleElement(cohort_metrics, 2)), sumMerge(tupleElement(cohort_metrics, 3))) AS cohort_metrics, %(hogql_val_7)s AS cohort_period
FROM (
- SELECT tuple(uniqState(events.distinct_id), countStateIf(equals(events.event, %(hogql_val_0)s)), sumStateIf(1, equals(events.event, %(hogql_val_1)s))) AS cohort_metrics, %(hogql_val_2)s AS cohort_period
- FROM events
- WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_3)s, 6, %(hogql_val_4)s)), less(events.timestamp, toDateTime64(%(hogql_val_5)s, 6, %(hogql_val_6)s))))
+ SELECT tuple(uniqState(events.distinct_id), countStateIf(equals(events.event, %(hogql_val_0)s)), sumStateIf(1, equals(events.event, %(hogql_val_1)s))) AS cohort_metrics, %(hogql_val_2)s AS cohort_period
+ FROM events
+ WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_3)s, 6, %(hogql_val_4)s)), less(events.timestamp, toDateTime64(%(hogql_val_5)s, 6, %(hogql_val_6)s))))
LIMIT 50000 UNION ALL
SELECT tuple(uniqMerge(tupleElement(cohort_metrics, 1)), countMerge(tupleElement(cohort_metrics, 2)), sumMerge(tupleElement(cohort_metrics, 3))) AS cohort_metrics, %(hogql_val_15)s AS cohort_period
FROM (
- SELECT tuple(uniqState(events.distinct_id), countStateIf(equals(events.event, %(hogql_val_8)s)), sumStateIf(1, equals(events.event, %(hogql_val_9)s))) AS cohort_metrics, %(hogql_val_10)s AS cohort_period
- FROM events
- WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_11)s, 6, %(hogql_val_12)s)), less(events.timestamp, toDateTime64(%(hogql_val_13)s, 6, %(hogql_val_14)s))))
+ SELECT tuple(uniqState(events.distinct_id), countStateIf(equals(events.event, %(hogql_val_8)s)), sumStateIf(1, equals(events.event, %(hogql_val_9)s))) AS cohort_metrics, %(hogql_val_10)s AS cohort_period
+ FROM events
+ WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_11)s, 6, %(hogql_val_12)s)), less(events.timestamp, toDateTime64(%(hogql_val_13)s, 6, %(hogql_val_14)s))))
LIMIT 50000 UNION ALL
SELECT tuple(uniqMerge(tupleElement(cohort_metrics, 1)), countMerge(tupleElement(cohort_metrics, 2)), sumMerge(tupleElement(cohort_metrics, 3))) AS cohort_metrics, %(hogql_val_23)s AS cohort_period
FROM (
- SELECT tuple(uniqState(events.distinct_id), countStateIf(equals(events.event, %(hogql_val_16)s)), sumStateIf(1, equals(events.event, %(hogql_val_17)s))) AS cohort_metrics, %(hogql_val_18)s AS cohort_period
- FROM events
- WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_19)s, 6, %(hogql_val_20)s)), less(events.timestamp, toDateTime64(%(hogql_val_21)s, 6, %(hogql_val_22)s))))
+ SELECT tuple(uniqState(events.distinct_id), countStateIf(equals(events.event, %(hogql_val_16)s)), sumStateIf(1, equals(events.event, %(hogql_val_17)s))) AS cohort_metrics, %(hogql_val_18)s AS cohort_period
+ FROM events
+ WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_19)s, 6, %(hogql_val_20)s)), less(events.timestamp, toDateTime64(%(hogql_val_21)s, 6, %(hogql_val_22)s))))
LIMIT 50000
'''
# ---
@@ -27,18 +27,18 @@
SELECT tuple(uniqMerge(tupleElement(comprehensive_metrics, 1)), countMerge(tupleElement(comprehensive_metrics, 2)), sumMerge(tupleElement(comprehensive_metrics, 3)), avgMerge(tupleElement(comprehensive_metrics, 4))) AS comprehensive_metrics, source AS source
FROM (
- SELECT tuple(uniqState(events.distinct_id), countStateIf(equals(events.event, %(hogql_val_0)s)), sumStateIf(1, equals(events.event, %(hogql_val_1)s)), avgStateIf(1, equals(events.event, %(hogql_val_2)s))) AS comprehensive_metrics, replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_3)s), ''), 'null'), '^"|"$', '') AS source
- FROM events
- WHERE and(equals(events.team_id, 420), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_4)s), ''), 'null'), '^"|"$', ''), %(hogql_val_5)s), 0))
- GROUP BY source)
+ SELECT tuple(uniqState(events.distinct_id), countStateIf(equals(events.event, %(hogql_val_0)s)), sumStateIf(1, equals(events.event, %(hogql_val_1)s)), avgStateIf(1, equals(events.event, %(hogql_val_2)s))) AS comprehensive_metrics, replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_3)s), ''), 'null'), '^"|"$', '') AS source
+ FROM events
+ WHERE and(equals(events.team_id, 420), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_4)s), ''), 'null'), '^"|"$', ''), %(hogql_val_5)s), 0))
+ GROUP BY source)
GROUP BY source
LIMIT 50000 UNION ALL
SELECT tuple(uniqMerge(tupleElement(comprehensive_metrics, 1)), countMerge(tupleElement(comprehensive_metrics, 2)), sumMerge(tupleElement(comprehensive_metrics, 3)), avgMerge(tupleElement(comprehensive_metrics, 4))) AS comprehensive_metrics, source AS source
FROM (
- SELECT tuple(uniqState(events.distinct_id), countStateIf(equals(events.event, %(hogql_val_6)s)), sumStateIf(1, equals(events.event, %(hogql_val_7)s)), avgStateIf(1, equals(events.event, %(hogql_val_8)s))) AS comprehensive_metrics, replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_9)s), ''), 'null'), '^"|"$', '') AS source
- FROM events
- WHERE and(equals(events.team_id, 420), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_10)s), ''), 'null'), '^"|"$', ''), %(hogql_val_11)s), 0))
- GROUP BY source)
+ SELECT tuple(uniqState(events.distinct_id), countStateIf(equals(events.event, %(hogql_val_6)s)), sumStateIf(1, equals(events.event, %(hogql_val_7)s)), avgStateIf(1, equals(events.event, %(hogql_val_8)s))) AS comprehensive_metrics, replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_9)s), ''), 'null'), '^"|"$', '') AS source
+ FROM events
+ WHERE and(equals(events.team_id, 420), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_10)s), ''), 'null'), '^"|"$', ''), %(hogql_val_11)s), 0))
+ GROUP BY source)
GROUP BY source
LIMIT 50000
'''
@@ -48,15 +48,15 @@
SELECT tuple(uniqMerge(tupleElement(funnel_metrics, 1)), countMerge(tupleElement(funnel_metrics, 2)), countMerge(tupleElement(funnel_metrics, 3))) AS funnel_metrics
FROM (
- SELECT tuple(uniqState(events.distinct_id), countStateIf(equals(events.event, %(hogql_val_0)s)), countStateIf(equals(events.event, %(hogql_val_1)s))) AS funnel_metrics
- FROM events
- WHERE and(equals(events.team_id, 420), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_2)s), ''), 'null'), '^"|"$', ''), %(hogql_val_3)s), 0)))
+ SELECT tuple(uniqState(events.distinct_id), countStateIf(equals(events.event, %(hogql_val_0)s)), countStateIf(equals(events.event, %(hogql_val_1)s))) AS funnel_metrics
+ FROM events
+ WHERE and(equals(events.team_id, 420), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_2)s), ''), 'null'), '^"|"$', ''), %(hogql_val_3)s), 0)))
LIMIT 50000 UNION ALL
SELECT tuple(uniqMerge(tupleElement(funnel_metrics, 1)), countMerge(tupleElement(funnel_metrics, 2)), countMerge(tupleElement(funnel_metrics, 3))) AS funnel_metrics
FROM (
- SELECT tuple(uniqState(events.distinct_id), countStateIf(equals(events.event, %(hogql_val_4)s)), countStateIf(equals(events.event, %(hogql_val_5)s))) AS funnel_metrics
- FROM events
- WHERE and(equals(events.team_id, 420), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_6)s), ''), 'null'), '^"|"$', ''), %(hogql_val_7)s), 0)))
+ SELECT tuple(uniqState(events.distinct_id), countStateIf(equals(events.event, %(hogql_val_4)s)), countStateIf(equals(events.event, %(hogql_val_5)s))) AS funnel_metrics
+ FROM events
+ WHERE and(equals(events.team_id, 420), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_6)s), ''), 'null'), '^"|"$', ''), %(hogql_val_7)s), 0)))
LIMIT 50000
'''
# ---
@@ -65,18 +65,18 @@
SELECT tuple(uniqMerge(tupleElement(user_metrics, 1)), countMerge(tupleElement(user_metrics, 2))) AS user_metrics, tuple(sumMerge(tupleElement(activity_metrics, 1)), avgMerge(tupleElement(activity_metrics, 2))) AS activity_metrics, date AS date
FROM (
- SELECT tuple(uniqState(events.distinct_id), countState()) AS user_metrics, tuple(sumState(1), avgState(1)) AS activity_metrics, toDate(toTimeZone(events.timestamp, %(hogql_val_0)s)) AS date
- FROM events
- WHERE and(equals(events.team_id, 420), less(toDate(toTimeZone(events.timestamp, %(hogql_val_1)s)), %(hogql_val_2)s))
- GROUP BY date)
+ SELECT tuple(uniqState(events.distinct_id), countState()) AS user_metrics, tuple(sumState(1), avgState(1)) AS activity_metrics, toDate(toTimeZone(events.timestamp, %(hogql_val_0)s)) AS date
+ FROM events
+ WHERE and(equals(events.team_id, 420), less(toDate(toTimeZone(events.timestamp, %(hogql_val_1)s)), %(hogql_val_2)s))
+ GROUP BY date)
GROUP BY date
LIMIT 50000 UNION ALL
SELECT tuple(uniqMerge(tupleElement(user_metrics, 1)), countMerge(tupleElement(user_metrics, 2))) AS user_metrics, tuple(sumMerge(tupleElement(activity_metrics, 1)), avgMerge(tupleElement(activity_metrics, 2))) AS activity_metrics, date AS date
FROM (
- SELECT tuple(uniqState(events.distinct_id), countState()) AS user_metrics, tuple(sumState(1), avgState(1)) AS activity_metrics, toDate(toTimeZone(events.timestamp, %(hogql_val_3)s)) AS date
- FROM events
- WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_4)s, 6, %(hogql_val_5)s)))
- GROUP BY date)
+ SELECT tuple(uniqState(events.distinct_id), countState()) AS user_metrics, tuple(sumState(1), avgState(1)) AS activity_metrics, toDate(toTimeZone(events.timestamp, %(hogql_val_3)s)) AS date
+ FROM events
+ WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_4)s, 6, %(hogql_val_5)s)))
+ GROUP BY date)
GROUP BY date
LIMIT 50000
'''
@@ -86,15 +86,15 @@
SELECT tuple(uniqMerge(tupleElement(metrics, 1)), countMerge(tupleElement(metrics, 2)), sumMerge(tupleElement(metrics, 3))) AS metrics
FROM (
- SELECT tuple(uniqStateIf(events.distinct_id, equals(events.event, %(hogql_val_0)s)), countStateIf(equals(events.event, %(hogql_val_1)s)), sumStateIf(1, 1)) AS metrics
- FROM events
- WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_2)s, 6, %(hogql_val_3)s)), less(events.timestamp, toDateTime64(%(hogql_val_4)s, 6, %(hogql_val_5)s))))
+ SELECT tuple(uniqStateIf(events.distinct_id, equals(events.event, %(hogql_val_0)s)), countStateIf(equals(events.event, %(hogql_val_1)s)), sumStateIf(1, 1)) AS metrics
+ FROM events
+ WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_2)s, 6, %(hogql_val_3)s)), less(events.timestamp, toDateTime64(%(hogql_val_4)s, 6, %(hogql_val_5)s))))
LIMIT 50000 UNION ALL
SELECT tuple(uniqMerge(tupleElement(metrics, 1)), countMerge(tupleElement(metrics, 2)), sumMerge(tupleElement(metrics, 3))) AS metrics
FROM (
- SELECT tuple(uniqStateIf(events.distinct_id, equals(events.event, %(hogql_val_6)s)), countStateIf(equals(events.event, %(hogql_val_7)s)), sumStateIf(1, 1)) AS metrics
- FROM events
- WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_8)s, 6, %(hogql_val_9)s))))
+ SELECT tuple(uniqStateIf(events.distinct_id, equals(events.event, %(hogql_val_6)s)), countStateIf(equals(events.event, %(hogql_val_7)s)), sumStateIf(1, 1)) AS metrics
+ FROM events
+ WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_8)s, 6, %(hogql_val_9)s))))
LIMIT 50000
'''
# ---
@@ -103,15 +103,15 @@
SELECT tuple(uniqMerge(tupleElement(metrics, 1)), countMerge(tupleElement(metrics, 2)), sumMerge(tupleElement(metrics, 3))) AS metrics, %(hogql_val_4)s AS source_type
FROM (
- SELECT tuple(uniqState(events.distinct_id), countState(), sumStateIf(1, equals(events.event, %(hogql_val_0)s))) AS metrics, %(hogql_val_1)s AS source_type
- FROM events
- WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_2)s, 6, %(hogql_val_3)s))))
+ SELECT tuple(uniqState(events.distinct_id), countState(), sumStateIf(1, equals(events.event, %(hogql_val_0)s))) AS metrics, %(hogql_val_1)s AS source_type
+ FROM events
+ WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_2)s, 6, %(hogql_val_3)s))))
LIMIT 50000 UNION ALL
SELECT tuple(uniqMerge(tupleElement(metrics, 1)), countMerge(tupleElement(metrics, 2)), sumMerge(tupleElement(metrics, 3))) AS metrics, %(hogql_val_9)s AS source_type
FROM (
- SELECT tuple(uniqState(events.distinct_id), countState(), sumStateIf(1, equals(events.event, %(hogql_val_5)s))) AS metrics, %(hogql_val_6)s AS source_type
- FROM events
- WHERE and(equals(events.team_id, 420), less(events.timestamp, toDateTime64(%(hogql_val_7)s, 6, %(hogql_val_8)s))))
+ SELECT tuple(uniqState(events.distinct_id), countState(), sumStateIf(1, equals(events.event, %(hogql_val_5)s))) AS metrics, %(hogql_val_6)s AS source_type
+ FROM events
+ WHERE and(equals(events.team_id, 420), less(events.timestamp, toDateTime64(%(hogql_val_7)s, 6, %(hogql_val_8)s))))
LIMIT 50000
'''
# ---
@@ -120,26 +120,26 @@
SELECT tuple(uniqMerge(tupleElement(user_metrics, 1)), countMerge(tupleElement(user_metrics, 2))) AS user_metrics, tuple(sumMerge(tupleElement(value_metrics, 1)), avgMerge(tupleElement(value_metrics, 2))) AS value_metrics, host AS host
FROM (
- SELECT tuple(uniqState(events.distinct_id), countState()) AS user_metrics, tuple(sumState(1), avgState(1)) AS value_metrics, replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '') AS host
- FROM events
- WHERE and(equals(events.team_id, 420), less(events.timestamp, toDateTime64(%(hogql_val_1)s, 6, %(hogql_val_2)s)))
- GROUP BY host)
+ SELECT tuple(uniqState(events.distinct_id), countState()) AS user_metrics, tuple(sumState(1), avgState(1)) AS value_metrics, replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '') AS host
+ FROM events
+ WHERE and(equals(events.team_id, 420), less(events.timestamp, toDateTime64(%(hogql_val_1)s, 6, %(hogql_val_2)s)))
+ GROUP BY host)
GROUP BY host
LIMIT 50000 UNION ALL
SELECT tuple(uniqMerge(tupleElement(user_metrics, 1)), countMerge(tupleElement(user_metrics, 2))) AS user_metrics, tuple(sumMerge(tupleElement(value_metrics, 1)), avgMerge(tupleElement(value_metrics, 2))) AS value_metrics, host AS host
FROM (
- SELECT tuple(uniqState(events.distinct_id), countState()) AS user_metrics, tuple(sumState(1), avgState(1)) AS value_metrics, replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_3)s), ''), 'null'), '^"|"$', '') AS host
- FROM events
- WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_4)s, 6, %(hogql_val_5)s)), less(events.timestamp, toDateTime64(%(hogql_val_6)s, 6, %(hogql_val_7)s)))
- GROUP BY host)
+ SELECT tuple(uniqState(events.distinct_id), countState()) AS user_metrics, tuple(sumState(1), avgState(1)) AS value_metrics, replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_3)s), ''), 'null'), '^"|"$', '') AS host
+ FROM events
+ WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_4)s, 6, %(hogql_val_5)s)), less(events.timestamp, toDateTime64(%(hogql_val_6)s, 6, %(hogql_val_7)s)))
+ GROUP BY host)
GROUP BY host
LIMIT 50000 UNION ALL
SELECT tuple(uniqMerge(tupleElement(user_metrics, 1)), countMerge(tupleElement(user_metrics, 2))) AS user_metrics, tuple(sumMerge(tupleElement(value_metrics, 1)), avgMerge(tupleElement(value_metrics, 2))) AS value_metrics, host AS host
FROM (
- SELECT tuple(uniqState(events.distinct_id), countState()) AS user_metrics, tuple(sumState(1), avgState(1)) AS value_metrics, replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_8)s), ''), 'null'), '^"|"$', '') AS host
- FROM events
- WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_9)s, 6, %(hogql_val_10)s)))
- GROUP BY host)
+ SELECT tuple(uniqState(events.distinct_id), countState()) AS user_metrics, tuple(sumState(1), avgState(1)) AS value_metrics, replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_8)s), ''), 'null'), '^"|"$', '') AS host
+ FROM events
+ WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_9)s, 6, %(hogql_val_10)s)))
+ GROUP BY host)
GROUP BY host
LIMIT 50000
'''
@@ -149,15 +149,15 @@
SELECT countMerge(total_pageviews) AS total_pageviews
FROM (
- SELECT countState() AS total_pageviews
- FROM events
- WHERE and(equals(events.team_id, 420), less(events.timestamp, minus(now64(6, %(hogql_val_0)s), toIntervalDay(1)))))
+ SELECT countState() AS total_pageviews
+ FROM events
+ WHERE and(equals(events.team_id, 420), less(events.timestamp, minus(now64(6, %(hogql_val_0)s), toIntervalDay(1)))))
LIMIT 50000 UNION ALL
SELECT countMerge(total_pageviews) AS total_pageviews
FROM (
- SELECT countState() AS total_pageviews
- FROM events
- WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, minus(now64(6, %(hogql_val_1)s), toIntervalDay(1)))))
+ SELECT countState() AS total_pageviews
+ FROM events
+ WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, minus(now64(6, %(hogql_val_1)s), toIntervalDay(1)))))
LIMIT 50000
'''
# ---
@@ -166,18 +166,18 @@
SELECT tuple(uniqMerge(tupleElement(daily_metrics, 1)), countMerge(tupleElement(daily_metrics, 2)), sumMerge(tupleElement(daily_metrics, 3))) AS daily_metrics, date AS date
FROM (
- SELECT tuple(uniqState(events.distinct_id), countState(), sumStateIf(1, equals(events.event, %(hogql_val_0)s))) AS daily_metrics, toDate(toTimeZone(events.timestamp, %(hogql_val_1)s)) AS date
- FROM events
- WHERE and(equals(events.team_id, 420), less(toDate(toTimeZone(events.timestamp, %(hogql_val_2)s)), %(hogql_val_3)s))
- GROUP BY date)
+ SELECT tuple(uniqState(events.distinct_id), countState(), sumStateIf(1, equals(events.event, %(hogql_val_0)s))) AS daily_metrics, toDate(toTimeZone(events.timestamp, %(hogql_val_1)s)) AS date
+ FROM events
+ WHERE and(equals(events.team_id, 420), less(toDate(toTimeZone(events.timestamp, %(hogql_val_2)s)), %(hogql_val_3)s))
+ GROUP BY date)
GROUP BY date
LIMIT 50000 UNION ALL
SELECT tuple(uniqMerge(tupleElement(daily_metrics, 1)), countMerge(tupleElement(daily_metrics, 2)), sumMerge(tupleElement(daily_metrics, 3))) AS daily_metrics, date AS date
FROM (
- SELECT tuple(uniqState(events.distinct_id), countState(), sumStateIf(1, equals(events.event, %(hogql_val_4)s))) AS daily_metrics, toDate(toTimeZone(events.timestamp, %(hogql_val_5)s)) AS date
- FROM events
- WHERE and(equals(events.team_id, 420), equals(toDate(toTimeZone(events.timestamp, %(hogql_val_6)s)), %(hogql_val_7)s))
- GROUP BY date)
+ SELECT tuple(uniqState(events.distinct_id), countState(), sumStateIf(1, equals(events.event, %(hogql_val_4)s))) AS daily_metrics, toDate(toTimeZone(events.timestamp, %(hogql_val_5)s)) AS date
+ FROM events
+ WHERE and(equals(events.team_id, 420), equals(toDate(toTimeZone(events.timestamp, %(hogql_val_6)s)), %(hogql_val_7)s))
+ GROUP BY date)
GROUP BY date
LIMIT 50000
'''
@@ -187,18 +187,18 @@
SELECT tuple(uniqMerge(tupleElement(stats_tuple, 1)), countMerge(tupleElement(stats_tuple, 2)), sumMerge(tupleElement(stats_tuple, 3))) AS stats_tuple, date_key AS date_key
FROM (
- SELECT tuple(uniqState(events.distinct_id), countStateIf(1), sumState(1)) AS stats_tuple, toDate(toTimeZone(events.timestamp, %(hogql_val_0)s)) AS date_key
- FROM events
- WHERE and(equals(events.team_id, 420), less(toDate(toTimeZone(events.timestamp, %(hogql_val_1)s)), %(hogql_val_2)s))
- GROUP BY date_key)
+ SELECT tuple(uniqState(events.distinct_id), countStateIf(1), sumState(1)) AS stats_tuple, toDate(toTimeZone(events.timestamp, %(hogql_val_0)s)) AS date_key
+ FROM events
+ WHERE and(equals(events.team_id, 420), less(toDate(toTimeZone(events.timestamp, %(hogql_val_1)s)), %(hogql_val_2)s))
+ GROUP BY date_key)
GROUP BY date_key
LIMIT 50000 UNION ALL
SELECT tuple(uniqMerge(tupleElement(stats_tuple, 1)), countMerge(tupleElement(stats_tuple, 2)), sumMerge(tupleElement(stats_tuple, 3))) AS stats_tuple, date_key AS date_key
FROM (
- SELECT tuple(uniqState(events.distinct_id), countStateIf(1), sumState(1)) AS stats_tuple, toDate(toTimeZone(events.timestamp, %(hogql_val_3)s)) AS date_key
- FROM events
- WHERE and(equals(events.team_id, 420), greaterOrEquals(toDate(toTimeZone(events.timestamp, %(hogql_val_4)s)), %(hogql_val_5)s))
- GROUP BY date_key)
+ SELECT tuple(uniqState(events.distinct_id), countStateIf(1), sumState(1)) AS stats_tuple, toDate(toTimeZone(events.timestamp, %(hogql_val_3)s)) AS date_key
+ FROM events
+ WHERE and(equals(events.team_id, 420), greaterOrEquals(toDate(toTimeZone(events.timestamp, %(hogql_val_4)s)), %(hogql_val_5)s))
+ GROUP BY date_key)
GROUP BY date_key
LIMIT 50000
'''
@@ -208,9 +208,9 @@
SELECT uniqMerge(unique_users) AS unique_users, countMerge(total_events) AS total_events
FROM (
- SELECT uniqState(events.distinct_id) AS unique_users, countState() AS total_events
- FROM events
- WHERE and(equals(events.team_id, 420), equals(events.event, %(hogql_val_0)s)))
+ SELECT uniqState(events.distinct_id) AS unique_users, countState() AS total_events
+ FROM events
+ WHERE and(equals(events.team_id, 420), equals(events.event, %(hogql_val_0)s)))
LIMIT 50000
'''
# ---
@@ -219,12 +219,12 @@
SELECT uniqMerge(unique_users) AS unique_users, countMerge(total_events) AS total_events, host AS host
FROM (
- SELECT uniqState(events.distinct_id) AS unique_users, countState() AS total_events, replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '') AS host
- FROM events
- WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_1)s, 6, %(hogql_val_2)s)))
- GROUP BY host
- ORDER BY total_events DESC
- LIMIT 10)
+ SELECT uniqState(events.distinct_id) AS unique_users, countState() AS total_events, replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '') AS host
+ FROM events
+ WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_1)s, 6, %(hogql_val_2)s)))
+ GROUP BY host
+ ORDER BY total_events DESC
+ LIMIT 10)
GROUP BY host
ORDER BY total_events DESC
LIMIT 10
@@ -235,15 +235,15 @@
SELECT tuple(uniqMerge(tupleElement(user_metrics, 1)), countMerge(tupleElement(user_metrics, 2))) AS user_metrics, tuple(sumMerge(tupleElement(session_metrics, 1)), avgMerge(tupleElement(session_metrics, 2))) AS session_metrics
FROM (
- SELECT tuple(uniqState(events.distinct_id), countState()) AS user_metrics, tuple(sumState(1), avgState(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', ''), %(hogql_val_1)s))) AS session_metrics
- FROM events
- WHERE and(equals(events.team_id, 420), less(events.timestamp, toDateTime64(%(hogql_val_2)s, 6, %(hogql_val_3)s))))
+ SELECT tuple(uniqState(events.distinct_id), countState()) AS user_metrics, tuple(sumState(1), avgState(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', ''), %(hogql_val_1)s))) AS session_metrics
+ FROM events
+ WHERE and(equals(events.team_id, 420), less(events.timestamp, toDateTime64(%(hogql_val_2)s, 6, %(hogql_val_3)s))))
LIMIT 50000 UNION ALL
SELECT tuple(uniqMerge(tupleElement(user_metrics, 1)), countMerge(tupleElement(user_metrics, 2))) AS user_metrics, tuple(sumMerge(tupleElement(session_metrics, 1)), avgMerge(tupleElement(session_metrics, 2))) AS session_metrics
FROM (
- SELECT tuple(uniqState(events.distinct_id), countState()) AS user_metrics, tuple(sumState(1), avgState(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_4)s), ''), 'null'), '^"|"$', ''), %(hogql_val_5)s))) AS session_metrics
- FROM events
- WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_6)s, 6, %(hogql_val_7)s))))
+ SELECT tuple(uniqState(events.distinct_id), countState()) AS user_metrics, tuple(sumState(1), avgState(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_4)s), ''), 'null'), '^"|"$', ''), %(hogql_val_5)s))) AS session_metrics
+ FROM events
+ WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_6)s, 6, %(hogql_val_7)s))))
LIMIT 50000
'''
# ---
@@ -252,9 +252,9 @@
SELECT tuple(uniqMerge(tupleElement(user_stats, 1)), countMerge(tupleElement(user_stats, 2))) AS user_stats, tuple(sumMerge(tupleElement(metric_stats, 1)), avgMerge(tupleElement(metric_stats, 2))) AS metric_stats
FROM (
- SELECT tuple(uniqState(events.distinct_id), countState()) AS user_stats, tuple(sumState(1), avgState(1)) AS metric_stats
- FROM events
- WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_0)s, 6, %(hogql_val_1)s))))
+ SELECT tuple(uniqState(events.distinct_id), countState()) AS user_stats, tuple(sumState(1), avgState(1)) AS metric_stats
+ FROM events
+ WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_0)s, 6, %(hogql_val_1)s))))
LIMIT 50000
'''
# ---
@@ -263,12 +263,12 @@
SELECT sumMerge(total_filtered_count) AS total_filtered_count
FROM (
- SELECT sumState(filtered_count) AS total_filtered_count
- FROM (
- SELECT countIf(equals(events.event, %(hogql_val_0)s)) AS filtered_count
- FROM events
- WHERE equals(events.team_id, 420)
- GROUP BY events.distinct_id))
+ SELECT sumState(filtered_count) AS total_filtered_count
+ FROM (
+ SELECT countIf(equals(events.event, %(hogql_val_0)s)) AS filtered_count
+ FROM events
+ WHERE equals(events.team_id, 420)
+ GROUP BY events.distinct_id))
LIMIT 50000
'''
# ---
@@ -277,10 +277,10 @@
SELECT host AS host, uniqMerge(unique_users) AS unique_users, sumMerge(click_count) AS click_count, avgMerge(avg_duration) AS avg_duration
FROM (
- SELECT replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '') AS host, uniqState(events.distinct_id) AS unique_users, sumStateIf(1, equals(events.event, %(hogql_val_1)s)) AS click_count, avgState(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_2)s), ''), 'null'), '^"|"$', ''), %(hogql_val_3)s)) AS avg_duration
- FROM events
- WHERE equals(events.team_id, 420)
- GROUP BY host)
+ SELECT replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '') AS host, uniqState(events.distinct_id) AS unique_users, sumStateIf(1, equals(events.event, %(hogql_val_1)s)) AS click_count, avgState(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_2)s), ''), 'null'), '^"|"$', ''), %(hogql_val_3)s)) AS avg_duration
+ FROM events
+ WHERE equals(events.team_id, 420)
+ GROUP BY host)
GROUP BY host
LIMIT 50000
'''
@@ -290,14 +290,14 @@
SELECT sumMerge(final_users) AS final_users, avgMerge(final_avg_duration) AS final_avg_duration
FROM (
- SELECT sumState(combined_stats.total_users) AS final_users, avgState(combined_stats.avg_duration) AS final_avg_duration
- FROM (
- SELECT tuple(uniqState(events.distinct_id), avgState(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', ''), %(hogql_val_1)s))) AS user_duration_tuple, tupleElement(user_duration_tuple, 1) AS total_users, tupleElement(user_duration_tuple, 2) AS avg_duration
- FROM events
- WHERE and(equals(events.team_id, 420), equals(events.event, %(hogql_val_2)s), less(events.timestamp, toDateTime64(%(hogql_val_3)s, 6, %(hogql_val_4)s))) UNION ALL
- SELECT tuple(uniqState(events.distinct_id), avgState(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_5)s), ''), 'null'), '^"|"$', ''), %(hogql_val_6)s))) AS user_duration_tuple, tupleElement(user_duration_tuple, 1) AS total_users, tupleElement(user_duration_tuple, 2) AS avg_duration
- FROM events
- WHERE and(equals(events.team_id, 420), equals(events.event, %(hogql_val_7)s), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_8)s, 6, %(hogql_val_9)s)))) AS combined_stats)
+ SELECT sumState(combined_stats.total_users) AS final_users, avgState(combined_stats.avg_duration) AS final_avg_duration
+ FROM (
+ SELECT tuple(uniqState(events.distinct_id), avgState(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', ''), %(hogql_val_1)s))) AS user_duration_tuple, tupleElement(user_duration_tuple, 1) AS total_users, tupleElement(user_duration_tuple, 2) AS avg_duration
+ FROM events
+ WHERE and(equals(events.team_id, 420), equals(events.event, %(hogql_val_2)s), less(events.timestamp, toDateTime64(%(hogql_val_3)s, 6, %(hogql_val_4)s))) UNION ALL
+ SELECT tuple(uniqState(events.distinct_id), avgState(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_5)s), ''), 'null'), '^"|"$', ''), %(hogql_val_6)s))) AS user_duration_tuple, tupleElement(user_duration_tuple, 1) AS total_users, tupleElement(user_duration_tuple, 2) AS avg_duration
+ FROM events
+ WHERE and(equals(events.team_id, 420), equals(events.event, %(hogql_val_7)s), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_8)s, 6, %(hogql_val_9)s)))) AS combined_stats)
LIMIT 50000
'''
# ---
@@ -325,12 +325,12 @@
SELECT sumMerge(total_pageviews) AS total_pageviews, uniqMerge(unique_users) AS unique_users
FROM (
- SELECT sumState(pageview_count) AS total_pageviews, uniqState(user_id) AS unique_users
- FROM (
- SELECT events.distinct_id AS user_id, countIf(equals(events.event, %(hogql_val_0)s)) AS pageview_count
- FROM events
- WHERE equals(events.team_id, 420)
- GROUP BY events.distinct_id))
+ SELECT sumState(pageview_count) AS total_pageviews, uniqState(user_id) AS unique_users
+ FROM (
+ SELECT events.distinct_id AS user_id, countIf(equals(events.event, %(hogql_val_0)s)) AS pageview_count
+ FROM events
+ WHERE equals(events.team_id, 420)
+ GROUP BY events.distinct_id))
LIMIT 50000
'''
# ---
@@ -339,9 +339,9 @@
SELECT uniqMerge(unique_users) AS unique_users, NULL AS previous_unique_users, countMerge(total_events) AS total_events, 123 AS constant_value
FROM (
- SELECT uniqState(events.distinct_id) AS unique_users, NULL AS previous_unique_users, countState() AS total_events, 123 AS constant_value
- FROM events
- WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_0)s, 6, %(hogql_val_1)s))))
+ SELECT uniqState(events.distinct_id) AS unique_users, NULL AS previous_unique_users, countState() AS total_events, 123 AS constant_value
+ FROM events
+ WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_0)s, 6, %(hogql_val_1)s))))
LIMIT 50000
'''
# ---
@@ -350,21 +350,21 @@
SELECT tuple(uniqMerge(tupleElement(pageview_metrics, 1)), countMerge(tupleElement(pageview_metrics, 2))) AS pageview_metrics
FROM (
- SELECT tuple(uniqState(events.distinct_id), countStateIf(equals(events.event, %(hogql_val_0)s))) AS pageview_metrics
- FROM events
- WHERE and(equals(events.team_id, 420), less(events.timestamp, toDateTime64(%(hogql_val_1)s, 6, %(hogql_val_2)s))))
+ SELECT tuple(uniqState(events.distinct_id), countStateIf(equals(events.event, %(hogql_val_0)s))) AS pageview_metrics
+ FROM events
+ WHERE and(equals(events.team_id, 420), less(events.timestamp, toDateTime64(%(hogql_val_1)s, 6, %(hogql_val_2)s))))
LIMIT 50000 UNION ALL
SELECT tuple(uniqMerge(tupleElement(pageview_metrics, 1)), countMerge(tupleElement(pageview_metrics, 2))) AS pageview_metrics
FROM (
- SELECT tuple(uniqState(events.distinct_id), countStateIf(equals(events.event, %(hogql_val_3)s))) AS pageview_metrics
- FROM events
- WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_4)s, 6, %(hogql_val_5)s)), less(events.timestamp, toDateTime64(%(hogql_val_6)s, 6, %(hogql_val_7)s))))
+ SELECT tuple(uniqState(events.distinct_id), countStateIf(equals(events.event, %(hogql_val_3)s))) AS pageview_metrics
+ FROM events
+ WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_4)s, 6, %(hogql_val_5)s)), less(events.timestamp, toDateTime64(%(hogql_val_6)s, 6, %(hogql_val_7)s))))
LIMIT 50000 UNION ALL
SELECT tuple(uniqMerge(tupleElement(pageview_metrics, 1)), countMerge(tupleElement(pageview_metrics, 2))) AS pageview_metrics
FROM (
- SELECT tuple(uniqState(events.distinct_id), countStateIf(equals(events.event, %(hogql_val_8)s))) AS pageview_metrics
- FROM events
- WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_9)s, 6, %(hogql_val_10)s))))
+ SELECT tuple(uniqState(events.distinct_id), countStateIf(equals(events.event, %(hogql_val_8)s))) AS pageview_metrics
+ FROM events
+ WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_9)s, 6, %(hogql_val_10)s))))
LIMIT 50000
'''
# ---
@@ -391,9 +391,9 @@
SELECT tuple(uniqMerge(tupleElement(user_stats, 1)), countMerge(tupleElement(user_stats, 2))) AS user_stats, tuple(sumMerge(tupleElement(metric_stats, 1)), avgMerge(tupleElement(metric_stats, 2))) AS metric_stats
FROM (
- SELECT tuple(uniqState(events.distinct_id), countState()) AS user_stats, tuple(sumState(1), avgState(1)) AS metric_stats
- FROM events
- WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_0)s, 6, %(hogql_val_1)s))))
+ SELECT tuple(uniqState(events.distinct_id), countState()) AS user_stats, tuple(sumState(1), avgState(1)) AS metric_stats
+ FROM events
+ WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_0)s, 6, %(hogql_val_1)s))))
LIMIT 50000
'''
# ---
@@ -402,11 +402,11 @@
SELECT sumMerge(aggregated_total) AS aggregated_total
FROM (
- SELECT sumState(filtered_metrics.total_count) AS aggregated_total
- FROM (
- SELECT tuple(uniq(events.distinct_id), count()) AS user_metrics, sum(1) AS total_count
- FROM events
- WHERE equals(events.team_id, 420)) AS filtered_metrics)
+ SELECT sumState(filtered_metrics.total_count) AS aggregated_total
+ FROM (
+ SELECT tuple(uniq(events.distinct_id), count()) AS user_metrics, sum(1) AS total_count
+ FROM events
+ WHERE equals(events.team_id, 420)) AS filtered_metrics)
LIMIT 50000
'''
# ---
@@ -415,15 +415,15 @@
SELECT tuple(uniqMerge(tupleElement(pageview_stats, 1)), countMerge(tupleElement(pageview_stats, 2))) AS pageview_stats, tuple(uniqMerge(tupleElement(click_stats, 1)), countMerge(tupleElement(click_stats, 2))) AS click_stats
FROM (
- SELECT tuple(uniqStateIf(events.distinct_id, equals(events.event, %(hogql_val_0)s)), countStateIf(equals(events.event, %(hogql_val_1)s))) AS pageview_stats, tuple(uniqStateIf(events.distinct_id, equals(events.event, %(hogql_val_2)s)), countStateIf(equals(events.event, %(hogql_val_3)s))) AS click_stats
- FROM events
- WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_4)s, 6, %(hogql_val_5)s)), lessOrEquals(events.timestamp, toDateTime64(%(hogql_val_6)s, 6, %(hogql_val_7)s))))
+ SELECT tuple(uniqStateIf(events.distinct_id, equals(events.event, %(hogql_val_0)s)), countStateIf(equals(events.event, %(hogql_val_1)s))) AS pageview_stats, tuple(uniqStateIf(events.distinct_id, equals(events.event, %(hogql_val_2)s)), countStateIf(equals(events.event, %(hogql_val_3)s))) AS click_stats
+ FROM events
+ WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_4)s, 6, %(hogql_val_5)s)), lessOrEquals(events.timestamp, toDateTime64(%(hogql_val_6)s, 6, %(hogql_val_7)s))))
LIMIT 50000 UNION ALL
SELECT tuple(uniqMerge(tupleElement(pageview_stats, 1)), countMerge(tupleElement(pageview_stats, 2))) AS pageview_stats, tuple(uniqMerge(tupleElement(click_stats, 1)), countMerge(tupleElement(click_stats, 2))) AS click_stats
FROM (
- SELECT tuple(uniqStateIf(events.distinct_id, equals(events.event, %(hogql_val_8)s)), countStateIf(equals(events.event, %(hogql_val_9)s))) AS pageview_stats, tuple(uniqStateIf(events.distinct_id, equals(events.event, %(hogql_val_10)s)), countStateIf(equals(events.event, %(hogql_val_11)s))) AS click_stats
- FROM events
- WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_12)s, 6, %(hogql_val_13)s)), lessOrEquals(events.timestamp, toDateTime64(%(hogql_val_14)s, 6, %(hogql_val_15)s))))
+ SELECT tuple(uniqStateIf(events.distinct_id, equals(events.event, %(hogql_val_8)s)), countStateIf(equals(events.event, %(hogql_val_9)s))) AS pageview_stats, tuple(uniqStateIf(events.distinct_id, equals(events.event, %(hogql_val_10)s)), countStateIf(equals(events.event, %(hogql_val_11)s))) AS click_stats
+ FROM events
+ WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_12)s, 6, %(hogql_val_13)s)), lessOrEquals(events.timestamp, toDateTime64(%(hogql_val_14)s, 6, %(hogql_val_15)s))))
LIMIT 50000
'''
# ---
@@ -432,9 +432,9 @@
SELECT tuple(uniqMerge(tupleElement(conditional_stats, 1)), countMerge(tupleElement(conditional_stats, 2)), sumMerge(tupleElement(conditional_stats, 3))) AS conditional_stats
FROM (
- SELECT tuple(uniqStateIf(events.distinct_id, 1), countStateIf(1), sumStateIf(1, 1)) AS conditional_stats
- FROM events
- WHERE equals(events.team_id, 420))
+ SELECT tuple(uniqStateIf(events.distinct_id, 1), countStateIf(1), sumStateIf(1, 1)) AS conditional_stats
+ FROM events
+ WHERE equals(events.team_id, 420))
LIMIT 50000
'''
# ---
@@ -443,9 +443,9 @@
SELECT tuple(uniqMerge(tupleElement(mixed_tuple, 1)), tupleElement(mixed_tuple, 2), countMerge(tupleElement(mixed_tuple, 3)), tupleElement(mixed_tuple, 4), tupleElement(mixed_tuple, 5)) AS mixed_tuple
FROM (
- SELECT tuple(uniqState(events.distinct_id), NULL, countState(), %(hogql_val_0)s, 42) AS mixed_tuple
- FROM events
- WHERE equals(events.team_id, 420))
+ SELECT tuple(uniqState(events.distinct_id), NULL, countState(), %(hogql_val_0)s, 42) AS mixed_tuple
+ FROM events
+ WHERE equals(events.team_id, 420))
LIMIT 50000
'''
# ---
@@ -454,9 +454,9 @@
SELECT tuple(uniqMerge(tupleElement(mixed_stats, 1)), tupleElement(mixed_stats, 2), countMerge(tupleElement(mixed_stats, 3))) AS mixed_stats, sumMerge(total_sum) AS total_sum
FROM (
- SELECT tuple(uniqState(events.distinct_id), %(hogql_val_0)s, countState()) AS mixed_stats, sumState(1) AS total_sum
- FROM events
- WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_1)s, 6, %(hogql_val_2)s))))
+ SELECT tuple(uniqState(events.distinct_id), %(hogql_val_0)s, countState()) AS mixed_stats, sumState(1) AS total_sum
+ FROM events
+ WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_1)s, 6, %(hogql_val_2)s))))
LIMIT 50000
'''
# ---
@@ -465,10 +465,10 @@
SELECT tuple(uniqMerge(tupleElement(user_stats, 1)), countMerge(tupleElement(user_stats, 2))) AS user_stats, host AS host
FROM (
- SELECT tuple(uniqState(events.distinct_id), countState()) AS user_stats, replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '') AS host
- FROM events
- WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_1)s, 6, %(hogql_val_2)s)))
- GROUP BY host)
+ SELECT tuple(uniqState(events.distinct_id), countState()) AS user_stats, replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '') AS host
+ FROM events
+ WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_1)s, 6, %(hogql_val_2)s)))
+ GROUP BY host)
GROUP BY host
LIMIT 50000
'''
@@ -478,15 +478,15 @@
SELECT countMerge(total_pageviews) AS total_pageviews
FROM (
- SELECT countState() AS total_pageviews
- FROM events
- WHERE and(equals(events.team_id, 420), less(events.timestamp, minus(now64(6, %(hogql_val_0)s), toIntervalDay(1)))))
+ SELECT countState() AS total_pageviews
+ FROM events
+ WHERE and(equals(events.team_id, 420), less(events.timestamp, minus(now64(6, %(hogql_val_0)s), toIntervalDay(1)))))
LIMIT 50000 UNION ALL
SELECT countMerge(total_pageviews) AS total_pageviews
FROM (
- SELECT countState() AS total_pageviews
- FROM events
- WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, minus(now64(6, %(hogql_val_1)s), toIntervalDay(1)))))
+ SELECT countState() AS total_pageviews
+ FROM events
+ WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, minus(now64(6, %(hogql_val_1)s), toIntervalDay(1)))))
LIMIT 50000
'''
# ---
@@ -495,15 +495,15 @@
SELECT tuple(uniqMerge(tupleElement(user_stats, 1)), countMerge(tupleElement(user_stats, 2))) AS user_stats, tuple(sumMerge(tupleElement(metric_stats, 1)), avgMerge(tupleElement(metric_stats, 2))) AS metric_stats, %(hogql_val_3)s AS data_source
FROM (
- SELECT tuple(uniqState(events.distinct_id), countState()) AS user_stats, tuple(sumState(1), avgState(1)) AS metric_stats, %(hogql_val_0)s AS data_source
- FROM events
- WHERE and(equals(events.team_id, 420), less(events.timestamp, toDateTime64(%(hogql_val_1)s, 6, %(hogql_val_2)s))))
+ SELECT tuple(uniqState(events.distinct_id), countState()) AS user_stats, tuple(sumState(1), avgState(1)) AS metric_stats, %(hogql_val_0)s AS data_source
+ FROM events
+ WHERE and(equals(events.team_id, 420), less(events.timestamp, toDateTime64(%(hogql_val_1)s, 6, %(hogql_val_2)s))))
LIMIT 50000 UNION ALL
SELECT tuple(uniqMerge(tupleElement(user_stats, 1)), countMerge(tupleElement(user_stats, 2))) AS user_stats, tuple(sumMerge(tupleElement(metric_stats, 1)), avgMerge(tupleElement(metric_stats, 2))) AS metric_stats, %(hogql_val_7)s AS data_source
FROM (
- SELECT tuple(uniqState(events.distinct_id), countState()) AS user_stats, tuple(sumState(1), avgState(1)) AS metric_stats, %(hogql_val_4)s AS data_source
- FROM events
- WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_5)s, 6, %(hogql_val_6)s))))
+ SELECT tuple(uniqState(events.distinct_id), countState()) AS user_stats, tuple(sumState(1), avgState(1)) AS metric_stats, %(hogql_val_4)s AS data_source
+ FROM events
+ WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_5)s, 6, %(hogql_val_6)s))))
LIMIT 50000
'''
# ---
@@ -512,9 +512,9 @@
SELECT uniqMerge(unique_users) AS unique_users, countMerge(total_events) AS total_events
FROM (
- SELECT uniqState(events.distinct_id) AS unique_users, countState() AS total_events
- FROM events
- WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_0)s, 6, %(hogql_val_1)s))))
+ SELECT uniqState(events.distinct_id) AS unique_users, countState() AS total_events
+ FROM events
+ WHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_0)s, 6, %(hogql_val_1)s))))
LIMIT 50000
'''
# ---
@@ -523,10 +523,10 @@
SELECT host AS host, countMerge(total_count) AS total_count, countMerge(click_count) AS click_count
FROM (
- SELECT replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '') AS host, countState() AS total_count, countStateIf(equals(events.event, %(hogql_val_1)s)) AS click_count
- FROM events
- WHERE equals(events.team_id, 420)
- GROUP BY host)
+ SELECT replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '') AS host, countState() AS total_count, countStateIf(equals(events.event, %(hogql_val_1)s)) AS click_count
+ FROM events
+ WHERE equals(events.team_id, 420)
+ GROUP BY host)
GROUP BY host
LIMIT 50000
'''
diff --git a/posthog/hogql_queries/test/__snapshots__/test_actors_query_runner.ambr b/posthog/hogql_queries/test/__snapshots__/test_actors_query_runner.ambr
index 5d9cfb23dda0..ea5853ae6ad8 100644
--- a/posthog/hogql_queries/test/__snapshots__/test_actors_query_runner.ambr
+++ b/posthog/hogql_queries/test/__snapshots__/test_actors_query_runner.ambr
@@ -5,9 +5,9 @@
SELECT id, id, 1
FROM persons
WHERE or(ilike(toString(properties.email), '%SEARCHSTRING%'), ilike(toString(properties.name), '%SEARCHSTRING%'), ilike(toString(id), '%SEARCHSTRING%'), in(id, (
- SELECT person_id
- FROM person_distinct_ids
- WHERE ilike(distinct_id, '%SEARCHSTRING%'))))
+ SELECT person_id
+ FROM person_distinct_ids
+ WHERE ilike(distinct_id, '%SEARCHSTRING%'))))
ORDER BY id ASC
LIMIT 50000
'''
diff --git a/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_adapters.ambr b/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_adapters.ambr
index c6941c2d53b1..b7c56be4093d 100644
--- a/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_adapters.ambr
+++ b/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_adapters.ambr
@@ -287,11 +287,11 @@
SELECT campaign AS campaign, source AS source, impressions AS impressions, clicks AS clicks, cost AS cost
FROM (
- SELECT toString(campaign_id) AS match_key, toString(campaign_id) AS campaign, toString(campaign_id) AS id, toString('Facebook') AS source, toFloat(coalesce(toFloat(impressions), 0)) AS impressions, toFloat(coalesce(toFloat(clicks), 0)) AS clicks, toFloat(convertCurrency(currency, 'USD', coalesce(toFloat(spend), 0))) AS cost, toFloat(coalesce(toFloat(conversions), 0)) AS reported_conversion, 0 AS reported_conversion_value
- FROM facebook_table
- WHERE and(greaterOrEquals(toDateTime(date), toDateTime('2024-01-01 00:00:00')), lessOrEquals(toDateTime(date), toDateTime('2024-12-31 23:59:59'))) UNION ALL
- SELECT toString(campaign_name) AS match_key, toString(campaign_name) AS campaign, toString(campaign_name) AS id, toString('TikTok') AS source, toFloat(coalesce(toFloat(impressions), 0)) AS impressions, toFloat(coalesce(toFloat(clicks), 0)) AS clicks, toFloat(convertCurrency(currency, 'USD', coalesce(toFloat(spend), 0))) AS cost, toFloat(coalesce(toFloat(conversions), 0)) AS reported_conversion, 0 AS reported_conversion_value
- FROM tiktok_table
- WHERE and(greaterOrEquals(toDateTime(date), toDateTime('2024-01-01 00:00:00')), lessOrEquals(toDateTime(date), toDateTime('2024-12-31 23:59:59')))) AS all_marketing_data
+ SELECT toString(campaign_id) AS match_key, toString(campaign_id) AS campaign, toString(campaign_id) AS id, toString('Facebook') AS source, toFloat(coalesce(toFloat(impressions), 0)) AS impressions, toFloat(coalesce(toFloat(clicks), 0)) AS clicks, toFloat(convertCurrency(currency, 'USD', coalesce(toFloat(spend), 0))) AS cost, toFloat(coalesce(toFloat(conversions), 0)) AS reported_conversion, 0 AS reported_conversion_value
+ FROM facebook_table
+ WHERE and(greaterOrEquals(toDateTime(date), toDateTime('2024-01-01 00:00:00')), lessOrEquals(toDateTime(date), toDateTime('2024-12-31 23:59:59'))) UNION ALL
+ SELECT toString(campaign_name) AS match_key, toString(campaign_name) AS campaign, toString(campaign_name) AS id, toString('TikTok') AS source, toFloat(coalesce(toFloat(impressions), 0)) AS impressions, toFloat(coalesce(toFloat(clicks), 0)) AS clicks, toFloat(convertCurrency(currency, 'USD', coalesce(toFloat(spend), 0))) AS cost, toFloat(coalesce(toFloat(conversions), 0)) AS reported_conversion, 0 AS reported_conversion_value
+ FROM tiktok_table
+ WHERE and(greaterOrEquals(toDateTime(date), toDateTime('2024-01-01 00:00:00')), lessOrEquals(toDateTime(date), toDateTime('2024-12-31 23:59:59')))) AS all_marketing_data
'''
# ---
diff --git a/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_conversion_goal_processor.ambr b/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_conversion_goal_processor.ambr
index 748dddaf44d9..41c50d706ba0 100644
--- a/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_conversion_goal_processor.ambr
+++ b/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_conversion_goal_processor.ambr
@@ -3,89 +3,89 @@
'''
SELECT
- if(notEmpty(campaign_name), campaign_name, 'organic') AS match_key,
- if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
- if(notEmpty(campaign_id), campaign_id, '-') AS id,
- if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
- count() AS conversion_0
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS match_key,
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
+ if(notEmpty(campaign_id), campaign_id, '-') AS id,
+ if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
+ count() AS conversion_0
FROM
- (
- SELECT
- person_id,
- if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name,
- if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name,
- if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name,
- if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name,
- if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name,
- if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name,
- if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name,
- if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name,
- if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name,
- '-' AS campaign_id,
- 1 AS conversion_value
+ (
+ SELECT
+ person_id,
+ if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name,
+ if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name,
+ if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name,
+ if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name,
+ if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name,
+ if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name,
+ if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name,
+ if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name,
+ if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name,
+ '-' AS campaign_id,
+ 1 AS conversion_value
+ FROM
+ (
+ SELECT
+ person_id,
+ conversion_timestamps[i] AS conversion_time,
+ conversion_math_values[i] AS conversion_math_value,
+ conversion_campaigns[i] AS conversion_campaign,
+ conversion_sources[i] AS conversion_source,
+ conversion_mediums[i] AS conversion_medium,
+ conversion_contents[i] AS conversion_content,
+ conversion_terms[i] AS conversion_term,
+ conversion_referring_domains[i] AS conversion_referring_domain,
+ conversion_gclids[i] AS conversion_gclid,
+ conversion_fbclids[i] AS conversion_fbclid,
+ conversion_gad_sources[i] AS conversion_gad_source,
+ arrayMax(arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 15552000))), utm_timestamps)) AS last_utm_timestamp,
+ if(isNotNull(last_utm_timestamp), utm_campaigns[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_campaign,
+ if(isNotNull(last_utm_timestamp), utm_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_source,
+ if(isNotNull(last_utm_timestamp), utm_mediums[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_medium,
+ if(isNotNull(last_utm_timestamp), utm_contents[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_content,
+ if(isNotNull(last_utm_timestamp), utm_terms[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_term,
+ if(isNotNull(last_utm_timestamp), utm_referring_domains[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_referring_domain,
+ if(isNotNull(last_utm_timestamp), utm_gclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gclid,
+ if(isNotNull(last_utm_timestamp), utm_fbclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_fbclid,
+ if(isNotNull(last_utm_timestamp), utm_gad_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gad_source
FROM
- (
- SELECT
- person_id,
- conversion_timestamps[i] AS conversion_time,
- conversion_math_values[i] AS conversion_math_value,
- conversion_campaigns[i] AS conversion_campaign,
- conversion_sources[i] AS conversion_source,
- conversion_mediums[i] AS conversion_medium,
- conversion_contents[i] AS conversion_content,
- conversion_terms[i] AS conversion_term,
- conversion_referring_domains[i] AS conversion_referring_domain,
- conversion_gclids[i] AS conversion_gclid,
- conversion_fbclids[i] AS conversion_fbclid,
- conversion_gad_sources[i] AS conversion_gad_source,
- arrayMax(arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 15552000))), utm_timestamps)) AS last_utm_timestamp,
- if(isNotNull(last_utm_timestamp), utm_campaigns[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_campaign,
- if(isNotNull(last_utm_timestamp), utm_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_source,
- if(isNotNull(last_utm_timestamp), utm_mediums[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_medium,
- if(isNotNull(last_utm_timestamp), utm_contents[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_content,
- if(isNotNull(last_utm_timestamp), utm_terms[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_term,
- if(isNotNull(last_utm_timestamp), utm_referring_domains[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_referring_domain,
- if(isNotNull(last_utm_timestamp), utm_gclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gclid,
- if(isNotNull(last_utm_timestamp), utm_fbclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_fbclid,
- if(isNotNull(last_utm_timestamp), utm_gad_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gad_source
- FROM
- (
- SELECT
- events.person_id,
- arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'user signed up'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps,
- arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'user signed up'), toFloat(1), 0))) AS conversion_math_values,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'user signed up'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'user signed up'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'user signed up'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'user signed up'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'user signed up'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'user signed up'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'user signed up'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'user signed up'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'user signed up'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources,
- arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
- FROM
- events
- WHERE
- or(and(equals(events.event, 'user signed up'), greaterOrEquals(events.timestamp, toDateTime('2025-06-06')), lessOrEquals(events.timestamp, toDateTime('2025-07-07'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2025-06-06'), toIntervalSecond(15552000))), lessOrEquals(events.timestamp, toDateTime('2025-07-07'))))
- GROUP BY
- events.person_id
- HAVING
- greater(length(conversion_timestamps), 0))
- ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversions
+ (
+ SELECT
+ events.person_id,
+ arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'user signed up'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps,
+ arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'user signed up'), toFloat(1), 0))) AS conversion_math_values,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'user signed up'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'user signed up'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'user signed up'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'user signed up'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'user signed up'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'user signed up'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'user signed up'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'user signed up'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'user signed up'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources,
+ arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
+ FROM
+ events
+ WHERE
+ or(and(equals(events.event, 'user signed up'), greaterOrEquals(events.timestamp, toDateTime('2025-06-06')), lessOrEquals(events.timestamp, toDateTime('2025-07-07'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2025-06-06'), toIntervalSecond(15552000))), lessOrEquals(events.timestamp, toDateTime('2025-07-07'))))
+ GROUP BY
+ events.person_id
+ HAVING
+ greater(length(conversion_timestamps), 0))
+ ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversions
GROUP BY
- campaign,
- id,
- source
+ campaign,
+ id,
+ source
LIMIT 100
'''
# ---
@@ -93,89 +93,89 @@
'''
SELECT
- if(notEmpty(campaign_name), campaign_name, 'organic') AS match_key,
- if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
- if(notEmpty(campaign_id), campaign_id, '-') AS id,
- if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
- count() AS conversion_0
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS match_key,
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
+ if(notEmpty(campaign_id), campaign_id, '-') AS id,
+ if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
+ count() AS conversion_0
FROM
- (
- SELECT
- person_id,
- if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name,
- if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name,
- if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name,
- if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name,
- if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name,
- if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name,
- if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name,
- if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name,
- if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name,
- '-' AS campaign_id,
- 1 AS conversion_value
+ (
+ SELECT
+ person_id,
+ if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name,
+ if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name,
+ if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name,
+ if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name,
+ if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name,
+ if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name,
+ if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name,
+ if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name,
+ if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name,
+ '-' AS campaign_id,
+ 1 AS conversion_value
+ FROM
+ (
+ SELECT
+ person_id,
+ conversion_timestamps[i] AS conversion_time,
+ conversion_math_values[i] AS conversion_math_value,
+ conversion_campaigns[i] AS conversion_campaign,
+ conversion_sources[i] AS conversion_source,
+ conversion_mediums[i] AS conversion_medium,
+ conversion_contents[i] AS conversion_content,
+ conversion_terms[i] AS conversion_term,
+ conversion_referring_domains[i] AS conversion_referring_domain,
+ conversion_gclids[i] AS conversion_gclid,
+ conversion_fbclids[i] AS conversion_fbclid,
+ conversion_gad_sources[i] AS conversion_gad_source,
+ arrayMax(arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 15552000))), utm_timestamps)) AS last_utm_timestamp,
+ if(isNotNull(last_utm_timestamp), utm_campaigns[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_campaign,
+ if(isNotNull(last_utm_timestamp), utm_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_source,
+ if(isNotNull(last_utm_timestamp), utm_mediums[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_medium,
+ if(isNotNull(last_utm_timestamp), utm_contents[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_content,
+ if(isNotNull(last_utm_timestamp), utm_terms[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_term,
+ if(isNotNull(last_utm_timestamp), utm_referring_domains[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_referring_domain,
+ if(isNotNull(last_utm_timestamp), utm_gclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gclid,
+ if(isNotNull(last_utm_timestamp), utm_fbclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_fbclid,
+ if(isNotNull(last_utm_timestamp), utm_gad_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gad_source
FROM
- (
- SELECT
- person_id,
- conversion_timestamps[i] AS conversion_time,
- conversion_math_values[i] AS conversion_math_value,
- conversion_campaigns[i] AS conversion_campaign,
- conversion_sources[i] AS conversion_source,
- conversion_mediums[i] AS conversion_medium,
- conversion_contents[i] AS conversion_content,
- conversion_terms[i] AS conversion_term,
- conversion_referring_domains[i] AS conversion_referring_domain,
- conversion_gclids[i] AS conversion_gclid,
- conversion_fbclids[i] AS conversion_fbclid,
- conversion_gad_sources[i] AS conversion_gad_source,
- arrayMax(arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 15552000))), utm_timestamps)) AS last_utm_timestamp,
- if(isNotNull(last_utm_timestamp), utm_campaigns[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_campaign,
- if(isNotNull(last_utm_timestamp), utm_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_source,
- if(isNotNull(last_utm_timestamp), utm_mediums[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_medium,
- if(isNotNull(last_utm_timestamp), utm_contents[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_content,
- if(isNotNull(last_utm_timestamp), utm_terms[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_term,
- if(isNotNull(last_utm_timestamp), utm_referring_domains[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_referring_domain,
- if(isNotNull(last_utm_timestamp), utm_gclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gclid,
- if(isNotNull(last_utm_timestamp), utm_fbclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_fbclid,
- if(isNotNull(last_utm_timestamp), utm_gad_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gad_source
- FROM
- (
- SELECT
- events.person_id,
- arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'user signed up'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps,
- arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'user signed up'), toFloat(1), 0))) AS conversion_math_values,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'user signed up'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'user signed up'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'user signed up'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'user signed up'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'user signed up'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'user signed up'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'user signed up'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'user signed up'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'user signed up'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources,
- arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
- FROM
- events
- WHERE
- or(and(equals(events.event, 'user signed up'), greaterOrEquals(events.timestamp, toDateTime('2025-06-06')), lessOrEquals(events.timestamp, toDateTime('2025-07-07'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2025-06-06'), toIntervalSecond(15552000))), lessOrEquals(events.timestamp, toDateTime('2025-07-07'))))
- GROUP BY
- events.person_id
- HAVING
- greater(length(conversion_timestamps), 0))
- ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversions
+ (
+ SELECT
+ events.person_id,
+ arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'user signed up'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps,
+ arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'user signed up'), toFloat(1), 0))) AS conversion_math_values,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'user signed up'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'user signed up'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'user signed up'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'user signed up'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'user signed up'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'user signed up'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'user signed up'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'user signed up'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'user signed up'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources,
+ arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
+ FROM
+ events
+ WHERE
+ or(and(equals(events.event, 'user signed up'), greaterOrEquals(events.timestamp, toDateTime('2025-06-06')), lessOrEquals(events.timestamp, toDateTime('2025-07-07'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2025-06-06'), toIntervalSecond(15552000))), lessOrEquals(events.timestamp, toDateTime('2025-07-07'))))
+ GROUP BY
+ events.person_id
+ HAVING
+ greater(length(conversion_timestamps), 0))
+ ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversions
GROUP BY
- campaign,
- id,
- source
+ campaign,
+ id,
+ source
LIMIT 100
'''
# ---
@@ -183,89 +183,89 @@
'''
SELECT
- if(notEmpty(campaign_name), campaign_name, 'organic') AS match_key,
- if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
- if(notEmpty(campaign_id), campaign_id, '-') AS id,
- if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
- count() AS conversion_0
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS match_key,
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
+ if(notEmpty(campaign_id), campaign_id, '-') AS id,
+ if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
+ count() AS conversion_0
FROM
- (
- SELECT
- person_id,
- if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name,
- if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name,
- if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name,
- if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name,
- if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name,
- if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name,
- if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name,
- if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name,
- if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name,
- '-' AS campaign_id,
- 1 AS conversion_value
+ (
+ SELECT
+ person_id,
+ if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name,
+ if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name,
+ if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name,
+ if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name,
+ if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name,
+ if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name,
+ if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name,
+ if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name,
+ if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name,
+ '-' AS campaign_id,
+ 1 AS conversion_value
+ FROM
+ (
+ SELECT
+ person_id,
+ conversion_timestamps[i] AS conversion_time,
+ conversion_math_values[i] AS conversion_math_value,
+ conversion_campaigns[i] AS conversion_campaign,
+ conversion_sources[i] AS conversion_source,
+ conversion_mediums[i] AS conversion_medium,
+ conversion_contents[i] AS conversion_content,
+ conversion_terms[i] AS conversion_term,
+ conversion_referring_domains[i] AS conversion_referring_domain,
+ conversion_gclids[i] AS conversion_gclid,
+ conversion_fbclids[i] AS conversion_fbclid,
+ conversion_gad_sources[i] AS conversion_gad_source,
+ arrayMax(arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 7776000))), utm_timestamps)) AS last_utm_timestamp,
+ if(isNotNull(last_utm_timestamp), utm_campaigns[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_campaign,
+ if(isNotNull(last_utm_timestamp), utm_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_source,
+ if(isNotNull(last_utm_timestamp), utm_mediums[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_medium,
+ if(isNotNull(last_utm_timestamp), utm_contents[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_content,
+ if(isNotNull(last_utm_timestamp), utm_terms[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_term,
+ if(isNotNull(last_utm_timestamp), utm_referring_domains[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_referring_domain,
+ if(isNotNull(last_utm_timestamp), utm_gclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gclid,
+ if(isNotNull(last_utm_timestamp), utm_fbclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_fbclid,
+ if(isNotNull(last_utm_timestamp), utm_gad_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gad_source
FROM
- (
- SELECT
- person_id,
- conversion_timestamps[i] AS conversion_time,
- conversion_math_values[i] AS conversion_math_value,
- conversion_campaigns[i] AS conversion_campaign,
- conversion_sources[i] AS conversion_source,
- conversion_mediums[i] AS conversion_medium,
- conversion_contents[i] AS conversion_content,
- conversion_terms[i] AS conversion_term,
- conversion_referring_domains[i] AS conversion_referring_domain,
- conversion_gclids[i] AS conversion_gclid,
- conversion_fbclids[i] AS conversion_fbclid,
- conversion_gad_sources[i] AS conversion_gad_source,
- arrayMax(arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 7776000))), utm_timestamps)) AS last_utm_timestamp,
- if(isNotNull(last_utm_timestamp), utm_campaigns[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_campaign,
- if(isNotNull(last_utm_timestamp), utm_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_source,
- if(isNotNull(last_utm_timestamp), utm_mediums[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_medium,
- if(isNotNull(last_utm_timestamp), utm_contents[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_content,
- if(isNotNull(last_utm_timestamp), utm_terms[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_term,
- if(isNotNull(last_utm_timestamp), utm_referring_domains[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_referring_domain,
- if(isNotNull(last_utm_timestamp), utm_gclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gclid,
- if(isNotNull(last_utm_timestamp), utm_fbclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_fbclid,
- if(isNotNull(last_utm_timestamp), utm_gad_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gad_source
- FROM
- (
- SELECT
- events.person_id,
- arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps,
- arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toFloat(1), 0))) AS conversion_math_values,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources,
- arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
- FROM
- events
- WHERE
- or(and(equals(events.event, 'purchase'), greaterOrEquals(events.timestamp, toDateTime('2023-03-20'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2023-03-20'), toIntervalSecond(7776000)))))
- GROUP BY
- events.person_id
- HAVING
- greater(length(conversion_timestamps), 0))
- ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversions
+ (
+ SELECT
+ events.person_id,
+ arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps,
+ arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toFloat(1), 0))) AS conversion_math_values,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources,
+ arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
+ FROM
+ events
+ WHERE
+ or(and(equals(events.event, 'purchase'), greaterOrEquals(events.timestamp, toDateTime('2023-03-20'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2023-03-20'), toIntervalSecond(7776000)))))
+ GROUP BY
+ events.person_id
+ HAVING
+ greater(length(conversion_timestamps), 0))
+ ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversions
GROUP BY
- campaign,
- id,
- source
+ campaign,
+ id,
+ source
LIMIT 100
'''
# ---
@@ -273,89 +273,89 @@
'''
SELECT
- if(notEmpty(campaign_name), campaign_name, 'organic') AS match_key,
- if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
- if(notEmpty(campaign_id), campaign_id, '-') AS id,
- if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
- count() AS conversion_0
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS match_key,
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
+ if(notEmpty(campaign_id), campaign_id, '-') AS id,
+ if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
+ count() AS conversion_0
FROM
- (
- SELECT
- person_id,
- if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name,
- if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name,
- if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name,
- if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name,
- if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name,
- if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name,
- if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name,
- if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name,
- if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name,
- '-' AS campaign_id,
- 1 AS conversion_value
+ (
+ SELECT
+ person_id,
+ if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name,
+ if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name,
+ if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name,
+ if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name,
+ if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name,
+ if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name,
+ if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name,
+ if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name,
+ if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name,
+ '-' AS campaign_id,
+ 1 AS conversion_value
+ FROM
+ (
+ SELECT
+ person_id,
+ conversion_timestamps[i] AS conversion_time,
+ conversion_math_values[i] AS conversion_math_value,
+ conversion_campaigns[i] AS conversion_campaign,
+ conversion_sources[i] AS conversion_source,
+ conversion_mediums[i] AS conversion_medium,
+ conversion_contents[i] AS conversion_content,
+ conversion_terms[i] AS conversion_term,
+ conversion_referring_domains[i] AS conversion_referring_domain,
+ conversion_gclids[i] AS conversion_gclid,
+ conversion_fbclids[i] AS conversion_fbclid,
+ conversion_gad_sources[i] AS conversion_gad_source,
+ arrayMax(arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 7776000))), utm_timestamps)) AS last_utm_timestamp,
+ if(isNotNull(last_utm_timestamp), utm_campaigns[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_campaign,
+ if(isNotNull(last_utm_timestamp), utm_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_source,
+ if(isNotNull(last_utm_timestamp), utm_mediums[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_medium,
+ if(isNotNull(last_utm_timestamp), utm_contents[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_content,
+ if(isNotNull(last_utm_timestamp), utm_terms[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_term,
+ if(isNotNull(last_utm_timestamp), utm_referring_domains[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_referring_domain,
+ if(isNotNull(last_utm_timestamp), utm_gclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gclid,
+ if(isNotNull(last_utm_timestamp), utm_fbclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_fbclid,
+ if(isNotNull(last_utm_timestamp), utm_gad_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gad_source
FROM
- (
- SELECT
- person_id,
- conversion_timestamps[i] AS conversion_time,
- conversion_math_values[i] AS conversion_math_value,
- conversion_campaigns[i] AS conversion_campaign,
- conversion_sources[i] AS conversion_source,
- conversion_mediums[i] AS conversion_medium,
- conversion_contents[i] AS conversion_content,
- conversion_terms[i] AS conversion_term,
- conversion_referring_domains[i] AS conversion_referring_domain,
- conversion_gclids[i] AS conversion_gclid,
- conversion_fbclids[i] AS conversion_fbclid,
- conversion_gad_sources[i] AS conversion_gad_source,
- arrayMax(arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 7776000))), utm_timestamps)) AS last_utm_timestamp,
- if(isNotNull(last_utm_timestamp), utm_campaigns[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_campaign,
- if(isNotNull(last_utm_timestamp), utm_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_source,
- if(isNotNull(last_utm_timestamp), utm_mediums[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_medium,
- if(isNotNull(last_utm_timestamp), utm_contents[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_content,
- if(isNotNull(last_utm_timestamp), utm_terms[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_term,
- if(isNotNull(last_utm_timestamp), utm_referring_domains[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_referring_domain,
- if(isNotNull(last_utm_timestamp), utm_gclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gclid,
- if(isNotNull(last_utm_timestamp), utm_fbclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_fbclid,
- if(isNotNull(last_utm_timestamp), utm_gad_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gad_source
- FROM
- (
- SELECT
- events.person_id,
- arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps,
- arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toFloat(1), 0))) AS conversion_math_values,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources,
- arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
- FROM
- events
- WHERE
- or(and(equals(events.event, 'purchase'), greaterOrEquals(events.timestamp, toDateTime('2023-05-01'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2023-05-01'), toIntervalSecond(7776000)))))
- GROUP BY
- events.person_id
- HAVING
- greater(length(conversion_timestamps), 0))
- ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversions
+ (
+ SELECT
+ events.person_id,
+ arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps,
+ arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toFloat(1), 0))) AS conversion_math_values,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources,
+ arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
+ FROM
+ events
+ WHERE
+ or(and(equals(events.event, 'purchase'), greaterOrEquals(events.timestamp, toDateTime('2023-05-01'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2023-05-01'), toIntervalSecond(7776000)))))
+ GROUP BY
+ events.person_id
+ HAVING
+ greater(length(conversion_timestamps), 0))
+ ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversions
GROUP BY
- campaign,
- id,
- source
+ campaign,
+ id,
+ source
LIMIT 100
'''
# ---
@@ -363,89 +363,89 @@
'''
SELECT
- if(notEmpty(campaign_name), campaign_name, 'organic') AS match_key,
- if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
- if(notEmpty(campaign_id), campaign_id, '-') AS id,
- if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
- count() AS conversion_0
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS match_key,
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
+ if(notEmpty(campaign_id), campaign_id, '-') AS id,
+ if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
+ count() AS conversion_0
FROM
- (
- SELECT
- person_id,
- if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name,
- if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name,
- if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name,
- if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name,
- if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name,
- if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name,
- if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name,
- if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name,
- if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name,
- '-' AS campaign_id,
- 1 AS conversion_value
+ (
+ SELECT
+ person_id,
+ if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name,
+ if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name,
+ if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name,
+ if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name,
+ if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name,
+ if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name,
+ if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name,
+ if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name,
+ if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name,
+ '-' AS campaign_id,
+ 1 AS conversion_value
+ FROM
+ (
+ SELECT
+ person_id,
+ conversion_timestamps[i] AS conversion_time,
+ conversion_math_values[i] AS conversion_math_value,
+ conversion_campaigns[i] AS conversion_campaign,
+ conversion_sources[i] AS conversion_source,
+ conversion_mediums[i] AS conversion_medium,
+ conversion_contents[i] AS conversion_content,
+ conversion_terms[i] AS conversion_term,
+ conversion_referring_domains[i] AS conversion_referring_domain,
+ conversion_gclids[i] AS conversion_gclid,
+ conversion_fbclids[i] AS conversion_fbclid,
+ conversion_gad_sources[i] AS conversion_gad_source,
+ arrayMax(arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 7776000))), utm_timestamps)) AS last_utm_timestamp,
+ if(isNotNull(last_utm_timestamp), utm_campaigns[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_campaign,
+ if(isNotNull(last_utm_timestamp), utm_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_source,
+ if(isNotNull(last_utm_timestamp), utm_mediums[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_medium,
+ if(isNotNull(last_utm_timestamp), utm_contents[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_content,
+ if(isNotNull(last_utm_timestamp), utm_terms[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_term,
+ if(isNotNull(last_utm_timestamp), utm_referring_domains[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_referring_domain,
+ if(isNotNull(last_utm_timestamp), utm_gclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gclid,
+ if(isNotNull(last_utm_timestamp), utm_fbclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_fbclid,
+ if(isNotNull(last_utm_timestamp), utm_gad_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gad_source
FROM
- (
- SELECT
- person_id,
- conversion_timestamps[i] AS conversion_time,
- conversion_math_values[i] AS conversion_math_value,
- conversion_campaigns[i] AS conversion_campaign,
- conversion_sources[i] AS conversion_source,
- conversion_mediums[i] AS conversion_medium,
- conversion_contents[i] AS conversion_content,
- conversion_terms[i] AS conversion_term,
- conversion_referring_domains[i] AS conversion_referring_domain,
- conversion_gclids[i] AS conversion_gclid,
- conversion_fbclids[i] AS conversion_fbclid,
- conversion_gad_sources[i] AS conversion_gad_source,
- arrayMax(arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 7776000))), utm_timestamps)) AS last_utm_timestamp,
- if(isNotNull(last_utm_timestamp), utm_campaigns[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_campaign,
- if(isNotNull(last_utm_timestamp), utm_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_source,
- if(isNotNull(last_utm_timestamp), utm_mediums[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_medium,
- if(isNotNull(last_utm_timestamp), utm_contents[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_content,
- if(isNotNull(last_utm_timestamp), utm_terms[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_term,
- if(isNotNull(last_utm_timestamp), utm_referring_domains[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_referring_domain,
- if(isNotNull(last_utm_timestamp), utm_gclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gclid,
- if(isNotNull(last_utm_timestamp), utm_fbclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_fbclid,
- if(isNotNull(last_utm_timestamp), utm_gad_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gad_source
- FROM
- (
- SELECT
- events.person_id,
- arrayFilter(x -> greater(x, 0), groupArray(if(equals(event, 'sign_up'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps,
- arrayFilter(x -> greater(x, 0), groupArray(if(equals(event, 'sign_up'), toFloat(1), 0))) AS conversion_math_values,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'sign_up'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'sign_up'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'sign_up'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'sign_up'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'sign_up'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'sign_up'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'sign_up'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'sign_up'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'sign_up'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources,
- arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
- FROM
- events
- WHERE
- or(and(equals(event, 'sign_up'), greaterOrEquals(events.timestamp, toDateTime('2023-01-01'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2023-01-01'), toIntervalSecond(7776000)))))
- GROUP BY
- events.person_id
- HAVING
- greater(length(conversion_timestamps), 0))
- ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversions
+ (
+ SELECT
+ events.person_id,
+ arrayFilter(x -> greater(x, 0), groupArray(if(equals(event, 'sign_up'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps,
+ arrayFilter(x -> greater(x, 0), groupArray(if(equals(event, 'sign_up'), toFloat(1), 0))) AS conversion_math_values,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'sign_up'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'sign_up'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'sign_up'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'sign_up'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'sign_up'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'sign_up'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'sign_up'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'sign_up'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'sign_up'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources,
+ arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
+ FROM
+ events
+ WHERE
+ or(and(equals(event, 'sign_up'), greaterOrEquals(events.timestamp, toDateTime('2023-01-01'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2023-01-01'), toIntervalSecond(7776000)))))
+ GROUP BY
+ events.person_id
+ HAVING
+ greater(length(conversion_timestamps), 0))
+ ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversions
GROUP BY
- campaign,
- id,
- source
+ campaign,
+ id,
+ source
LIMIT 100
'''
# ---
@@ -453,89 +453,89 @@
'''
SELECT
- if(notEmpty(campaign_name), campaign_name, 'organic') AS match_key,
- if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
- if(notEmpty(campaign_id), campaign_id, '-') AS id,
- if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
- count() AS conversion_0
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS match_key,
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
+ if(notEmpty(campaign_id), campaign_id, '-') AS id,
+ if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
+ count() AS conversion_0
FROM
- (
- SELECT
- person_id,
- if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name,
- if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name,
- if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name,
- if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name,
- if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name,
- if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name,
- if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name,
- if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name,
- if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name,
- '-' AS campaign_id,
- 1 AS conversion_value
+ (
+ SELECT
+ person_id,
+ if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name,
+ if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name,
+ if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name,
+ if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name,
+ if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name,
+ if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name,
+ if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name,
+ if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name,
+ if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name,
+ '-' AS campaign_id,
+ 1 AS conversion_value
+ FROM
+ (
+ SELECT
+ person_id,
+ conversion_timestamps[i] AS conversion_time,
+ conversion_math_values[i] AS conversion_math_value,
+ conversion_campaigns[i] AS conversion_campaign,
+ conversion_sources[i] AS conversion_source,
+ conversion_mediums[i] AS conversion_medium,
+ conversion_contents[i] AS conversion_content,
+ conversion_terms[i] AS conversion_term,
+ conversion_referring_domains[i] AS conversion_referring_domain,
+ conversion_gclids[i] AS conversion_gclid,
+ conversion_fbclids[i] AS conversion_fbclid,
+ conversion_gad_sources[i] AS conversion_gad_source,
+ arrayMax(arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 7776000))), utm_timestamps)) AS last_utm_timestamp,
+ if(isNotNull(last_utm_timestamp), utm_campaigns[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_campaign,
+ if(isNotNull(last_utm_timestamp), utm_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_source,
+ if(isNotNull(last_utm_timestamp), utm_mediums[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_medium,
+ if(isNotNull(last_utm_timestamp), utm_contents[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_content,
+ if(isNotNull(last_utm_timestamp), utm_terms[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_term,
+ if(isNotNull(last_utm_timestamp), utm_referring_domains[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_referring_domain,
+ if(isNotNull(last_utm_timestamp), utm_gclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gclid,
+ if(isNotNull(last_utm_timestamp), utm_fbclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_fbclid,
+ if(isNotNull(last_utm_timestamp), utm_gad_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gad_source
FROM
- (
- SELECT
- person_id,
- conversion_timestamps[i] AS conversion_time,
- conversion_math_values[i] AS conversion_math_value,
- conversion_campaigns[i] AS conversion_campaign,
- conversion_sources[i] AS conversion_source,
- conversion_mediums[i] AS conversion_medium,
- conversion_contents[i] AS conversion_content,
- conversion_terms[i] AS conversion_term,
- conversion_referring_domains[i] AS conversion_referring_domain,
- conversion_gclids[i] AS conversion_gclid,
- conversion_fbclids[i] AS conversion_fbclid,
- conversion_gad_sources[i] AS conversion_gad_source,
- arrayMax(arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 7776000))), utm_timestamps)) AS last_utm_timestamp,
- if(isNotNull(last_utm_timestamp), utm_campaigns[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_campaign,
- if(isNotNull(last_utm_timestamp), utm_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_source,
- if(isNotNull(last_utm_timestamp), utm_mediums[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_medium,
- if(isNotNull(last_utm_timestamp), utm_contents[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_content,
- if(isNotNull(last_utm_timestamp), utm_terms[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_term,
- if(isNotNull(last_utm_timestamp), utm_referring_domains[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_referring_domain,
- if(isNotNull(last_utm_timestamp), utm_gclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gclid,
- if(isNotNull(last_utm_timestamp), utm_fbclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_fbclid,
- if(isNotNull(last_utm_timestamp), utm_gad_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gad_source
- FROM
- (
- SELECT
- events.person_id,
- arrayFilter(x -> greater(x, 0), groupArray(if(equals(event, 'sign_up'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps,
- arrayFilter(x -> greater(x, 0), groupArray(if(equals(event, 'sign_up'), toFloat(1), 0))) AS conversion_math_values,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'sign_up'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'sign_up'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'sign_up'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'sign_up'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'sign_up'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'sign_up'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'sign_up'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'sign_up'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'sign_up'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources,
- arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
- FROM
- events
- WHERE
- or(and(equals(event, 'sign_up'), greaterOrEquals(events.timestamp, toDateTime('2023-05-01')), lessOrEquals(events.timestamp, toDateTime('2023-05-03'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2023-05-01'), toIntervalSecond(7776000))), lessOrEquals(events.timestamp, toDateTime('2023-05-03'))))
- GROUP BY
- events.person_id
- HAVING
- greater(length(conversion_timestamps), 0))
- ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversions
+ (
+ SELECT
+ events.person_id,
+ arrayFilter(x -> greater(x, 0), groupArray(if(equals(event, 'sign_up'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps,
+ arrayFilter(x -> greater(x, 0), groupArray(if(equals(event, 'sign_up'), toFloat(1), 0))) AS conversion_math_values,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'sign_up'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'sign_up'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'sign_up'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'sign_up'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'sign_up'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'sign_up'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'sign_up'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'sign_up'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'sign_up'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources,
+ arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
+ FROM
+ events
+ WHERE
+ or(and(equals(event, 'sign_up'), greaterOrEquals(events.timestamp, toDateTime('2023-05-01')), lessOrEquals(events.timestamp, toDateTime('2023-05-03'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2023-05-01'), toIntervalSecond(7776000))), lessOrEquals(events.timestamp, toDateTime('2023-05-03'))))
+ GROUP BY
+ events.person_id
+ HAVING
+ greater(length(conversion_timestamps), 0))
+ ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversions
GROUP BY
- campaign,
- id,
- source
+ campaign,
+ id,
+ source
LIMIT 100
'''
# ---
@@ -543,89 +543,89 @@
'''
SELECT
- if(notEmpty(campaign_name), campaign_name, 'organic') AS match_key,
- if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
- if(notEmpty(campaign_id), campaign_id, '-') AS id,
- if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
- count() AS conversion_0
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS match_key,
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
+ if(notEmpty(campaign_id), campaign_id, '-') AS id,
+ if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
+ count() AS conversion_0
FROM
- (
- SELECT
- person_id,
- if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name,
- if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name,
- if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name,
- if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name,
- if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name,
- if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name,
- if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name,
- if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name,
- if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name,
- '-' AS campaign_id,
- 1 AS conversion_value
+ (
+ SELECT
+ person_id,
+ if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name,
+ if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name,
+ if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name,
+ if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name,
+ if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name,
+ if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name,
+ if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name,
+ if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name,
+ if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name,
+ '-' AS campaign_id,
+ 1 AS conversion_value
+ FROM
+ (
+ SELECT
+ person_id,
+ conversion_timestamps[i] AS conversion_time,
+ conversion_math_values[i] AS conversion_math_value,
+ conversion_campaigns[i] AS conversion_campaign,
+ conversion_sources[i] AS conversion_source,
+ conversion_mediums[i] AS conversion_medium,
+ conversion_contents[i] AS conversion_content,
+ conversion_terms[i] AS conversion_term,
+ conversion_referring_domains[i] AS conversion_referring_domain,
+ conversion_gclids[i] AS conversion_gclid,
+ conversion_fbclids[i] AS conversion_fbclid,
+ conversion_gad_sources[i] AS conversion_gad_source,
+ arrayMax(arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 7776000))), utm_timestamps)) AS last_utm_timestamp,
+ if(isNotNull(last_utm_timestamp), utm_campaigns[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_campaign,
+ if(isNotNull(last_utm_timestamp), utm_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_source,
+ if(isNotNull(last_utm_timestamp), utm_mediums[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_medium,
+ if(isNotNull(last_utm_timestamp), utm_contents[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_content,
+ if(isNotNull(last_utm_timestamp), utm_terms[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_term,
+ if(isNotNull(last_utm_timestamp), utm_referring_domains[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_referring_domain,
+ if(isNotNull(last_utm_timestamp), utm_gclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gclid,
+ if(isNotNull(last_utm_timestamp), utm_fbclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_fbclid,
+ if(isNotNull(last_utm_timestamp), utm_gad_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gad_source
FROM
- (
- SELECT
- person_id,
- conversion_timestamps[i] AS conversion_time,
- conversion_math_values[i] AS conversion_math_value,
- conversion_campaigns[i] AS conversion_campaign,
- conversion_sources[i] AS conversion_source,
- conversion_mediums[i] AS conversion_medium,
- conversion_contents[i] AS conversion_content,
- conversion_terms[i] AS conversion_term,
- conversion_referring_domains[i] AS conversion_referring_domain,
- conversion_gclids[i] AS conversion_gclid,
- conversion_fbclids[i] AS conversion_fbclid,
- conversion_gad_sources[i] AS conversion_gad_source,
- arrayMax(arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 7776000))), utm_timestamps)) AS last_utm_timestamp,
- if(isNotNull(last_utm_timestamp), utm_campaigns[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_campaign,
- if(isNotNull(last_utm_timestamp), utm_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_source,
- if(isNotNull(last_utm_timestamp), utm_mediums[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_medium,
- if(isNotNull(last_utm_timestamp), utm_contents[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_content,
- if(isNotNull(last_utm_timestamp), utm_terms[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_term,
- if(isNotNull(last_utm_timestamp), utm_referring_domains[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_referring_domain,
- if(isNotNull(last_utm_timestamp), utm_gclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gclid,
- if(isNotNull(last_utm_timestamp), utm_fbclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_fbclid,
- if(isNotNull(last_utm_timestamp), utm_gad_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gad_source
- FROM
- (
- SELECT
- events.person_id,
- arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'sign_up'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps,
- arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'sign_up'), toFloat(1), 0))) AS conversion_math_values,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'sign_up'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'sign_up'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'sign_up'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'sign_up'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'sign_up'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'sign_up'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'sign_up'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'sign_up'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'sign_up'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources,
- arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
- FROM
- events
- WHERE
- or(and(equals(events.event, 'sign_up'), greaterOrEquals(events.timestamp, toDateTime('2023-01-01'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2023-01-01'), toIntervalSecond(7776000)))))
- GROUP BY
- events.person_id
- HAVING
- greater(length(conversion_timestamps), 0))
- ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversions
+ (
+ SELECT
+ events.person_id,
+ arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'sign_up'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps,
+ arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'sign_up'), toFloat(1), 0))) AS conversion_math_values,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'sign_up'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'sign_up'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'sign_up'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'sign_up'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'sign_up'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'sign_up'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'sign_up'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'sign_up'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'sign_up'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources,
+ arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
+ FROM
+ events
+ WHERE
+ or(and(equals(events.event, 'sign_up'), greaterOrEquals(events.timestamp, toDateTime('2023-01-01'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2023-01-01'), toIntervalSecond(7776000)))))
+ GROUP BY
+ events.person_id
+ HAVING
+ greater(length(conversion_timestamps), 0))
+ ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversions
GROUP BY
- campaign,
- id,
- source
+ campaign,
+ id,
+ source
LIMIT 100
'''
# ---
@@ -633,89 +633,89 @@
'''
SELECT
- if(notEmpty(campaign_name), campaign_name, 'organic') AS match_key,
- if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
- if(notEmpty(campaign_id), campaign_id, '-') AS id,
- if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
- count() AS conversion_1
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS match_key,
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
+ if(notEmpty(campaign_id), campaign_id, '-') AS id,
+ if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
+ count() AS conversion_1
FROM
- (
- SELECT
- person_id,
- if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name,
- if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name,
- if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name,
- if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name,
- if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name,
- if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name,
- if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name,
- if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name,
- if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name,
- '-' AS campaign_id,
- 1 AS conversion_value
+ (
+ SELECT
+ person_id,
+ if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name,
+ if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name,
+ if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name,
+ if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name,
+ if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name,
+ if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name,
+ if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name,
+ if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name,
+ if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name,
+ '-' AS campaign_id,
+ 1 AS conversion_value
+ FROM
+ (
+ SELECT
+ person_id,
+ conversion_timestamps[i] AS conversion_time,
+ conversion_math_values[i] AS conversion_math_value,
+ conversion_campaigns[i] AS conversion_campaign,
+ conversion_sources[i] AS conversion_source,
+ conversion_mediums[i] AS conversion_medium,
+ conversion_contents[i] AS conversion_content,
+ conversion_terms[i] AS conversion_term,
+ conversion_referring_domains[i] AS conversion_referring_domain,
+ conversion_gclids[i] AS conversion_gclid,
+ conversion_fbclids[i] AS conversion_fbclid,
+ conversion_gad_sources[i] AS conversion_gad_source,
+ arrayMax(arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 7776000))), utm_timestamps)) AS last_utm_timestamp,
+ if(isNotNull(last_utm_timestamp), utm_campaigns[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_campaign,
+ if(isNotNull(last_utm_timestamp), utm_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_source,
+ if(isNotNull(last_utm_timestamp), utm_mediums[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_medium,
+ if(isNotNull(last_utm_timestamp), utm_contents[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_content,
+ if(isNotNull(last_utm_timestamp), utm_terms[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_term,
+ if(isNotNull(last_utm_timestamp), utm_referring_domains[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_referring_domain,
+ if(isNotNull(last_utm_timestamp), utm_gclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gclid,
+ if(isNotNull(last_utm_timestamp), utm_fbclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_fbclid,
+ if(isNotNull(last_utm_timestamp), utm_gad_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gad_source
FROM
- (
- SELECT
- person_id,
- conversion_timestamps[i] AS conversion_time,
- conversion_math_values[i] AS conversion_math_value,
- conversion_campaigns[i] AS conversion_campaign,
- conversion_sources[i] AS conversion_source,
- conversion_mediums[i] AS conversion_medium,
- conversion_contents[i] AS conversion_content,
- conversion_terms[i] AS conversion_term,
- conversion_referring_domains[i] AS conversion_referring_domain,
- conversion_gclids[i] AS conversion_gclid,
- conversion_fbclids[i] AS conversion_fbclid,
- conversion_gad_sources[i] AS conversion_gad_source,
- arrayMax(arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 7776000))), utm_timestamps)) AS last_utm_timestamp,
- if(isNotNull(last_utm_timestamp), utm_campaigns[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_campaign,
- if(isNotNull(last_utm_timestamp), utm_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_source,
- if(isNotNull(last_utm_timestamp), utm_mediums[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_medium,
- if(isNotNull(last_utm_timestamp), utm_contents[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_content,
- if(isNotNull(last_utm_timestamp), utm_terms[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_term,
- if(isNotNull(last_utm_timestamp), utm_referring_domains[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_referring_domain,
- if(isNotNull(last_utm_timestamp), utm_gclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gclid,
- if(isNotNull(last_utm_timestamp), utm_fbclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_fbclid,
- if(isNotNull(last_utm_timestamp), utm_gad_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gad_source
- FROM
- (
- SELECT
- events.person_id,
- arrayFilter(x -> greater(x, 0), groupArray(if(or(equals(event, 'sign_up'), equals(event, 'activate_account')), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps,
- arrayFilter(x -> greater(x, 0), groupArray(if(or(equals(event, 'sign_up'), equals(event, 'activate_account')), toFloat(1), 0))) AS conversion_math_values,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(or(equals(event, 'sign_up'), equals(event, 'activate_account')), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(or(equals(event, 'sign_up'), equals(event, 'activate_account')), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(or(equals(event, 'sign_up'), equals(event, 'activate_account')), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(or(equals(event, 'sign_up'), equals(event, 'activate_account')), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(or(equals(event, 'sign_up'), equals(event, 'activate_account')), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(or(equals(event, 'sign_up'), equals(event, 'activate_account')), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(or(equals(event, 'sign_up'), equals(event, 'activate_account')), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(or(equals(event, 'sign_up'), equals(event, 'activate_account')), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(or(equals(event, 'sign_up'), equals(event, 'activate_account')), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources,
- arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
- FROM
- events
- WHERE
- or(and(or(equals(event, 'sign_up'), equals(event, 'activate_account')), greaterOrEquals(events.timestamp, toDateTime('2023-06-01')), lessOrEquals(events.timestamp, toDateTime('2023-06-02'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2023-06-01'), toIntervalSecond(7776000))), lessOrEquals(events.timestamp, toDateTime('2023-06-02'))))
- GROUP BY
- events.person_id
- HAVING
- greater(length(conversion_timestamps), 0))
- ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversions
+ (
+ SELECT
+ events.person_id,
+ arrayFilter(x -> greater(x, 0), groupArray(if(or(equals(event, 'sign_up'), equals(event, 'activate_account')), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps,
+ arrayFilter(x -> greater(x, 0), groupArray(if(or(equals(event, 'sign_up'), equals(event, 'activate_account')), toFloat(1), 0))) AS conversion_math_values,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(or(equals(event, 'sign_up'), equals(event, 'activate_account')), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(or(equals(event, 'sign_up'), equals(event, 'activate_account')), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(or(equals(event, 'sign_up'), equals(event, 'activate_account')), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(or(equals(event, 'sign_up'), equals(event, 'activate_account')), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(or(equals(event, 'sign_up'), equals(event, 'activate_account')), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(or(equals(event, 'sign_up'), equals(event, 'activate_account')), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(or(equals(event, 'sign_up'), equals(event, 'activate_account')), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(or(equals(event, 'sign_up'), equals(event, 'activate_account')), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(or(equals(event, 'sign_up'), equals(event, 'activate_account')), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources,
+ arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
+ FROM
+ events
+ WHERE
+ or(and(or(equals(event, 'sign_up'), equals(event, 'activate_account')), greaterOrEquals(events.timestamp, toDateTime('2023-06-01')), lessOrEquals(events.timestamp, toDateTime('2023-06-02'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2023-06-01'), toIntervalSecond(7776000))), lessOrEquals(events.timestamp, toDateTime('2023-06-02'))))
+ GROUP BY
+ events.person_id
+ HAVING
+ greater(length(conversion_timestamps), 0))
+ ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversions
GROUP BY
- campaign,
- id,
- source
+ campaign,
+ id,
+ source
LIMIT 100
'''
# ---
@@ -723,89 +723,89 @@
'''
SELECT
- if(notEmpty(campaign_name), campaign_name, 'organic') AS match_key,
- if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
- if(notEmpty(campaign_id), campaign_id, '-') AS id,
- if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
- coalesce(sum(conversion_value), 0) AS conversion_0
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS match_key,
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
+ if(notEmpty(campaign_id), campaign_id, '-') AS id,
+ if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
+ coalesce(sum(conversion_value), 0) AS conversion_0
FROM
- (
- SELECT
- person_id,
- if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name,
- if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name,
- if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name,
- if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name,
- if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name,
- if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name,
- if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name,
- if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name,
- if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name,
- '-' AS campaign_id,
- toFloat(conversion_math_value) AS conversion_value
+ (
+ SELECT
+ person_id,
+ if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name,
+ if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name,
+ if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name,
+ if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name,
+ if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name,
+ if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name,
+ if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name,
+ if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name,
+ if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name,
+ '-' AS campaign_id,
+ toFloat(conversion_math_value) AS conversion_value
+ FROM
+ (
+ SELECT
+ person_id,
+ conversion_timestamps[i] AS conversion_time,
+ conversion_math_values[i] AS conversion_math_value,
+ conversion_campaigns[i] AS conversion_campaign,
+ conversion_sources[i] AS conversion_source,
+ conversion_mediums[i] AS conversion_medium,
+ conversion_contents[i] AS conversion_content,
+ conversion_terms[i] AS conversion_term,
+ conversion_referring_domains[i] AS conversion_referring_domain,
+ conversion_gclids[i] AS conversion_gclid,
+ conversion_fbclids[i] AS conversion_fbclid,
+ conversion_gad_sources[i] AS conversion_gad_source,
+ arrayMax(arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 7776000))), utm_timestamps)) AS last_utm_timestamp,
+ if(isNotNull(last_utm_timestamp), utm_campaigns[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_campaign,
+ if(isNotNull(last_utm_timestamp), utm_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_source,
+ if(isNotNull(last_utm_timestamp), utm_mediums[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_medium,
+ if(isNotNull(last_utm_timestamp), utm_contents[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_content,
+ if(isNotNull(last_utm_timestamp), utm_terms[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_term,
+ if(isNotNull(last_utm_timestamp), utm_referring_domains[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_referring_domain,
+ if(isNotNull(last_utm_timestamp), utm_gclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gclid,
+ if(isNotNull(last_utm_timestamp), utm_fbclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_fbclid,
+ if(isNotNull(last_utm_timestamp), utm_gad_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gad_source
FROM
- (
- SELECT
- person_id,
- conversion_timestamps[i] AS conversion_time,
- conversion_math_values[i] AS conversion_math_value,
- conversion_campaigns[i] AS conversion_campaign,
- conversion_sources[i] AS conversion_source,
- conversion_mediums[i] AS conversion_medium,
- conversion_contents[i] AS conversion_content,
- conversion_terms[i] AS conversion_term,
- conversion_referring_domains[i] AS conversion_referring_domain,
- conversion_gclids[i] AS conversion_gclid,
- conversion_fbclids[i] AS conversion_fbclid,
- conversion_gad_sources[i] AS conversion_gad_source,
- arrayMax(arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 7776000))), utm_timestamps)) AS last_utm_timestamp,
- if(isNotNull(last_utm_timestamp), utm_campaigns[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_campaign,
- if(isNotNull(last_utm_timestamp), utm_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_source,
- if(isNotNull(last_utm_timestamp), utm_mediums[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_medium,
- if(isNotNull(last_utm_timestamp), utm_contents[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_content,
- if(isNotNull(last_utm_timestamp), utm_terms[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_term,
- if(isNotNull(last_utm_timestamp), utm_referring_domains[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_referring_domain,
- if(isNotNull(last_utm_timestamp), utm_gclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gclid,
- if(isNotNull(last_utm_timestamp), utm_fbclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_fbclid,
- if(isNotNull(last_utm_timestamp), utm_gad_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gad_source
- FROM
- (
- SELECT
- events.person_id,
- arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps,
- arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), coalesce(toFloat(events.properties.revenue), 0.0), 0))) AS conversion_math_values,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources,
- arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
- FROM
- events
- WHERE
- or(and(equals(events.event, 'purchase'), greaterOrEquals(events.timestamp, toDateTime('2023-01-01'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2023-01-01'), toIntervalSecond(7776000)))))
- GROUP BY
- events.person_id
- HAVING
- greater(length(conversion_timestamps), 0))
- ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversions
+ (
+ SELECT
+ events.person_id,
+ arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps,
+ arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), coalesce(toFloat(events.properties.revenue), 0.0), 0))) AS conversion_math_values,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources,
+ arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
+ FROM
+ events
+ WHERE
+ or(and(equals(events.event, 'purchase'), greaterOrEquals(events.timestamp, toDateTime('2023-01-01'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2023-01-01'), toIntervalSecond(7776000)))))
+ GROUP BY
+ events.person_id
+ HAVING
+ greater(length(conversion_timestamps), 0))
+ ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversions
GROUP BY
- campaign,
- id,
- source
+ campaign,
+ id,
+ source
LIMIT 100
'''
# ---
@@ -813,115 +813,115 @@
'''
SELECT
- if(notEmpty(campaign_name), campaign_name, 'organic') AS match_key,
- if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
- if(notEmpty(campaign_id), campaign_id, '-') AS id,
- if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
- coalesce(sum(conversion_value), 0) AS conversion_0
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS match_key,
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
+ if(notEmpty(campaign_id), campaign_id, '-') AS id,
+ if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
+ coalesce(sum(conversion_value), 0) AS conversion_0
FROM
+ (
+ SELECT
+ person_id,
+ if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name,
+ if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name,
+ if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name,
+ if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name,
+ if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name,
+ if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name,
+ if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name,
+ if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name,
+ if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name,
+ '-' AS campaign_id,
+ multiply(toFloat(conversion_math_value), attribution_weight) AS conversion_value
+ FROM
+ (
+ SELECT
+ person_id,
+ conversion_math_value,
+ conversion_campaign,
+ conversion_source,
+ conversion_medium,
+ conversion_content,
+ conversion_term,
+ conversion_referring_domain,
+ conversion_gclid,
+ conversion_fbclid,
+ conversion_gad_source,
+ filtered_utm_campaigns[tp_idx] AS fallback_campaign,
+ filtered_utm_sources[tp_idx] AS fallback_source,
+ filtered_utm_mediums[tp_idx] AS fallback_medium,
+ filtered_utm_contents[tp_idx] AS fallback_content,
+ filtered_utm_terms[tp_idx] AS fallback_term,
+ filtered_utm_referring_domains[tp_idx] AS fallback_referring_domain,
+ filtered_utm_gclids[tp_idx] AS fallback_gclid,
+ filtered_utm_fbclids[tp_idx] AS fallback_fbclid,
+ filtered_utm_gad_sources[tp_idx] AS fallback_gad_source,
+ attribution_weights[tp_idx] AS attribution_weight
+ FROM
(
- SELECT
+ SELECT
person_id,
- if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name,
- if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name,
- if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name,
- if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name,
- if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name,
- if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name,
- if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name,
- if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name,
- if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name,
- '-' AS campaign_id,
- multiply(toFloat(conversion_math_value), attribution_weight) AS conversion_value
- FROM
- (
- SELECT
- person_id,
- conversion_math_value,
- conversion_campaign,
- conversion_source,
- conversion_medium,
- conversion_content,
- conversion_term,
- conversion_referring_domain,
- conversion_gclid,
- conversion_fbclid,
- conversion_gad_source,
- filtered_utm_campaigns[tp_idx] AS fallback_campaign,
- filtered_utm_sources[tp_idx] AS fallback_source,
- filtered_utm_mediums[tp_idx] AS fallback_medium,
- filtered_utm_contents[tp_idx] AS fallback_content,
- filtered_utm_terms[tp_idx] AS fallback_term,
- filtered_utm_referring_domains[tp_idx] AS fallback_referring_domain,
- filtered_utm_gclids[tp_idx] AS fallback_gclid,
- filtered_utm_fbclids[tp_idx] AS fallback_fbclid,
- filtered_utm_gad_sources[tp_idx] AS fallback_gad_source,
- attribution_weights[tp_idx] AS attribution_weight
+ conversion_timestamps[i] AS conversion_time,
+ conversion_math_values[i] AS conversion_math_value,
+ conversion_campaigns[i] AS conversion_campaign,
+ conversion_sources[i] AS conversion_source,
+ conversion_mediums[i] AS conversion_medium,
+ conversion_contents[i] AS conversion_content,
+ conversion_terms[i] AS conversion_term,
+ conversion_referring_domains[i] AS conversion_referring_domain,
+ conversion_gclids[i] AS conversion_gclid,
+ conversion_fbclids[i] AS conversion_fbclid,
+ conversion_gad_sources[i] AS conversion_gad_source,
+ arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 7776000))), utm_timestamps) AS filtered_utm_timestamps,
+ arrayMap(idx -> utm_campaigns[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_campaigns,
+ arrayMap(idx -> utm_sources[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_sources,
+ arrayMap(idx -> utm_mediums[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_mediums,
+ arrayMap(idx -> utm_contents[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_contents,
+ arrayMap(idx -> utm_terms[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_terms,
+ arrayMap(idx -> utm_referring_domains[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_referring_domains,
+ arrayMap(idx -> utm_gclids[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_gclids,
+ arrayMap(idx -> utm_fbclids[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_fbclids,
+ arrayMap(idx -> utm_gad_sources[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_gad_sources,
+ arrayMap(_x -> divide(1.0, greatest(toFloat(length(filtered_utm_timestamps)), 1.0)), filtered_utm_timestamps) AS attribution_weights
+ FROM
+ (
+ SELECT
+ events.person_id,
+ arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps,
+ arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), coalesce(toFloat(events.properties.revenue), 0.0), 0))) AS conversion_math_values,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources,
+ arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
FROM
- (
- SELECT
- person_id,
- conversion_timestamps[i] AS conversion_time,
- conversion_math_values[i] AS conversion_math_value,
- conversion_campaigns[i] AS conversion_campaign,
- conversion_sources[i] AS conversion_source,
- conversion_mediums[i] AS conversion_medium,
- conversion_contents[i] AS conversion_content,
- conversion_terms[i] AS conversion_term,
- conversion_referring_domains[i] AS conversion_referring_domain,
- conversion_gclids[i] AS conversion_gclid,
- conversion_fbclids[i] AS conversion_fbclid,
- conversion_gad_sources[i] AS conversion_gad_source,
- arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 7776000))), utm_timestamps) AS filtered_utm_timestamps,
- arrayMap(idx -> utm_campaigns[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_campaigns,
- arrayMap(idx -> utm_sources[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_sources,
- arrayMap(idx -> utm_mediums[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_mediums,
- arrayMap(idx -> utm_contents[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_contents,
- arrayMap(idx -> utm_terms[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_terms,
- arrayMap(idx -> utm_referring_domains[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_referring_domains,
- arrayMap(idx -> utm_gclids[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_gclids,
- arrayMap(idx -> utm_fbclids[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_fbclids,
- arrayMap(idx -> utm_gad_sources[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_gad_sources,
- arrayMap(_x -> divide(1.0, greatest(toFloat(length(filtered_utm_timestamps)), 1.0)), filtered_utm_timestamps) AS attribution_weights
- FROM
- (
- SELECT
- events.person_id,
- arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps,
- arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), coalesce(toFloat(events.properties.revenue), 0.0), 0))) AS conversion_math_values,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources,
- arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
- FROM
- events
- WHERE
- or(and(equals(events.event, 'purchase'), greaterOrEquals(events.timestamp, toDateTime('2023-03-01')), lessOrEquals(events.timestamp, toDateTime('2023-03-31'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2023-03-01'), toIntervalSecond(7776000))), lessOrEquals(events.timestamp, toDateTime('2023-03-31'))))
- GROUP BY
- events.person_id
- HAVING
- greater(length(conversion_timestamps), 0))
- ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)
- ARRAY JOIN arrayEnumerate(attribution_weights) AS tp_idx)) AS attributed_conversions
+ events
+ WHERE
+ or(and(equals(events.event, 'purchase'), greaterOrEquals(events.timestamp, toDateTime('2023-03-01')), lessOrEquals(events.timestamp, toDateTime('2023-03-31'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2023-03-01'), toIntervalSecond(7776000))), lessOrEquals(events.timestamp, toDateTime('2023-03-31'))))
+ GROUP BY
+ events.person_id
+ HAVING
+ greater(length(conversion_timestamps), 0))
+ ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)
+ ARRAY JOIN arrayEnumerate(attribution_weights) AS tp_idx)) AS attributed_conversions
GROUP BY
- campaign,
- id,
- source
+ campaign,
+ id,
+ source
LIMIT 100
'''
# ---
@@ -929,115 +929,115 @@
'''
SELECT
- if(notEmpty(campaign_name), campaign_name, 'organic') AS match_key,
- if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
- if(notEmpty(campaign_id), campaign_id, '-') AS id,
- if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
- coalesce(sum(conversion_value), 0) AS conversion_0
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS match_key,
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
+ if(notEmpty(campaign_id), campaign_id, '-') AS id,
+ if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
+ coalesce(sum(conversion_value), 0) AS conversion_0
FROM
+ (
+ SELECT
+ person_id,
+ if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name,
+ if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name,
+ if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name,
+ if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name,
+ if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name,
+ if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name,
+ if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name,
+ if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name,
+ if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name,
+ '-' AS campaign_id,
+ multiply(1, attribution_weight) AS conversion_value
+ FROM
+ (
+ SELECT
+ person_id,
+ conversion_math_value,
+ conversion_campaign,
+ conversion_source,
+ conversion_medium,
+ conversion_content,
+ conversion_term,
+ conversion_referring_domain,
+ conversion_gclid,
+ conversion_fbclid,
+ conversion_gad_source,
+ filtered_utm_campaigns[tp_idx] AS fallback_campaign,
+ filtered_utm_sources[tp_idx] AS fallback_source,
+ filtered_utm_mediums[tp_idx] AS fallback_medium,
+ filtered_utm_contents[tp_idx] AS fallback_content,
+ filtered_utm_terms[tp_idx] AS fallback_term,
+ filtered_utm_referring_domains[tp_idx] AS fallback_referring_domain,
+ filtered_utm_gclids[tp_idx] AS fallback_gclid,
+ filtered_utm_fbclids[tp_idx] AS fallback_fbclid,
+ filtered_utm_gad_sources[tp_idx] AS fallback_gad_source,
+ attribution_weights[tp_idx] AS attribution_weight
+ FROM
(
- SELECT
+ SELECT
person_id,
- if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name,
- if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name,
- if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name,
- if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name,
- if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name,
- if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name,
- if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name,
- if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name,
- if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name,
- '-' AS campaign_id,
- multiply(1, attribution_weight) AS conversion_value
- FROM
- (
- SELECT
- person_id,
- conversion_math_value,
- conversion_campaign,
- conversion_source,
- conversion_medium,
- conversion_content,
- conversion_term,
- conversion_referring_domain,
- conversion_gclid,
- conversion_fbclid,
- conversion_gad_source,
- filtered_utm_campaigns[tp_idx] AS fallback_campaign,
- filtered_utm_sources[tp_idx] AS fallback_source,
- filtered_utm_mediums[tp_idx] AS fallback_medium,
- filtered_utm_contents[tp_idx] AS fallback_content,
- filtered_utm_terms[tp_idx] AS fallback_term,
- filtered_utm_referring_domains[tp_idx] AS fallback_referring_domain,
- filtered_utm_gclids[tp_idx] AS fallback_gclid,
- filtered_utm_fbclids[tp_idx] AS fallback_fbclid,
- filtered_utm_gad_sources[tp_idx] AS fallback_gad_source,
- attribution_weights[tp_idx] AS attribution_weight
+ conversion_timestamps[i] AS conversion_time,
+ conversion_math_values[i] AS conversion_math_value,
+ conversion_campaigns[i] AS conversion_campaign,
+ conversion_sources[i] AS conversion_source,
+ conversion_mediums[i] AS conversion_medium,
+ conversion_contents[i] AS conversion_content,
+ conversion_terms[i] AS conversion_term,
+ conversion_referring_domains[i] AS conversion_referring_domain,
+ conversion_gclids[i] AS conversion_gclid,
+ conversion_fbclids[i] AS conversion_fbclid,
+ conversion_gad_sources[i] AS conversion_gad_source,
+ arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 7776000))), utm_timestamps) AS filtered_utm_timestamps,
+ arrayMap(idx -> utm_campaigns[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_campaigns,
+ arrayMap(idx -> utm_sources[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_sources,
+ arrayMap(idx -> utm_mediums[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_mediums,
+ arrayMap(idx -> utm_contents[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_contents,
+ arrayMap(idx -> utm_terms[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_terms,
+ arrayMap(idx -> utm_referring_domains[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_referring_domains,
+ arrayMap(idx -> utm_gclids[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_gclids,
+ arrayMap(idx -> utm_fbclids[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_fbclids,
+ arrayMap(idx -> utm_gad_sources[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_gad_sources,
+ arrayMap(_x -> divide(1.0, greatest(toFloat(length(filtered_utm_timestamps)), 1.0)), filtered_utm_timestamps) AS attribution_weights
+ FROM
+ (
+ SELECT
+ events.person_id,
+ arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps,
+ arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toFloat(1), 0))) AS conversion_math_values,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources,
+ arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
FROM
- (
- SELECT
- person_id,
- conversion_timestamps[i] AS conversion_time,
- conversion_math_values[i] AS conversion_math_value,
- conversion_campaigns[i] AS conversion_campaign,
- conversion_sources[i] AS conversion_source,
- conversion_mediums[i] AS conversion_medium,
- conversion_contents[i] AS conversion_content,
- conversion_terms[i] AS conversion_term,
- conversion_referring_domains[i] AS conversion_referring_domain,
- conversion_gclids[i] AS conversion_gclid,
- conversion_fbclids[i] AS conversion_fbclid,
- conversion_gad_sources[i] AS conversion_gad_source,
- arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 7776000))), utm_timestamps) AS filtered_utm_timestamps,
- arrayMap(idx -> utm_campaigns[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_campaigns,
- arrayMap(idx -> utm_sources[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_sources,
- arrayMap(idx -> utm_mediums[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_mediums,
- arrayMap(idx -> utm_contents[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_contents,
- arrayMap(idx -> utm_terms[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_terms,
- arrayMap(idx -> utm_referring_domains[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_referring_domains,
- arrayMap(idx -> utm_gclids[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_gclids,
- arrayMap(idx -> utm_fbclids[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_fbclids,
- arrayMap(idx -> utm_gad_sources[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_gad_sources,
- arrayMap(_x -> divide(1.0, greatest(toFloat(length(filtered_utm_timestamps)), 1.0)), filtered_utm_timestamps) AS attribution_weights
- FROM
- (
- SELECT
- events.person_id,
- arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps,
- arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toFloat(1), 0))) AS conversion_math_values,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources,
- arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
- FROM
- events
- WHERE
- or(and(equals(events.event, 'purchase'), greaterOrEquals(events.timestamp, toDateTime('2023-03-01')), lessOrEquals(events.timestamp, toDateTime('2023-03-31'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2023-03-01'), toIntervalSecond(7776000))), lessOrEquals(events.timestamp, toDateTime('2023-03-31'))))
- GROUP BY
- events.person_id
- HAVING
- greater(length(conversion_timestamps), 0))
- ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)
- ARRAY JOIN arrayEnumerate(attribution_weights) AS tp_idx)) AS attributed_conversions
+ events
+ WHERE
+ or(and(equals(events.event, 'purchase'), greaterOrEquals(events.timestamp, toDateTime('2023-03-01')), lessOrEquals(events.timestamp, toDateTime('2023-03-31'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2023-03-01'), toIntervalSecond(7776000))), lessOrEquals(events.timestamp, toDateTime('2023-03-31'))))
+ GROUP BY
+ events.person_id
+ HAVING
+ greater(length(conversion_timestamps), 0))
+ ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)
+ ARRAY JOIN arrayEnumerate(attribution_weights) AS tp_idx)) AS attributed_conversions
GROUP BY
- campaign,
- id,
- source
+ campaign,
+ id,
+ source
LIMIT 100
'''
# ---
@@ -1045,115 +1045,115 @@
'''
SELECT
- if(notEmpty(campaign_name), campaign_name, 'organic') AS match_key,
- if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
- if(notEmpty(campaign_id), campaign_id, '-') AS id,
- if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
- coalesce(sum(conversion_value), 0) AS conversion_0
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS match_key,
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
+ if(notEmpty(campaign_id), campaign_id, '-') AS id,
+ if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
+ coalesce(sum(conversion_value), 0) AS conversion_0
FROM
+ (
+ SELECT
+ person_id,
+ if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name,
+ if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name,
+ if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name,
+ if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name,
+ if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name,
+ if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name,
+ if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name,
+ if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name,
+ if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name,
+ '-' AS campaign_id,
+ multiply(1, attribution_weight) AS conversion_value
+ FROM
+ (
+ SELECT
+ person_id,
+ conversion_math_value,
+ conversion_campaign,
+ conversion_source,
+ conversion_medium,
+ conversion_content,
+ conversion_term,
+ conversion_referring_domain,
+ conversion_gclid,
+ conversion_fbclid,
+ conversion_gad_source,
+ filtered_utm_campaigns[tp_idx] AS fallback_campaign,
+ filtered_utm_sources[tp_idx] AS fallback_source,
+ filtered_utm_mediums[tp_idx] AS fallback_medium,
+ filtered_utm_contents[tp_idx] AS fallback_content,
+ filtered_utm_terms[tp_idx] AS fallback_term,
+ filtered_utm_referring_domains[tp_idx] AS fallback_referring_domain,
+ filtered_utm_gclids[tp_idx] AS fallback_gclid,
+ filtered_utm_fbclids[tp_idx] AS fallback_fbclid,
+ filtered_utm_gad_sources[tp_idx] AS fallback_gad_source,
+ attribution_weights[tp_idx] AS attribution_weight
+ FROM
(
- SELECT
+ SELECT
person_id,
- if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name,
- if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name,
- if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name,
- if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name,
- if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name,
- if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name,
- if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name,
- if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name,
- if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name,
- '-' AS campaign_id,
- multiply(1, attribution_weight) AS conversion_value
- FROM
- (
- SELECT
- person_id,
- conversion_math_value,
- conversion_campaign,
- conversion_source,
- conversion_medium,
- conversion_content,
- conversion_term,
- conversion_referring_domain,
- conversion_gclid,
- conversion_fbclid,
- conversion_gad_source,
- filtered_utm_campaigns[tp_idx] AS fallback_campaign,
- filtered_utm_sources[tp_idx] AS fallback_source,
- filtered_utm_mediums[tp_idx] AS fallback_medium,
- filtered_utm_contents[tp_idx] AS fallback_content,
- filtered_utm_terms[tp_idx] AS fallback_term,
- filtered_utm_referring_domains[tp_idx] AS fallback_referring_domain,
- filtered_utm_gclids[tp_idx] AS fallback_gclid,
- filtered_utm_fbclids[tp_idx] AS fallback_fbclid,
- filtered_utm_gad_sources[tp_idx] AS fallback_gad_source,
- attribution_weights[tp_idx] AS attribution_weight
+ conversion_timestamps[i] AS conversion_time,
+ conversion_math_values[i] AS conversion_math_value,
+ conversion_campaigns[i] AS conversion_campaign,
+ conversion_sources[i] AS conversion_source,
+ conversion_mediums[i] AS conversion_medium,
+ conversion_contents[i] AS conversion_content,
+ conversion_terms[i] AS conversion_term,
+ conversion_referring_domains[i] AS conversion_referring_domain,
+ conversion_gclids[i] AS conversion_gclid,
+ conversion_fbclids[i] AS conversion_fbclid,
+ conversion_gad_sources[i] AS conversion_gad_source,
+ arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 7776000))), utm_timestamps) AS filtered_utm_timestamps,
+ arrayMap(idx -> utm_campaigns[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_campaigns,
+ arrayMap(idx -> utm_sources[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_sources,
+ arrayMap(idx -> utm_mediums[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_mediums,
+ arrayMap(idx -> utm_contents[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_contents,
+ arrayMap(idx -> utm_terms[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_terms,
+ arrayMap(idx -> utm_referring_domains[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_referring_domains,
+ arrayMap(idx -> utm_gclids[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_gclids,
+ arrayMap(idx -> utm_fbclids[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_fbclids,
+ arrayMap(idx -> utm_gad_sources[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_gad_sources,
+ if(equals(length(filtered_utm_timestamps), 0), [], if(equals(length(filtered_utm_timestamps), 1), [1.0], if(equals(length(filtered_utm_timestamps), 2), [0.5, 0.5], arrayMap(w -> divide(w, greatest(arraySum(arrayMap(_pw -> ifNull(_pw, 0.0), arrayMap(ts -> if(equals(ts, arrayMin(filtered_utm_timestamps)), 0.4, if(equals(ts, arrayMax(filtered_utm_timestamps)), 0.4, divide(0.2, greatest(minus(toFloat(length(filtered_utm_timestamps)), 2.0), 1.0)))), filtered_utm_timestamps))), 1e-06)), arrayMap(_pw -> ifNull(_pw, 0.0), arrayMap(ts -> if(equals(ts, arrayMin(filtered_utm_timestamps)), 0.4, if(equals(ts, arrayMax(filtered_utm_timestamps)), 0.4, divide(0.2, greatest(minus(toFloat(length(filtered_utm_timestamps)), 2.0), 1.0)))), filtered_utm_timestamps)))))) AS attribution_weights
+ FROM
+ (
+ SELECT
+ events.person_id,
+ arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps,
+ arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toFloat(1), 0))) AS conversion_math_values,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources,
+ arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
FROM
- (
- SELECT
- person_id,
- conversion_timestamps[i] AS conversion_time,
- conversion_math_values[i] AS conversion_math_value,
- conversion_campaigns[i] AS conversion_campaign,
- conversion_sources[i] AS conversion_source,
- conversion_mediums[i] AS conversion_medium,
- conversion_contents[i] AS conversion_content,
- conversion_terms[i] AS conversion_term,
- conversion_referring_domains[i] AS conversion_referring_domain,
- conversion_gclids[i] AS conversion_gclid,
- conversion_fbclids[i] AS conversion_fbclid,
- conversion_gad_sources[i] AS conversion_gad_source,
- arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 7776000))), utm_timestamps) AS filtered_utm_timestamps,
- arrayMap(idx -> utm_campaigns[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_campaigns,
- arrayMap(idx -> utm_sources[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_sources,
- arrayMap(idx -> utm_mediums[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_mediums,
- arrayMap(idx -> utm_contents[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_contents,
- arrayMap(idx -> utm_terms[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_terms,
- arrayMap(idx -> utm_referring_domains[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_referring_domains,
- arrayMap(idx -> utm_gclids[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_gclids,
- arrayMap(idx -> utm_fbclids[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_fbclids,
- arrayMap(idx -> utm_gad_sources[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_gad_sources,
- if(equals(length(filtered_utm_timestamps), 0), [], if(equals(length(filtered_utm_timestamps), 1), [1.0], if(equals(length(filtered_utm_timestamps), 2), [0.5, 0.5], arrayMap(w -> divide(w, greatest(arraySum(arrayMap(_pw -> ifNull(_pw, 0.0), arrayMap(ts -> if(equals(ts, arrayMin(filtered_utm_timestamps)), 0.4, if(equals(ts, arrayMax(filtered_utm_timestamps)), 0.4, divide(0.2, greatest(minus(toFloat(length(filtered_utm_timestamps)), 2.0), 1.0)))), filtered_utm_timestamps))), 1e-06)), arrayMap(_pw -> ifNull(_pw, 0.0), arrayMap(ts -> if(equals(ts, arrayMin(filtered_utm_timestamps)), 0.4, if(equals(ts, arrayMax(filtered_utm_timestamps)), 0.4, divide(0.2, greatest(minus(toFloat(length(filtered_utm_timestamps)), 2.0), 1.0)))), filtered_utm_timestamps)))))) AS attribution_weights
- FROM
- (
- SELECT
- events.person_id,
- arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps,
- arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toFloat(1), 0))) AS conversion_math_values,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources,
- arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
- FROM
- events
- WHERE
- or(and(equals(events.event, 'purchase'), greaterOrEquals(events.timestamp, toDateTime('2023-03-01')), lessOrEquals(events.timestamp, toDateTime('2023-03-31'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2023-03-01'), toIntervalSecond(7776000))), lessOrEquals(events.timestamp, toDateTime('2023-03-31'))))
- GROUP BY
- events.person_id
- HAVING
- greater(length(conversion_timestamps), 0))
- ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)
- ARRAY JOIN arrayEnumerate(attribution_weights) AS tp_idx)) AS attributed_conversions
+ events
+ WHERE
+ or(and(equals(events.event, 'purchase'), greaterOrEquals(events.timestamp, toDateTime('2023-03-01')), lessOrEquals(events.timestamp, toDateTime('2023-03-31'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2023-03-01'), toIntervalSecond(7776000))), lessOrEquals(events.timestamp, toDateTime('2023-03-31'))))
+ GROUP BY
+ events.person_id
+ HAVING
+ greater(length(conversion_timestamps), 0))
+ ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)
+ ARRAY JOIN arrayEnumerate(attribution_weights) AS tp_idx)) AS attributed_conversions
GROUP BY
- campaign,
- id,
- source
+ campaign,
+ id,
+ source
LIMIT 100
'''
# ---
@@ -1161,115 +1161,115 @@
'''
SELECT
- if(notEmpty(campaign_name), campaign_name, 'organic') AS match_key,
- if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
- if(notEmpty(campaign_id), campaign_id, '-') AS id,
- if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
- coalesce(sum(conversion_value), 0) AS conversion_0
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS match_key,
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
+ if(notEmpty(campaign_id), campaign_id, '-') AS id,
+ if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
+ coalesce(sum(conversion_value), 0) AS conversion_0
FROM
+ (
+ SELECT
+ person_id,
+ if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name,
+ if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name,
+ if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name,
+ if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name,
+ if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name,
+ if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name,
+ if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name,
+ if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name,
+ if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name,
+ '-' AS campaign_id,
+ multiply(1, attribution_weight) AS conversion_value
+ FROM
+ (
+ SELECT
+ person_id,
+ conversion_math_value,
+ conversion_campaign,
+ conversion_source,
+ conversion_medium,
+ conversion_content,
+ conversion_term,
+ conversion_referring_domain,
+ conversion_gclid,
+ conversion_fbclid,
+ conversion_gad_source,
+ filtered_utm_campaigns[tp_idx] AS fallback_campaign,
+ filtered_utm_sources[tp_idx] AS fallback_source,
+ filtered_utm_mediums[tp_idx] AS fallback_medium,
+ filtered_utm_contents[tp_idx] AS fallback_content,
+ filtered_utm_terms[tp_idx] AS fallback_term,
+ filtered_utm_referring_domains[tp_idx] AS fallback_referring_domain,
+ filtered_utm_gclids[tp_idx] AS fallback_gclid,
+ filtered_utm_fbclids[tp_idx] AS fallback_fbclid,
+ filtered_utm_gad_sources[tp_idx] AS fallback_gad_source,
+ attribution_weights[tp_idx] AS attribution_weight
+ FROM
(
- SELECT
+ SELECT
person_id,
- if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name,
- if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name,
- if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name,
- if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name,
- if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name,
- if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name,
- if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name,
- if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name,
- if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name,
- '-' AS campaign_id,
- multiply(1, attribution_weight) AS conversion_value
- FROM
- (
- SELECT
- person_id,
- conversion_math_value,
- conversion_campaign,
- conversion_source,
- conversion_medium,
- conversion_content,
- conversion_term,
- conversion_referring_domain,
- conversion_gclid,
- conversion_fbclid,
- conversion_gad_source,
- filtered_utm_campaigns[tp_idx] AS fallback_campaign,
- filtered_utm_sources[tp_idx] AS fallback_source,
- filtered_utm_mediums[tp_idx] AS fallback_medium,
- filtered_utm_contents[tp_idx] AS fallback_content,
- filtered_utm_terms[tp_idx] AS fallback_term,
- filtered_utm_referring_domains[tp_idx] AS fallback_referring_domain,
- filtered_utm_gclids[tp_idx] AS fallback_gclid,
- filtered_utm_fbclids[tp_idx] AS fallback_fbclid,
- filtered_utm_gad_sources[tp_idx] AS fallback_gad_source,
- attribution_weights[tp_idx] AS attribution_weight
+ conversion_timestamps[i] AS conversion_time,
+ conversion_math_values[i] AS conversion_math_value,
+ conversion_campaigns[i] AS conversion_campaign,
+ conversion_sources[i] AS conversion_source,
+ conversion_mediums[i] AS conversion_medium,
+ conversion_contents[i] AS conversion_content,
+ conversion_terms[i] AS conversion_term,
+ conversion_referring_domains[i] AS conversion_referring_domain,
+ conversion_gclids[i] AS conversion_gclid,
+ conversion_fbclids[i] AS conversion_fbclid,
+ conversion_gad_sources[i] AS conversion_gad_source,
+ arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 7776000))), utm_timestamps) AS filtered_utm_timestamps,
+ arrayMap(idx -> utm_campaigns[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_campaigns,
+ arrayMap(idx -> utm_sources[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_sources,
+ arrayMap(idx -> utm_mediums[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_mediums,
+ arrayMap(idx -> utm_contents[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_contents,
+ arrayMap(idx -> utm_terms[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_terms,
+ arrayMap(idx -> utm_referring_domains[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_referring_domains,
+ arrayMap(idx -> utm_gclids[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_gclids,
+ arrayMap(idx -> utm_fbclids[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_fbclids,
+ arrayMap(idx -> utm_gad_sources[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_gad_sources,
+ arrayMap(w -> divide(w, greatest(arraySum(arrayMap(ts -> exp(multiply(-0.6931471805599453, divide(minus(conversion_timestamps[i], ts), 1944000))), filtered_utm_timestamps)), 1e-06)), arrayMap(ts -> exp(multiply(-0.6931471805599453, divide(minus(conversion_timestamps[i], ts), 1944000))), filtered_utm_timestamps)) AS attribution_weights
+ FROM
+ (
+ SELECT
+ events.person_id,
+ arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps,
+ arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toFloat(1), 0))) AS conversion_math_values,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources,
+ arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
FROM
- (
- SELECT
- person_id,
- conversion_timestamps[i] AS conversion_time,
- conversion_math_values[i] AS conversion_math_value,
- conversion_campaigns[i] AS conversion_campaign,
- conversion_sources[i] AS conversion_source,
- conversion_mediums[i] AS conversion_medium,
- conversion_contents[i] AS conversion_content,
- conversion_terms[i] AS conversion_term,
- conversion_referring_domains[i] AS conversion_referring_domain,
- conversion_gclids[i] AS conversion_gclid,
- conversion_fbclids[i] AS conversion_fbclid,
- conversion_gad_sources[i] AS conversion_gad_source,
- arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 7776000))), utm_timestamps) AS filtered_utm_timestamps,
- arrayMap(idx -> utm_campaigns[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_campaigns,
- arrayMap(idx -> utm_sources[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_sources,
- arrayMap(idx -> utm_mediums[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_mediums,
- arrayMap(idx -> utm_contents[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_contents,
- arrayMap(idx -> utm_terms[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_terms,
- arrayMap(idx -> utm_referring_domains[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_referring_domains,
- arrayMap(idx -> utm_gclids[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_gclids,
- arrayMap(idx -> utm_fbclids[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_fbclids,
- arrayMap(idx -> utm_gad_sources[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_gad_sources,
- arrayMap(w -> divide(w, greatest(arraySum(arrayMap(ts -> exp(multiply(-0.6931471805599453, divide(minus(conversion_timestamps[i], ts), 1944000))), filtered_utm_timestamps)), 1e-06)), arrayMap(ts -> exp(multiply(-0.6931471805599453, divide(minus(conversion_timestamps[i], ts), 1944000))), filtered_utm_timestamps)) AS attribution_weights
- FROM
- (
- SELECT
- events.person_id,
- arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps,
- arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toFloat(1), 0))) AS conversion_math_values,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources,
- arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
- FROM
- events
- WHERE
- or(and(equals(events.event, 'purchase'), greaterOrEquals(events.timestamp, toDateTime('2023-03-01')), lessOrEquals(events.timestamp, toDateTime('2023-03-31'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2023-03-01'), toIntervalSecond(7776000))), lessOrEquals(events.timestamp, toDateTime('2023-03-31'))))
- GROUP BY
- events.person_id
- HAVING
- greater(length(conversion_timestamps), 0))
- ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)
- ARRAY JOIN arrayEnumerate(attribution_weights) AS tp_idx)) AS attributed_conversions
+ events
+ WHERE
+ or(and(equals(events.event, 'purchase'), greaterOrEquals(events.timestamp, toDateTime('2023-03-01')), lessOrEquals(events.timestamp, toDateTime('2023-03-31'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2023-03-01'), toIntervalSecond(7776000))), lessOrEquals(events.timestamp, toDateTime('2023-03-31'))))
+ GROUP BY
+ events.person_id
+ HAVING
+ greater(length(conversion_timestamps), 0))
+ ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)
+ ARRAY JOIN arrayEnumerate(attribution_weights) AS tp_idx)) AS attributed_conversions
GROUP BY
- campaign,
- id,
- source
+ campaign,
+ id,
+ source
LIMIT 100
'''
# ---
@@ -1277,89 +1277,89 @@
'''
SELECT
- if(notEmpty(campaign_name), campaign_name, 'organic') AS match_key,
- if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
- if(notEmpty(campaign_id), campaign_id, '-') AS id,
- if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
- count() AS conversion_0
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS match_key,
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
+ if(notEmpty(campaign_id), campaign_id, '-') AS id,
+ if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
+ count() AS conversion_0
FROM
- (
- SELECT
- person_id,
- if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name,
- if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name,
- if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name,
- if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name,
- if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name,
- if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name,
- if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name,
- if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name,
- if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name,
- '-' AS campaign_id,
- 1 AS conversion_value
+ (
+ SELECT
+ person_id,
+ if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name,
+ if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name,
+ if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name,
+ if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name,
+ if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name,
+ if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name,
+ if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name,
+ if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name,
+ if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name,
+ '-' AS campaign_id,
+ 1 AS conversion_value
+ FROM
+ (
+ SELECT
+ person_id,
+ conversion_timestamps[i] AS conversion_time,
+ conversion_math_values[i] AS conversion_math_value,
+ conversion_campaigns[i] AS conversion_campaign,
+ conversion_sources[i] AS conversion_source,
+ conversion_mediums[i] AS conversion_medium,
+ conversion_contents[i] AS conversion_content,
+ conversion_terms[i] AS conversion_term,
+ conversion_referring_domains[i] AS conversion_referring_domain,
+ conversion_gclids[i] AS conversion_gclid,
+ conversion_fbclids[i] AS conversion_fbclid,
+ conversion_gad_sources[i] AS conversion_gad_source,
+ arrayMax(arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 7776000))), utm_timestamps)) AS last_utm_timestamp,
+ if(isNotNull(last_utm_timestamp), utm_campaigns[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_campaign,
+ if(isNotNull(last_utm_timestamp), utm_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_source,
+ if(isNotNull(last_utm_timestamp), utm_mediums[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_medium,
+ if(isNotNull(last_utm_timestamp), utm_contents[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_content,
+ if(isNotNull(last_utm_timestamp), utm_terms[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_term,
+ if(isNotNull(last_utm_timestamp), utm_referring_domains[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_referring_domain,
+ if(isNotNull(last_utm_timestamp), utm_gclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gclid,
+ if(isNotNull(last_utm_timestamp), utm_fbclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_fbclid,
+ if(isNotNull(last_utm_timestamp), utm_gad_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gad_source
FROM
- (
- SELECT
- person_id,
- conversion_timestamps[i] AS conversion_time,
- conversion_math_values[i] AS conversion_math_value,
- conversion_campaigns[i] AS conversion_campaign,
- conversion_sources[i] AS conversion_source,
- conversion_mediums[i] AS conversion_medium,
- conversion_contents[i] AS conversion_content,
- conversion_terms[i] AS conversion_term,
- conversion_referring_domains[i] AS conversion_referring_domain,
- conversion_gclids[i] AS conversion_gclid,
- conversion_fbclids[i] AS conversion_fbclid,
- conversion_gad_sources[i] AS conversion_gad_source,
- arrayMax(arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 7776000))), utm_timestamps)) AS last_utm_timestamp,
- if(isNotNull(last_utm_timestamp), utm_campaigns[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_campaign,
- if(isNotNull(last_utm_timestamp), utm_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_source,
- if(isNotNull(last_utm_timestamp), utm_mediums[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_medium,
- if(isNotNull(last_utm_timestamp), utm_contents[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_content,
- if(isNotNull(last_utm_timestamp), utm_terms[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_term,
- if(isNotNull(last_utm_timestamp), utm_referring_domains[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_referring_domain,
- if(isNotNull(last_utm_timestamp), utm_gclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gclid,
- if(isNotNull(last_utm_timestamp), utm_fbclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_fbclid,
- if(isNotNull(last_utm_timestamp), utm_gad_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gad_source
- FROM
- (
- SELECT
- events.person_id,
- arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps,
- arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toFloat(1), 0))) AS conversion_math_values,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources,
- arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
- FROM
- events
- WHERE
- or(and(equals(events.event, 'purchase'), greaterOrEquals(events.timestamp, toDateTime('2023-04-01'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2023-04-01'), toIntervalSecond(7776000)))))
- GROUP BY
- events.person_id
- HAVING
- greater(length(conversion_timestamps), 0))
- ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversions
+ (
+ SELECT
+ events.person_id,
+ arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps,
+ arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toFloat(1), 0))) AS conversion_math_values,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources,
+ arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
+ FROM
+ events
+ WHERE
+ or(and(equals(events.event, 'purchase'), greaterOrEquals(events.timestamp, toDateTime('2023-04-01'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2023-04-01'), toIntervalSecond(7776000)))))
+ GROUP BY
+ events.person_id
+ HAVING
+ greater(length(conversion_timestamps), 0))
+ ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversions
GROUP BY
- campaign,
- id,
- source
+ campaign,
+ id,
+ source
LIMIT 100
'''
# ---
@@ -1367,89 +1367,89 @@
'''
SELECT
- if(notEmpty(campaign_name), campaign_name, 'organic') AS match_key,
- if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
- if(notEmpty(campaign_id), campaign_id, '-') AS id,
- if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
- count() AS conversion_0
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS match_key,
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
+ if(notEmpty(campaign_id), campaign_id, '-') AS id,
+ if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
+ count() AS conversion_0
FROM
- (
- SELECT
- person_id,
- if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name,
- if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name,
- if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name,
- if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name,
- if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name,
- if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name,
- if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name,
- if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name,
- if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name,
- '-' AS campaign_id,
- 1 AS conversion_value
+ (
+ SELECT
+ person_id,
+ if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name,
+ if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name,
+ if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name,
+ if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name,
+ if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name,
+ if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name,
+ if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name,
+ if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name,
+ if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name,
+ '-' AS campaign_id,
+ 1 AS conversion_value
+ FROM
+ (
+ SELECT
+ person_id,
+ conversion_timestamps[i] AS conversion_time,
+ conversion_math_values[i] AS conversion_math_value,
+ conversion_campaigns[i] AS conversion_campaign,
+ conversion_sources[i] AS conversion_source,
+ conversion_mediums[i] AS conversion_medium,
+ conversion_contents[i] AS conversion_content,
+ conversion_terms[i] AS conversion_term,
+ conversion_referring_domains[i] AS conversion_referring_domain,
+ conversion_gclids[i] AS conversion_gclid,
+ conversion_fbclids[i] AS conversion_fbclid,
+ conversion_gad_sources[i] AS conversion_gad_source,
+ arrayMax(arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 7776000))), utm_timestamps)) AS last_utm_timestamp,
+ if(isNotNull(last_utm_timestamp), utm_campaigns[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_campaign,
+ if(isNotNull(last_utm_timestamp), utm_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_source,
+ if(isNotNull(last_utm_timestamp), utm_mediums[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_medium,
+ if(isNotNull(last_utm_timestamp), utm_contents[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_content,
+ if(isNotNull(last_utm_timestamp), utm_terms[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_term,
+ if(isNotNull(last_utm_timestamp), utm_referring_domains[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_referring_domain,
+ if(isNotNull(last_utm_timestamp), utm_gclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gclid,
+ if(isNotNull(last_utm_timestamp), utm_fbclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_fbclid,
+ if(isNotNull(last_utm_timestamp), utm_gad_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gad_source
FROM
- (
- SELECT
- person_id,
- conversion_timestamps[i] AS conversion_time,
- conversion_math_values[i] AS conversion_math_value,
- conversion_campaigns[i] AS conversion_campaign,
- conversion_sources[i] AS conversion_source,
- conversion_mediums[i] AS conversion_medium,
- conversion_contents[i] AS conversion_content,
- conversion_terms[i] AS conversion_term,
- conversion_referring_domains[i] AS conversion_referring_domain,
- conversion_gclids[i] AS conversion_gclid,
- conversion_fbclids[i] AS conversion_fbclid,
- conversion_gad_sources[i] AS conversion_gad_source,
- arrayMax(arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 7776000))), utm_timestamps)) AS last_utm_timestamp,
- if(isNotNull(last_utm_timestamp), utm_campaigns[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_campaign,
- if(isNotNull(last_utm_timestamp), utm_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_source,
- if(isNotNull(last_utm_timestamp), utm_mediums[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_medium,
- if(isNotNull(last_utm_timestamp), utm_contents[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_content,
- if(isNotNull(last_utm_timestamp), utm_terms[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_term,
- if(isNotNull(last_utm_timestamp), utm_referring_domains[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_referring_domain,
- if(isNotNull(last_utm_timestamp), utm_gclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gclid,
- if(isNotNull(last_utm_timestamp), utm_fbclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_fbclid,
- if(isNotNull(last_utm_timestamp), utm_gad_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gad_source
- FROM
- (
- SELECT
- events.person_id,
- arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps,
- arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toFloat(1), 0))) AS conversion_math_values,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources,
- arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
- FROM
- events
- WHERE
- or(and(equals(events.event, 'purchase'), greaterOrEquals(events.timestamp, toDateTime('2023-05-01'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2023-05-01'), toIntervalSecond(7776000)))))
- GROUP BY
- events.person_id
- HAVING
- greater(length(conversion_timestamps), 0))
- ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversions
+ (
+ SELECT
+ events.person_id,
+ arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps,
+ arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toFloat(1), 0))) AS conversion_math_values,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources,
+ arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
+ FROM
+ events
+ WHERE
+ or(and(equals(events.event, 'purchase'), greaterOrEquals(events.timestamp, toDateTime('2023-05-01'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2023-05-01'), toIntervalSecond(7776000)))))
+ GROUP BY
+ events.person_id
+ HAVING
+ greater(length(conversion_timestamps), 0))
+ ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversions
GROUP BY
- campaign,
- id,
- source
+ campaign,
+ id,
+ source
LIMIT 100
'''
# ---
@@ -1457,89 +1457,89 @@
'''
SELECT
- if(notEmpty(campaign_name), campaign_name, 'organic') AS match_key,
- if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
- if(notEmpty(campaign_id), campaign_id, '-') AS id,
- if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
- count() AS conversion_0
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS match_key,
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
+ if(notEmpty(campaign_id), campaign_id, '-') AS id,
+ if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
+ count() AS conversion_0
FROM
- (
- SELECT
- person_id,
- if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name,
- if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name,
- if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name,
- if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name,
- if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name,
- if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name,
- if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name,
- if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name,
- if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name,
- '-' AS campaign_id,
- 1 AS conversion_value
+ (
+ SELECT
+ person_id,
+ if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name,
+ if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name,
+ if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name,
+ if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name,
+ if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name,
+ if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name,
+ if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name,
+ if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name,
+ if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name,
+ '-' AS campaign_id,
+ 1 AS conversion_value
+ FROM
+ (
+ SELECT
+ person_id,
+ conversion_timestamps[i] AS conversion_time,
+ conversion_math_values[i] AS conversion_math_value,
+ conversion_campaigns[i] AS conversion_campaign,
+ conversion_sources[i] AS conversion_source,
+ conversion_mediums[i] AS conversion_medium,
+ conversion_contents[i] AS conversion_content,
+ conversion_terms[i] AS conversion_term,
+ conversion_referring_domains[i] AS conversion_referring_domain,
+ conversion_gclids[i] AS conversion_gclid,
+ conversion_fbclids[i] AS conversion_fbclid,
+ conversion_gad_sources[i] AS conversion_gad_source,
+ arrayMax(arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 31536000))), utm_timestamps)) AS last_utm_timestamp,
+ if(isNotNull(last_utm_timestamp), utm_campaigns[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_campaign,
+ if(isNotNull(last_utm_timestamp), utm_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_source,
+ if(isNotNull(last_utm_timestamp), utm_mediums[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_medium,
+ if(isNotNull(last_utm_timestamp), utm_contents[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_content,
+ if(isNotNull(last_utm_timestamp), utm_terms[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_term,
+ if(isNotNull(last_utm_timestamp), utm_referring_domains[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_referring_domain,
+ if(isNotNull(last_utm_timestamp), utm_gclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gclid,
+ if(isNotNull(last_utm_timestamp), utm_fbclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_fbclid,
+ if(isNotNull(last_utm_timestamp), utm_gad_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gad_source
FROM
- (
- SELECT
- person_id,
- conversion_timestamps[i] AS conversion_time,
- conversion_math_values[i] AS conversion_math_value,
- conversion_campaigns[i] AS conversion_campaign,
- conversion_sources[i] AS conversion_source,
- conversion_mediums[i] AS conversion_medium,
- conversion_contents[i] AS conversion_content,
- conversion_terms[i] AS conversion_term,
- conversion_referring_domains[i] AS conversion_referring_domain,
- conversion_gclids[i] AS conversion_gclid,
- conversion_fbclids[i] AS conversion_fbclid,
- conversion_gad_sources[i] AS conversion_gad_source,
- arrayMax(arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 31536000))), utm_timestamps)) AS last_utm_timestamp,
- if(isNotNull(last_utm_timestamp), utm_campaigns[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_campaign,
- if(isNotNull(last_utm_timestamp), utm_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_source,
- if(isNotNull(last_utm_timestamp), utm_mediums[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_medium,
- if(isNotNull(last_utm_timestamp), utm_contents[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_content,
- if(isNotNull(last_utm_timestamp), utm_terms[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_term,
- if(isNotNull(last_utm_timestamp), utm_referring_domains[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_referring_domain,
- if(isNotNull(last_utm_timestamp), utm_gclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gclid,
- if(isNotNull(last_utm_timestamp), utm_fbclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_fbclid,
- if(isNotNull(last_utm_timestamp), utm_gad_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gad_source
- FROM
- (
- SELECT
- events.person_id,
- arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps,
- arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toFloat(1), 0))) AS conversion_math_values,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources,
- arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
- FROM
- events
- WHERE
- or(and(equals(events.event, 'purchase'), greaterOrEquals(events.timestamp, toDateTime('2023-12-01'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2023-12-01'), toIntervalSecond(31536000)))))
- GROUP BY
- events.person_id
- HAVING
- greater(length(conversion_timestamps), 0))
- ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversions
+ (
+ SELECT
+ events.person_id,
+ arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps,
+ arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toFloat(1), 0))) AS conversion_math_values,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources,
+ arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
+ FROM
+ events
+ WHERE
+ or(and(equals(events.event, 'purchase'), greaterOrEquals(events.timestamp, toDateTime('2023-12-01'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2023-12-01'), toIntervalSecond(31536000)))))
+ GROUP BY
+ events.person_id
+ HAVING
+ greater(length(conversion_timestamps), 0))
+ ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversions
GROUP BY
- campaign,
- id,
- source
+ campaign,
+ id,
+ source
LIMIT 100
'''
# ---
@@ -1547,89 +1547,89 @@
'''
SELECT
- if(notEmpty(campaign_name), campaign_name, 'organic') AS match_key,
- if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
- if(notEmpty(campaign_id), campaign_id, '-') AS id,
- if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
- count() AS conversion_0
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS match_key,
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
+ if(notEmpty(campaign_id), campaign_id, '-') AS id,
+ if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
+ count() AS conversion_0
FROM
- (
- SELECT
- person_id,
- if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name,
- if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name,
- if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name,
- if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name,
- if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name,
- if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name,
- if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name,
- if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name,
- if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name,
- '-' AS campaign_id,
- 1 AS conversion_value
+ (
+ SELECT
+ person_id,
+ if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name,
+ if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name,
+ if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name,
+ if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name,
+ if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name,
+ if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name,
+ if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name,
+ if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name,
+ if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name,
+ '-' AS campaign_id,
+ 1 AS conversion_value
+ FROM
+ (
+ SELECT
+ person_id,
+ conversion_timestamps[i] AS conversion_time,
+ conversion_math_values[i] AS conversion_math_value,
+ conversion_campaigns[i] AS conversion_campaign,
+ conversion_sources[i] AS conversion_source,
+ conversion_mediums[i] AS conversion_medium,
+ conversion_contents[i] AS conversion_content,
+ conversion_terms[i] AS conversion_term,
+ conversion_referring_domains[i] AS conversion_referring_domain,
+ conversion_gclids[i] AS conversion_gclid,
+ conversion_fbclids[i] AS conversion_fbclid,
+ conversion_gad_sources[i] AS conversion_gad_source,
+ arrayMin(arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 7776000))), utm_timestamps)) AS last_utm_timestamp,
+ if(isNotNull(last_utm_timestamp), utm_campaigns[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_campaign,
+ if(isNotNull(last_utm_timestamp), utm_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_source,
+ if(isNotNull(last_utm_timestamp), utm_mediums[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_medium,
+ if(isNotNull(last_utm_timestamp), utm_contents[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_content,
+ if(isNotNull(last_utm_timestamp), utm_terms[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_term,
+ if(isNotNull(last_utm_timestamp), utm_referring_domains[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_referring_domain,
+ if(isNotNull(last_utm_timestamp), utm_gclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gclid,
+ if(isNotNull(last_utm_timestamp), utm_fbclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_fbclid,
+ if(isNotNull(last_utm_timestamp), utm_gad_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gad_source
FROM
- (
- SELECT
- person_id,
- conversion_timestamps[i] AS conversion_time,
- conversion_math_values[i] AS conversion_math_value,
- conversion_campaigns[i] AS conversion_campaign,
- conversion_sources[i] AS conversion_source,
- conversion_mediums[i] AS conversion_medium,
- conversion_contents[i] AS conversion_content,
- conversion_terms[i] AS conversion_term,
- conversion_referring_domains[i] AS conversion_referring_domain,
- conversion_gclids[i] AS conversion_gclid,
- conversion_fbclids[i] AS conversion_fbclid,
- conversion_gad_sources[i] AS conversion_gad_source,
- arrayMin(arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 7776000))), utm_timestamps)) AS last_utm_timestamp,
- if(isNotNull(last_utm_timestamp), utm_campaigns[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_campaign,
- if(isNotNull(last_utm_timestamp), utm_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_source,
- if(isNotNull(last_utm_timestamp), utm_mediums[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_medium,
- if(isNotNull(last_utm_timestamp), utm_contents[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_content,
- if(isNotNull(last_utm_timestamp), utm_terms[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_term,
- if(isNotNull(last_utm_timestamp), utm_referring_domains[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_referring_domain,
- if(isNotNull(last_utm_timestamp), utm_gclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gclid,
- if(isNotNull(last_utm_timestamp), utm_fbclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_fbclid,
- if(isNotNull(last_utm_timestamp), utm_gad_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gad_source
- FROM
- (
- SELECT
- events.person_id,
- arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps,
- arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toFloat(1), 0))) AS conversion_math_values,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources,
- arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
- FROM
- events
- WHERE
- or(and(equals(events.event, 'purchase'), greaterOrEquals(events.timestamp, toDateTime('2023-05-01'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2023-05-01'), toIntervalSecond(7776000)))))
- GROUP BY
- events.person_id
- HAVING
- greater(length(conversion_timestamps), 0))
- ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversions
+ (
+ SELECT
+ events.person_id,
+ arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps,
+ arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toFloat(1), 0))) AS conversion_math_values,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources,
+ arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
+ FROM
+ events
+ WHERE
+ or(and(equals(events.event, 'purchase'), greaterOrEquals(events.timestamp, toDateTime('2023-05-01'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2023-05-01'), toIntervalSecond(7776000)))))
+ GROUP BY
+ events.person_id
+ HAVING
+ greater(length(conversion_timestamps), 0))
+ ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversions
GROUP BY
- campaign,
- id,
- source
+ campaign,
+ id,
+ source
LIMIT 100
'''
# ---
@@ -1637,89 +1637,89 @@
'''
SELECT
- if(notEmpty(campaign_name), campaign_name, 'organic') AS match_key,
- if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
- if(notEmpty(campaign_id), campaign_id, '-') AS id,
- if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
- count() AS conversion_0
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS match_key,
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
+ if(notEmpty(campaign_id), campaign_id, '-') AS id,
+ if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
+ count() AS conversion_0
FROM
- (
- SELECT
- person_id,
- if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name,
- if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name,
- if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name,
- if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name,
- if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name,
- if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name,
- if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name,
- if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name,
- if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name,
- '-' AS campaign_id,
- 1 AS conversion_value
+ (
+ SELECT
+ person_id,
+ if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name,
+ if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name,
+ if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name,
+ if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name,
+ if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name,
+ if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name,
+ if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name,
+ if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name,
+ if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name,
+ '-' AS campaign_id,
+ 1 AS conversion_value
+ FROM
+ (
+ SELECT
+ person_id,
+ conversion_timestamps[i] AS conversion_time,
+ conversion_math_values[i] AS conversion_math_value,
+ conversion_campaigns[i] AS conversion_campaign,
+ conversion_sources[i] AS conversion_source,
+ conversion_mediums[i] AS conversion_medium,
+ conversion_contents[i] AS conversion_content,
+ conversion_terms[i] AS conversion_term,
+ conversion_referring_domains[i] AS conversion_referring_domain,
+ conversion_gclids[i] AS conversion_gclid,
+ conversion_fbclids[i] AS conversion_fbclid,
+ conversion_gad_sources[i] AS conversion_gad_source,
+ arrayMax(arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 7776000))), utm_timestamps)) AS last_utm_timestamp,
+ if(isNotNull(last_utm_timestamp), utm_campaigns[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_campaign,
+ if(isNotNull(last_utm_timestamp), utm_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_source,
+ if(isNotNull(last_utm_timestamp), utm_mediums[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_medium,
+ if(isNotNull(last_utm_timestamp), utm_contents[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_content,
+ if(isNotNull(last_utm_timestamp), utm_terms[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_term,
+ if(isNotNull(last_utm_timestamp), utm_referring_domains[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_referring_domain,
+ if(isNotNull(last_utm_timestamp), utm_gclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gclid,
+ if(isNotNull(last_utm_timestamp), utm_fbclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_fbclid,
+ if(isNotNull(last_utm_timestamp), utm_gad_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gad_source
FROM
- (
- SELECT
- person_id,
- conversion_timestamps[i] AS conversion_time,
- conversion_math_values[i] AS conversion_math_value,
- conversion_campaigns[i] AS conversion_campaign,
- conversion_sources[i] AS conversion_source,
- conversion_mediums[i] AS conversion_medium,
- conversion_contents[i] AS conversion_content,
- conversion_terms[i] AS conversion_term,
- conversion_referring_domains[i] AS conversion_referring_domain,
- conversion_gclids[i] AS conversion_gclid,
- conversion_fbclids[i] AS conversion_fbclid,
- conversion_gad_sources[i] AS conversion_gad_source,
- arrayMax(arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 7776000))), utm_timestamps)) AS last_utm_timestamp,
- if(isNotNull(last_utm_timestamp), utm_campaigns[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_campaign,
- if(isNotNull(last_utm_timestamp), utm_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_source,
- if(isNotNull(last_utm_timestamp), utm_mediums[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_medium,
- if(isNotNull(last_utm_timestamp), utm_contents[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_content,
- if(isNotNull(last_utm_timestamp), utm_terms[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_term,
- if(isNotNull(last_utm_timestamp), utm_referring_domains[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_referring_domain,
- if(isNotNull(last_utm_timestamp), utm_gclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gclid,
- if(isNotNull(last_utm_timestamp), utm_fbclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_fbclid,
- if(isNotNull(last_utm_timestamp), utm_gad_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gad_source
- FROM
- (
- SELECT
- events.person_id,
- arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps,
- arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toFloat(1), 0))) AS conversion_math_values,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources,
- arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
- FROM
- events
- WHERE
- or(and(equals(events.event, 'purchase'), greaterOrEquals(events.timestamp, toDateTime('2023-05-01'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2023-05-01'), toIntervalSecond(7776000)))))
- GROUP BY
- events.person_id
- HAVING
- greater(length(conversion_timestamps), 0))
- ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversions
+ (
+ SELECT
+ events.person_id,
+ arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps,
+ arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toFloat(1), 0))) AS conversion_math_values,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources,
+ arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
+ FROM
+ events
+ WHERE
+ or(and(equals(events.event, 'purchase'), greaterOrEquals(events.timestamp, toDateTime('2023-05-01'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2023-05-01'), toIntervalSecond(7776000)))))
+ GROUP BY
+ events.person_id
+ HAVING
+ greater(length(conversion_timestamps), 0))
+ ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversions
GROUP BY
- campaign,
- id,
- source
+ campaign,
+ id,
+ source
LIMIT 100
'''
# ---
@@ -1727,89 +1727,89 @@
'''
SELECT
- if(notEmpty(campaign_name), campaign_name, 'organic') AS match_key,
- if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
- if(notEmpty(campaign_id), campaign_id, '-') AS id,
- if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
- count() AS conversion_0
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS match_key,
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
+ if(notEmpty(campaign_id), campaign_id, '-') AS id,
+ if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
+ count() AS conversion_0
FROM
- (
- SELECT
- person_id,
- if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name,
- if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name,
- if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name,
- if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name,
- if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name,
- if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name,
- if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name,
- if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name,
- if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name,
- '-' AS campaign_id,
- 1 AS conversion_value
+ (
+ SELECT
+ person_id,
+ if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name,
+ if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name,
+ if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name,
+ if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name,
+ if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name,
+ if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name,
+ if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name,
+ if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name,
+ if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name,
+ '-' AS campaign_id,
+ 1 AS conversion_value
+ FROM
+ (
+ SELECT
+ person_id,
+ conversion_timestamps[i] AS conversion_time,
+ conversion_math_values[i] AS conversion_math_value,
+ conversion_campaigns[i] AS conversion_campaign,
+ conversion_sources[i] AS conversion_source,
+ conversion_mediums[i] AS conversion_medium,
+ conversion_contents[i] AS conversion_content,
+ conversion_terms[i] AS conversion_term,
+ conversion_referring_domains[i] AS conversion_referring_domain,
+ conversion_gclids[i] AS conversion_gclid,
+ conversion_fbclids[i] AS conversion_fbclid,
+ conversion_gad_sources[i] AS conversion_gad_source,
+ arrayMax(arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 7776000))), utm_timestamps)) AS last_utm_timestamp,
+ if(isNotNull(last_utm_timestamp), utm_campaigns[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_campaign,
+ if(isNotNull(last_utm_timestamp), utm_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_source,
+ if(isNotNull(last_utm_timestamp), utm_mediums[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_medium,
+ if(isNotNull(last_utm_timestamp), utm_contents[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_content,
+ if(isNotNull(last_utm_timestamp), utm_terms[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_term,
+ if(isNotNull(last_utm_timestamp), utm_referring_domains[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_referring_domain,
+ if(isNotNull(last_utm_timestamp), utm_gclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gclid,
+ if(isNotNull(last_utm_timestamp), utm_fbclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_fbclid,
+ if(isNotNull(last_utm_timestamp), utm_gad_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gad_source
FROM
- (
- SELECT
- person_id,
- conversion_timestamps[i] AS conversion_time,
- conversion_math_values[i] AS conversion_math_value,
- conversion_campaigns[i] AS conversion_campaign,
- conversion_sources[i] AS conversion_source,
- conversion_mediums[i] AS conversion_medium,
- conversion_contents[i] AS conversion_content,
- conversion_terms[i] AS conversion_term,
- conversion_referring_domains[i] AS conversion_referring_domain,
- conversion_gclids[i] AS conversion_gclid,
- conversion_fbclids[i] AS conversion_fbclid,
- conversion_gad_sources[i] AS conversion_gad_source,
- arrayMax(arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 7776000))), utm_timestamps)) AS last_utm_timestamp,
- if(isNotNull(last_utm_timestamp), utm_campaigns[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_campaign,
- if(isNotNull(last_utm_timestamp), utm_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_source,
- if(isNotNull(last_utm_timestamp), utm_mediums[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_medium,
- if(isNotNull(last_utm_timestamp), utm_contents[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_content,
- if(isNotNull(last_utm_timestamp), utm_terms[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_term,
- if(isNotNull(last_utm_timestamp), utm_referring_domains[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_referring_domain,
- if(isNotNull(last_utm_timestamp), utm_gclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gclid,
- if(isNotNull(last_utm_timestamp), utm_fbclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_fbclid,
- if(isNotNull(last_utm_timestamp), utm_gad_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gad_source
- FROM
- (
- SELECT
- events.person_id,
- arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps,
- arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toFloat(1), 0))) AS conversion_math_values,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources,
- arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
- FROM
- events
- WHERE
- or(and(equals(events.event, 'purchase'), greaterOrEquals(events.timestamp, toDateTime('2023-05-01'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2023-05-01'), toIntervalSecond(7776000)))))
- GROUP BY
- events.person_id
- HAVING
- greater(length(conversion_timestamps), 0))
- ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversions
+ (
+ SELECT
+ events.person_id,
+ arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps,
+ arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toFloat(1), 0))) AS conversion_math_values,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources,
+ arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
+ FROM
+ events
+ WHERE
+ or(and(equals(events.event, 'purchase'), greaterOrEquals(events.timestamp, toDateTime('2023-05-01'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2023-05-01'), toIntervalSecond(7776000)))))
+ GROUP BY
+ events.person_id
+ HAVING
+ greater(length(conversion_timestamps), 0))
+ ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversions
GROUP BY
- campaign,
- id,
- source
+ campaign,
+ id,
+ source
LIMIT 100
'''
# ---
diff --git a/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_conversion_goal_processor_refactor.ambr b/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_conversion_goal_processor_refactor.ambr
index 4402c01afb29..c224daf20ca7 100644
--- a/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_conversion_goal_processor_refactor.ambr
+++ b/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_conversion_goal_processor_refactor.ambr
@@ -3,78 +3,78 @@
'''
SELECT
- person_id AS person_id,
- if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name,
- if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name,
- if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name,
- if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name,
- if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name,
- if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name,
- if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name,
- if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name,
- if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name,
- 1 AS conversion_value,
- fromUnixTimestamp(conversion_time) AS conversion_timestamp,
- fromUnixTimestamp(if(greater(last_utm_timestamp, 0), last_utm_timestamp, conversion_time)) AS touchpoint_timestamp,
- 1.0 AS touchpoint_weight
+ person_id AS person_id,
+ if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name,
+ if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name,
+ if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name,
+ if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name,
+ if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name,
+ if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name,
+ if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name,
+ if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name,
+ if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name,
+ 1 AS conversion_value,
+ fromUnixTimestamp(conversion_time) AS conversion_timestamp,
+ fromUnixTimestamp(if(greater(last_utm_timestamp, 0), last_utm_timestamp, conversion_time)) AS touchpoint_timestamp,
+ 1.0 AS touchpoint_weight
FROM
- (
- SELECT
- person_id,
- conversion_timestamps[i] AS conversion_time,
- conversion_math_values[i] AS conversion_math_value,
- conversion_campaigns[i] AS conversion_campaign,
- conversion_sources[i] AS conversion_source,
- conversion_mediums[i] AS conversion_medium,
- conversion_contents[i] AS conversion_content,
- conversion_terms[i] AS conversion_term,
- conversion_referring_domains[i] AS conversion_referring_domain,
- conversion_gclids[i] AS conversion_gclid,
- conversion_fbclids[i] AS conversion_fbclid,
- conversion_gad_sources[i] AS conversion_gad_source,
- arrayMax(arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 7776000))), utm_timestamps)) AS last_utm_timestamp,
- if(isNotNull(last_utm_timestamp), utm_campaigns[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_campaign,
- if(isNotNull(last_utm_timestamp), utm_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_source,
- if(isNotNull(last_utm_timestamp), utm_mediums[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_medium,
- if(isNotNull(last_utm_timestamp), utm_contents[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_content,
- if(isNotNull(last_utm_timestamp), utm_terms[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_term,
- if(isNotNull(last_utm_timestamp), utm_referring_domains[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_referring_domain,
- if(isNotNull(last_utm_timestamp), utm_gclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gclid,
- if(isNotNull(last_utm_timestamp), utm_fbclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_fbclid,
- if(isNotNull(last_utm_timestamp), utm_gad_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gad_source
+ (
+ SELECT
+ person_id,
+ conversion_timestamps[i] AS conversion_time,
+ conversion_math_values[i] AS conversion_math_value,
+ conversion_campaigns[i] AS conversion_campaign,
+ conversion_sources[i] AS conversion_source,
+ conversion_mediums[i] AS conversion_medium,
+ conversion_contents[i] AS conversion_content,
+ conversion_terms[i] AS conversion_term,
+ conversion_referring_domains[i] AS conversion_referring_domain,
+ conversion_gclids[i] AS conversion_gclid,
+ conversion_fbclids[i] AS conversion_fbclid,
+ conversion_gad_sources[i] AS conversion_gad_source,
+ arrayMax(arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 7776000))), utm_timestamps)) AS last_utm_timestamp,
+ if(isNotNull(last_utm_timestamp), utm_campaigns[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_campaign,
+ if(isNotNull(last_utm_timestamp), utm_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_source,
+ if(isNotNull(last_utm_timestamp), utm_mediums[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_medium,
+ if(isNotNull(last_utm_timestamp), utm_contents[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_content,
+ if(isNotNull(last_utm_timestamp), utm_terms[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_term,
+ if(isNotNull(last_utm_timestamp), utm_referring_domains[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_referring_domain,
+ if(isNotNull(last_utm_timestamp), utm_gclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gclid,
+ if(isNotNull(last_utm_timestamp), utm_fbclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_fbclid,
+ if(isNotNull(last_utm_timestamp), utm_gad_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gad_source
+ FROM
+ (
+ SELECT
+ events.person_id,
+ arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps,
+ arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toFloat(1), 0))) AS conversion_math_values,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources,
+ arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
FROM
- (
- SELECT
- events.person_id,
- arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps,
- arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toFloat(1), 0))) AS conversion_math_values,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources,
- arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
- FROM
- events
- WHERE
- or(and(equals(events.event, 'purchase'), greaterOrEquals(events.timestamp, toDateTime('2025-01-01 00:00:00.000000')), lessOrEquals(events.timestamp, toDateTime('2025-01-02 00:00:00.000000'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2025-01-01 00:00:00.000000'), toIntervalSecond(7776000))), lessOrEquals(events.timestamp, toDateTime('2025-01-02 00:00:00.000000'))))
- GROUP BY
- events.person_id
- HAVING
- greater(length(conversion_timestamps), 0))
+ events
+ WHERE
+ or(and(equals(events.event, 'purchase'), greaterOrEquals(events.timestamp, toDateTime('2025-01-01 00:00:00.000000')), lessOrEquals(events.timestamp, toDateTime('2025-01-02 00:00:00.000000'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2025-01-01 00:00:00.000000'), toIntervalSecond(7776000))), lessOrEquals(events.timestamp, toDateTime('2025-01-02 00:00:00.000000'))))
+ GROUP BY
+ events.person_id
+ HAVING
+ greater(length(conversion_timestamps), 0))
ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)
LIMIT 50000
'''
@@ -83,34 +83,34 @@
'''
SELECT
- if(notEmpty(campaign_name), campaign_name, 'organic') AS match_key,
- if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
- if(notEmpty(campaign_id), campaign_id, '-') AS id,
- if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
- count() AS conversion_0
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS match_key,
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
+ if(notEmpty(campaign_id), campaign_id, '-') AS id,
+ if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
+ count() AS conversion_0
FROM
- (
- SELECT
- person_id,
- campaign_name,
- source_name,
- medium_name,
- content_name,
- term_name,
- referring_domain_name,
- gclid_name,
- fbclid_name,
- gad_source_name,
- '-' AS campaign_id,
- multiply(conversion_value, touchpoint_weight) AS conversion_value
- FROM
- conversion_goal_attributed_preaggregated
- WHERE
- and(in(job_id, tuple('00000000-0000-0000-0000-000000000001')), equals(team_id, 420), greaterOrEquals(conversion_timestamp, toDateTime('2025-01-01 00:00:00.000000')), lessOrEquals(conversion_timestamp, toDateTime('2025-01-31 00:00:00.000000')))) AS attributed_conversions
+ (
+ SELECT
+ person_id,
+ campaign_name,
+ source_name,
+ medium_name,
+ content_name,
+ term_name,
+ referring_domain_name,
+ gclid_name,
+ fbclid_name,
+ gad_source_name,
+ '-' AS campaign_id,
+ multiply(conversion_value, touchpoint_weight) AS conversion_value
+ FROM
+ conversion_goal_attributed_preaggregated
+ WHERE
+ and(in(job_id, tuple('00000000-0000-0000-0000-000000000001')), equals(team_id, 420), greaterOrEquals(conversion_timestamp, toDateTime('2025-01-01 00:00:00.000000')), lessOrEquals(conversion_timestamp, toDateTime('2025-01-31 00:00:00.000000')))) AS attributed_conversions
GROUP BY
- campaign,
- id,
- source
+ campaign,
+ id,
+ source
LIMIT 50000
'''
# ---
@@ -119,17 +119,17 @@
SELECT if(notEmpty(campaign_name), campaign_name, 'organic') AS match_key, if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign, if(notEmpty(campaign_id), campaign_id, '-') AS id, if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source, coalesce(sum(conversion_value), 0) AS conversion_0
FROM (
- SELECT person_id, if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name, if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name, if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name, if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name, if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name, if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name, if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name, if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name, if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name, '-' AS campaign_id, multiply(1, attribution_weight) AS conversion_value
- FROM (
- SELECT person_id, conversion_math_value, conversion_campaign, conversion_source, conversion_medium, conversion_content, conversion_term, conversion_referring_domain, conversion_gclid, conversion_fbclid, conversion_gad_source, filtered_utm_campaigns[tp_idx] AS fallback_campaign, filtered_utm_sources[tp_idx] AS fallback_source, filtered_utm_mediums[tp_idx] AS fallback_medium, filtered_utm_contents[tp_idx] AS fallback_content, filtered_utm_terms[tp_idx] AS fallback_term, filtered_utm_referring_domains[tp_idx] AS fallback_referring_domain, filtered_utm_gclids[tp_idx] AS fallback_gclid, filtered_utm_fbclids[tp_idx] AS fallback_fbclid, filtered_utm_gad_sources[tp_idx] AS fallback_gad_source, attribution_weights[tp_idx] AS attribution_weight
- FROM (
- SELECT person_id, conversion_timestamps[i] AS conversion_time, conversion_math_values[i] AS conversion_math_value, conversion_campaigns[i] AS conversion_campaign, conversion_sources[i] AS conversion_source, conversion_mediums[i] AS conversion_medium, conversion_contents[i] AS conversion_content, conversion_terms[i] AS conversion_term, conversion_referring_domains[i] AS conversion_referring_domain, conversion_gclids[i] AS conversion_gclid, conversion_fbclids[i] AS conversion_fbclid, conversion_gad_sources[i] AS conversion_gad_source, arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 7776000))), utm_timestamps) AS filtered_utm_timestamps, arrayMap(idx -> utm_campaigns[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_campaigns, arrayMap(idx -> utm_sources[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_sources, arrayMap(idx -> utm_mediums[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_mediums, arrayMap(idx -> utm_contents[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_contents, arrayMap(idx -> utm_terms[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_terms, arrayMap(idx -> utm_referring_domains[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_referring_domains, arrayMap(idx -> utm_gclids[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_gclids, arrayMap(idx -> utm_fbclids[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_fbclids, arrayMap(idx -> utm_gad_sources[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_gad_sources, arrayMap(_x -> divide(1.0, greatest(toFloat(length(filtered_utm_timestamps)), 1.0)), filtered_utm_timestamps) AS attribution_weights
- FROM (
- SELECT events.person_id, arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps, arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toFloat(1), 0))) AS conversion_math_values, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources, arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
- FROM events
- WHERE or(equals(events.event, 'purchase'), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))))
- GROUP BY events.person_id
- HAVING greater(length(conversion_timestamps), 0)) ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i) ARRAY JOIN arrayEnumerate(attribution_weights) AS tp_idx)) AS attributed_conversions
+ SELECT person_id, if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name, if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name, if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name, if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name, if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name, if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name, if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name, if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name, if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name, '-' AS campaign_id, multiply(1, attribution_weight) AS conversion_value
+ FROM (
+ SELECT person_id, conversion_math_value, conversion_campaign, conversion_source, conversion_medium, conversion_content, conversion_term, conversion_referring_domain, conversion_gclid, conversion_fbclid, conversion_gad_source, filtered_utm_campaigns[tp_idx] AS fallback_campaign, filtered_utm_sources[tp_idx] AS fallback_source, filtered_utm_mediums[tp_idx] AS fallback_medium, filtered_utm_contents[tp_idx] AS fallback_content, filtered_utm_terms[tp_idx] AS fallback_term, filtered_utm_referring_domains[tp_idx] AS fallback_referring_domain, filtered_utm_gclids[tp_idx] AS fallback_gclid, filtered_utm_fbclids[tp_idx] AS fallback_fbclid, filtered_utm_gad_sources[tp_idx] AS fallback_gad_source, attribution_weights[tp_idx] AS attribution_weight
+ FROM (
+ SELECT person_id, conversion_timestamps[i] AS conversion_time, conversion_math_values[i] AS conversion_math_value, conversion_campaigns[i] AS conversion_campaign, conversion_sources[i] AS conversion_source, conversion_mediums[i] AS conversion_medium, conversion_contents[i] AS conversion_content, conversion_terms[i] AS conversion_term, conversion_referring_domains[i] AS conversion_referring_domain, conversion_gclids[i] AS conversion_gclid, conversion_fbclids[i] AS conversion_fbclid, conversion_gad_sources[i] AS conversion_gad_source, arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 7776000))), utm_timestamps) AS filtered_utm_timestamps, arrayMap(idx -> utm_campaigns[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_campaigns, arrayMap(idx -> utm_sources[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_sources, arrayMap(idx -> utm_mediums[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_mediums, arrayMap(idx -> utm_contents[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_contents, arrayMap(idx -> utm_terms[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_terms, arrayMap(idx -> utm_referring_domains[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_referring_domains, arrayMap(idx -> utm_gclids[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_gclids, arrayMap(idx -> utm_fbclids[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_fbclids, arrayMap(idx -> utm_gad_sources[idx], arrayFilter(j -> and(lessOrEquals(utm_timestamps[j], conversion_timestamps[i]), greaterOrEquals(utm_timestamps[j], minus(conversion_timestamps[i], 7776000))), arrayEnumerate(utm_timestamps))) AS filtered_utm_gad_sources, arrayMap(_x -> divide(1.0, greatest(toFloat(length(filtered_utm_timestamps)), 1.0)), filtered_utm_timestamps) AS attribution_weights
+ FROM (
+ SELECT events.person_id, arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps, arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toFloat(1), 0))) AS conversion_math_values, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources, arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
+ FROM events
+ WHERE or(equals(events.event, 'purchase'), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))))
+ GROUP BY events.person_id
+ HAVING greater(length(conversion_timestamps), 0)) ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i) ARRAY JOIN arrayEnumerate(attribution_weights) AS tp_idx)) AS attributed_conversions
GROUP BY campaign, id, source
LIMIT 50000
'''
@@ -139,15 +139,15 @@
SELECT if(notEmpty(campaign_name), campaign_name, 'organic') AS match_key, if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign, if(notEmpty(campaign_id), campaign_id, '-') AS id, if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source, count() AS conversion_0
FROM (
- SELECT person_id, if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name, if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name, if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name, if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name, if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name, if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name, if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name, if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name, if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name, '-' AS campaign_id, 1 AS conversion_value
- FROM (
- SELECT person_id, conversion_timestamps[i] AS conversion_time, conversion_math_values[i] AS conversion_math_value, conversion_campaigns[i] AS conversion_campaign, conversion_sources[i] AS conversion_source, conversion_mediums[i] AS conversion_medium, conversion_contents[i] AS conversion_content, conversion_terms[i] AS conversion_term, conversion_referring_domains[i] AS conversion_referring_domain, conversion_gclids[i] AS conversion_gclid, conversion_fbclids[i] AS conversion_fbclid, conversion_gad_sources[i] AS conversion_gad_source, arrayMax(arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 7776000))), utm_timestamps)) AS last_utm_timestamp, if(isNotNull(last_utm_timestamp), utm_campaigns[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_campaign, if(isNotNull(last_utm_timestamp), utm_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_source, if(isNotNull(last_utm_timestamp), utm_mediums[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_medium, if(isNotNull(last_utm_timestamp), utm_contents[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_content, if(isNotNull(last_utm_timestamp), utm_terms[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_term, if(isNotNull(last_utm_timestamp), utm_referring_domains[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_referring_domain, if(isNotNull(last_utm_timestamp), utm_gclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gclid, if(isNotNull(last_utm_timestamp), utm_fbclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_fbclid, if(isNotNull(last_utm_timestamp), utm_gad_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gad_source
- FROM (
- SELECT events.person_id, arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps, arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toFloat(1), 0))) AS conversion_math_values, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources, arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
- FROM events
- WHERE or(equals(events.event, 'purchase'), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))))
- GROUP BY events.person_id
- HAVING greater(length(conversion_timestamps), 0)) ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversions
+ SELECT person_id, if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name, if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name, if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name, if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name, if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name, if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name, if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name, if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name, if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name, '-' AS campaign_id, 1 AS conversion_value
+ FROM (
+ SELECT person_id, conversion_timestamps[i] AS conversion_time, conversion_math_values[i] AS conversion_math_value, conversion_campaigns[i] AS conversion_campaign, conversion_sources[i] AS conversion_source, conversion_mediums[i] AS conversion_medium, conversion_contents[i] AS conversion_content, conversion_terms[i] AS conversion_term, conversion_referring_domains[i] AS conversion_referring_domain, conversion_gclids[i] AS conversion_gclid, conversion_fbclids[i] AS conversion_fbclid, conversion_gad_sources[i] AS conversion_gad_source, arrayMax(arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 7776000))), utm_timestamps)) AS last_utm_timestamp, if(isNotNull(last_utm_timestamp), utm_campaigns[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_campaign, if(isNotNull(last_utm_timestamp), utm_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_source, if(isNotNull(last_utm_timestamp), utm_mediums[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_medium, if(isNotNull(last_utm_timestamp), utm_contents[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_content, if(isNotNull(last_utm_timestamp), utm_terms[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_term, if(isNotNull(last_utm_timestamp), utm_referring_domains[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_referring_domain, if(isNotNull(last_utm_timestamp), utm_gclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gclid, if(isNotNull(last_utm_timestamp), utm_fbclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_fbclid, if(isNotNull(last_utm_timestamp), utm_gad_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gad_source
+ FROM (
+ SELECT events.person_id, arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps, arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toFloat(1), 0))) AS conversion_math_values, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources, arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
+ FROM events
+ WHERE or(equals(events.event, 'purchase'), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))))
+ GROUP BY events.person_id
+ HAVING greater(length(conversion_timestamps), 0)) ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversions
GROUP BY campaign, id, source
LIMIT 50000
'''
@@ -157,15 +157,15 @@
SELECT if(notEmpty(campaign_name), campaign_name, 'organic') AS match_key, if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign, if(notEmpty(campaign_id), campaign_id, '-') AS id, if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source, count() AS conversion_0
FROM (
- SELECT person_id, if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name, if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name, if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name, if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name, if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name, if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name, if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name, if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name, if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name, '-' AS campaign_id, 1 AS conversion_value
- FROM (
- SELECT person_id, conversion_timestamps[i] AS conversion_time, conversion_math_values[i] AS conversion_math_value, conversion_campaigns[i] AS conversion_campaign, conversion_sources[i] AS conversion_source, conversion_mediums[i] AS conversion_medium, conversion_contents[i] AS conversion_content, conversion_terms[i] AS conversion_term, conversion_referring_domains[i] AS conversion_referring_domain, conversion_gclids[i] AS conversion_gclid, conversion_fbclids[i] AS conversion_fbclid, conversion_gad_sources[i] AS conversion_gad_source, arrayMax(arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 7776000))), utm_timestamps)) AS last_utm_timestamp, if(isNotNull(last_utm_timestamp), utm_campaigns[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_campaign, if(isNotNull(last_utm_timestamp), utm_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_source, if(isNotNull(last_utm_timestamp), utm_mediums[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_medium, if(isNotNull(last_utm_timestamp), utm_contents[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_content, if(isNotNull(last_utm_timestamp), utm_terms[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_term, if(isNotNull(last_utm_timestamp), utm_referring_domains[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_referring_domain, if(isNotNull(last_utm_timestamp), utm_gclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gclid, if(isNotNull(last_utm_timestamp), utm_fbclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_fbclid, if(isNotNull(last_utm_timestamp), utm_gad_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gad_source
- FROM (
- SELECT events.person_id, arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps, arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toFloat(1), 0))) AS conversion_math_values, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources, arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
- FROM events
- WHERE and(or(and(equals(events.event, 'purchase'), greaterOrEquals(events.timestamp, toDateTime('2023-01-01'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2023-01-01'), toIntervalSecond(7776000))))), equals(properties.plan, 'pro'))
- GROUP BY events.person_id
- HAVING greater(length(conversion_timestamps), 0)) ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversions
+ SELECT person_id, if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name, if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name, if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name, if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name, if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name, if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name, if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name, if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name, if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name, '-' AS campaign_id, 1 AS conversion_value
+ FROM (
+ SELECT person_id, conversion_timestamps[i] AS conversion_time, conversion_math_values[i] AS conversion_math_value, conversion_campaigns[i] AS conversion_campaign, conversion_sources[i] AS conversion_source, conversion_mediums[i] AS conversion_medium, conversion_contents[i] AS conversion_content, conversion_terms[i] AS conversion_term, conversion_referring_domains[i] AS conversion_referring_domain, conversion_gclids[i] AS conversion_gclid, conversion_fbclids[i] AS conversion_fbclid, conversion_gad_sources[i] AS conversion_gad_source, arrayMax(arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 7776000))), utm_timestamps)) AS last_utm_timestamp, if(isNotNull(last_utm_timestamp), utm_campaigns[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_campaign, if(isNotNull(last_utm_timestamp), utm_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_source, if(isNotNull(last_utm_timestamp), utm_mediums[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_medium, if(isNotNull(last_utm_timestamp), utm_contents[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_content, if(isNotNull(last_utm_timestamp), utm_terms[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_term, if(isNotNull(last_utm_timestamp), utm_referring_domains[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_referring_domain, if(isNotNull(last_utm_timestamp), utm_gclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gclid, if(isNotNull(last_utm_timestamp), utm_fbclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_fbclid, if(isNotNull(last_utm_timestamp), utm_gad_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gad_source
+ FROM (
+ SELECT events.person_id, arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps, arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toFloat(1), 0))) AS conversion_math_values, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources, arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
+ FROM events
+ WHERE and(or(and(equals(events.event, 'purchase'), greaterOrEquals(events.timestamp, toDateTime('2023-01-01'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2023-01-01'), toIntervalSecond(7776000))))), equals(properties.plan, 'pro'))
+ GROUP BY events.person_id
+ HAVING greater(length(conversion_timestamps), 0)) ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversions
GROUP BY campaign, id, source
LIMIT 50000
'''
diff --git a/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_conversion_goals_aggregator.ambr b/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_conversion_goals_aggregator.ambr
index aec6347f355a..5c1d19036233 100644
--- a/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_conversion_goals_aggregator.ambr
+++ b/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_conversion_goals_aggregator.ambr
@@ -3,45 +3,45 @@
'''
SELECT
- multiIf(and(in(lower(conv.source), ['google', 'adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), in(lower(conv.campaign), ['spring_sale_2024', 'spring-sale-2024'])), 'Spring Sale 2024', and(in(lower(conv.source), ['google', 'adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), in(lower(conv.campaign), ['holiday_campaign', 'holiday_promo'])), 'Holiday Promo', conv.campaign) AS campaign,
- conv.id AS id,
- conv.source AS source,
- multiIf(and(in(lower(conv.source), ['google', 'adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), in(lower(conv.campaign), ['spring_sale_2024', 'spring-sale-2024'])), 'Spring Sale 2024', and(in(lower(conv.source), ['google', 'adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), in(lower(conv.campaign), ['holiday_campaign', 'holiday_promo'])), 'Holiday Promo', conv.campaign) AS match_key,
- sum(conv.conversion_0) AS conversion_0
+ multiIf(and(in(lower(conv.source), ['google', 'adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), in(lower(conv.campaign), ['spring_sale_2024', 'spring-sale-2024'])), 'Spring Sale 2024', and(in(lower(conv.source), ['google', 'adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), in(lower(conv.campaign), ['holiday_campaign', 'holiday_promo'])), 'Holiday Promo', conv.campaign) AS campaign,
+ conv.id AS id,
+ conv.source AS source,
+ multiIf(and(in(lower(conv.source), ['google', 'adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), in(lower(conv.campaign), ['spring_sale_2024', 'spring-sale-2024'])), 'Spring Sale 2024', and(in(lower(conv.source), ['google', 'adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), in(lower(conv.campaign), ['holiday_campaign', 'holiday_promo'])), 'Holiday Promo', conv.campaign) AS match_key,
+ sum(conv.conversion_0) AS conversion_0
FROM
- (
- SELECT
- if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
- if(notEmpty(campaign_id), campaign_id, '-') AS id,
- if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
- count() AS conversion_0
+ (
+ SELECT
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
+ if(notEmpty(campaign_id), campaign_id, '-') AS id,
+ if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
+ count() AS conversion_0
+ FROM
+ (
+ SELECT
+ person_id,
+ campaign_name,
+ source_name,
+ medium_name,
+ content_name,
+ term_name,
+ referring_domain_name,
+ gclid_name,
+ fbclid_name,
+ gad_source_name,
+ '-' AS campaign_id,
+ multiply(conversion_value, touchpoint_weight) AS conversion_value
FROM
- (
- SELECT
- person_id,
- campaign_name,
- source_name,
- medium_name,
- content_name,
- term_name,
- referring_domain_name,
- gclid_name,
- fbclid_name,
- gad_source_name,
- '-' AS campaign_id,
- multiply(conversion_value, touchpoint_weight) AS conversion_value
- FROM
- conversion_goal_attributed_preaggregated
- WHERE
- and(in(job_id, tuple('00000000-0000-0000-0000-000000000001')), equals(team_id, 420), greaterOrEquals(conversion_timestamp, toDateTime('2023-01-01 00:00:00.000000')), lessOrEquals(conversion_timestamp, toDateTime('2023-01-31 23:59:59.999999')))) AS attributed_conversions
- GROUP BY
- campaign,
- id,
- source) AS conv
+ conversion_goal_attributed_preaggregated
+ WHERE
+ and(in(job_id, tuple('00000000-0000-0000-0000-000000000001')), equals(team_id, 420), greaterOrEquals(conversion_timestamp, toDateTime('2023-01-01 00:00:00.000000')), lessOrEquals(conversion_timestamp, toDateTime('2023-01-31 23:59:59.999999')))) AS attributed_conversions
+ GROUP BY
+ campaign,
+ id,
+ source) AS conv
GROUP BY
- multiIf(and(in(lower(conv.source), ['google', 'adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), in(lower(conv.campaign), ['spring_sale_2024', 'spring-sale-2024'])), 'Spring Sale 2024', and(in(lower(conv.source), ['google', 'adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), in(lower(conv.campaign), ['holiday_campaign', 'holiday_promo'])), 'Holiday Promo', conv.campaign),
- conv.id,
- conv.source
+ multiIf(and(in(lower(conv.source), ['google', 'adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), in(lower(conv.campaign), ['spring_sale_2024', 'spring-sale-2024'])), 'Spring Sale 2024', and(in(lower(conv.source), ['google', 'adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), in(lower(conv.campaign), ['holiday_campaign', 'holiday_promo'])), 'Holiday Promo', conv.campaign),
+ conv.id,
+ conv.source
LIMIT 100
'''
# ---
@@ -49,45 +49,45 @@
'''
SELECT
- conv.campaign AS campaign,
- conv.id AS id,
- conv.source AS source,
- conv.campaign AS match_key,
- sum(conv.conversion_0) AS conversion_0
+ conv.campaign AS campaign,
+ conv.id AS id,
+ conv.source AS source,
+ conv.campaign AS match_key,
+ sum(conv.conversion_0) AS conversion_0
FROM
- (
- SELECT
- if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
- if(notEmpty(campaign_id), campaign_id, '-') AS id,
- if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
- count() AS conversion_0
+ (
+ SELECT
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
+ if(notEmpty(campaign_id), campaign_id, '-') AS id,
+ if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
+ count() AS conversion_0
+ FROM
+ (
+ SELECT
+ person_id,
+ campaign_name,
+ source_name,
+ medium_name,
+ content_name,
+ term_name,
+ referring_domain_name,
+ gclid_name,
+ fbclid_name,
+ gad_source_name,
+ '-' AS campaign_id,
+ multiply(conversion_value, touchpoint_weight) AS conversion_value
FROM
- (
- SELECT
- person_id,
- campaign_name,
- source_name,
- medium_name,
- content_name,
- term_name,
- referring_domain_name,
- gclid_name,
- fbclid_name,
- gad_source_name,
- '-' AS campaign_id,
- multiply(conversion_value, touchpoint_weight) AS conversion_value
- FROM
- conversion_goal_attributed_preaggregated
- WHERE
- and(in(job_id, tuple('00000000-0000-0000-0000-000000000001')), equals(team_id, 420), greaterOrEquals(conversion_timestamp, toDateTime('2023-01-01 00:00:00.000000')), lessOrEquals(conversion_timestamp, toDateTime('2023-01-31 23:59:59.999999')))) AS attributed_conversions
- GROUP BY
- campaign,
- id,
- source) AS conv
+ conversion_goal_attributed_preaggregated
+ WHERE
+ and(in(job_id, tuple('00000000-0000-0000-0000-000000000001')), equals(team_id, 420), greaterOrEquals(conversion_timestamp, toDateTime('2023-01-01 00:00:00.000000')), lessOrEquals(conversion_timestamp, toDateTime('2023-01-31 23:59:59.999999')))) AS attributed_conversions
+ GROUP BY
+ campaign,
+ id,
+ source) AS conv
GROUP BY
- conv.campaign,
- conv.id,
- conv.source
+ conv.campaign,
+ conv.id,
+ conv.source
LIMIT 100
'''
# ---
@@ -95,45 +95,45 @@
'''
SELECT
- multiIf(and(in(lower(conv.source), ['google', 'adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), in(lower(conv.campaign), ['messy_campaign_1', 'messy_campaign_2', 'messy_campaign_3'])), 'Clean Campaign Name', conv.campaign) AS campaign,
- conv.id AS id,
- conv.source AS source,
- multiIf(and(in(lower(conv.source), ['google', 'adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), in(lower(conv.campaign), ['messy_campaign_1', 'messy_campaign_2', 'messy_campaign_3'])), 'Clean Campaign Name', conv.campaign) AS match_key,
- sum(conv.conversion_0) AS conversion_0
+ multiIf(and(in(lower(conv.source), ['google', 'adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), in(lower(conv.campaign), ['messy_campaign_1', 'messy_campaign_2', 'messy_campaign_3'])), 'Clean Campaign Name', conv.campaign) AS campaign,
+ conv.id AS id,
+ conv.source AS source,
+ multiIf(and(in(lower(conv.source), ['google', 'adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), in(lower(conv.campaign), ['messy_campaign_1', 'messy_campaign_2', 'messy_campaign_3'])), 'Clean Campaign Name', conv.campaign) AS match_key,
+ sum(conv.conversion_0) AS conversion_0
FROM
- (
- SELECT
- if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
- if(notEmpty(campaign_id), campaign_id, '-') AS id,
- if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
- count() AS conversion_0
+ (
+ SELECT
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
+ if(notEmpty(campaign_id), campaign_id, '-') AS id,
+ if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
+ count() AS conversion_0
+ FROM
+ (
+ SELECT
+ person_id,
+ campaign_name,
+ source_name,
+ medium_name,
+ content_name,
+ term_name,
+ referring_domain_name,
+ gclid_name,
+ fbclid_name,
+ gad_source_name,
+ '-' AS campaign_id,
+ multiply(conversion_value, touchpoint_weight) AS conversion_value
FROM
- (
- SELECT
- person_id,
- campaign_name,
- source_name,
- medium_name,
- content_name,
- term_name,
- referring_domain_name,
- gclid_name,
- fbclid_name,
- gad_source_name,
- '-' AS campaign_id,
- multiply(conversion_value, touchpoint_weight) AS conversion_value
- FROM
- conversion_goal_attributed_preaggregated
- WHERE
- and(in(job_id, tuple('00000000-0000-0000-0000-000000000001')), equals(team_id, 420), greaterOrEquals(conversion_timestamp, toDateTime('2023-01-01 00:00:00.000000')), lessOrEquals(conversion_timestamp, toDateTime('2023-01-31 23:59:59.999999')))) AS attributed_conversions
- GROUP BY
- campaign,
- id,
- source) AS conv
+ conversion_goal_attributed_preaggregated
+ WHERE
+ and(in(job_id, tuple('00000000-0000-0000-0000-000000000001')), equals(team_id, 420), greaterOrEquals(conversion_timestamp, toDateTime('2023-01-01 00:00:00.000000')), lessOrEquals(conversion_timestamp, toDateTime('2023-01-31 23:59:59.999999')))) AS attributed_conversions
+ GROUP BY
+ campaign,
+ id,
+ source) AS conv
GROUP BY
- multiIf(and(in(lower(conv.source), ['google', 'adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), in(lower(conv.campaign), ['messy_campaign_1', 'messy_campaign_2', 'messy_campaign_3'])), 'Clean Campaign Name', conv.campaign),
- conv.id,
- conv.source
+ multiIf(and(in(lower(conv.source), ['google', 'adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), in(lower(conv.campaign), ['messy_campaign_1', 'messy_campaign_2', 'messy_campaign_3'])), 'Clean Campaign Name', conv.campaign),
+ conv.id,
+ conv.source
LIMIT 100
'''
# ---
@@ -141,45 +141,45 @@
'''
SELECT
- multiIf(and(in(lower(conv.source), ['google', 'adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), in(lower(conv.campaign), ['spring_sale_2024', 'spring-sale-2024', 'spring_sale'])), 'Spring Sale 2024', and(in(lower(conv.source), ['google', 'adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), in(lower(conv.campaign), ['bf_2024', 'blackfriday', 'bf-promo'])), 'Black Friday', and(in(lower(conv.source), ['meta', 'facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), in(lower(conv.campaign), ['spring_sale_fb', 'springsalefb'])), 'Spring Sale 2024', and(in(lower(conv.source), ['meta', 'facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), in(lower(conv.campaign), ['summer_2024', 'summer_promo'])), 'Summer Campaign', conv.campaign) AS campaign,
- conv.id AS id,
- conv.source AS source,
- multiIf(and(in(lower(conv.source), ['google', 'adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), in(lower(conv.campaign), ['spring_sale_2024', 'spring-sale-2024', 'spring_sale'])), 'Spring Sale 2024', and(in(lower(conv.source), ['google', 'adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), in(lower(conv.campaign), ['bf_2024', 'blackfriday', 'bf-promo'])), 'Black Friday', and(in(lower(conv.source), ['meta', 'facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), in(lower(conv.campaign), ['spring_sale_fb', 'springsalefb'])), 'Spring Sale 2024', and(in(lower(conv.source), ['meta', 'facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), in(lower(conv.campaign), ['summer_2024', 'summer_promo'])), 'Summer Campaign', conv.campaign) AS match_key,
- sum(conv.conversion_0) AS conversion_0
+ multiIf(and(in(lower(conv.source), ['google', 'adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), in(lower(conv.campaign), ['spring_sale_2024', 'spring-sale-2024', 'spring_sale'])), 'Spring Sale 2024', and(in(lower(conv.source), ['google', 'adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), in(lower(conv.campaign), ['bf_2024', 'blackfriday', 'bf-promo'])), 'Black Friday', and(in(lower(conv.source), ['meta', 'facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), in(lower(conv.campaign), ['spring_sale_fb', 'springsalefb'])), 'Spring Sale 2024', and(in(lower(conv.source), ['meta', 'facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), in(lower(conv.campaign), ['summer_2024', 'summer_promo'])), 'Summer Campaign', conv.campaign) AS campaign,
+ conv.id AS id,
+ conv.source AS source,
+ multiIf(and(in(lower(conv.source), ['google', 'adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), in(lower(conv.campaign), ['spring_sale_2024', 'spring-sale-2024', 'spring_sale'])), 'Spring Sale 2024', and(in(lower(conv.source), ['google', 'adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), in(lower(conv.campaign), ['bf_2024', 'blackfriday', 'bf-promo'])), 'Black Friday', and(in(lower(conv.source), ['meta', 'facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), in(lower(conv.campaign), ['spring_sale_fb', 'springsalefb'])), 'Spring Sale 2024', and(in(lower(conv.source), ['meta', 'facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), in(lower(conv.campaign), ['summer_2024', 'summer_promo'])), 'Summer Campaign', conv.campaign) AS match_key,
+ sum(conv.conversion_0) AS conversion_0
FROM
- (
- SELECT
- if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
- if(notEmpty(campaign_id), campaign_id, '-') AS id,
- if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
- count() AS conversion_0
+ (
+ SELECT
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
+ if(notEmpty(campaign_id), campaign_id, '-') AS id,
+ if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
+ count() AS conversion_0
+ FROM
+ (
+ SELECT
+ person_id,
+ campaign_name,
+ source_name,
+ medium_name,
+ content_name,
+ term_name,
+ referring_domain_name,
+ gclid_name,
+ fbclid_name,
+ gad_source_name,
+ '-' AS campaign_id,
+ multiply(conversion_value, touchpoint_weight) AS conversion_value
FROM
- (
- SELECT
- person_id,
- campaign_name,
- source_name,
- medium_name,
- content_name,
- term_name,
- referring_domain_name,
- gclid_name,
- fbclid_name,
- gad_source_name,
- '-' AS campaign_id,
- multiply(conversion_value, touchpoint_weight) AS conversion_value
- FROM
- conversion_goal_attributed_preaggregated
- WHERE
- and(in(job_id, tuple('00000000-0000-0000-0000-000000000001')), equals(team_id, 420), greaterOrEquals(conversion_timestamp, toDateTime('2023-01-01 00:00:00.000000')), lessOrEquals(conversion_timestamp, toDateTime('2023-01-31 23:59:59.999999')))) AS attributed_conversions
- GROUP BY
- campaign,
- id,
- source) AS conv
+ conversion_goal_attributed_preaggregated
+ WHERE
+ and(in(job_id, tuple('00000000-0000-0000-0000-000000000001')), equals(team_id, 420), greaterOrEquals(conversion_timestamp, toDateTime('2023-01-01 00:00:00.000000')), lessOrEquals(conversion_timestamp, toDateTime('2023-01-31 23:59:59.999999')))) AS attributed_conversions
+ GROUP BY
+ campaign,
+ id,
+ source) AS conv
GROUP BY
- multiIf(and(in(lower(conv.source), ['google', 'adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), in(lower(conv.campaign), ['spring_sale_2024', 'spring-sale-2024', 'spring_sale'])), 'Spring Sale 2024', and(in(lower(conv.source), ['google', 'adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), in(lower(conv.campaign), ['bf_2024', 'blackfriday', 'bf-promo'])), 'Black Friday', and(in(lower(conv.source), ['meta', 'facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), in(lower(conv.campaign), ['spring_sale_fb', 'springsalefb'])), 'Spring Sale 2024', and(in(lower(conv.source), ['meta', 'facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), in(lower(conv.campaign), ['summer_2024', 'summer_promo'])), 'Summer Campaign', conv.campaign),
- conv.id,
- conv.source
+ multiIf(and(in(lower(conv.source), ['google', 'adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), in(lower(conv.campaign), ['spring_sale_2024', 'spring-sale-2024', 'spring_sale'])), 'Spring Sale 2024', and(in(lower(conv.source), ['google', 'adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), in(lower(conv.campaign), ['bf_2024', 'blackfriday', 'bf-promo'])), 'Black Friday', and(in(lower(conv.source), ['meta', 'facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), in(lower(conv.campaign), ['spring_sale_fb', 'springsalefb'])), 'Spring Sale 2024', and(in(lower(conv.source), ['meta', 'facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), in(lower(conv.campaign), ['summer_2024', 'summer_promo'])), 'Summer Campaign', conv.campaign),
+ conv.id,
+ conv.source
LIMIT 100
'''
# ---
@@ -187,111 +187,111 @@
'''
SELECT
- conv.campaign AS campaign,
- conv.id AS id,
- conv.source AS source,
- conv.campaign AS match_key,
- sum(conv.conversion_0) AS conversion_0,
- sum(conv.conversion_1) AS conversion_1,
- sum(conv.conversion_2) AS conversion_2
+ conv.campaign AS campaign,
+ conv.id AS id,
+ conv.source AS source,
+ conv.campaign AS match_key,
+ sum(conv.conversion_0) AS conversion_0,
+ sum(conv.conversion_1) AS conversion_1,
+ sum(conv.conversion_2) AS conversion_2
FROM
- (
- SELECT
- if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
- if(notEmpty(campaign_id), campaign_id, '-') AS id,
- if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
- count() AS conversion_0,
- 0 AS conversion_1,
- 0 AS conversion_2
+ (
+ SELECT
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
+ if(notEmpty(campaign_id), campaign_id, '-') AS id,
+ if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
+ count() AS conversion_0,
+ 0 AS conversion_1,
+ 0 AS conversion_2
+ FROM
+ (
+ SELECT
+ person_id,
+ campaign_name,
+ source_name,
+ medium_name,
+ content_name,
+ term_name,
+ referring_domain_name,
+ gclid_name,
+ fbclid_name,
+ gad_source_name,
+ '-' AS campaign_id,
+ multiply(conversion_value, touchpoint_weight) AS conversion_value
FROM
- (
- SELECT
- person_id,
- campaign_name,
- source_name,
- medium_name,
- content_name,
- term_name,
- referring_domain_name,
- gclid_name,
- fbclid_name,
- gad_source_name,
- '-' AS campaign_id,
- multiply(conversion_value, touchpoint_weight) AS conversion_value
- FROM
- conversion_goal_attributed_preaggregated
- WHERE
- and(in(job_id, tuple('00000000-0000-0000-0000-000000000001')), equals(team_id, 420), greaterOrEquals(conversion_timestamp, toDateTime('2023-01-01 00:00:00.000000')), lessOrEquals(conversion_timestamp, toDateTime('2023-01-31 23:59:59.999999')))) AS attributed_conversions
- GROUP BY
- campaign,
- id,
- source
+ conversion_goal_attributed_preaggregated
+ WHERE
+ and(in(job_id, tuple('00000000-0000-0000-0000-000000000001')), equals(team_id, 420), greaterOrEquals(conversion_timestamp, toDateTime('2023-01-01 00:00:00.000000')), lessOrEquals(conversion_timestamp, toDateTime('2023-01-31 23:59:59.999999')))) AS attributed_conversions
+ GROUP BY
+ campaign,
+ id,
+ source
UNION ALL
+ SELECT
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
+ if(notEmpty(campaign_id), campaign_id, '-') AS id,
+ if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
+ 0 AS conversion_0,
+ count() AS conversion_1,
+ 0 AS conversion_2
+ FROM
+ (
SELECT
- if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
- if(notEmpty(campaign_id), campaign_id, '-') AS id,
- if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
- 0 AS conversion_0,
- count() AS conversion_1,
- 0 AS conversion_2
+ person_id,
+ campaign_name,
+ source_name,
+ medium_name,
+ content_name,
+ term_name,
+ referring_domain_name,
+ gclid_name,
+ fbclid_name,
+ gad_source_name,
+ '-' AS campaign_id,
+ multiply(conversion_value, touchpoint_weight) AS conversion_value
FROM
- (
- SELECT
- person_id,
- campaign_name,
- source_name,
- medium_name,
- content_name,
- term_name,
- referring_domain_name,
- gclid_name,
- fbclid_name,
- gad_source_name,
- '-' AS campaign_id,
- multiply(conversion_value, touchpoint_weight) AS conversion_value
- FROM
- conversion_goal_attributed_preaggregated
- WHERE
- and(in(job_id, tuple('00000000-0000-0000-0000-000000000002')), equals(team_id, 420), greaterOrEquals(conversion_timestamp, toDateTime('2023-01-01 00:00:00.000000')), lessOrEquals(conversion_timestamp, toDateTime('2023-01-31 23:59:59.999999')))) AS attributed_conversions
- GROUP BY
- campaign,
- id,
- source
+ conversion_goal_attributed_preaggregated
+ WHERE
+ and(in(job_id, tuple('00000000-0000-0000-0000-000000000002')), equals(team_id, 420), greaterOrEquals(conversion_timestamp, toDateTime('2023-01-01 00:00:00.000000')), lessOrEquals(conversion_timestamp, toDateTime('2023-01-31 23:59:59.999999')))) AS attributed_conversions
+ GROUP BY
+ campaign,
+ id,
+ source
UNION ALL
+ SELECT
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
+ if(notEmpty(campaign_id), campaign_id, '-') AS id,
+ if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
+ 0 AS conversion_0,
+ 0 AS conversion_1,
+ count() AS conversion_2
+ FROM
+ (
SELECT
- if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
- if(notEmpty(campaign_id), campaign_id, '-') AS id,
- if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
- 0 AS conversion_0,
- 0 AS conversion_1,
- count() AS conversion_2
+ person_id,
+ campaign_name,
+ source_name,
+ medium_name,
+ content_name,
+ term_name,
+ referring_domain_name,
+ gclid_name,
+ fbclid_name,
+ gad_source_name,
+ '-' AS campaign_id,
+ multiply(conversion_value, touchpoint_weight) AS conversion_value
FROM
- (
- SELECT
- person_id,
- campaign_name,
- source_name,
- medium_name,
- content_name,
- term_name,
- referring_domain_name,
- gclid_name,
- fbclid_name,
- gad_source_name,
- '-' AS campaign_id,
- multiply(conversion_value, touchpoint_weight) AS conversion_value
- FROM
- conversion_goal_attributed_preaggregated
- WHERE
- and(in(job_id, tuple('00000000-0000-0000-0000-000000000003')), equals(team_id, 420), greaterOrEquals(conversion_timestamp, toDateTime('2023-01-01 00:00:00.000000')), lessOrEquals(conversion_timestamp, toDateTime('2023-01-31 23:59:59.999999')))) AS attributed_conversions
- GROUP BY
- campaign,
- id,
- source) AS conv
+ conversion_goal_attributed_preaggregated
+ WHERE
+ and(in(job_id, tuple('00000000-0000-0000-0000-000000000003')), equals(team_id, 420), greaterOrEquals(conversion_timestamp, toDateTime('2023-01-01 00:00:00.000000')), lessOrEquals(conversion_timestamp, toDateTime('2023-01-31 23:59:59.999999')))) AS attributed_conversions
+ GROUP BY
+ campaign,
+ id,
+ source) AS conv
GROUP BY
- conv.campaign,
- conv.id,
- conv.source
+ conv.campaign,
+ conv.id,
+ conv.source
LIMIT 100
'''
# ---
@@ -299,77 +299,77 @@
'''
SELECT
- conv.campaign AS campaign,
- conv.id AS id,
- conv.source AS source,
- conv.campaign AS match_key,
- sum(conv.conversion_0) AS conversion_0,
- sum(conv.conversion_1) AS conversion_1
+ conv.campaign AS campaign,
+ conv.id AS id,
+ conv.source AS source,
+ conv.campaign AS match_key,
+ sum(conv.conversion_0) AS conversion_0,
+ sum(conv.conversion_1) AS conversion_1
FROM
- (
- SELECT
- if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
- if(notEmpty(campaign_id), campaign_id, '-') AS id,
- if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
- count() AS conversion_0,
- 0 AS conversion_1
+ (
+ SELECT
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
+ if(notEmpty(campaign_id), campaign_id, '-') AS id,
+ if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
+ count() AS conversion_0,
+ 0 AS conversion_1
+ FROM
+ (
+ SELECT
+ person_id,
+ campaign_name,
+ source_name,
+ medium_name,
+ content_name,
+ term_name,
+ referring_domain_name,
+ gclid_name,
+ fbclid_name,
+ gad_source_name,
+ '-' AS campaign_id,
+ multiply(conversion_value, touchpoint_weight) AS conversion_value
FROM
- (
- SELECT
- person_id,
- campaign_name,
- source_name,
- medium_name,
- content_name,
- term_name,
- referring_domain_name,
- gclid_name,
- fbclid_name,
- gad_source_name,
- '-' AS campaign_id,
- multiply(conversion_value, touchpoint_weight) AS conversion_value
- FROM
- conversion_goal_attributed_preaggregated
- WHERE
- and(in(job_id, tuple('00000000-0000-0000-0000-000000000001')), equals(team_id, 420), greaterOrEquals(conversion_timestamp, toDateTime('2023-01-01 00:00:00.000000')), lessOrEquals(conversion_timestamp, toDateTime('2023-01-31 23:59:59.999999')))) AS attributed_conversions
- GROUP BY
- campaign,
- id,
- source
+ conversion_goal_attributed_preaggregated
+ WHERE
+ and(in(job_id, tuple('00000000-0000-0000-0000-000000000001')), equals(team_id, 420), greaterOrEquals(conversion_timestamp, toDateTime('2023-01-01 00:00:00.000000')), lessOrEquals(conversion_timestamp, toDateTime('2023-01-31 23:59:59.999999')))) AS attributed_conversions
+ GROUP BY
+ campaign,
+ id,
+ source
UNION ALL
+ SELECT
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
+ if(notEmpty(campaign_id), campaign_id, '-') AS id,
+ if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
+ 0 AS conversion_0,
+ count() AS conversion_1
+ FROM
+ (
SELECT
- if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
- if(notEmpty(campaign_id), campaign_id, '-') AS id,
- if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
- 0 AS conversion_0,
- count() AS conversion_1
+ person_id,
+ campaign_name,
+ source_name,
+ medium_name,
+ content_name,
+ term_name,
+ referring_domain_name,
+ gclid_name,
+ fbclid_name,
+ gad_source_name,
+ '-' AS campaign_id,
+ multiply(conversion_value, touchpoint_weight) AS conversion_value
FROM
- (
- SELECT
- person_id,
- campaign_name,
- source_name,
- medium_name,
- content_name,
- term_name,
- referring_domain_name,
- gclid_name,
- fbclid_name,
- gad_source_name,
- '-' AS campaign_id,
- multiply(conversion_value, touchpoint_weight) AS conversion_value
- FROM
- conversion_goal_attributed_preaggregated
- WHERE
- and(in(job_id, tuple('00000000-0000-0000-0000-000000000002')), equals(team_id, 420), greaterOrEquals(conversion_timestamp, toDateTime('2023-01-01 00:00:00.000000')), lessOrEquals(conversion_timestamp, toDateTime('2023-01-31 23:59:59.999999')))) AS attributed_conversions
- GROUP BY
- campaign,
- id,
- source) AS conv
+ conversion_goal_attributed_preaggregated
+ WHERE
+ and(in(job_id, tuple('00000000-0000-0000-0000-000000000002')), equals(team_id, 420), greaterOrEquals(conversion_timestamp, toDateTime('2023-01-01 00:00:00.000000')), lessOrEquals(conversion_timestamp, toDateTime('2023-01-31 23:59:59.999999')))) AS attributed_conversions
+ GROUP BY
+ campaign,
+ id,
+ source) AS conv
GROUP BY
- conv.campaign,
- conv.id,
- conv.source
+ conv.campaign,
+ conv.id,
+ conv.source
LIMIT 100
'''
# ---
diff --git a/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_marketing_analytics_aggregated_query_runner.ambr b/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_marketing_analytics_aggregated_query_runner.ambr
index f4f496c12f52..0c5dd3a7721d 100644
--- a/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_marketing_analytics_aggregated_query_runner.ambr
+++ b/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_marketing_analytics_aggregated_query_runner.ambr
@@ -2,25 +2,25 @@
# name: TestMarketingAnalyticsAggregatedQueryRunner.test_join_uses_match_key_not_or_condition
'''
WITH campaign_costs AS (
- SELECT campaign, id, source, any(match_key) AS match_key, sum(ifNull(toFloat(cost), 0)) AS total_cost, sum(ifNull(toFloat(clicks), 0)) AS total_clicks, sum(ifNull(toFloat(impressions), 0)) AS total_impressions, sum(ifNull(toFloat(reported_conversion), 0)) AS total_reported_conversions, sum(ifNull(toFloat(reported_conversion_value), 0)) AS total_reported_conversion_value
- FROM (
- SELECT 'Campaign' AS campaign, 'id1' AS id, 'google' AS source, 100 AS impressions, 10 AS clicks, 50.0 AS cost, 5 AS reported_conversion, 'Campaign' AS match_key)
- GROUP BY campaign, id, source), ucg AS (
- SELECT conv.campaign AS campaign, conv.id AS id, conv.source AS source, conv.campaign AS match_key, sum(conv.conversion_0) AS conversion_0
- FROM (
- SELECT if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign, if(notEmpty(campaign_id), campaign_id, '-') AS id, if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source, count() AS conversion_0
- FROM (
- SELECT person_id, if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name, if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name, if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name, if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name, if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name, if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name, if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name, if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name, if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name, '-' AS campaign_id, 1 AS conversion_value
- FROM (
- SELECT person_id, conversion_timestamps[i] AS conversion_time, conversion_math_values[i] AS conversion_math_value, conversion_campaigns[i] AS conversion_campaign, conversion_sources[i] AS conversion_source, conversion_mediums[i] AS conversion_medium, conversion_contents[i] AS conversion_content, conversion_terms[i] AS conversion_term, conversion_referring_domains[i] AS conversion_referring_domain, conversion_gclids[i] AS conversion_gclid, conversion_fbclids[i] AS conversion_fbclid, conversion_gad_sources[i] AS conversion_gad_source, arrayMax(arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 7776000))), utm_timestamps)) AS last_utm_timestamp, if(isNotNull(last_utm_timestamp), utm_campaigns[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_campaign, if(isNotNull(last_utm_timestamp), utm_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_source, if(isNotNull(last_utm_timestamp), utm_mediums[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_medium, if(isNotNull(last_utm_timestamp), utm_contents[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_content, if(isNotNull(last_utm_timestamp), utm_terms[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_term, if(isNotNull(last_utm_timestamp), utm_referring_domains[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_referring_domain, if(isNotNull(last_utm_timestamp), utm_gclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gclid, if(isNotNull(last_utm_timestamp), utm_fbclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_fbclid, if(isNotNull(last_utm_timestamp), utm_gad_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gad_source
- FROM (
- SELECT events.person_id, arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps, arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toFloat(1), 0))) AS conversion_math_values, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources, arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
- FROM events
- WHERE or(and(equals(events.event, 'purchase'), greaterOrEquals(events.timestamp, toDateTime('2023-01-01 00:00:00')), lessOrEquals(events.timestamp, toDateTime('2023-01-31 23:59:59'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2023-01-01 00:00:00'), toIntervalSecond(7776000))), lessOrEquals(events.timestamp, toDateTime('2023-01-31 23:59:59'))))
- GROUP BY events.person_id
- HAVING greater(length(conversion_timestamps), 0)) ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversions
- GROUP BY campaign, id, source) AS conv
- GROUP BY conv.campaign, conv.id, conv.source)
+ SELECT campaign, id, source, any(match_key) AS match_key, sum(ifNull(toFloat(cost), 0)) AS total_cost, sum(ifNull(toFloat(clicks), 0)) AS total_clicks, sum(ifNull(toFloat(impressions), 0)) AS total_impressions, sum(ifNull(toFloat(reported_conversion), 0)) AS total_reported_conversions, sum(ifNull(toFloat(reported_conversion_value), 0)) AS total_reported_conversion_value
+ FROM (
+ SELECT 'Campaign' AS campaign, 'id1' AS id, 'google' AS source, 100 AS impressions, 10 AS clicks, 50.0 AS cost, 5 AS reported_conversion, 'Campaign' AS match_key)
+ GROUP BY campaign, id, source), ucg AS (
+ SELECT conv.campaign AS campaign, conv.id AS id, conv.source AS source, conv.campaign AS match_key, sum(conv.conversion_0) AS conversion_0
+ FROM (
+ SELECT if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign, if(notEmpty(campaign_id), campaign_id, '-') AS id, if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source, count() AS conversion_0
+ FROM (
+ SELECT person_id, if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name, if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name, if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name, if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name, if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name, if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name, if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name, if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name, if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name, '-' AS campaign_id, 1 AS conversion_value
+ FROM (
+ SELECT person_id, conversion_timestamps[i] AS conversion_time, conversion_math_values[i] AS conversion_math_value, conversion_campaigns[i] AS conversion_campaign, conversion_sources[i] AS conversion_source, conversion_mediums[i] AS conversion_medium, conversion_contents[i] AS conversion_content, conversion_terms[i] AS conversion_term, conversion_referring_domains[i] AS conversion_referring_domain, conversion_gclids[i] AS conversion_gclid, conversion_fbclids[i] AS conversion_fbclid, conversion_gad_sources[i] AS conversion_gad_source, arrayMax(arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 7776000))), utm_timestamps)) AS last_utm_timestamp, if(isNotNull(last_utm_timestamp), utm_campaigns[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_campaign, if(isNotNull(last_utm_timestamp), utm_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_source, if(isNotNull(last_utm_timestamp), utm_mediums[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_medium, if(isNotNull(last_utm_timestamp), utm_contents[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_content, if(isNotNull(last_utm_timestamp), utm_terms[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_term, if(isNotNull(last_utm_timestamp), utm_referring_domains[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_referring_domain, if(isNotNull(last_utm_timestamp), utm_gclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gclid, if(isNotNull(last_utm_timestamp), utm_fbclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_fbclid, if(isNotNull(last_utm_timestamp), utm_gad_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gad_source
+ FROM (
+ SELECT events.person_id, arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps, arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'purchase'), toFloat(1), 0))) AS conversion_math_values, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids, arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'purchase'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources, arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids, arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
+ FROM events
+ WHERE or(and(equals(events.event, 'purchase'), greaterOrEquals(events.timestamp, toDateTime('2023-01-01 00:00:00')), lessOrEquals(events.timestamp, toDateTime('2023-01-31 23:59:59'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2023-01-01 00:00:00'), toIntervalSecond(7776000))), lessOrEquals(events.timestamp, toDateTime('2023-01-31 23:59:59'))))
+ GROUP BY events.person_id
+ HAVING greater(length(conversion_timestamps), 0)) ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversions
+ GROUP BY campaign, id, source) AS conv
+ GROUP BY conv.campaign, conv.id, conv.source)
SELECT sum(round(campaign_costs.total_cost, 2)) AS Cost, sum(round(campaign_costs.total_clicks, 0)) AS Clicks, sum(round(campaign_costs.total_impressions, 0)) AS Impressions, sum(round(campaign_costs.total_reported_conversions, 2)) AS `Reported Conversions`, sum(round(campaign_costs.total_reported_conversion_value, 2)) AS `Reported Conversion Value`, sum(ucg.conversion_0) AS `Test Purchase`
FROM campaign_costs LEFT JOIN ucg AS ucg ON and(equals(campaign_costs.match_key, ucg.match_key), equals(campaign_costs.source, ucg.source))
'''
diff --git a/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_marketing_analytics_table_query_runner.ambr b/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_marketing_analytics_table_query_runner.ambr
index d27a2854a0a6..1b84ed76c3e3 100644
--- a/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_marketing_analytics_table_query_runner.ambr
+++ b/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_marketing_analytics_table_query_runner.ambr
@@ -3,28 +3,28 @@
'''
SELECT
- multiIf(match(lower(nullIf(nullIf('', ''), 'null')), 'cross-network'), 'Cross Network', or(in(lower(nullIf(nullIf('cpc', ''), 'null')), tuple('cpc', 'cpm', 'cpv', 'cpa', 'ppc', 'retargeting')), startsWith(lower(nullIf(nullIf('cpc', ''), 'null')), 'paid'), false, notEquals(nullIf(nullIf(NULL, ''), 'null'), NULL)), coalesce(lookupPaidSourceType(lower(nullIf(nullIf(if(equals(lower(source), 'meta'), 'facebook', source), ''), 'null'))), if(match(lower(nullIf(nullIf('', ''), 'null')), '^(.*(([^a-df-z]|^)shop|shopping).*)$'), 'Paid Shopping', NULL), lookupPaidMediumType(lower(nullIf(nullIf('cpc', ''), 'null'))), lookupPaidSourceType('$direct'), multiIf(equals(nullIf(nullIf(NULL, ''), 'null'), '1'), 'Paid Search', match(lower(nullIf(nullIf('', ''), 'null')), '^(.*video.*)$'), 'Paid Video', false, 'Paid Social', 'Paid Unknown')), and(equals('$direct', '$direct'), equals(lower(nullIf(nullIf('cpc', ''), 'null')), NULL), or(equals(lower(nullIf(nullIf(if(equals(lower(source), 'meta'), 'facebook', source), ''), 'null')), NULL), in(lower(nullIf(nullIf(if(equals(lower(source), 'meta'), 'facebook', source), ''), 'null')), tuple('(direct)', 'direct', '$direct'))), not(false)), 'Direct', coalesce(lookupOrganicSourceType(lower(nullIf(nullIf(if(equals(lower(source), 'meta'), 'facebook', source), ''), 'null'))), if(match(lower(nullIf(nullIf('', ''), 'null')), '^(.*(([^a-df-z]|^)shop|shopping).*)$'), 'Organic Shopping', NULL), lookupOrganicMediumType(lower(nullIf(nullIf('cpc', ''), 'null'))), lookupOrganicSourceType('$direct'), multiIf(match(lower(nullIf(nullIf('', ''), 'null')), '^(.*video.*)$'), 'Organic Video', match(lower(nullIf(nullIf('cpc', ''), 'null')), 'push$'), 'Push', false, 'Organic Social', equals('$direct', '$direct'), 'Direct', notEquals('$direct', NULL), 'Referral', 'Unknown'))) AS campaign,
- '' AS id,
- '' AS match_key,
- sum(ifNull(toFloat(cost), 0)) AS total_cost,
- sum(ifNull(toFloat(clicks), 0)) AS total_clicks,
- sum(ifNull(toFloat(impressions), 0)) AS total_impressions,
- sum(ifNull(toFloat(reported_conversion), 0)) AS total_reported_conversions,
- sum(ifNull(toFloat(reported_conversion_value), 0)) AS total_reported_conversion_value
+ multiIf(match(lower(nullIf(nullIf('', ''), 'null')), 'cross-network'), 'Cross Network', or(in(lower(nullIf(nullIf('cpc', ''), 'null')), tuple('cpc', 'cpm', 'cpv', 'cpa', 'ppc', 'retargeting')), startsWith(lower(nullIf(nullIf('cpc', ''), 'null')), 'paid'), false, notEquals(nullIf(nullIf(NULL, ''), 'null'), NULL)), coalesce(lookupPaidSourceType(lower(nullIf(nullIf(if(equals(lower(source), 'meta'), 'facebook', source), ''), 'null'))), if(match(lower(nullIf(nullIf('', ''), 'null')), '^(.*(([^a-df-z]|^)shop|shopping).*)$'), 'Paid Shopping', NULL), lookupPaidMediumType(lower(nullIf(nullIf('cpc', ''), 'null'))), lookupPaidSourceType('$direct'), multiIf(equals(nullIf(nullIf(NULL, ''), 'null'), '1'), 'Paid Search', match(lower(nullIf(nullIf('', ''), 'null')), '^(.*video.*)$'), 'Paid Video', false, 'Paid Social', 'Paid Unknown')), and(equals('$direct', '$direct'), equals(lower(nullIf(nullIf('cpc', ''), 'null')), NULL), or(equals(lower(nullIf(nullIf(if(equals(lower(source), 'meta'), 'facebook', source), ''), 'null')), NULL), in(lower(nullIf(nullIf(if(equals(lower(source), 'meta'), 'facebook', source), ''), 'null')), tuple('(direct)', 'direct', '$direct'))), not(false)), 'Direct', coalesce(lookupOrganicSourceType(lower(nullIf(nullIf(if(equals(lower(source), 'meta'), 'facebook', source), ''), 'null'))), if(match(lower(nullIf(nullIf('', ''), 'null')), '^(.*(([^a-df-z]|^)shop|shopping).*)$'), 'Organic Shopping', NULL), lookupOrganicMediumType(lower(nullIf(nullIf('cpc', ''), 'null'))), lookupOrganicSourceType('$direct'), multiIf(match(lower(nullIf(nullIf('', ''), 'null')), '^(.*video.*)$'), 'Organic Video', match(lower(nullIf(nullIf('cpc', ''), 'null')), 'push$'), 'Push', false, 'Organic Social', equals('$direct', '$direct'), 'Direct', notEquals('$direct', NULL), 'Referral', 'Unknown'))) AS campaign,
+ '' AS id,
+ '' AS match_key,
+ sum(ifNull(toFloat(cost), 0)) AS total_cost,
+ sum(ifNull(toFloat(clicks), 0)) AS total_clicks,
+ sum(ifNull(toFloat(impressions), 0)) AS total_impressions,
+ sum(ifNull(toFloat(reported_conversion), 0)) AS total_reported_conversions,
+ sum(ifNull(toFloat(reported_conversion_value), 0)) AS total_reported_conversion_value
FROM
- (
- SELECT
- '' AS match_key,
- 'campaign_meta' AS campaign,
- 'id_meta' AS id,
- 'meta' AS source,
- 0.0 AS impressions,
- 0.0 AS clicks,
- 0.0 AS cost,
- 0.0 AS reported_conversion,
- 0.0 AS reported_conversion_value)
+ (
+ SELECT
+ '' AS match_key,
+ 'campaign_meta' AS campaign,
+ 'id_meta' AS id,
+ 'meta' AS source,
+ 0.0 AS impressions,
+ 0.0 AS clicks,
+ 0.0 AS cost,
+ 0.0 AS reported_conversion,
+ 0.0 AS reported_conversion_value)
GROUP BY
- multiIf(match(lower(nullIf(nullIf('', ''), 'null')), 'cross-network'), 'Cross Network', or(in(lower(nullIf(nullIf('cpc', ''), 'null')), tuple('cpc', 'cpm', 'cpv', 'cpa', 'ppc', 'retargeting')), startsWith(lower(nullIf(nullIf('cpc', ''), 'null')), 'paid'), false, notEquals(nullIf(nullIf(NULL, ''), 'null'), NULL)), coalesce(lookupPaidSourceType(lower(nullIf(nullIf(if(equals(lower(source), 'meta'), 'facebook', source), ''), 'null'))), if(match(lower(nullIf(nullIf('', ''), 'null')), '^(.*(([^a-df-z]|^)shop|shopping).*)$'), 'Paid Shopping', NULL), lookupPaidMediumType(lower(nullIf(nullIf('cpc', ''), 'null'))), lookupPaidSourceType('$direct'), multiIf(equals(nullIf(nullIf(NULL, ''), 'null'), '1'), 'Paid Search', match(lower(nullIf(nullIf('', ''), 'null')), '^(.*video.*)$'), 'Paid Video', false, 'Paid Social', 'Paid Unknown')), and(equals('$direct', '$direct'), equals(lower(nullIf(nullIf('cpc', ''), 'null')), NULL), or(equals(lower(nullIf(nullIf(if(equals(lower(source), 'meta'), 'facebook', source), ''), 'null')), NULL), in(lower(nullIf(nullIf(if(equals(lower(source), 'meta'), 'facebook', source), ''), 'null')), tuple('(direct)', 'direct', '$direct'))), not(false)), 'Direct', coalesce(lookupOrganicSourceType(lower(nullIf(nullIf(if(equals(lower(source), 'meta'), 'facebook', source), ''), 'null'))), if(match(lower(nullIf(nullIf('', ''), 'null')), '^(.*(([^a-df-z]|^)shop|shopping).*)$'), 'Organic Shopping', NULL), lookupOrganicMediumType(lower(nullIf(nullIf('cpc', ''), 'null'))), lookupOrganicSourceType('$direct'), multiIf(match(lower(nullIf(nullIf('', ''), 'null')), '^(.*video.*)$'), 'Organic Video', match(lower(nullIf(nullIf('cpc', ''), 'null')), 'push$'), 'Push', false, 'Organic Social', equals('$direct', '$direct'), 'Direct', notEquals('$direct', NULL), 'Referral', 'Unknown')))
+ multiIf(match(lower(nullIf(nullIf('', ''), 'null')), 'cross-network'), 'Cross Network', or(in(lower(nullIf(nullIf('cpc', ''), 'null')), tuple('cpc', 'cpm', 'cpv', 'cpa', 'ppc', 'retargeting')), startsWith(lower(nullIf(nullIf('cpc', ''), 'null')), 'paid'), false, notEquals(nullIf(nullIf(NULL, ''), 'null'), NULL)), coalesce(lookupPaidSourceType(lower(nullIf(nullIf(if(equals(lower(source), 'meta'), 'facebook', source), ''), 'null'))), if(match(lower(nullIf(nullIf('', ''), 'null')), '^(.*(([^a-df-z]|^)shop|shopping).*)$'), 'Paid Shopping', NULL), lookupPaidMediumType(lower(nullIf(nullIf('cpc', ''), 'null'))), lookupPaidSourceType('$direct'), multiIf(equals(nullIf(nullIf(NULL, ''), 'null'), '1'), 'Paid Search', match(lower(nullIf(nullIf('', ''), 'null')), '^(.*video.*)$'), 'Paid Video', false, 'Paid Social', 'Paid Unknown')), and(equals('$direct', '$direct'), equals(lower(nullIf(nullIf('cpc', ''), 'null')), NULL), or(equals(lower(nullIf(nullIf(if(equals(lower(source), 'meta'), 'facebook', source), ''), 'null')), NULL), in(lower(nullIf(nullIf(if(equals(lower(source), 'meta'), 'facebook', source), ''), 'null')), tuple('(direct)', 'direct', '$direct'))), not(false)), 'Direct', coalesce(lookupOrganicSourceType(lower(nullIf(nullIf(if(equals(lower(source), 'meta'), 'facebook', source), ''), 'null'))), if(match(lower(nullIf(nullIf('', ''), 'null')), '^(.*(([^a-df-z]|^)shop|shopping).*)$'), 'Organic Shopping', NULL), lookupOrganicMediumType(lower(nullIf(nullIf('cpc', ''), 'null'))), lookupOrganicSourceType('$direct'), multiIf(match(lower(nullIf(nullIf('', ''), 'null')), '^(.*video.*)$'), 'Organic Video', match(lower(nullIf(nullIf('cpc', ''), 'null')), 'push$'), 'Push', false, 'Organic Social', equals('$direct', '$direct'), 'Direct', notEquals('$direct', NULL), 'Referral', 'Unknown')))
LIMIT 100
'''
# ---
diff --git a/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_marketing_analytics_table_query_runner_business.ambr b/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_marketing_analytics_table_query_runner_business.ambr
index ac2d90b9d9b0..9b415d9d05f9 100644
--- a/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_marketing_analytics_table_query_runner_business.ambr
+++ b/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_marketing_analytics_table_query_runner_business.ambr
@@ -2,54 +2,54 @@
# name: TestMarketingAnalyticsTableQueryRunnerBusiness.test_comprehensive_marketing_analytics_basic
'''
WITH
- campaign_costs AS (
- SELECT
- campaign,
- id,
- source,
- any(match_key) AS match_key,
- sum(ifNull(toFloat(cost), 0)) AS total_cost,
- sum(ifNull(toFloat(clicks), 0)) AS total_clicks,
- sum(ifNull(toFloat(impressions), 0)) AS total_impressions,
- sum(ifNull(toFloat(reported_conversion), 0)) AS total_reported_conversions,
- sum(ifNull(toFloat(reported_conversion_value), 0)) AS total_reported_conversion_value
- FROM
- (
- SELECT
- toString(campaign1) AS match_key,
- toString(campaign1) AS campaign,
- toString(campaign1) AS id,
- toString('Facebook Ads') AS source,
- toFloat(coalesce(toFloat(impressions1), 0)) AS impressions,
- toFloat(coalesce(toFloat(clicks1), 0)) AS clicks,
- toFloat(convertCurrency('USD', 'USD', coalesce(toFloat(spend1), 0))) AS cost,
- toFloat(coalesce(toFloat(conversions1), 0)) AS reported_conversion,
- 0 AS reported_conversion_value
- FROM
- `bigquery.posthog_test.posthog_test_facebook_ads_table` AS posthog_test_facebook_ads_table
- WHERE
- and(greaterOrEquals(toDateTime(date1), toDateTime('2024-11-01 00:00:00')), lessOrEquals(toDateTime(date1), toDateTime('2024-11-30 23:59:59'))))
- GROUP BY
- campaign,
- id,
- source)
+ campaign_costs AS (
+ SELECT
+ campaign,
+ id,
+ source,
+ any(match_key) AS match_key,
+ sum(ifNull(toFloat(cost), 0)) AS total_cost,
+ sum(ifNull(toFloat(clicks), 0)) AS total_clicks,
+ sum(ifNull(toFloat(impressions), 0)) AS total_impressions,
+ sum(ifNull(toFloat(reported_conversion), 0)) AS total_reported_conversions,
+ sum(ifNull(toFloat(reported_conversion_value), 0)) AS total_reported_conversion_value
+ FROM
+ (
+ SELECT
+ toString(campaign1) AS match_key,
+ toString(campaign1) AS campaign,
+ toString(campaign1) AS id,
+ toString('Facebook Ads') AS source,
+ toFloat(coalesce(toFloat(impressions1), 0)) AS impressions,
+ toFloat(coalesce(toFloat(clicks1), 0)) AS clicks,
+ toFloat(convertCurrency('USD', 'USD', coalesce(toFloat(spend1), 0))) AS cost,
+ toFloat(coalesce(toFloat(conversions1), 0)) AS reported_conversion,
+ 0 AS reported_conversion_value
+ FROM
+ `bigquery.posthog_test.posthog_test_facebook_ads_table` AS posthog_test_facebook_ads_table
+ WHERE
+ and(greaterOrEquals(toDateTime(date1), toDateTime('2024-11-01 00:00:00')), lessOrEquals(toDateTime(date1), toDateTime('2024-11-30 23:59:59'))))
+ GROUP BY
+ campaign,
+ id,
+ source)
SELECT
- campaign_costs.id AS ID,
- campaign_costs.campaign AS Campaign,
- campaign_costs.source AS Source,
- round(campaign_costs.total_cost, 2) AS Cost,
- round(campaign_costs.total_clicks, 0) AS Clicks,
- round(campaign_costs.total_impressions, 0) AS Impressions,
- round(divide(campaign_costs.total_cost, nullif(campaign_costs.total_clicks, 0)), 2) AS CPC,
- round(multiply(divide(campaign_costs.total_clicks, nullif(campaign_costs.total_impressions, 0)), 100), 2) AS CTR,
- round(campaign_costs.total_reported_conversions, 2) AS `Reported Conversions`,
- round(campaign_costs.total_reported_conversion_value, 2) AS `Reported Conversion Value`,
- round(divide(campaign_costs.total_reported_conversion_value, nullif(campaign_costs.total_cost, 0)), 2) AS `Reported ROAS`,
- round(divide(campaign_costs.total_cost, nullif(campaign_costs.total_reported_conversions, 0)), 2) AS `Cost per Reported Conversions`
+ campaign_costs.id AS ID,
+ campaign_costs.campaign AS Campaign,
+ campaign_costs.source AS Source,
+ round(campaign_costs.total_cost, 2) AS Cost,
+ round(campaign_costs.total_clicks, 0) AS Clicks,
+ round(campaign_costs.total_impressions, 0) AS Impressions,
+ round(divide(campaign_costs.total_cost, nullif(campaign_costs.total_clicks, 0)), 2) AS CPC,
+ round(multiply(divide(campaign_costs.total_clicks, nullif(campaign_costs.total_impressions, 0)), 100), 2) AS CTR,
+ round(campaign_costs.total_reported_conversions, 2) AS `Reported Conversions`,
+ round(campaign_costs.total_reported_conversion_value, 2) AS `Reported Conversion Value`,
+ round(divide(campaign_costs.total_reported_conversion_value, nullif(campaign_costs.total_cost, 0)), 2) AS `Reported ROAS`,
+ round(divide(campaign_costs.total_cost, nullif(campaign_costs.total_reported_conversions, 0)), 2) AS `Cost per Reported Conversions`
FROM
- campaign_costs
+ campaign_costs
ORDER BY
- ID ASC
+ ID ASC
LIMIT 101
OFFSET 0
'''
@@ -57,154 +57,154 @@
# name: TestMarketingAnalyticsTableQueryRunnerBusiness.test_conversion_goal_basic_setup
'''
WITH
- campaign_costs AS (
- SELECT
- campaign,
- id,
- source,
- any(match_key) AS match_key,
- sum(ifNull(toFloat(cost), 0)) AS total_cost,
- sum(ifNull(toFloat(clicks), 0)) AS total_clicks,
- sum(ifNull(toFloat(impressions), 0)) AS total_impressions,
- sum(ifNull(toFloat(reported_conversion), 0)) AS total_reported_conversions,
- sum(ifNull(toFloat(reported_conversion_value), 0)) AS total_reported_conversion_value
- FROM
- (
- SELECT
- toString(campaign1) AS match_key,
- toString(campaign1) AS campaign,
- toString(campaign1) AS id,
- toString('Facebook Ads') AS source,
- toFloat(coalesce(toFloat(impressions1), 0)) AS impressions,
- toFloat(coalesce(toFloat(clicks1), 0)) AS clicks,
- toFloat(convertCurrency('USD', 'USD', coalesce(toFloat(spend1), 0))) AS cost,
- toFloat(coalesce(toFloat(conversions1), 0)) AS reported_conversion,
- 0 AS reported_conversion_value
- FROM
- `bigquery.posthog_test.posthog_test_facebook_ads_table` AS posthog_test_facebook_ads_table
- WHERE
- and(greaterOrEquals(toDateTime(date1), toDateTime('2024-01-01 00:00:00')), lessOrEquals(toDateTime(date1), toDateTime('2024-12-31 23:59:59'))))
- GROUP BY
- campaign,
- id,
- source),
- ucg AS (
- SELECT
- conv.campaign AS campaign,
- conv.id AS id,
- conv.source AS source,
- conv.campaign AS match_key,
- sum(conv.conversion_0) AS conversion_0
+ campaign_costs AS (
+ SELECT
+ campaign,
+ id,
+ source,
+ any(match_key) AS match_key,
+ sum(ifNull(toFloat(cost), 0)) AS total_cost,
+ sum(ifNull(toFloat(clicks), 0)) AS total_clicks,
+ sum(ifNull(toFloat(impressions), 0)) AS total_impressions,
+ sum(ifNull(toFloat(reported_conversion), 0)) AS total_reported_conversions,
+ sum(ifNull(toFloat(reported_conversion_value), 0)) AS total_reported_conversion_value
+ FROM
+ (
+ SELECT
+ toString(campaign1) AS match_key,
+ toString(campaign1) AS campaign,
+ toString(campaign1) AS id,
+ toString('Facebook Ads') AS source,
+ toFloat(coalesce(toFloat(impressions1), 0)) AS impressions,
+ toFloat(coalesce(toFloat(clicks1), 0)) AS clicks,
+ toFloat(convertCurrency('USD', 'USD', coalesce(toFloat(spend1), 0))) AS cost,
+ toFloat(coalesce(toFloat(conversions1), 0)) AS reported_conversion,
+ 0 AS reported_conversion_value
+ FROM
+ `bigquery.posthog_test.posthog_test_facebook_ads_table` AS posthog_test_facebook_ads_table
+ WHERE
+ and(greaterOrEquals(toDateTime(date1), toDateTime('2024-01-01 00:00:00')), lessOrEquals(toDateTime(date1), toDateTime('2024-12-31 23:59:59'))))
+ GROUP BY
+ campaign,
+ id,
+ source),
+ ucg AS (
+ SELECT
+ conv.campaign AS campaign,
+ conv.id AS id,
+ conv.source AS source,
+ conv.campaign AS match_key,
+ sum(conv.conversion_0) AS conversion_0
+ FROM
+ (
+ SELECT
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
+ if(notEmpty(campaign_id), campaign_id, '-') AS id,
+ if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
+ count() AS conversion_0
+ FROM
+ (
+ SELECT
+ person_id,
+ if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name,
+ if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name,
+ if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name,
+ if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name,
+ if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name,
+ if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name,
+ if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name,
+ if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name,
+ if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name,
+ '-' AS campaign_id,
+ 1 AS conversion_value
+ FROM
+ (
+ SELECT
+ person_id,
+ conversion_timestamps[i] AS conversion_time,
+ conversion_math_values[i] AS conversion_math_value,
+ conversion_campaigns[i] AS conversion_campaign,
+ conversion_sources[i] AS conversion_source,
+ conversion_mediums[i] AS conversion_medium,
+ conversion_contents[i] AS conversion_content,
+ conversion_terms[i] AS conversion_term,
+ conversion_referring_domains[i] AS conversion_referring_domain,
+ conversion_gclids[i] AS conversion_gclid,
+ conversion_fbclids[i] AS conversion_fbclid,
+ conversion_gad_sources[i] AS conversion_gad_source,
+ arrayMax(arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 7776000))), utm_timestamps)) AS last_utm_timestamp,
+ if(isNotNull(last_utm_timestamp), utm_campaigns[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_campaign,
+ if(isNotNull(last_utm_timestamp), utm_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_source,
+ if(isNotNull(last_utm_timestamp), utm_mediums[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_medium,
+ if(isNotNull(last_utm_timestamp), utm_contents[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_content,
+ if(isNotNull(last_utm_timestamp), utm_terms[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_term,
+ if(isNotNull(last_utm_timestamp), utm_referring_domains[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_referring_domain,
+ if(isNotNull(last_utm_timestamp), utm_gclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gclid,
+ if(isNotNull(last_utm_timestamp), utm_fbclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_fbclid,
+ if(isNotNull(last_utm_timestamp), utm_gad_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gad_source
FROM
- (
- SELECT
- if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
- if(notEmpty(campaign_id), campaign_id, '-') AS id,
- if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
- count() AS conversion_0
- FROM
- (
- SELECT
- person_id,
- if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name,
- if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name,
- if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name,
- if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name,
- if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name,
- if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name,
- if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name,
- if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name,
- if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name,
- '-' AS campaign_id,
- 1 AS conversion_value
- FROM
- (
- SELECT
- person_id,
- conversion_timestamps[i] AS conversion_time,
- conversion_math_values[i] AS conversion_math_value,
- conversion_campaigns[i] AS conversion_campaign,
- conversion_sources[i] AS conversion_source,
- conversion_mediums[i] AS conversion_medium,
- conversion_contents[i] AS conversion_content,
- conversion_terms[i] AS conversion_term,
- conversion_referring_domains[i] AS conversion_referring_domain,
- conversion_gclids[i] AS conversion_gclid,
- conversion_fbclids[i] AS conversion_fbclid,
- conversion_gad_sources[i] AS conversion_gad_source,
- arrayMax(arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 7776000))), utm_timestamps)) AS last_utm_timestamp,
- if(isNotNull(last_utm_timestamp), utm_campaigns[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_campaign,
- if(isNotNull(last_utm_timestamp), utm_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_source,
- if(isNotNull(last_utm_timestamp), utm_mediums[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_medium,
- if(isNotNull(last_utm_timestamp), utm_contents[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_content,
- if(isNotNull(last_utm_timestamp), utm_terms[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_term,
- if(isNotNull(last_utm_timestamp), utm_referring_domains[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_referring_domain,
- if(isNotNull(last_utm_timestamp), utm_gclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gclid,
- if(isNotNull(last_utm_timestamp), utm_fbclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_fbclid,
- if(isNotNull(last_utm_timestamp), utm_gad_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gad_source
- FROM
- (
+ (
+ SELECT
+ events.person_id,
+ arrayFilter(x -> greater(x, 0), groupArray(if(equals(event, 'test_event'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps,
+ arrayFilter(x -> greater(x, 0), groupArray(if(equals(event, 'test_event'), toFloat(1), 0))) AS conversion_math_values,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources,
+ arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
+ FROM
+ events
+ WHERE
+ or(and(equals(event, 'test_event'), greaterOrEquals(events.timestamp, toDateTime('2024-01-01 00:00:00')), lessOrEquals(events.timestamp, toDateTime('2024-12-31 23:59:59'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2024-01-01 00:00:00'), toIntervalSecond(7776000))), lessOrEquals(events.timestamp, toDateTime('2024-12-31 23:59:59'))))
+ GROUP BY
+ events.person_id
+ HAVING
+ greater(length(conversion_timestamps), 0))
+ ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversions
+ GROUP BY
+ campaign,
+ id,
+ source) AS conv
+ GROUP BY
+ conv.campaign,
+ conv.id,
+ conv.source)
SELECT
- events.person_id,
- arrayFilter(x -> greater(x, 0), groupArray(if(equals(event, 'test_event'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps,
- arrayFilter(x -> greater(x, 0), groupArray(if(equals(event, 'test_event'), toFloat(1), 0))) AS conversion_math_values,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources,
- arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
- FROM
- events
- WHERE
- or(and(equals(event, 'test_event'), greaterOrEquals(events.timestamp, toDateTime('2024-01-01 00:00:00')), lessOrEquals(events.timestamp, toDateTime('2024-12-31 23:59:59'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2024-01-01 00:00:00'), toIntervalSecond(7776000))), lessOrEquals(events.timestamp, toDateTime('2024-12-31 23:59:59'))))
- GROUP BY
- events.person_id
- HAVING
- greater(length(conversion_timestamps), 0))
- ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversions
- GROUP BY
- campaign,
- id,
- source) AS conv
- GROUP BY
- conv.campaign,
- conv.id,
- conv.source)
- SELECT
- campaign_costs.id AS ID,
- campaign_costs.campaign AS Campaign,
- campaign_costs.source AS Source,
- round(campaign_costs.total_cost, 2) AS Cost,
- round(campaign_costs.total_clicks, 0) AS Clicks,
- round(campaign_costs.total_impressions, 0) AS Impressions,
- round(divide(campaign_costs.total_cost, nullif(campaign_costs.total_clicks, 0)), 2) AS CPC,
- round(multiply(divide(campaign_costs.total_clicks, nullif(campaign_costs.total_impressions, 0)), 100), 2) AS CTR,
- round(campaign_costs.total_reported_conversions, 2) AS `Reported Conversions`,
- round(campaign_costs.total_reported_conversion_value, 2) AS `Reported Conversion Value`,
- round(divide(campaign_costs.total_reported_conversion_value, nullif(campaign_costs.total_cost, 0)), 2) AS `Reported ROAS`,
- round(divide(campaign_costs.total_cost, nullif(campaign_costs.total_reported_conversions, 0)), 2) AS `Cost per Reported Conversions`,
- ucg.conversion_0 AS `Sign Up Conversions`,
- round(divide(campaign_costs.total_cost, nullif(ucg.conversion_0, 0)), 2) AS `Cost per Sign Up Conversions`
+ campaign_costs.id AS ID,
+ campaign_costs.campaign AS Campaign,
+ campaign_costs.source AS Source,
+ round(campaign_costs.total_cost, 2) AS Cost,
+ round(campaign_costs.total_clicks, 0) AS Clicks,
+ round(campaign_costs.total_impressions, 0) AS Impressions,
+ round(divide(campaign_costs.total_cost, nullif(campaign_costs.total_clicks, 0)), 2) AS CPC,
+ round(multiply(divide(campaign_costs.total_clicks, nullif(campaign_costs.total_impressions, 0)), 100), 2) AS CTR,
+ round(campaign_costs.total_reported_conversions, 2) AS `Reported Conversions`,
+ round(campaign_costs.total_reported_conversion_value, 2) AS `Reported Conversion Value`,
+ round(divide(campaign_costs.total_reported_conversion_value, nullif(campaign_costs.total_cost, 0)), 2) AS `Reported ROAS`,
+ round(divide(campaign_costs.total_cost, nullif(campaign_costs.total_reported_conversions, 0)), 2) AS `Cost per Reported Conversions`,
+ ucg.conversion_0 AS `Sign Up Conversions`,
+ round(divide(campaign_costs.total_cost, nullif(ucg.conversion_0, 0)), 2) AS `Cost per Sign Up Conversions`
FROM
- campaign_costs
- LEFT JOIN ucg ON and(equals(campaign_costs.match_key, ucg.match_key), equals(campaign_costs.source, ucg.source))
+ campaign_costs
+ LEFT JOIN ucg ON and(equals(campaign_costs.match_key, ucg.match_key), equals(campaign_costs.source, ucg.source))
ORDER BY
- Cost DESC,
- ID ASC
+ Cost DESC,
+ ID ASC
LIMIT 101
OFFSET 0
'''
@@ -212,55 +212,55 @@
# name: TestMarketingAnalyticsTableQueryRunnerBusiness.test_facebook_ads_single_source_execution
'''
WITH
- campaign_costs AS (
- SELECT
- campaign,
- id,
- source,
- any(match_key) AS match_key,
- sum(ifNull(toFloat(cost), 0)) AS total_cost,
- sum(ifNull(toFloat(clicks), 0)) AS total_clicks,
- sum(ifNull(toFloat(impressions), 0)) AS total_impressions,
- sum(ifNull(toFloat(reported_conversion), 0)) AS total_reported_conversions,
- sum(ifNull(toFloat(reported_conversion_value), 0)) AS total_reported_conversion_value
- FROM
- (
- SELECT
- toString(campaign1) AS match_key,
- toString(campaign1) AS campaign,
- toString(campaign1) AS id,
- toString('Facebook Ads') AS source,
- toFloat(coalesce(toFloat(impressions1), 0)) AS impressions,
- toFloat(coalesce(toFloat(clicks1), 0)) AS clicks,
- toFloat(convertCurrency('USD', 'USD', coalesce(toFloat(spend1), 0))) AS cost,
- toFloat(coalesce(toFloat(conversions1), 0)) AS reported_conversion,
- 0 AS reported_conversion_value
- FROM
- `bigquery.posthog_test.posthog_test_facebook_ads_table` AS posthog_test_facebook_ads_table
- WHERE
- and(greaterOrEquals(toDateTime(date1), toDateTime('2024-01-01 00:00:00')), lessOrEquals(toDateTime(date1), toDateTime('2024-12-31 23:59:59'))))
- GROUP BY
- campaign,
- id,
- source)
+ campaign_costs AS (
+ SELECT
+ campaign,
+ id,
+ source,
+ any(match_key) AS match_key,
+ sum(ifNull(toFloat(cost), 0)) AS total_cost,
+ sum(ifNull(toFloat(clicks), 0)) AS total_clicks,
+ sum(ifNull(toFloat(impressions), 0)) AS total_impressions,
+ sum(ifNull(toFloat(reported_conversion), 0)) AS total_reported_conversions,
+ sum(ifNull(toFloat(reported_conversion_value), 0)) AS total_reported_conversion_value
+ FROM
+ (
+ SELECT
+ toString(campaign1) AS match_key,
+ toString(campaign1) AS campaign,
+ toString(campaign1) AS id,
+ toString('Facebook Ads') AS source,
+ toFloat(coalesce(toFloat(impressions1), 0)) AS impressions,
+ toFloat(coalesce(toFloat(clicks1), 0)) AS clicks,
+ toFloat(convertCurrency('USD', 'USD', coalesce(toFloat(spend1), 0))) AS cost,
+ toFloat(coalesce(toFloat(conversions1), 0)) AS reported_conversion,
+ 0 AS reported_conversion_value
+ FROM
+ `bigquery.posthog_test.posthog_test_facebook_ads_table` AS posthog_test_facebook_ads_table
+ WHERE
+ and(greaterOrEquals(toDateTime(date1), toDateTime('2024-01-01 00:00:00')), lessOrEquals(toDateTime(date1), toDateTime('2024-12-31 23:59:59'))))
+ GROUP BY
+ campaign,
+ id,
+ source)
SELECT
- campaign_costs.id AS ID,
- campaign_costs.campaign AS Campaign,
- campaign_costs.source AS Source,
- round(campaign_costs.total_cost, 2) AS Cost,
- round(campaign_costs.total_clicks, 0) AS Clicks,
- round(campaign_costs.total_impressions, 0) AS Impressions,
- round(divide(campaign_costs.total_cost, nullif(campaign_costs.total_clicks, 0)), 2) AS CPC,
- round(multiply(divide(campaign_costs.total_clicks, nullif(campaign_costs.total_impressions, 0)), 100), 2) AS CTR,
- round(campaign_costs.total_reported_conversions, 2) AS `Reported Conversions`,
- round(campaign_costs.total_reported_conversion_value, 2) AS `Reported Conversion Value`,
- round(divide(campaign_costs.total_reported_conversion_value, nullif(campaign_costs.total_cost, 0)), 2) AS `Reported ROAS`,
- round(divide(campaign_costs.total_cost, nullif(campaign_costs.total_reported_conversions, 0)), 2) AS `Cost per Reported Conversions`
+ campaign_costs.id AS ID,
+ campaign_costs.campaign AS Campaign,
+ campaign_costs.source AS Source,
+ round(campaign_costs.total_cost, 2) AS Cost,
+ round(campaign_costs.total_clicks, 0) AS Clicks,
+ round(campaign_costs.total_impressions, 0) AS Impressions,
+ round(divide(campaign_costs.total_cost, nullif(campaign_costs.total_clicks, 0)), 2) AS CPC,
+ round(multiply(divide(campaign_costs.total_clicks, nullif(campaign_costs.total_impressions, 0)), 100), 2) AS CTR,
+ round(campaign_costs.total_reported_conversions, 2) AS `Reported Conversions`,
+ round(campaign_costs.total_reported_conversion_value, 2) AS `Reported Conversion Value`,
+ round(divide(campaign_costs.total_reported_conversion_value, nullif(campaign_costs.total_cost, 0)), 2) AS `Reported ROAS`,
+ round(divide(campaign_costs.total_cost, nullif(campaign_costs.total_reported_conversions, 0)), 2) AS `Cost per Reported Conversions`
FROM
- campaign_costs
+ campaign_costs
ORDER BY
- Cost DESC,
- ID ASC
+ Cost DESC,
+ ID ASC
LIMIT 101
OFFSET 0
'''
@@ -268,55 +268,55 @@
# name: TestMarketingAnalyticsTableQueryRunnerBusiness.test_facebook_only_multi_source_execution
'''
WITH
- campaign_costs AS (
- SELECT
- campaign,
- id,
- source,
- any(match_key) AS match_key,
- sum(ifNull(toFloat(cost), 0)) AS total_cost,
- sum(ifNull(toFloat(clicks), 0)) AS total_clicks,
- sum(ifNull(toFloat(impressions), 0)) AS total_impressions,
- sum(ifNull(toFloat(reported_conversion), 0)) AS total_reported_conversions,
- sum(ifNull(toFloat(reported_conversion_value), 0)) AS total_reported_conversion_value
- FROM
- (
- SELECT
- toString(campaign1) AS match_key,
- toString(campaign1) AS campaign,
- toString(campaign1) AS id,
- toString('Facebook Ads') AS source,
- toFloat(coalesce(toFloat(impressions1), 0)) AS impressions,
- toFloat(coalesce(toFloat(clicks1), 0)) AS clicks,
- toFloat(convertCurrency('USD', 'USD', coalesce(toFloat(spend1), 0))) AS cost,
- toFloat(coalesce(toFloat(conversions1), 0)) AS reported_conversion,
- 0 AS reported_conversion_value
- FROM
- `bigquery.posthog_test.posthog_test_facebook_ads_table` AS posthog_test_facebook_ads_table
- WHERE
- and(greaterOrEquals(toDateTime(date1), toDateTime('2024-01-01 00:00:00')), lessOrEquals(toDateTime(date1), toDateTime('2024-12-31 23:59:59'))))
- GROUP BY
- campaign,
- id,
- source)
+ campaign_costs AS (
+ SELECT
+ campaign,
+ id,
+ source,
+ any(match_key) AS match_key,
+ sum(ifNull(toFloat(cost), 0)) AS total_cost,
+ sum(ifNull(toFloat(clicks), 0)) AS total_clicks,
+ sum(ifNull(toFloat(impressions), 0)) AS total_impressions,
+ sum(ifNull(toFloat(reported_conversion), 0)) AS total_reported_conversions,
+ sum(ifNull(toFloat(reported_conversion_value), 0)) AS total_reported_conversion_value
+ FROM
+ (
+ SELECT
+ toString(campaign1) AS match_key,
+ toString(campaign1) AS campaign,
+ toString(campaign1) AS id,
+ toString('Facebook Ads') AS source,
+ toFloat(coalesce(toFloat(impressions1), 0)) AS impressions,
+ toFloat(coalesce(toFloat(clicks1), 0)) AS clicks,
+ toFloat(convertCurrency('USD', 'USD', coalesce(toFloat(spend1), 0))) AS cost,
+ toFloat(coalesce(toFloat(conversions1), 0)) AS reported_conversion,
+ 0 AS reported_conversion_value
+ FROM
+ `bigquery.posthog_test.posthog_test_facebook_ads_table` AS posthog_test_facebook_ads_table
+ WHERE
+ and(greaterOrEquals(toDateTime(date1), toDateTime('2024-01-01 00:00:00')), lessOrEquals(toDateTime(date1), toDateTime('2024-12-31 23:59:59'))))
+ GROUP BY
+ campaign,
+ id,
+ source)
SELECT
- campaign_costs.id AS ID,
- campaign_costs.campaign AS Campaign,
- campaign_costs.source AS Source,
- round(campaign_costs.total_cost, 2) AS Cost,
- round(campaign_costs.total_clicks, 0) AS Clicks,
- round(campaign_costs.total_impressions, 0) AS Impressions,
- round(divide(campaign_costs.total_cost, nullif(campaign_costs.total_clicks, 0)), 2) AS CPC,
- round(multiply(divide(campaign_costs.total_clicks, nullif(campaign_costs.total_impressions, 0)), 100), 2) AS CTR,
- round(campaign_costs.total_reported_conversions, 2) AS `Reported Conversions`,
- round(campaign_costs.total_reported_conversion_value, 2) AS `Reported Conversion Value`,
- round(divide(campaign_costs.total_reported_conversion_value, nullif(campaign_costs.total_cost, 0)), 2) AS `Reported ROAS`,
- round(divide(campaign_costs.total_cost, nullif(campaign_costs.total_reported_conversions, 0)), 2) AS `Cost per Reported Conversions`
+ campaign_costs.id AS ID,
+ campaign_costs.campaign AS Campaign,
+ campaign_costs.source AS Source,
+ round(campaign_costs.total_cost, 2) AS Cost,
+ round(campaign_costs.total_clicks, 0) AS Clicks,
+ round(campaign_costs.total_impressions, 0) AS Impressions,
+ round(divide(campaign_costs.total_cost, nullif(campaign_costs.total_clicks, 0)), 2) AS CPC,
+ round(multiply(divide(campaign_costs.total_clicks, nullif(campaign_costs.total_impressions, 0)), 100), 2) AS CTR,
+ round(campaign_costs.total_reported_conversions, 2) AS `Reported Conversions`,
+ round(campaign_costs.total_reported_conversion_value, 2) AS `Reported Conversion Value`,
+ round(divide(campaign_costs.total_reported_conversion_value, nullif(campaign_costs.total_cost, 0)), 2) AS `Reported ROAS`,
+ round(divide(campaign_costs.total_cost, nullif(campaign_costs.total_reported_conversions, 0)), 2) AS `Cost per Reported Conversions`
FROM
- campaign_costs
+ campaign_costs
ORDER BY
- Cost DESC,
- ID ASC
+ Cost DESC,
+ ID ASC
LIMIT 101
OFFSET 0
'''
@@ -325,85 +325,85 @@
dict({
'query': '''
WITH
- campaign_costs AS (
- SELECT
- campaign,
- id,
- source,
- any(match_key) AS match_key,
- sum(ifNull(toFloat(cost), 0)) AS total_cost,
- sum(ifNull(toFloat(clicks), 0)) AS total_clicks,
- sum(ifNull(toFloat(impressions), 0)) AS total_impressions,
- sum(ifNull(toFloat(reported_conversion), 0)) AS total_reported_conversions,
- sum(ifNull(toFloat(reported_conversion_value), 0)) AS total_reported_conversion_value
- FROM
- (
- SELECT
- toString(campaign1) AS match_key,
- toString(campaign1) AS campaign,
- toString(campaign1) AS id,
- toString('Facebook Ads') AS source,
- toFloat(coalesce(toFloat(impressions1), 0)) AS impressions,
- toFloat(coalesce(toFloat(clicks1), 0)) AS clicks,
- toFloat(convertCurrency('USD', 'USD', coalesce(toFloat(spend1), 0))) AS cost,
- toFloat(coalesce(toFloat(conversions1), 0)) AS reported_conversion,
- 0 AS reported_conversion_value
- FROM
- `bigquery.posthog_test.posthog_test_facebook_ads_table` AS posthog_test_facebook_ads_table
- WHERE
- and(greaterOrEquals(toDateTime(date1), toDateTime('2024-01-01 00:00:00')), lessOrEquals(toDateTime(date1), toDateTime('2024-12-31 23:59:59')))
- UNION ALL
- SELECT
- toString(campaign2) AS match_key,
- toString(campaign2) AS campaign,
- toString(campaign2) AS id,
- toString('TikTok Ads') AS source,
- toFloat(coalesce(toFloat(impressions2), 0)) AS impressions,
- toFloat(coalesce(toFloat(clicks2), 0)) AS clicks,
- toFloat(convertCurrency('USD', 'USD', coalesce(toFloat(spend2), 0))) AS cost,
- 0 AS reported_conversion,
- 0 AS reported_conversion_value
- FROM
- `bigquery.posthog_test.posthog_test_tiktok_ads_table` AS posthog_test_tiktok_ads_table
- WHERE
- and(greaterOrEquals(toDateTime(date2), toDateTime('2024-01-01 00:00:00')), lessOrEquals(toDateTime(date2), toDateTime('2024-12-31 23:59:59')))
- UNION ALL
- SELECT
- toString(campaign3) AS match_key,
- toString(campaign3) AS campaign,
- toString(campaign3) AS id,
- toString('LinkedIn Ads') AS source,
- toFloat(coalesce(toFloat(impressions3), 0)) AS impressions,
- toFloat(coalesce(toFloat(clicks3), 0)) AS clicks,
- toFloat(convertCurrency('USD', 'USD', coalesce(toFloat(spend3), 0))) AS cost,
- 0 AS reported_conversion,
- 0 AS reported_conversion_value
- FROM
- `bigquery.posthog_test.posthog_test_linkedin_ads_table` AS posthog_test_linkedin_ads_table
- WHERE
- and(greaterOrEquals(toDateTime(date3), toDateTime('2024-01-01 00:00:00')), lessOrEquals(toDateTime(date3), toDateTime('2024-12-31 23:59:59'))))
- GROUP BY
- campaign,
- id,
- source)
+ campaign_costs AS (
+ SELECT
+ campaign,
+ id,
+ source,
+ any(match_key) AS match_key,
+ sum(ifNull(toFloat(cost), 0)) AS total_cost,
+ sum(ifNull(toFloat(clicks), 0)) AS total_clicks,
+ sum(ifNull(toFloat(impressions), 0)) AS total_impressions,
+ sum(ifNull(toFloat(reported_conversion), 0)) AS total_reported_conversions,
+ sum(ifNull(toFloat(reported_conversion_value), 0)) AS total_reported_conversion_value
+ FROM
+ (
+ SELECT
+ toString(campaign1) AS match_key,
+ toString(campaign1) AS campaign,
+ toString(campaign1) AS id,
+ toString('Facebook Ads') AS source,
+ toFloat(coalesce(toFloat(impressions1), 0)) AS impressions,
+ toFloat(coalesce(toFloat(clicks1), 0)) AS clicks,
+ toFloat(convertCurrency('USD', 'USD', coalesce(toFloat(spend1), 0))) AS cost,
+ toFloat(coalesce(toFloat(conversions1), 0)) AS reported_conversion,
+ 0 AS reported_conversion_value
+ FROM
+ `bigquery.posthog_test.posthog_test_facebook_ads_table` AS posthog_test_facebook_ads_table
+ WHERE
+ and(greaterOrEquals(toDateTime(date1), toDateTime('2024-01-01 00:00:00')), lessOrEquals(toDateTime(date1), toDateTime('2024-12-31 23:59:59')))
+ UNION ALL
+ SELECT
+ toString(campaign2) AS match_key,
+ toString(campaign2) AS campaign,
+ toString(campaign2) AS id,
+ toString('TikTok Ads') AS source,
+ toFloat(coalesce(toFloat(impressions2), 0)) AS impressions,
+ toFloat(coalesce(toFloat(clicks2), 0)) AS clicks,
+ toFloat(convertCurrency('USD', 'USD', coalesce(toFloat(spend2), 0))) AS cost,
+ 0 AS reported_conversion,
+ 0 AS reported_conversion_value
+ FROM
+ `bigquery.posthog_test.posthog_test_tiktok_ads_table` AS posthog_test_tiktok_ads_table
+ WHERE
+ and(greaterOrEquals(toDateTime(date2), toDateTime('2024-01-01 00:00:00')), lessOrEquals(toDateTime(date2), toDateTime('2024-12-31 23:59:59')))
+ UNION ALL
+ SELECT
+ toString(campaign3) AS match_key,
+ toString(campaign3) AS campaign,
+ toString(campaign3) AS id,
+ toString('LinkedIn Ads') AS source,
+ toFloat(coalesce(toFloat(impressions3), 0)) AS impressions,
+ toFloat(coalesce(toFloat(clicks3), 0)) AS clicks,
+ toFloat(convertCurrency('USD', 'USD', coalesce(toFloat(spend3), 0))) AS cost,
+ 0 AS reported_conversion,
+ 0 AS reported_conversion_value
+ FROM
+ `bigquery.posthog_test.posthog_test_linkedin_ads_table` AS posthog_test_linkedin_ads_table
+ WHERE
+ and(greaterOrEquals(toDateTime(date3), toDateTime('2024-01-01 00:00:00')), lessOrEquals(toDateTime(date3), toDateTime('2024-12-31 23:59:59'))))
+ GROUP BY
+ campaign,
+ id,
+ source)
SELECT
- campaign_costs.id AS ID,
- campaign_costs.campaign AS Campaign,
- campaign_costs.source AS Source,
- round(campaign_costs.total_cost, 2) AS Cost,
- round(campaign_costs.total_clicks, 0) AS Clicks,
- round(campaign_costs.total_impressions, 0) AS Impressions,
- round(divide(campaign_costs.total_cost, nullif(campaign_costs.total_clicks, 0)), 2) AS CPC,
- round(multiply(divide(campaign_costs.total_clicks, nullif(campaign_costs.total_impressions, 0)), 100), 2) AS CTR,
- round(campaign_costs.total_reported_conversions, 2) AS `Reported Conversions`,
- round(campaign_costs.total_reported_conversion_value, 2) AS `Reported Conversion Value`,
- round(divide(campaign_costs.total_reported_conversion_value, nullif(campaign_costs.total_cost, 0)), 2) AS `Reported ROAS`,
- round(divide(campaign_costs.total_cost, nullif(campaign_costs.total_reported_conversions, 0)), 2) AS `Cost per Reported Conversions`
+ campaign_costs.id AS ID,
+ campaign_costs.campaign AS Campaign,
+ campaign_costs.source AS Source,
+ round(campaign_costs.total_cost, 2) AS Cost,
+ round(campaign_costs.total_clicks, 0) AS Clicks,
+ round(campaign_costs.total_impressions, 0) AS Impressions,
+ round(divide(campaign_costs.total_cost, nullif(campaign_costs.total_clicks, 0)), 2) AS CPC,
+ round(multiply(divide(campaign_costs.total_clicks, nullif(campaign_costs.total_impressions, 0)), 100), 2) AS CTR,
+ round(campaign_costs.total_reported_conversions, 2) AS `Reported Conversions`,
+ round(campaign_costs.total_reported_conversion_value, 2) AS `Reported Conversion Value`,
+ round(divide(campaign_costs.total_reported_conversion_value, nullif(campaign_costs.total_cost, 0)), 2) AS `Reported ROAS`,
+ round(divide(campaign_costs.total_cost, nullif(campaign_costs.total_reported_conversions, 0)), 2) AS `Cost per Reported Conversions`
FROM
- campaign_costs
+ campaign_costs
ORDER BY
- Cost DESC,
- ID ASC
+ Cost DESC,
+ ID ASC
LIMIT 101
OFFSET 0
''',
diff --git a/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_marketing_analytics_table_query_runner_compare.ambr b/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_marketing_analytics_table_query_runner_compare.ambr
index 7ba9940783fd..4a3a8a6a87e1 100644
--- a/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_marketing_analytics_table_query_runner_compare.ambr
+++ b/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_marketing_analytics_table_query_runner_compare.ambr
@@ -2,38 +2,206 @@
# name: TestMarketingAnalyticsTableQueryRunnerCompare.test_comprehensive_marketing_analytics_basic
'''
WITH
- campaign_costs AS (
+ campaign_costs AS (
+ SELECT
+ campaign,
+ id,
+ source,
+ any(match_key) AS match_key,
+ sum(ifNull(toFloat(cost), 0)) AS total_cost,
+ sum(ifNull(toFloat(clicks), 0)) AS total_clicks,
+ sum(ifNull(toFloat(impressions), 0)) AS total_impressions,
+ sum(ifNull(toFloat(reported_conversion), 0)) AS total_reported_conversions,
+ sum(ifNull(toFloat(reported_conversion_value), 0)) AS total_reported_conversion_value
+ FROM
+ (
+ SELECT
+ toString(campaign1) AS match_key,
+ toString(campaign1) AS campaign,
+ toString(campaign1) AS id,
+ toString('Facebook Ads') AS source,
+ toFloat(coalesce(toFloat(impressions1), 0)) AS impressions,
+ toFloat(coalesce(toFloat(clicks1), 0)) AS clicks,
+ toFloat(convertCurrency('USD', 'USD', coalesce(toFloat(spend1), 0))) AS cost,
+ toFloat(coalesce(toFloat(conversions1), 0)) AS reported_conversion,
+ 0 AS reported_conversion_value
+ FROM
+ `bigquery.posthog_test.posthog_test_facebook_ads_table` AS posthog_test_facebook_ads_table
+ WHERE
+ and(greaterOrEquals(toDateTime(date1), toDateTime('2024-11-01 00:00:00')), lessOrEquals(toDateTime(date1), toDateTime('2024-11-30 23:59:59'))))
+ GROUP BY
+ campaign,
+ id,
+ source)
SELECT
- campaign,
- id,
- source,
- any(match_key) AS match_key,
- sum(ifNull(toFloat(cost), 0)) AS total_cost,
- sum(ifNull(toFloat(clicks), 0)) AS total_clicks,
- sum(ifNull(toFloat(impressions), 0)) AS total_impressions,
- sum(ifNull(toFloat(reported_conversion), 0)) AS total_reported_conversions,
- sum(ifNull(toFloat(reported_conversion_value), 0)) AS total_reported_conversion_value
- FROM
- (
+ campaign_costs.id AS ID,
+ campaign_costs.campaign AS Campaign,
+ campaign_costs.source AS Source,
+ round(campaign_costs.total_cost, 2) AS Cost,
+ round(campaign_costs.total_clicks, 0) AS Clicks,
+ round(campaign_costs.total_impressions, 0) AS Impressions,
+ round(divide(campaign_costs.total_cost, nullif(campaign_costs.total_clicks, 0)), 2) AS CPC,
+ round(multiply(divide(campaign_costs.total_clicks, nullif(campaign_costs.total_impressions, 0)), 100), 2) AS CTR,
+ round(campaign_costs.total_reported_conversions, 2) AS `Reported Conversions`,
+ round(campaign_costs.total_reported_conversion_value, 2) AS `Reported Conversion Value`,
+ round(divide(campaign_costs.total_reported_conversion_value, nullif(campaign_costs.total_cost, 0)), 2) AS `Reported ROAS`,
+ round(divide(campaign_costs.total_cost, nullif(campaign_costs.total_reported_conversions, 0)), 2) AS `Cost per Reported Conversions`
+ FROM
+ campaign_costs
+ ORDER BY
+ ID ASC
+ LIMIT 101
+ OFFSET 0
+ '''
+# ---
+# name: TestMarketingAnalyticsTableQueryRunnerCompare.test_conversion_goal_basic_setup
+ '''
+
SELECT
- toString(campaign1) AS match_key,
- toString(campaign1) AS campaign,
- toString(campaign1) AS id,
- toString('Facebook Ads') AS source,
- toFloat(coalesce(toFloat(impressions1), 0)) AS impressions,
- toFloat(coalesce(toFloat(clicks1), 0)) AS clicks,
- toFloat(convertCurrency('USD', 'USD', coalesce(toFloat(spend1), 0))) AS cost,
- toFloat(coalesce(toFloat(conversions1), 0)) AS reported_conversion,
- 0 AS reported_conversion_value
+ tuple(current_period.ID, previous_period.ID) AS ID,
+ tuple(current_period.Campaign, previous_period.Campaign) AS Campaign,
+ tuple(current_period.Source, previous_period.Source) AS Source,
+ tuple(current_period.Cost, previous_period.Cost) AS Cost,
+ tuple(current_period.Clicks, previous_period.Clicks) AS Clicks,
+ tuple(current_period.Impressions, previous_period.Impressions) AS Impressions,
+ tuple(current_period.CPC, previous_period.CPC) AS CPC,
+ tuple(current_period.CTR, previous_period.CTR) AS CTR,
+ tuple(current_period.`Reported Conversions`, previous_period.`Reported Conversions`) AS `Reported Conversions`,
+ tuple(current_period.`Reported Conversion Value`, previous_period.`Reported Conversion Value`) AS `Reported Conversion Value`,
+ tuple(current_period.`Reported ROAS`, previous_period.`Reported ROAS`) AS `Reported ROAS`,
+ tuple(current_period.`Cost per Reported Conversions`, previous_period.`Cost per Reported Conversions`) AS `Cost per Reported Conversions`,
+ tuple(current_period.`Sign Up Conversions`, previous_period.`Sign Up Conversions`) AS `Sign Up Conversions`,
+ tuple(current_period.`Cost per Sign Up Conversions`, previous_period.`Cost per Sign Up Conversions`) AS `Cost per Sign Up Conversions`
+ FROM
+ (WITH
+ campaign_costs AS (
+ SELECT
+ campaign,
+ id,
+ source,
+ any(match_key) AS match_key,
+ sum(ifNull(toFloat(cost), 0)) AS total_cost,
+ sum(ifNull(toFloat(clicks), 0)) AS total_clicks,
+ sum(ifNull(toFloat(impressions), 0)) AS total_impressions,
+ sum(ifNull(toFloat(reported_conversion), 0)) AS total_reported_conversions,
+ sum(ifNull(toFloat(reported_conversion_value), 0)) AS total_reported_conversion_value
+ FROM
+ (
+ SELECT
+ toString(campaign1) AS match_key,
+ toString(campaign1) AS campaign,
+ toString(campaign1) AS id,
+ toString('Facebook Ads') AS source,
+ toFloat(coalesce(toFloat(impressions1), 0)) AS impressions,
+ toFloat(coalesce(toFloat(clicks1), 0)) AS clicks,
+ toFloat(convertCurrency('USD', 'USD', coalesce(toFloat(spend1), 0))) AS cost,
+ toFloat(coalesce(toFloat(conversions1), 0)) AS reported_conversion,
+ 0 AS reported_conversion_value
+ FROM
+ `bigquery.posthog_test.posthog_test_facebook_ads_table` AS posthog_test_facebook_ads_table
+ WHERE
+ and(greaterOrEquals(toDateTime(date1), toDateTime('2024-11-01 00:00:00')), lessOrEquals(toDateTime(date1), toDateTime('2024-12-31 23:59:59'))))
+ GROUP BY
+ campaign,
+ id,
+ source),
+ ucg AS (
+ SELECT
+ conv.campaign AS campaign,
+ conv.id AS id,
+ conv.source AS source,
+ conv.campaign AS match_key,
+ sum(conv.conversion_0) AS conversion_0
+ FROM
+ (
+ SELECT
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
+ if(notEmpty(campaign_id), campaign_id, '-') AS id,
+ if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
+ count() AS conversion_0
+ FROM
+ (
+ SELECT
+ person_id,
+ if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name,
+ if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name,
+ if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name,
+ if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name,
+ if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name,
+ if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name,
+ if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name,
+ if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name,
+ if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name,
+ '-' AS campaign_id,
+ 1 AS conversion_value
+ FROM
+ (
+ SELECT
+ person_id,
+ conversion_timestamps[i] AS conversion_time,
+ conversion_math_values[i] AS conversion_math_value,
+ conversion_campaigns[i] AS conversion_campaign,
+ conversion_sources[i] AS conversion_source,
+ conversion_mediums[i] AS conversion_medium,
+ conversion_contents[i] AS conversion_content,
+ conversion_terms[i] AS conversion_term,
+ conversion_referring_domains[i] AS conversion_referring_domain,
+ conversion_gclids[i] AS conversion_gclid,
+ conversion_fbclids[i] AS conversion_fbclid,
+ conversion_gad_sources[i] AS conversion_gad_source,
+ arrayMax(arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 7776000))), utm_timestamps)) AS last_utm_timestamp,
+ if(isNotNull(last_utm_timestamp), utm_campaigns[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_campaign,
+ if(isNotNull(last_utm_timestamp), utm_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_source,
+ if(isNotNull(last_utm_timestamp), utm_mediums[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_medium,
+ if(isNotNull(last_utm_timestamp), utm_contents[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_content,
+ if(isNotNull(last_utm_timestamp), utm_terms[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_term,
+ if(isNotNull(last_utm_timestamp), utm_referring_domains[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_referring_domain,
+ if(isNotNull(last_utm_timestamp), utm_gclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gclid,
+ if(isNotNull(last_utm_timestamp), utm_fbclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_fbclid,
+ if(isNotNull(last_utm_timestamp), utm_gad_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gad_source
+ FROM
+ (
+ SELECT
+ events.person_id,
+ arrayFilter(x -> greater(x, 0), groupArray(if(equals(event, 'test_event'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps,
+ arrayFilter(x -> greater(x, 0), groupArray(if(equals(event, 'test_event'), toFloat(1), 0))) AS conversion_math_values,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources,
+ arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
FROM
- `bigquery.posthog_test.posthog_test_facebook_ads_table` AS posthog_test_facebook_ads_table
+ events
WHERE
- and(greaterOrEquals(toDateTime(date1), toDateTime('2024-11-01 00:00:00')), lessOrEquals(toDateTime(date1), toDateTime('2024-11-30 23:59:59'))))
- GROUP BY
- campaign,
- id,
- source)
- SELECT
+ or(and(equals(event, 'test_event'), greaterOrEquals(events.timestamp, toDateTime('2024-11-01 00:00:00')), lessOrEquals(events.timestamp, toDateTime('2024-12-31 23:59:59'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2024-11-01 00:00:00'), toIntervalSecond(7776000))), lessOrEquals(events.timestamp, toDateTime('2024-12-31 23:59:59'))))
+ GROUP BY
+ events.person_id
+ HAVING
+ greater(length(conversion_timestamps), 0))
+ ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversions
+ GROUP BY
+ campaign,
+ id,
+ source) AS conv
+ GROUP BY
+ conv.campaign,
+ conv.id,
+ conv.source)
+ SELECT
campaign_costs.id AS ID,
campaign_costs.campaign AS Campaign,
campaign_costs.source AS Source,
@@ -45,481 +213,564 @@
round(campaign_costs.total_reported_conversions, 2) AS `Reported Conversions`,
round(campaign_costs.total_reported_conversion_value, 2) AS `Reported Conversion Value`,
round(divide(campaign_costs.total_reported_conversion_value, nullif(campaign_costs.total_cost, 0)), 2) AS `Reported ROAS`,
- round(divide(campaign_costs.total_cost, nullif(campaign_costs.total_reported_conversions, 0)), 2) AS `Cost per Reported Conversions`
- FROM
+ round(divide(campaign_costs.total_cost, nullif(campaign_costs.total_reported_conversions, 0)), 2) AS `Cost per Reported Conversions`,
+ ucg.conversion_0 AS `Sign Up Conversions`,
+ round(divide(campaign_costs.total_cost, nullif(ucg.conversion_0, 0)), 2) AS `Cost per Sign Up Conversions`
+ FROM
campaign_costs
+ LEFT JOIN ucg ON and(equals(campaign_costs.match_key, ucg.match_key), equals(campaign_costs.source, ucg.source))) AS current_period
+ LEFT JOIN (WITH
+ campaign_costs AS (
+ SELECT
+ campaign,
+ id,
+ source,
+ any(match_key) AS match_key,
+ sum(ifNull(toFloat(cost), 0)) AS total_cost,
+ sum(ifNull(toFloat(clicks), 0)) AS total_clicks,
+ sum(ifNull(toFloat(impressions), 0)) AS total_impressions,
+ sum(ifNull(toFloat(reported_conversion), 0)) AS total_reported_conversions,
+ sum(ifNull(toFloat(reported_conversion_value), 0)) AS total_reported_conversion_value
+ FROM
+ (
+ SELECT
+ toString(campaign1) AS match_key,
+ toString(campaign1) AS campaign,
+ toString(campaign1) AS id,
+ toString('Facebook Ads') AS source,
+ toFloat(coalesce(toFloat(impressions1), 0)) AS impressions,
+ toFloat(coalesce(toFloat(clicks1), 0)) AS clicks,
+ toFloat(convertCurrency('USD', 'USD', coalesce(toFloat(spend1), 0))) AS cost,
+ toFloat(coalesce(toFloat(conversions1), 0)) AS reported_conversion,
+ 0 AS reported_conversion_value
+ FROM
+ `bigquery.posthog_test.posthog_test_facebook_ads_table` AS posthog_test_facebook_ads_table
+ WHERE
+ and(greaterOrEquals(toDateTime(date1), toDateTime('2024-09-01 00:00:00')), lessOrEquals(toDateTime(date1), toDateTime('2024-10-31 23:59:59'))))
+ GROUP BY
+ campaign,
+ id,
+ source),
+ ucg AS (
+ SELECT
+ conv.campaign AS campaign,
+ conv.id AS id,
+ conv.source AS source,
+ conv.campaign AS match_key,
+ sum(conv.conversion_0) AS conversion_0
+ FROM
+ (
+ SELECT
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
+ if(notEmpty(campaign_id), campaign_id, '-') AS id,
+ if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
+ count() AS conversion_0
+ FROM
+ (
+ SELECT
+ person_id,
+ if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name,
+ if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name,
+ if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name,
+ if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name,
+ if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name,
+ if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name,
+ if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name,
+ if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name,
+ if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name,
+ '-' AS campaign_id,
+ 1 AS conversion_value
+ FROM
+ (
+ SELECT
+ person_id,
+ conversion_timestamps[i] AS conversion_time,
+ conversion_math_values[i] AS conversion_math_value,
+ conversion_campaigns[i] AS conversion_campaign,
+ conversion_sources[i] AS conversion_source,
+ conversion_mediums[i] AS conversion_medium,
+ conversion_contents[i] AS conversion_content,
+ conversion_terms[i] AS conversion_term,
+ conversion_referring_domains[i] AS conversion_referring_domain,
+ conversion_gclids[i] AS conversion_gclid,
+ conversion_fbclids[i] AS conversion_fbclid,
+ conversion_gad_sources[i] AS conversion_gad_source,
+ arrayMax(arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 7776000))), utm_timestamps)) AS last_utm_timestamp,
+ if(isNotNull(last_utm_timestamp), utm_campaigns[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_campaign,
+ if(isNotNull(last_utm_timestamp), utm_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_source,
+ if(isNotNull(last_utm_timestamp), utm_mediums[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_medium,
+ if(isNotNull(last_utm_timestamp), utm_contents[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_content,
+ if(isNotNull(last_utm_timestamp), utm_terms[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_term,
+ if(isNotNull(last_utm_timestamp), utm_referring_domains[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_referring_domain,
+ if(isNotNull(last_utm_timestamp), utm_gclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gclid,
+ if(isNotNull(last_utm_timestamp), utm_fbclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_fbclid,
+ if(isNotNull(last_utm_timestamp), utm_gad_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gad_source
+ FROM
+ (
+ SELECT
+ events.person_id,
+ arrayFilter(x -> greater(x, 0), groupArray(if(equals(event, 'test_event'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps,
+ arrayFilter(x -> greater(x, 0), groupArray(if(equals(event, 'test_event'), toFloat(1), 0))) AS conversion_math_values,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources,
+ arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
+ FROM
+ events
+ WHERE
+ or(and(equals(event, 'test_event'), greaterOrEquals(events.timestamp, toDateTime('2024-09-01 00:00:00')), lessOrEquals(events.timestamp, toDateTime('2024-10-31 23:59:59'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2024-09-01 00:00:00'), toIntervalSecond(7776000))), lessOrEquals(events.timestamp, toDateTime('2024-10-31 23:59:59'))))
+ GROUP BY
+ events.person_id
+ HAVING
+ greater(length(conversion_timestamps), 0))
+ ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversions
+ GROUP BY
+ campaign,
+ id,
+ source) AS conv
+ GROUP BY
+ conv.campaign,
+ conv.id,
+ conv.source)
+ SELECT
+ campaign_costs.id AS ID,
+ campaign_costs.campaign AS Campaign,
+ campaign_costs.source AS Source,
+ round(campaign_costs.total_cost, 2) AS Cost,
+ round(campaign_costs.total_clicks, 0) AS Clicks,
+ round(campaign_costs.total_impressions, 0) AS Impressions,
+ round(divide(campaign_costs.total_cost, nullif(campaign_costs.total_clicks, 0)), 2) AS CPC,
+ round(multiply(divide(campaign_costs.total_clicks, nullif(campaign_costs.total_impressions, 0)), 100), 2) AS CTR,
+ round(campaign_costs.total_reported_conversions, 2) AS `Reported Conversions`,
+ round(campaign_costs.total_reported_conversion_value, 2) AS `Reported Conversion Value`,
+ round(divide(campaign_costs.total_reported_conversion_value, nullif(campaign_costs.total_cost, 0)), 2) AS `Reported ROAS`,
+ round(divide(campaign_costs.total_cost, nullif(campaign_costs.total_reported_conversions, 0)), 2) AS `Cost per Reported Conversions`,
+ ucg.conversion_0 AS `Sign Up Conversions`,
+ round(divide(campaign_costs.total_cost, nullif(ucg.conversion_0, 0)), 2) AS `Cost per Sign Up Conversions`
+ FROM
+ campaign_costs
+ LEFT JOIN ucg ON and(equals(campaign_costs.match_key, ucg.match_key), equals(campaign_costs.source, ucg.source))) AS previous_period ON and(equals(current_period.Campaign, previous_period.Campaign), equals(current_period.Source, previous_period.Source))
ORDER BY
- ID ASC
+ Cost DESC,
+ ID ASC
LIMIT 101
OFFSET 0
'''
# ---
-# name: TestMarketingAnalyticsTableQueryRunnerCompare.test_conversion_goal_basic_setup
+# name: TestMarketingAnalyticsTableQueryRunnerCompare.test_conversion_goal_with_mixed_match_fields
'''
SELECT
- tuple(current_period.ID, previous_period.ID) AS ID,
- tuple(current_period.Campaign, previous_period.Campaign) AS Campaign,
- tuple(current_period.Source, previous_period.Source) AS Source,
- tuple(current_period.Cost, previous_period.Cost) AS Cost,
- tuple(current_period.Clicks, previous_period.Clicks) AS Clicks,
- tuple(current_period.Impressions, previous_period.Impressions) AS Impressions,
- tuple(current_period.CPC, previous_period.CPC) AS CPC,
- tuple(current_period.CTR, previous_period.CTR) AS CTR,
- tuple(current_period.`Reported Conversions`, previous_period.`Reported Conversions`) AS `Reported Conversions`,
- tuple(current_period.`Reported Conversion Value`, previous_period.`Reported Conversion Value`) AS `Reported Conversion Value`,
- tuple(current_period.`Reported ROAS`, previous_period.`Reported ROAS`) AS `Reported ROAS`,
- tuple(current_period.`Cost per Reported Conversions`, previous_period.`Cost per Reported Conversions`) AS `Cost per Reported Conversions`,
- tuple(current_period.`Sign Up Conversions`, previous_period.`Sign Up Conversions`) AS `Sign Up Conversions`,
- tuple(current_period.`Cost per Sign Up Conversions`, previous_period.`Cost per Sign Up Conversions`) AS `Cost per Sign Up Conversions`
+ tuple(current_period.ID, previous_period.ID) AS ID,
+ tuple(current_period.Campaign, previous_period.Campaign) AS Campaign,
+ tuple(current_period.Source, previous_period.Source) AS Source,
+ tuple(current_period.Cost, previous_period.Cost) AS Cost,
+ tuple(current_period.Clicks, previous_period.Clicks) AS Clicks,
+ tuple(current_period.Impressions, previous_period.Impressions) AS Impressions,
+ tuple(current_period.CPC, previous_period.CPC) AS CPC,
+ tuple(current_period.CTR, previous_period.CTR) AS CTR,
+ tuple(current_period.`Reported Conversions`, previous_period.`Reported Conversions`) AS `Reported Conversions`,
+ tuple(current_period.`Reported Conversion Value`, previous_period.`Reported Conversion Value`) AS `Reported Conversion Value`,
+ tuple(current_period.`Reported ROAS`, previous_period.`Reported ROAS`) AS `Reported ROAS`,
+ tuple(current_period.`Cost per Reported Conversions`, previous_period.`Cost per Reported Conversions`) AS `Cost per Reported Conversions`,
+ tuple(current_period.`Sign Up Conversions`, previous_period.`Sign Up Conversions`) AS `Sign Up Conversions`,
+ tuple(current_period.`Cost per Sign Up Conversions`, previous_period.`Cost per Sign Up Conversions`) AS `Cost per Sign Up Conversions`
FROM
- (WITH
- campaign_costs AS (
- SELECT
- campaign,
- id,
- source,
- any(match_key) AS match_key,
- sum(ifNull(toFloat(cost), 0)) AS total_cost,
- sum(ifNull(toFloat(clicks), 0)) AS total_clicks,
- sum(ifNull(toFloat(impressions), 0)) AS total_impressions,
- sum(ifNull(toFloat(reported_conversion), 0)) AS total_reported_conversions,
- sum(ifNull(toFloat(reported_conversion_value), 0)) AS total_reported_conversion_value
- FROM
- (
- SELECT
- toString(campaign1) AS match_key,
- toString(campaign1) AS campaign,
- toString(campaign1) AS id,
- toString('Facebook Ads') AS source,
- toFloat(coalesce(toFloat(impressions1), 0)) AS impressions,
- toFloat(coalesce(toFloat(clicks1), 0)) AS clicks,
- toFloat(convertCurrency('USD', 'USD', coalesce(toFloat(spend1), 0))) AS cost,
- toFloat(coalesce(toFloat(conversions1), 0)) AS reported_conversion,
- 0 AS reported_conversion_value
- FROM
- `bigquery.posthog_test.posthog_test_facebook_ads_table` AS posthog_test_facebook_ads_table
- WHERE
- and(greaterOrEquals(toDateTime(date1), toDateTime('2024-11-01 00:00:00')), lessOrEquals(toDateTime(date1), toDateTime('2024-12-31 23:59:59'))))
- GROUP BY
- campaign,
- id,
- source),
- ucg AS (
- SELECT
- conv.campaign AS campaign,
- conv.id AS id,
- conv.source AS source,
- conv.campaign AS match_key,
- sum(conv.conversion_0) AS conversion_0
- FROM
- (
- SELECT
- if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
- if(notEmpty(campaign_id), campaign_id, '-') AS id,
- if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
- count() AS conversion_0
- FROM
- (
- SELECT
- person_id,
- if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name,
- if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name,
- if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name,
- if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name,
- if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name,
- if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name,
- if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name,
- if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name,
- if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name,
- '-' AS campaign_id,
- 1 AS conversion_value
- FROM
- (
- SELECT
- person_id,
- conversion_timestamps[i] AS conversion_time,
- conversion_math_values[i] AS conversion_math_value,
- conversion_campaigns[i] AS conversion_campaign,
- conversion_sources[i] AS conversion_source,
- conversion_mediums[i] AS conversion_medium,
- conversion_contents[i] AS conversion_content,
- conversion_terms[i] AS conversion_term,
- conversion_referring_domains[i] AS conversion_referring_domain,
- conversion_gclids[i] AS conversion_gclid,
- conversion_fbclids[i] AS conversion_fbclid,
- conversion_gad_sources[i] AS conversion_gad_source,
- arrayMax(arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 7776000))), utm_timestamps)) AS last_utm_timestamp,
- if(isNotNull(last_utm_timestamp), utm_campaigns[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_campaign,
- if(isNotNull(last_utm_timestamp), utm_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_source,
- if(isNotNull(last_utm_timestamp), utm_mediums[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_medium,
- if(isNotNull(last_utm_timestamp), utm_contents[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_content,
- if(isNotNull(last_utm_timestamp), utm_terms[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_term,
- if(isNotNull(last_utm_timestamp), utm_referring_domains[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_referring_domain,
- if(isNotNull(last_utm_timestamp), utm_gclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gclid,
- if(isNotNull(last_utm_timestamp), utm_fbclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_fbclid,
- if(isNotNull(last_utm_timestamp), utm_gad_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gad_source
- FROM
- (
- SELECT
- events.person_id,
- arrayFilter(x -> greater(x, 0), groupArray(if(equals(event, 'test_event'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps,
- arrayFilter(x -> greater(x, 0), groupArray(if(equals(event, 'test_event'), toFloat(1), 0))) AS conversion_math_values,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources,
- arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
- FROM
- events
- WHERE
- or(and(equals(event, 'test_event'), greaterOrEquals(events.timestamp, toDateTime('2024-11-01 00:00:00')), lessOrEquals(events.timestamp, toDateTime('2024-12-31 23:59:59'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2024-11-01 00:00:00'), toIntervalSecond(7776000))), lessOrEquals(events.timestamp, toDateTime('2024-12-31 23:59:59'))))
- GROUP BY
- events.person_id
- HAVING
- greater(length(conversion_timestamps), 0))
- ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversions
- GROUP BY
- campaign,
- id,
- source) AS conv
- GROUP BY
- conv.campaign,
- conv.id,
- conv.source)
+ (WITH
+ campaign_costs AS (
SELECT
- campaign_costs.id AS ID,
- campaign_costs.campaign AS Campaign,
- campaign_costs.source AS Source,
- round(campaign_costs.total_cost, 2) AS Cost,
- round(campaign_costs.total_clicks, 0) AS Clicks,
- round(campaign_costs.total_impressions, 0) AS Impressions,
- round(divide(campaign_costs.total_cost, nullif(campaign_costs.total_clicks, 0)), 2) AS CPC,
- round(multiply(divide(campaign_costs.total_clicks, nullif(campaign_costs.total_impressions, 0)), 100), 2) AS CTR,
- round(campaign_costs.total_reported_conversions, 2) AS `Reported Conversions`,
- round(campaign_costs.total_reported_conversion_value, 2) AS `Reported Conversion Value`,
- round(divide(campaign_costs.total_reported_conversion_value, nullif(campaign_costs.total_cost, 0)), 2) AS `Reported ROAS`,
- round(divide(campaign_costs.total_cost, nullif(campaign_costs.total_reported_conversions, 0)), 2) AS `Cost per Reported Conversions`,
- ucg.conversion_0 AS `Sign Up Conversions`,
- round(divide(campaign_costs.total_cost, nullif(ucg.conversion_0, 0)), 2) AS `Cost per Sign Up Conversions`
+ campaign,
+ id,
+ source,
+ any(match_key) AS match_key,
+ sum(ifNull(toFloat(cost), 0)) AS total_cost,
+ sum(ifNull(toFloat(clicks), 0)) AS total_clicks,
+ sum(ifNull(toFloat(impressions), 0)) AS total_impressions,
+ sum(ifNull(toFloat(reported_conversion), 0)) AS total_reported_conversions,
+ sum(ifNull(toFloat(reported_conversion_value), 0)) AS total_reported_conversion_value
FROM
- campaign_costs
- LEFT JOIN ucg ON and(equals(campaign_costs.match_key, ucg.match_key), equals(campaign_costs.source, ucg.source))) AS current_period
- LEFT JOIN (WITH
- campaign_costs AS (
- SELECT
- campaign,
- id,
- source,
- any(match_key) AS match_key,
- sum(ifNull(toFloat(cost), 0)) AS total_cost,
- sum(ifNull(toFloat(clicks), 0)) AS total_clicks,
- sum(ifNull(toFloat(impressions), 0)) AS total_impressions,
- sum(ifNull(toFloat(reported_conversion), 0)) AS total_reported_conversions,
- sum(ifNull(toFloat(reported_conversion_value), 0)) AS total_reported_conversion_value
- FROM
- (
- SELECT
- toString(campaign1) AS match_key,
- toString(campaign1) AS campaign,
- toString(campaign1) AS id,
- toString('Facebook Ads') AS source,
- toFloat(coalesce(toFloat(impressions1), 0)) AS impressions,
- toFloat(coalesce(toFloat(clicks1), 0)) AS clicks,
- toFloat(convertCurrency('USD', 'USD', coalesce(toFloat(spend1), 0))) AS cost,
- toFloat(coalesce(toFloat(conversions1), 0)) AS reported_conversion,
- 0 AS reported_conversion_value
- FROM
- `bigquery.posthog_test.posthog_test_facebook_ads_table` AS posthog_test_facebook_ads_table
- WHERE
- and(greaterOrEquals(toDateTime(date1), toDateTime('2024-09-01 00:00:00')), lessOrEquals(toDateTime(date1), toDateTime('2024-10-31 23:59:59'))))
- GROUP BY
- campaign,
- id,
- source),
- ucg AS (
- SELECT
- conv.campaign AS campaign,
- conv.id AS id,
- conv.source AS source,
- conv.campaign AS match_key,
- sum(conv.conversion_0) AS conversion_0
+ (
+ SELECT
+ toString(campaign1) AS match_key,
+ toString(campaign1) AS campaign,
+ toString(campaign1) AS id,
+ toString('Facebook Ads') AS source,
+ toFloat(coalesce(toFloat(impressions1), 0)) AS impressions,
+ toFloat(coalesce(toFloat(clicks1), 0)) AS clicks,
+ toFloat(convertCurrency('USD', 'USD', coalesce(toFloat(spend1), 0))) AS cost,
+ toFloat(coalesce(toFloat(conversions1), 0)) AS reported_conversion,
+ 0 AS reported_conversion_value
+ FROM
+ `bigquery.posthog_test.posthog_test_facebook_ads_table` AS posthog_test_facebook_ads_table
+ WHERE
+ and(greaterOrEquals(toDateTime(date1), toDateTime('2024-11-01 00:00:00')), lessOrEquals(toDateTime(date1), toDateTime('2024-12-31 23:59:59'))))
+ GROUP BY
+ campaign,
+ id,
+ source),
+ ucg AS (
+ SELECT
+ conv.campaign AS campaign,
+ conv.id AS id,
+ conv.source AS source,
+ conv.campaign AS match_key,
+ sum(conv.conversion_0) AS conversion_0
+ FROM
+ (
+ SELECT
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
+ if(notEmpty(campaign_id), campaign_id, '-') AS id,
+ if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
+ count() AS conversion_0
+ FROM
+ (
+ SELECT
+ person_id,
+ if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name,
+ if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name,
+ if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name,
+ if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name,
+ if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name,
+ if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name,
+ if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name,
+ if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name,
+ if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name,
+ '-' AS campaign_id,
+ 1 AS conversion_value
+ FROM
+ (
+ SELECT
+ person_id,
+ conversion_timestamps[i] AS conversion_time,
+ conversion_math_values[i] AS conversion_math_value,
+ conversion_campaigns[i] AS conversion_campaign,
+ conversion_sources[i] AS conversion_source,
+ conversion_mediums[i] AS conversion_medium,
+ conversion_contents[i] AS conversion_content,
+ conversion_terms[i] AS conversion_term,
+ conversion_referring_domains[i] AS conversion_referring_domain,
+ conversion_gclids[i] AS conversion_gclid,
+ conversion_fbclids[i] AS conversion_fbclid,
+ conversion_gad_sources[i] AS conversion_gad_source,
+ arrayMax(arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 7776000))), utm_timestamps)) AS last_utm_timestamp,
+ if(isNotNull(last_utm_timestamp), utm_campaigns[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_campaign,
+ if(isNotNull(last_utm_timestamp), utm_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_source,
+ if(isNotNull(last_utm_timestamp), utm_mediums[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_medium,
+ if(isNotNull(last_utm_timestamp), utm_contents[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_content,
+ if(isNotNull(last_utm_timestamp), utm_terms[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_term,
+ if(isNotNull(last_utm_timestamp), utm_referring_domains[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_referring_domain,
+ if(isNotNull(last_utm_timestamp), utm_gclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gclid,
+ if(isNotNull(last_utm_timestamp), utm_fbclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_fbclid,
+ if(isNotNull(last_utm_timestamp), utm_gad_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gad_source
+ FROM
+ (
+ SELECT
+ events.person_id,
+ arrayFilter(x -> greater(x, 0), groupArray(if(equals(event, 'test_event'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps,
+ arrayFilter(x -> greater(x, 0), groupArray(if(equals(event, 'test_event'), toFloat(1), 0))) AS conversion_math_values,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources,
+ arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
FROM
- (
- SELECT
- if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
- if(notEmpty(campaign_id), campaign_id, '-') AS id,
- if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
- count() AS conversion_0
- FROM
- (
- SELECT
- person_id,
- if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name,
- if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name,
- if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name,
- if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name,
- if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name,
- if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name,
- if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name,
- if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name,
- if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name,
- '-' AS campaign_id,
- 1 AS conversion_value
- FROM
- (
- SELECT
- person_id,
- conversion_timestamps[i] AS conversion_time,
- conversion_math_values[i] AS conversion_math_value,
- conversion_campaigns[i] AS conversion_campaign,
- conversion_sources[i] AS conversion_source,
- conversion_mediums[i] AS conversion_medium,
- conversion_contents[i] AS conversion_content,
- conversion_terms[i] AS conversion_term,
- conversion_referring_domains[i] AS conversion_referring_domain,
- conversion_gclids[i] AS conversion_gclid,
- conversion_fbclids[i] AS conversion_fbclid,
- conversion_gad_sources[i] AS conversion_gad_source,
- arrayMax(arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 7776000))), utm_timestamps)) AS last_utm_timestamp,
- if(isNotNull(last_utm_timestamp), utm_campaigns[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_campaign,
- if(isNotNull(last_utm_timestamp), utm_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_source,
- if(isNotNull(last_utm_timestamp), utm_mediums[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_medium,
- if(isNotNull(last_utm_timestamp), utm_contents[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_content,
- if(isNotNull(last_utm_timestamp), utm_terms[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_term,
- if(isNotNull(last_utm_timestamp), utm_referring_domains[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_referring_domain,
- if(isNotNull(last_utm_timestamp), utm_gclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gclid,
- if(isNotNull(last_utm_timestamp), utm_fbclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_fbclid,
- if(isNotNull(last_utm_timestamp), utm_gad_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gad_source
- FROM
- (
- SELECT
- events.person_id,
- arrayFilter(x -> greater(x, 0), groupArray(if(equals(event, 'test_event'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps,
- arrayFilter(x -> greater(x, 0), groupArray(if(equals(event, 'test_event'), toFloat(1), 0))) AS conversion_math_values,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources,
- arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
- FROM
- events
- WHERE
- or(and(equals(event, 'test_event'), greaterOrEquals(events.timestamp, toDateTime('2024-09-01 00:00:00')), lessOrEquals(events.timestamp, toDateTime('2024-10-31 23:59:59'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2024-09-01 00:00:00'), toIntervalSecond(7776000))), lessOrEquals(events.timestamp, toDateTime('2024-10-31 23:59:59'))))
- GROUP BY
- events.person_id
- HAVING
- greater(length(conversion_timestamps), 0))
- ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversions
- GROUP BY
- campaign,
- id,
- source) AS conv
+ events
+ WHERE
+ or(and(equals(event, 'test_event'), greaterOrEquals(events.timestamp, toDateTime('2024-11-01 00:00:00')), lessOrEquals(events.timestamp, toDateTime('2024-12-31 23:59:59'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2024-11-01 00:00:00'), toIntervalSecond(7776000))), lessOrEquals(events.timestamp, toDateTime('2024-12-31 23:59:59'))))
GROUP BY
- conv.campaign,
- conv.id,
- conv.source)
+ events.person_id
+ HAVING
+ greater(length(conversion_timestamps), 0))
+ ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversions
+ GROUP BY
+ campaign,
+ id,
+ source) AS conv
+ GROUP BY
+ conv.campaign,
+ conv.id,
+ conv.source)
+ SELECT
+ campaign_costs.id AS ID,
+ campaign_costs.campaign AS Campaign,
+ campaign_costs.source AS Source,
+ round(campaign_costs.total_cost, 2) AS Cost,
+ round(campaign_costs.total_clicks, 0) AS Clicks,
+ round(campaign_costs.total_impressions, 0) AS Impressions,
+ round(divide(campaign_costs.total_cost, nullif(campaign_costs.total_clicks, 0)), 2) AS CPC,
+ round(multiply(divide(campaign_costs.total_clicks, nullif(campaign_costs.total_impressions, 0)), 100), 2) AS CTR,
+ round(campaign_costs.total_reported_conversions, 2) AS `Reported Conversions`,
+ round(campaign_costs.total_reported_conversion_value, 2) AS `Reported Conversion Value`,
+ round(divide(campaign_costs.total_reported_conversion_value, nullif(campaign_costs.total_cost, 0)), 2) AS `Reported ROAS`,
+ round(divide(campaign_costs.total_cost, nullif(campaign_costs.total_reported_conversions, 0)), 2) AS `Cost per Reported Conversions`,
+ ucg.conversion_0 AS `Sign Up Conversions`,
+ round(divide(campaign_costs.total_cost, nullif(ucg.conversion_0, 0)), 2) AS `Cost per Sign Up Conversions`
+ FROM
+ campaign_costs
+ LEFT JOIN ucg ON and(equals(campaign_costs.match_key, ucg.match_key), equals(campaign_costs.source, ucg.source))) AS current_period
+ LEFT JOIN (WITH
+ campaign_costs AS (
SELECT
- campaign_costs.id AS ID,
- campaign_costs.campaign AS Campaign,
- campaign_costs.source AS Source,
- round(campaign_costs.total_cost, 2) AS Cost,
- round(campaign_costs.total_clicks, 0) AS Clicks,
- round(campaign_costs.total_impressions, 0) AS Impressions,
- round(divide(campaign_costs.total_cost, nullif(campaign_costs.total_clicks, 0)), 2) AS CPC,
- round(multiply(divide(campaign_costs.total_clicks, nullif(campaign_costs.total_impressions, 0)), 100), 2) AS CTR,
- round(campaign_costs.total_reported_conversions, 2) AS `Reported Conversions`,
- round(campaign_costs.total_reported_conversion_value, 2) AS `Reported Conversion Value`,
- round(divide(campaign_costs.total_reported_conversion_value, nullif(campaign_costs.total_cost, 0)), 2) AS `Reported ROAS`,
- round(divide(campaign_costs.total_cost, nullif(campaign_costs.total_reported_conversions, 0)), 2) AS `Cost per Reported Conversions`,
- ucg.conversion_0 AS `Sign Up Conversions`,
- round(divide(campaign_costs.total_cost, nullif(ucg.conversion_0, 0)), 2) AS `Cost per Sign Up Conversions`
+ campaign,
+ id,
+ source,
+ any(match_key) AS match_key,
+ sum(ifNull(toFloat(cost), 0)) AS total_cost,
+ sum(ifNull(toFloat(clicks), 0)) AS total_clicks,
+ sum(ifNull(toFloat(impressions), 0)) AS total_impressions,
+ sum(ifNull(toFloat(reported_conversion), 0)) AS total_reported_conversions,
+ sum(ifNull(toFloat(reported_conversion_value), 0)) AS total_reported_conversion_value
FROM
- campaign_costs
- LEFT JOIN ucg ON and(equals(campaign_costs.match_key, ucg.match_key), equals(campaign_costs.source, ucg.source))) AS previous_period ON and(equals(current_period.Campaign, previous_period.Campaign), equals(current_period.Source, previous_period.Source))
+ (
+ SELECT
+ toString(campaign1) AS match_key,
+ toString(campaign1) AS campaign,
+ toString(campaign1) AS id,
+ toString('Facebook Ads') AS source,
+ toFloat(coalesce(toFloat(impressions1), 0)) AS impressions,
+ toFloat(coalesce(toFloat(clicks1), 0)) AS clicks,
+ toFloat(convertCurrency('USD', 'USD', coalesce(toFloat(spend1), 0))) AS cost,
+ toFloat(coalesce(toFloat(conversions1), 0)) AS reported_conversion,
+ 0 AS reported_conversion_value
+ FROM
+ `bigquery.posthog_test.posthog_test_facebook_ads_table` AS posthog_test_facebook_ads_table
+ WHERE
+ and(greaterOrEquals(toDateTime(date1), toDateTime('2024-09-01 00:00:00')), lessOrEquals(toDateTime(date1), toDateTime('2024-10-31 23:59:59'))))
+ GROUP BY
+ campaign,
+ id,
+ source),
+ ucg AS (
+ SELECT
+ conv.campaign AS campaign,
+ conv.id AS id,
+ conv.source AS source,
+ conv.campaign AS match_key,
+ sum(conv.conversion_0) AS conversion_0
+ FROM
+ (
+ SELECT
+ if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
+ if(notEmpty(campaign_id), campaign_id, '-') AS id,
+ if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
+ count() AS conversion_0
+ FROM
+ (
+ SELECT
+ person_id,
+ if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name,
+ if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name,
+ if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name,
+ if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name,
+ if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name,
+ if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name,
+ if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name,
+ if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name,
+ if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name,
+ '-' AS campaign_id,
+ 1 AS conversion_value
+ FROM
+ (
+ SELECT
+ person_id,
+ conversion_timestamps[i] AS conversion_time,
+ conversion_math_values[i] AS conversion_math_value,
+ conversion_campaigns[i] AS conversion_campaign,
+ conversion_sources[i] AS conversion_source,
+ conversion_mediums[i] AS conversion_medium,
+ conversion_contents[i] AS conversion_content,
+ conversion_terms[i] AS conversion_term,
+ conversion_referring_domains[i] AS conversion_referring_domain,
+ conversion_gclids[i] AS conversion_gclid,
+ conversion_fbclids[i] AS conversion_fbclid,
+ conversion_gad_sources[i] AS conversion_gad_source,
+ arrayMax(arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 7776000))), utm_timestamps)) AS last_utm_timestamp,
+ if(isNotNull(last_utm_timestamp), utm_campaigns[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_campaign,
+ if(isNotNull(last_utm_timestamp), utm_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_source,
+ if(isNotNull(last_utm_timestamp), utm_mediums[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_medium,
+ if(isNotNull(last_utm_timestamp), utm_contents[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_content,
+ if(isNotNull(last_utm_timestamp), utm_terms[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_term,
+ if(isNotNull(last_utm_timestamp), utm_referring_domains[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_referring_domain,
+ if(isNotNull(last_utm_timestamp), utm_gclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gclid,
+ if(isNotNull(last_utm_timestamp), utm_fbclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_fbclid,
+ if(isNotNull(last_utm_timestamp), utm_gad_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gad_source
+ FROM
+ (
+ SELECT
+ events.person_id,
+ arrayFilter(x -> greater(x, 0), groupArray(if(equals(event, 'test_event'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps,
+ arrayFilter(x -> greater(x, 0), groupArray(if(equals(event, 'test_event'), toFloat(1), 0))) AS conversion_math_values,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids,
+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources,
+ arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids,
+ arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
+ FROM
+ events
+ WHERE
+ or(and(equals(event, 'test_event'), greaterOrEquals(events.timestamp, toDateTime('2024-09-01 00:00:00')), lessOrEquals(events.timestamp, toDateTime('2024-10-31 23:59:59'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2024-09-01 00:00:00'), toIntervalSecond(7776000))), lessOrEquals(events.timestamp, toDateTime('2024-10-31 23:59:59'))))
+ GROUP BY
+ events.person_id
+ HAVING
+ greater(length(conversion_timestamps), 0))
+ ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversions
+ GROUP BY
+ campaign,
+ id,
+ source) AS conv
+ GROUP BY
+ conv.campaign,
+ conv.id,
+ conv.source)
+ SELECT
+ campaign_costs.id AS ID,
+ campaign_costs.campaign AS Campaign,
+ campaign_costs.source AS Source,
+ round(campaign_costs.total_cost, 2) AS Cost,
+ round(campaign_costs.total_clicks, 0) AS Clicks,
+ round(campaign_costs.total_impressions, 0) AS Impressions,
+ round(divide(campaign_costs.total_cost, nullif(campaign_costs.total_clicks, 0)), 2) AS CPC,
+ round(multiply(divide(campaign_costs.total_clicks, nullif(campaign_costs.total_impressions, 0)), 100), 2) AS CTR,
+ round(campaign_costs.total_reported_conversions, 2) AS `Reported Conversions`,
+ round(campaign_costs.total_reported_conversion_value, 2) AS `Reported Conversion Value`,
+ round(divide(campaign_costs.total_reported_conversion_value, nullif(campaign_costs.total_cost, 0)), 2) AS `Reported ROAS`,
+ round(divide(campaign_costs.total_cost, nullif(campaign_costs.total_reported_conversions, 0)), 2) AS `Cost per Reported Conversions`,
+ ucg.conversion_0 AS `Sign Up Conversions`,
+ round(divide(campaign_costs.total_cost, nullif(ucg.conversion_0, 0)), 2) AS `Cost per Sign Up Conversions`
+ FROM
+ campaign_costs
+ LEFT JOIN ucg ON and(equals(campaign_costs.match_key, ucg.match_key), equals(campaign_costs.source, ucg.source))) AS previous_period ON and(equals(current_period.Campaign, previous_period.Campaign), equals(current_period.Source, previous_period.Source))
ORDER BY
- Cost DESC,
- ID ASC
+ Cost DESC,
+ ID ASC
LIMIT 101
OFFSET 0
'''
# ---
-# name: TestMarketingAnalyticsTableQueryRunnerCompare.test_conversion_goal_with_mixed_match_fields
- '''
-
- SELECT
- tuple(current_period.ID, previous_period.ID) AS ID,
- tuple(current_period.Campaign, previous_period.Campaign) AS Campaign,
- tuple(current_period.Source, previous_period.Source) AS Source,
- tuple(current_period.Cost, previous_period.Cost) AS Cost,
- tuple(current_period.Clicks, previous_period.Clicks) AS Clicks,
- tuple(current_period.Impressions, previous_period.Impressions) AS Impressions,
- tuple(current_period.CPC, previous_period.CPC) AS CPC,
- tuple(current_period.CTR, previous_period.CTR) AS CTR,
- tuple(current_period.`Reported Conversions`, previous_period.`Reported Conversions`) AS `Reported Conversions`,
- tuple(current_period.`Reported Conversion Value`, previous_period.`Reported Conversion Value`) AS `Reported Conversion Value`,
- tuple(current_period.`Reported ROAS`, previous_period.`Reported ROAS`) AS `Reported ROAS`,
- tuple(current_period.`Cost per Reported Conversions`, previous_period.`Cost per Reported Conversions`) AS `Cost per Reported Conversions`,
- tuple(current_period.`Sign Up Conversions`, previous_period.`Sign Up Conversions`) AS `Sign Up Conversions`,
- tuple(current_period.`Cost per Sign Up Conversions`, previous_period.`Cost per Sign Up Conversions`) AS `Cost per Sign Up Conversions`
- FROM
+# name: TestMarketingAnalyticsTableQueryRunnerCompare.test_multi_source_business_metrics_validation_with_compare
+ dict({
+ 'query': '''
+
+ SELECT
+ tuple(current_period.ID, previous_period.ID) AS ID,
+ tuple(current_period.Campaign, previous_period.Campaign) AS Campaign,
+ tuple(current_period.Source, previous_period.Source) AS Source,
+ tuple(current_period.Cost, previous_period.Cost) AS Cost,
+ tuple(current_period.Clicks, previous_period.Clicks) AS Clicks,
+ tuple(current_period.Impressions, previous_period.Impressions) AS Impressions,
+ tuple(current_period.CPC, previous_period.CPC) AS CPC,
+ tuple(current_period.CTR, previous_period.CTR) AS CTR,
+ tuple(current_period.`Reported Conversions`, previous_period.`Reported Conversions`) AS `Reported Conversions`,
+ tuple(current_period.`Reported Conversion Value`, previous_period.`Reported Conversion Value`) AS `Reported Conversion Value`,
+ tuple(current_period.`Reported ROAS`, previous_period.`Reported ROAS`) AS `Reported ROAS`,
+ tuple(current_period.`Cost per Reported Conversions`, previous_period.`Cost per Reported Conversions`) AS `Cost per Reported Conversions`
+ FROM
(WITH
campaign_costs AS (
- SELECT
- campaign,
- id,
- source,
- any(match_key) AS match_key,
- sum(ifNull(toFloat(cost), 0)) AS total_cost,
- sum(ifNull(toFloat(clicks), 0)) AS total_clicks,
- sum(ifNull(toFloat(impressions), 0)) AS total_impressions,
- sum(ifNull(toFloat(reported_conversion), 0)) AS total_reported_conversions,
- sum(ifNull(toFloat(reported_conversion_value), 0)) AS total_reported_conversion_value
- FROM
- (
- SELECT
- toString(campaign1) AS match_key,
- toString(campaign1) AS campaign,
- toString(campaign1) AS id,
- toString('Facebook Ads') AS source,
- toFloat(coalesce(toFloat(impressions1), 0)) AS impressions,
- toFloat(coalesce(toFloat(clicks1), 0)) AS clicks,
- toFloat(convertCurrency('USD', 'USD', coalesce(toFloat(spend1), 0))) AS cost,
- toFloat(coalesce(toFloat(conversions1), 0)) AS reported_conversion,
- 0 AS reported_conversion_value
- FROM
- `bigquery.posthog_test.posthog_test_facebook_ads_table` AS posthog_test_facebook_ads_table
- WHERE
- and(greaterOrEquals(toDateTime(date1), toDateTime('2024-11-01 00:00:00')), lessOrEquals(toDateTime(date1), toDateTime('2024-12-31 23:59:59'))))
- GROUP BY
- campaign,
- id,
- source),
- ucg AS (
- SELECT
- conv.campaign AS campaign,
- conv.id AS id,
- conv.source AS source,
- conv.campaign AS match_key,
- sum(conv.conversion_0) AS conversion_0
- FROM
- (
- SELECT
- if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
- if(notEmpty(campaign_id), campaign_id, '-') AS id,
- if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
- count() AS conversion_0
- FROM
- (
- SELECT
- person_id,
- if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name,
- if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name,
- if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name,
- if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name,
- if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name,
- if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name,
- if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name,
- if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name,
- if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name,
- '-' AS campaign_id,
- 1 AS conversion_value
- FROM
- (
- SELECT
- person_id,
- conversion_timestamps[i] AS conversion_time,
- conversion_math_values[i] AS conversion_math_value,
- conversion_campaigns[i] AS conversion_campaign,
- conversion_sources[i] AS conversion_source,
- conversion_mediums[i] AS conversion_medium,
- conversion_contents[i] AS conversion_content,
- conversion_terms[i] AS conversion_term,
- conversion_referring_domains[i] AS conversion_referring_domain,
- conversion_gclids[i] AS conversion_gclid,
- conversion_fbclids[i] AS conversion_fbclid,
- conversion_gad_sources[i] AS conversion_gad_source,
- arrayMax(arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 7776000))), utm_timestamps)) AS last_utm_timestamp,
- if(isNotNull(last_utm_timestamp), utm_campaigns[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_campaign,
- if(isNotNull(last_utm_timestamp), utm_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_source,
- if(isNotNull(last_utm_timestamp), utm_mediums[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_medium,
- if(isNotNull(last_utm_timestamp), utm_contents[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_content,
- if(isNotNull(last_utm_timestamp), utm_terms[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_term,
- if(isNotNull(last_utm_timestamp), utm_referring_domains[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_referring_domain,
- if(isNotNull(last_utm_timestamp), utm_gclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gclid,
- if(isNotNull(last_utm_timestamp), utm_fbclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_fbclid,
- if(isNotNull(last_utm_timestamp), utm_gad_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gad_source
- FROM
- (
- SELECT
- events.person_id,
- arrayFilter(x -> greater(x, 0), groupArray(if(equals(event, 'test_event'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps,
- arrayFilter(x -> greater(x, 0), groupArray(if(equals(event, 'test_event'), toFloat(1), 0))) AS conversion_math_values,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources,
- arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
- FROM
- events
- WHERE
- or(and(equals(event, 'test_event'), greaterOrEquals(events.timestamp, toDateTime('2024-11-01 00:00:00')), lessOrEquals(events.timestamp, toDateTime('2024-12-31 23:59:59'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2024-11-01 00:00:00'), toIntervalSecond(7776000))), lessOrEquals(events.timestamp, toDateTime('2024-12-31 23:59:59'))))
- GROUP BY
- events.person_id
- HAVING
- greater(length(conversion_timestamps), 0))
- ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversions
- GROUP BY
- campaign,
- id,
- source) AS conv
- GROUP BY
- conv.campaign,
- conv.id,
- conv.source)
- SELECT
+ SELECT
+ campaign,
+ id,
+ source,
+ any(match_key) AS match_key,
+ sum(ifNull(toFloat(cost), 0)) AS total_cost,
+ sum(ifNull(toFloat(clicks), 0)) AS total_clicks,
+ sum(ifNull(toFloat(impressions), 0)) AS total_impressions,
+ sum(ifNull(toFloat(reported_conversion), 0)) AS total_reported_conversions,
+ sum(ifNull(toFloat(reported_conversion_value), 0)) AS total_reported_conversion_value
+ FROM
+ (
+ SELECT
+ toString(campaign1) AS match_key,
+ toString(campaign1) AS campaign,
+ toString(campaign1) AS id,
+ toString('Facebook Ads') AS source,
+ toFloat(coalesce(toFloat(impressions1), 0)) AS impressions,
+ toFloat(coalesce(toFloat(clicks1), 0)) AS clicks,
+ toFloat(convertCurrency('USD', 'USD', coalesce(toFloat(spend1), 0))) AS cost,
+ toFloat(coalesce(toFloat(conversions1), 0)) AS reported_conversion,
+ 0 AS reported_conversion_value
+ FROM
+ `bigquery.posthog_test.posthog_test_facebook_ads_table` AS posthog_test_facebook_ads_table
+ WHERE
+ and(greaterOrEquals(toDateTime(date1), toDateTime('2024-11-01 00:00:00')), lessOrEquals(toDateTime(date1), toDateTime('2024-12-31 23:59:59')))
+ UNION ALL
+ SELECT
+ toString(campaign2) AS match_key,
+ toString(campaign2) AS campaign,
+ toString(campaign2) AS id,
+ toString('TikTok Ads') AS source,
+ toFloat(coalesce(toFloat(impressions2), 0)) AS impressions,
+ toFloat(coalesce(toFloat(clicks2), 0)) AS clicks,
+ toFloat(convertCurrency('USD', 'USD', coalesce(toFloat(spend2), 0))) AS cost,
+ 0 AS reported_conversion,
+ 0 AS reported_conversion_value
+ FROM
+ `bigquery.posthog_test.posthog_test_tiktok_ads_table` AS posthog_test_tiktok_ads_table
+ WHERE
+ and(greaterOrEquals(toDateTime(date2), toDateTime('2024-11-01 00:00:00')), lessOrEquals(toDateTime(date2), toDateTime('2024-12-31 23:59:59')))
+ UNION ALL
+ SELECT
+ toString(campaign3) AS match_key,
+ toString(campaign3) AS campaign,
+ toString(campaign3) AS id,
+ toString('LinkedIn Ads') AS source,
+ toFloat(coalesce(toFloat(impressions3), 0)) AS impressions,
+ toFloat(coalesce(toFloat(clicks3), 0)) AS clicks,
+ toFloat(convertCurrency('USD', 'USD', coalesce(toFloat(spend3), 0))) AS cost,
+ 0 AS reported_conversion,
+ 0 AS reported_conversion_value
+ FROM
+ `bigquery.posthog_test.posthog_test_linkedin_ads_table` AS posthog_test_linkedin_ads_table
+ WHERE
+ and(greaterOrEquals(toDateTime(date3), toDateTime('2024-11-01 00:00:00')), lessOrEquals(toDateTime(date3), toDateTime('2024-12-31 23:59:59'))))
+ GROUP BY
+ campaign,
+ id,
+ source)
+ SELECT
campaign_costs.id AS ID,
campaign_costs.campaign AS Campaign,
campaign_costs.source AS Source,
@@ -531,141 +782,72 @@
round(campaign_costs.total_reported_conversions, 2) AS `Reported Conversions`,
round(campaign_costs.total_reported_conversion_value, 2) AS `Reported Conversion Value`,
round(divide(campaign_costs.total_reported_conversion_value, nullif(campaign_costs.total_cost, 0)), 2) AS `Reported ROAS`,
- round(divide(campaign_costs.total_cost, nullif(campaign_costs.total_reported_conversions, 0)), 2) AS `Cost per Reported Conversions`,
- ucg.conversion_0 AS `Sign Up Conversions`,
- round(divide(campaign_costs.total_cost, nullif(ucg.conversion_0, 0)), 2) AS `Cost per Sign Up Conversions`
- FROM
- campaign_costs
- LEFT JOIN ucg ON and(equals(campaign_costs.match_key, ucg.match_key), equals(campaign_costs.source, ucg.source))) AS current_period
- LEFT JOIN (WITH
+ round(divide(campaign_costs.total_cost, nullif(campaign_costs.total_reported_conversions, 0)), 2) AS `Cost per Reported Conversions`
+ FROM
+ campaign_costs) AS current_period
+ LEFT JOIN (WITH
campaign_costs AS (
- SELECT
- campaign,
- id,
- source,
- any(match_key) AS match_key,
- sum(ifNull(toFloat(cost), 0)) AS total_cost,
- sum(ifNull(toFloat(clicks), 0)) AS total_clicks,
- sum(ifNull(toFloat(impressions), 0)) AS total_impressions,
- sum(ifNull(toFloat(reported_conversion), 0)) AS total_reported_conversions,
- sum(ifNull(toFloat(reported_conversion_value), 0)) AS total_reported_conversion_value
- FROM
- (
- SELECT
- toString(campaign1) AS match_key,
- toString(campaign1) AS campaign,
- toString(campaign1) AS id,
- toString('Facebook Ads') AS source,
- toFloat(coalesce(toFloat(impressions1), 0)) AS impressions,
- toFloat(coalesce(toFloat(clicks1), 0)) AS clicks,
- toFloat(convertCurrency('USD', 'USD', coalesce(toFloat(spend1), 0))) AS cost,
- toFloat(coalesce(toFloat(conversions1), 0)) AS reported_conversion,
- 0 AS reported_conversion_value
- FROM
- `bigquery.posthog_test.posthog_test_facebook_ads_table` AS posthog_test_facebook_ads_table
- WHERE
- and(greaterOrEquals(toDateTime(date1), toDateTime('2024-09-01 00:00:00')), lessOrEquals(toDateTime(date1), toDateTime('2024-10-31 23:59:59'))))
- GROUP BY
- campaign,
- id,
- source),
- ucg AS (
- SELECT
- conv.campaign AS campaign,
- conv.id AS id,
- conv.source AS source,
- conv.campaign AS match_key,
- sum(conv.conversion_0) AS conversion_0
- FROM
- (
- SELECT
- if(notEmpty(campaign_name), campaign_name, 'organic') AS campaign,
- if(notEmpty(campaign_id), campaign_id, '-') AS id,
- if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['microsoft']), 'bing', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['facebook', 'instagram', 'messenger', 'fb', 'whatsapp', 'audience_network', 'facebook_marketplace', 'threads']), 'meta', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['li']), 'linkedin', if(in(lower(if(notEmpty(source_name), source_name, 'organic')), ['adwords', 'youtube', 'display', 'gmail', 'google_maps', 'google_play', 'google_discover', 'admob', 'waze']), 'google', if(notEmpty(source_name), source_name, 'organic'))))) AS source,
- count() AS conversion_0
- FROM
- (
- SELECT
- person_id,
- if(notEmpty(conversion_campaign), conversion_campaign, if(notEmpty(fallback_campaign), fallback_campaign, '')) AS campaign_name,
- if(notEmpty(conversion_source), conversion_source, if(notEmpty(fallback_source), fallback_source, '')) AS source_name,
- if(notEmpty(conversion_medium), conversion_medium, if(notEmpty(fallback_medium), fallback_medium, '')) AS medium_name,
- if(notEmpty(conversion_content), conversion_content, if(notEmpty(fallback_content), fallback_content, '')) AS content_name,
- if(notEmpty(conversion_term), conversion_term, if(notEmpty(fallback_term), fallback_term, '')) AS term_name,
- if(notEmpty(conversion_referring_domain), conversion_referring_domain, if(notEmpty(fallback_referring_domain), fallback_referring_domain, '')) AS referring_domain_name,
- if(notEmpty(conversion_gclid), conversion_gclid, if(notEmpty(fallback_gclid), fallback_gclid, '')) AS gclid_name,
- if(notEmpty(conversion_fbclid), conversion_fbclid, if(notEmpty(fallback_fbclid), fallback_fbclid, '')) AS fbclid_name,
- if(notEmpty(conversion_gad_source), conversion_gad_source, if(notEmpty(fallback_gad_source), fallback_gad_source, '')) AS gad_source_name,
- '-' AS campaign_id,
- 1 AS conversion_value
- FROM
- (
- SELECT
- person_id,
- conversion_timestamps[i] AS conversion_time,
- conversion_math_values[i] AS conversion_math_value,
- conversion_campaigns[i] AS conversion_campaign,
- conversion_sources[i] AS conversion_source,
- conversion_mediums[i] AS conversion_medium,
- conversion_contents[i] AS conversion_content,
- conversion_terms[i] AS conversion_term,
- conversion_referring_domains[i] AS conversion_referring_domain,
- conversion_gclids[i] AS conversion_gclid,
- conversion_fbclids[i] AS conversion_fbclid,
- conversion_gad_sources[i] AS conversion_gad_source,
- arrayMax(arrayFilter(x -> and(lessOrEquals(x, conversion_timestamps[i]), greaterOrEquals(x, minus(conversion_timestamps[i], 7776000))), utm_timestamps)) AS last_utm_timestamp,
- if(isNotNull(last_utm_timestamp), utm_campaigns[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_campaign,
- if(isNotNull(last_utm_timestamp), utm_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_source,
- if(isNotNull(last_utm_timestamp), utm_mediums[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_medium,
- if(isNotNull(last_utm_timestamp), utm_contents[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_content,
- if(isNotNull(last_utm_timestamp), utm_terms[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_term,
- if(isNotNull(last_utm_timestamp), utm_referring_domains[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_referring_domain,
- if(isNotNull(last_utm_timestamp), utm_gclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gclid,
- if(isNotNull(last_utm_timestamp), utm_fbclids[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_fbclid,
- if(isNotNull(last_utm_timestamp), utm_gad_sources[indexOf(utm_timestamps, last_utm_timestamp)], '') AS fallback_gad_source
- FROM
- (
- SELECT
- events.person_id,
- arrayFilter(x -> greater(x, 0), groupArray(if(equals(event, 'test_event'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps,
- arrayFilter(x -> greater(x, 0), groupArray(if(equals(event, 'test_event'), toFloat(1), 0))) AS conversion_math_values,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids,
- arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(event, 'test_event'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources,
- arrayFilter(x -> greater(x, 0), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toUnixTimestamp(events.timestamp), 0))) AS utm_timestamps,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS utm_campaigns,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_source, '')), ''))) AS utm_sources,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_medium, '')), ''))) AS utm_mediums,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_content, '')), ''))) AS utm_contents,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.utm_term, '')), ''))) AS utm_terms,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS utm_referring_domains,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gclid, '')), ''))) AS utm_gclids,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$fbclid, '')), ''))) AS utm_fbclids,
- arrayFilter(x -> notEmpty(x), groupArray(if(and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, '')))), toString(ifNull(events.properties.$gad_source, '')), ''))) AS utm_gad_sources
- FROM
- events
- WHERE
- or(and(equals(event, 'test_event'), greaterOrEquals(events.timestamp, toDateTime('2024-09-01 00:00:00')), lessOrEquals(events.timestamp, toDateTime('2024-10-31 23:59:59'))), and(equals(events.event, '$pageview'), notEmpty(toString(ifNull(events.properties.utm_campaign, ''))), notEmpty(toString(ifNull(events.properties.utm_source, ''))), greaterOrEquals(events.timestamp, minus(toDateTime('2024-09-01 00:00:00'), toIntervalSecond(7776000))), lessOrEquals(events.timestamp, toDateTime('2024-10-31 23:59:59'))))
- GROUP BY
- events.person_id
- HAVING
- greater(length(conversion_timestamps), 0))
- ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversions
- GROUP BY
- campaign,
- id,
- source) AS conv
- GROUP BY
- conv.campaign,
- conv.id,
- conv.source)
- SELECT
+ SELECT
+ campaign,
+ id,
+ source,
+ any(match_key) AS match_key,
+ sum(ifNull(toFloat(cost), 0)) AS total_cost,
+ sum(ifNull(toFloat(clicks), 0)) AS total_clicks,
+ sum(ifNull(toFloat(impressions), 0)) AS total_impressions,
+ sum(ifNull(toFloat(reported_conversion), 0)) AS total_reported_conversions,
+ sum(ifNull(toFloat(reported_conversion_value), 0)) AS total_reported_conversion_value
+ FROM
+ (
+ SELECT
+ toString(campaign1) AS match_key,
+ toString(campaign1) AS campaign,
+ toString(campaign1) AS id,
+ toString('Facebook Ads') AS source,
+ toFloat(coalesce(toFloat(impressions1), 0)) AS impressions,
+ toFloat(coalesce(toFloat(clicks1), 0)) AS clicks,
+ toFloat(convertCurrency('USD', 'USD', coalesce(toFloat(spend1), 0))) AS cost,
+ toFloat(coalesce(toFloat(conversions1), 0)) AS reported_conversion,
+ 0 AS reported_conversion_value
+ FROM
+ `bigquery.posthog_test.posthog_test_facebook_ads_table` AS posthog_test_facebook_ads_table
+ WHERE
+ and(greaterOrEquals(toDateTime(date1), toDateTime('2024-09-01 00:00:00')), lessOrEquals(toDateTime(date1), toDateTime('2024-10-31 23:59:59')))
+ UNION ALL
+ SELECT
+ toString(campaign2) AS match_key,
+ toString(campaign2) AS campaign,
+ toString(campaign2) AS id,
+ toString('TikTok Ads') AS source,
+ toFloat(coalesce(toFloat(impressions2), 0)) AS impressions,
+ toFloat(coalesce(toFloat(clicks2), 0)) AS clicks,
+ toFloat(convertCurrency('USD', 'USD', coalesce(toFloat(spend2), 0))) AS cost,
+ 0 AS reported_conversion,
+ 0 AS reported_conversion_value
+ FROM
+ `bigquery.posthog_test.posthog_test_tiktok_ads_table` AS posthog_test_tiktok_ads_table
+ WHERE
+ and(greaterOrEquals(toDateTime(date2), toDateTime('2024-09-01 00:00:00')), lessOrEquals(toDateTime(date2), toDateTime('2024-10-31 23:59:59')))
+ UNION ALL
+ SELECT
+ toString(campaign3) AS match_key,
+ toString(campaign3) AS campaign,
+ toString(campaign3) AS id,
+ toString('LinkedIn Ads') AS source,
+ toFloat(coalesce(toFloat(impressions3), 0)) AS impressions,
+ toFloat(coalesce(toFloat(clicks3), 0)) AS clicks,
+ toFloat(convertCurrency('USD', 'USD', coalesce(toFloat(spend3), 0))) AS cost,
+ 0 AS reported_conversion,
+ 0 AS reported_conversion_value
+ FROM
+ `bigquery.posthog_test.posthog_test_linkedin_ads_table` AS posthog_test_linkedin_ads_table
+ WHERE
+ and(greaterOrEquals(toDateTime(date3), toDateTime('2024-09-01 00:00:00')), lessOrEquals(toDateTime(date3), toDateTime('2024-10-31 23:59:59'))))
+ GROUP BY
+ campaign,
+ id,
+ source)
+ SELECT
campaign_costs.id AS ID,
campaign_costs.campaign AS Campaign,
campaign_costs.source AS Source,
@@ -677,194 +859,12 @@
round(campaign_costs.total_reported_conversions, 2) AS `Reported Conversions`,
round(campaign_costs.total_reported_conversion_value, 2) AS `Reported Conversion Value`,
round(divide(campaign_costs.total_reported_conversion_value, nullif(campaign_costs.total_cost, 0)), 2) AS `Reported ROAS`,
- round(divide(campaign_costs.total_cost, nullif(campaign_costs.total_reported_conversions, 0)), 2) AS `Cost per Reported Conversions`,
- ucg.conversion_0 AS `Sign Up Conversions`,
- round(divide(campaign_costs.total_cost, nullif(ucg.conversion_0, 0)), 2) AS `Cost per Sign Up Conversions`
- FROM
- campaign_costs
- LEFT JOIN ucg ON and(equals(campaign_costs.match_key, ucg.match_key), equals(campaign_costs.source, ucg.source))) AS previous_period ON and(equals(current_period.Campaign, previous_period.Campaign), equals(current_period.Source, previous_period.Source))
- ORDER BY
- Cost DESC,
- ID ASC
- LIMIT 101
- OFFSET 0
- '''
-# ---
-# name: TestMarketingAnalyticsTableQueryRunnerCompare.test_multi_source_business_metrics_validation_with_compare
- dict({
- 'query': '''
-
- SELECT
- tuple(current_period.ID, previous_period.ID) AS ID,
- tuple(current_period.Campaign, previous_period.Campaign) AS Campaign,
- tuple(current_period.Source, previous_period.Source) AS Source,
- tuple(current_period.Cost, previous_period.Cost) AS Cost,
- tuple(current_period.Clicks, previous_period.Clicks) AS Clicks,
- tuple(current_period.Impressions, previous_period.Impressions) AS Impressions,
- tuple(current_period.CPC, previous_period.CPC) AS CPC,
- tuple(current_period.CTR, previous_period.CTR) AS CTR,
- tuple(current_period.`Reported Conversions`, previous_period.`Reported Conversions`) AS `Reported Conversions`,
- tuple(current_period.`Reported Conversion Value`, previous_period.`Reported Conversion Value`) AS `Reported Conversion Value`,
- tuple(current_period.`Reported ROAS`, previous_period.`Reported ROAS`) AS `Reported ROAS`,
- tuple(current_period.`Cost per Reported Conversions`, previous_period.`Cost per Reported Conversions`) AS `Cost per Reported Conversions`
- FROM
- (WITH
- campaign_costs AS (
- SELECT
- campaign,
- id,
- source,
- any(match_key) AS match_key,
- sum(ifNull(toFloat(cost), 0)) AS total_cost,
- sum(ifNull(toFloat(clicks), 0)) AS total_clicks,
- sum(ifNull(toFloat(impressions), 0)) AS total_impressions,
- sum(ifNull(toFloat(reported_conversion), 0)) AS total_reported_conversions,
- sum(ifNull(toFloat(reported_conversion_value), 0)) AS total_reported_conversion_value
- FROM
- (
- SELECT
- toString(campaign1) AS match_key,
- toString(campaign1) AS campaign,
- toString(campaign1) AS id,
- toString('Facebook Ads') AS source,
- toFloat(coalesce(toFloat(impressions1), 0)) AS impressions,
- toFloat(coalesce(toFloat(clicks1), 0)) AS clicks,
- toFloat(convertCurrency('USD', 'USD', coalesce(toFloat(spend1), 0))) AS cost,
- toFloat(coalesce(toFloat(conversions1), 0)) AS reported_conversion,
- 0 AS reported_conversion_value
- FROM
- `bigquery.posthog_test.posthog_test_facebook_ads_table` AS posthog_test_facebook_ads_table
- WHERE
- and(greaterOrEquals(toDateTime(date1), toDateTime('2024-11-01 00:00:00')), lessOrEquals(toDateTime(date1), toDateTime('2024-12-31 23:59:59')))
- UNION ALL
- SELECT
- toString(campaign2) AS match_key,
- toString(campaign2) AS campaign,
- toString(campaign2) AS id,
- toString('TikTok Ads') AS source,
- toFloat(coalesce(toFloat(impressions2), 0)) AS impressions,
- toFloat(coalesce(toFloat(clicks2), 0)) AS clicks,
- toFloat(convertCurrency('USD', 'USD', coalesce(toFloat(spend2), 0))) AS cost,
- 0 AS reported_conversion,
- 0 AS reported_conversion_value
- FROM
- `bigquery.posthog_test.posthog_test_tiktok_ads_table` AS posthog_test_tiktok_ads_table
- WHERE
- and(greaterOrEquals(toDateTime(date2), toDateTime('2024-11-01 00:00:00')), lessOrEquals(toDateTime(date2), toDateTime('2024-12-31 23:59:59')))
- UNION ALL
- SELECT
- toString(campaign3) AS match_key,
- toString(campaign3) AS campaign,
- toString(campaign3) AS id,
- toString('LinkedIn Ads') AS source,
- toFloat(coalesce(toFloat(impressions3), 0)) AS impressions,
- toFloat(coalesce(toFloat(clicks3), 0)) AS clicks,
- toFloat(convertCurrency('USD', 'USD', coalesce(toFloat(spend3), 0))) AS cost,
- 0 AS reported_conversion,
- 0 AS reported_conversion_value
- FROM
- `bigquery.posthog_test.posthog_test_linkedin_ads_table` AS posthog_test_linkedin_ads_table
- WHERE
- and(greaterOrEquals(toDateTime(date3), toDateTime('2024-11-01 00:00:00')), lessOrEquals(toDateTime(date3), toDateTime('2024-12-31 23:59:59'))))
- GROUP BY
- campaign,
- id,
- source)
- SELECT
- campaign_costs.id AS ID,
- campaign_costs.campaign AS Campaign,
- campaign_costs.source AS Source,
- round(campaign_costs.total_cost, 2) AS Cost,
- round(campaign_costs.total_clicks, 0) AS Clicks,
- round(campaign_costs.total_impressions, 0) AS Impressions,
- round(divide(campaign_costs.total_cost, nullif(campaign_costs.total_clicks, 0)), 2) AS CPC,
- round(multiply(divide(campaign_costs.total_clicks, nullif(campaign_costs.total_impressions, 0)), 100), 2) AS CTR,
- round(campaign_costs.total_reported_conversions, 2) AS `Reported Conversions`,
- round(campaign_costs.total_reported_conversion_value, 2) AS `Reported Conversion Value`,
- round(divide(campaign_costs.total_reported_conversion_value, nullif(campaign_costs.total_cost, 0)), 2) AS `Reported ROAS`,
- round(divide(campaign_costs.total_cost, nullif(campaign_costs.total_reported_conversions, 0)), 2) AS `Cost per Reported Conversions`
- FROM
- campaign_costs) AS current_period
- LEFT JOIN (WITH
- campaign_costs AS (
- SELECT
- campaign,
- id,
- source,
- any(match_key) AS match_key,
- sum(ifNull(toFloat(cost), 0)) AS total_cost,
- sum(ifNull(toFloat(clicks), 0)) AS total_clicks,
- sum(ifNull(toFloat(impressions), 0)) AS total_impressions,
- sum(ifNull(toFloat(reported_conversion), 0)) AS total_reported_conversions,
- sum(ifNull(toFloat(reported_conversion_value), 0)) AS total_reported_conversion_value
- FROM
- (
- SELECT
- toString(campaign1) AS match_key,
- toString(campaign1) AS campaign,
- toString(campaign1) AS id,
- toString('Facebook Ads') AS source,
- toFloat(coalesce(toFloat(impressions1), 0)) AS impressions,
- toFloat(coalesce(toFloat(clicks1), 0)) AS clicks,
- toFloat(convertCurrency('USD', 'USD', coalesce(toFloat(spend1), 0))) AS cost,
- toFloat(coalesce(toFloat(conversions1), 0)) AS reported_conversion,
- 0 AS reported_conversion_value
- FROM
- `bigquery.posthog_test.posthog_test_facebook_ads_table` AS posthog_test_facebook_ads_table
- WHERE
- and(greaterOrEquals(toDateTime(date1), toDateTime('2024-09-01 00:00:00')), lessOrEquals(toDateTime(date1), toDateTime('2024-10-31 23:59:59')))
- UNION ALL
- SELECT
- toString(campaign2) AS match_key,
- toString(campaign2) AS campaign,
- toString(campaign2) AS id,
- toString('TikTok Ads') AS source,
- toFloat(coalesce(toFloat(impressions2), 0)) AS impressions,
- toFloat(coalesce(toFloat(clicks2), 0)) AS clicks,
- toFloat(convertCurrency('USD', 'USD', coalesce(toFloat(spend2), 0))) AS cost,
- 0 AS reported_conversion,
- 0 AS reported_conversion_value
- FROM
- `bigquery.posthog_test.posthog_test_tiktok_ads_table` AS posthog_test_tiktok_ads_table
- WHERE
- and(greaterOrEquals(toDateTime(date2), toDateTime('2024-09-01 00:00:00')), lessOrEquals(toDateTime(date2), toDateTime('2024-10-31 23:59:59')))
- UNION ALL
- SELECT
- toString(campaign3) AS match_key,
- toString(campaign3) AS campaign,
- toString(campaign3) AS id,
- toString('LinkedIn Ads') AS source,
- toFloat(coalesce(toFloat(impressions3), 0)) AS impressions,
- toFloat(coalesce(toFloat(clicks3), 0)) AS clicks,
- toFloat(convertCurrency('USD', 'USD', coalesce(toFloat(spend3), 0))) AS cost,
- 0 AS reported_conversion,
- 0 AS reported_conversion_value
- FROM
- `bigquery.posthog_test.posthog_test_linkedin_ads_table` AS posthog_test_linkedin_ads_table
- WHERE
- and(greaterOrEquals(toDateTime(date3), toDateTime('2024-09-01 00:00:00')), lessOrEquals(toDateTime(date3), toDateTime('2024-10-31 23:59:59'))))
- GROUP BY
- campaign,
- id,
- source)
- SELECT
- campaign_costs.id AS ID,
- campaign_costs.campaign AS Campaign,
- campaign_costs.source AS Source,
- round(campaign_costs.total_cost, 2) AS Cost,
- round(campaign_costs.total_clicks, 0) AS Clicks,
- round(campaign_costs.total_impressions, 0) AS Impressions,
- round(divide(campaign_costs.total_cost, nullif(campaign_costs.total_clicks, 0)), 2) AS CPC,
- round(multiply(divide(campaign_costs.total_clicks, nullif(campaign_costs.total_impressions, 0)), 100), 2) AS CTR,
- round(campaign_costs.total_reported_conversions, 2) AS `Reported Conversions`,
- round(campaign_costs.total_reported_conversion_value, 2) AS `Reported Conversion Value`,
- round(divide(campaign_costs.total_reported_conversion_value, nullif(campaign_costs.total_cost, 0)), 2) AS `Reported ROAS`,
- round(divide(campaign_costs.total_cost, nullif(campaign_costs.total_reported_conversions, 0)), 2) AS `Cost per Reported Conversions`
- FROM
- campaign_costs) AS previous_period ON and(equals(current_period.Campaign, previous_period.Campaign), equals(current_period.Source, previous_period.Source))
+ round(divide(campaign_costs.total_cost, nullif(campaign_costs.total_reported_conversions, 0)), 2) AS `Cost per Reported Conversions`
+ FROM
+ campaign_costs) AS previous_period ON and(equals(current_period.Campaign, previous_period.Campaign), equals(current_period.Source, previous_period.Source))
ORDER BY
- Cost DESC,
- ID ASC
+ Cost DESC,
+ ID ASC
LIMIT 101
OFFSET 0
''',
diff --git a/products/web_analytics/backend/hogql_queries/test/__snapshots__/test_traffic_type_snapshot.ambr b/products/web_analytics/backend/hogql_queries/test/__snapshots__/test_traffic_type_snapshot.ambr
index e9465cb0cf52..82675b660c14 100644
--- a/products/web_analytics/backend/hogql_queries/test/__snapshots__/test_traffic_type_snapshot.ambr
+++ b/products/web_analytics/backend/hogql_queries/test/__snapshots__/test_traffic_type_snapshot.ambr
@@ -3,27 +3,27 @@
'''
-- ClickHouse
SELECT
- events.event AS event,
- replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '') AS user_agent
+ events.event AS event,
+ replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', '') AS user_agent
FROM
- events
+ events
WHERE
- and(equals(events.team_id, 99999), equals(events.event, %(hogql_val_1)s), not(ifNull(notEquals(multiMatchAnyIndex(ifNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_2)s), ''), 'null'), '^"|"$', ''), %(hogql_val_3)s), [%(hogql_val_4)s, %(hogql_val_5)s, %(hogql_val_6)s, %(hogql_val_7)s, %(hogql_val_8)s, %(hogql_val_9)s, %(hogql_val_10)s, %(hogql_val_11)s, %(hogql_val_12)s, %(hogql_val_13)s, %(hogql_val_14)s, %(hogql_val_15)s, %(hogql_val_16)s, %(hogql_val_17)s, %(hogql_val_18)s, %(hogql_val_19)s, %(hogql_val_20)s, %(hogql_val_21)s, %(hogql_val_22)s, %(hogql_val_23)s, %(hogql_val_24)s, %(hogql_val_25)s, %(hogql_val_26)s, %(hogql_val_27)s, %(hogql_val_28)s, %(hogql_val_29)s, %(hogql_val_30)s, %(hogql_val_31)s, %(hogql_val_32)s, %(hogql_val_33)s, %(hogql_val_34)s, %(hogql_val_35)s, %(hogql_val_36)s, %(hogql_val_37)s, %(hogql_val_38)s, %(hogql_val_39)s, %(hogql_val_40)s, %(hogql_val_41)s, %(hogql_val_42)s, %(hogql_val_43)s, %(hogql_val_44)s, %(hogql_val_45)s, %(hogql_val_46)s, %(hogql_val_47)s, %(hogql_val_48)s, %(hogql_val_49)s, %(hogql_val_50)s, %(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, %(hogql_val_59)s, %(hogql_val_60)s, %(hogql_val_61)s, %(hogql_val_62)s, %(hogql_val_63)s, %(hogql_val_64)s, %(hogql_val_65)s, %(hogql_val_66)s, %(hogql_val_67)s, %(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, %(hogql_val_79)s, %(hogql_val_80)s, %(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, %(hogql_val_91)s, %(hogql_val_92)s, %(hogql_val_93)s, %(hogql_val_94)s, %(hogql_val_95)s, %(hogql_val_96)s, %(hogql_val_97)s, %(hogql_val_98)s, %(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, %(hogql_val_107)s, %(hogql_val_108)s, %(hogql_val_109)s, %(hogql_val_110)s, %(hogql_val_111)s]), 0), 1)))
+ and(equals(events.team_id, 99999), equals(events.event, %(hogql_val_1)s), not(ifNull(notEquals(multiMatchAnyIndex(ifNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_2)s), ''), 'null'), '^"|"$', ''), %(hogql_val_3)s), [%(hogql_val_4)s, %(hogql_val_5)s, %(hogql_val_6)s, %(hogql_val_7)s, %(hogql_val_8)s, %(hogql_val_9)s, %(hogql_val_10)s, %(hogql_val_11)s, %(hogql_val_12)s, %(hogql_val_13)s, %(hogql_val_14)s, %(hogql_val_15)s, %(hogql_val_16)s, %(hogql_val_17)s, %(hogql_val_18)s, %(hogql_val_19)s, %(hogql_val_20)s, %(hogql_val_21)s, %(hogql_val_22)s, %(hogql_val_23)s, %(hogql_val_24)s, %(hogql_val_25)s, %(hogql_val_26)s, %(hogql_val_27)s, %(hogql_val_28)s, %(hogql_val_29)s, %(hogql_val_30)s, %(hogql_val_31)s, %(hogql_val_32)s, %(hogql_val_33)s, %(hogql_val_34)s, %(hogql_val_35)s, %(hogql_val_36)s, %(hogql_val_37)s, %(hogql_val_38)s, %(hogql_val_39)s, %(hogql_val_40)s, %(hogql_val_41)s, %(hogql_val_42)s, %(hogql_val_43)s, %(hogql_val_44)s, %(hogql_val_45)s, %(hogql_val_46)s, %(hogql_val_47)s, %(hogql_val_48)s, %(hogql_val_49)s, %(hogql_val_50)s, %(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, %(hogql_val_59)s, %(hogql_val_60)s, %(hogql_val_61)s, %(hogql_val_62)s, %(hogql_val_63)s, %(hogql_val_64)s, %(hogql_val_65)s, %(hogql_val_66)s, %(hogql_val_67)s, %(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, %(hogql_val_79)s, %(hogql_val_80)s, %(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, %(hogql_val_91)s, %(hogql_val_92)s, %(hogql_val_93)s, %(hogql_val_94)s, %(hogql_val_95)s, %(hogql_val_96)s, %(hogql_val_97)s, %(hogql_val_98)s, %(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, %(hogql_val_107)s, %(hogql_val_108)s, %(hogql_val_109)s, %(hogql_val_110)s, %(hogql_val_111)s]), 0), 1)))
ORDER BY
- user_agent ASC
+ user_agent ASC
LIMIT 100
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0, transform_null_in=1, optimize_min_equality_disjunction_chain_length=4294967295, allow_experimental_join_condition=1, use_hive_partitioning=0
-- HogQL
SELECT
- event,
- properties.$user_agent AS user_agent
+ event,
+ properties.$user_agent AS user_agent
FROM
- events
+ events
WHERE
- and(equals(event, '$pageview'), not(notEquals(multiMatchAnyIndex(ifNull(properties.$user_agent, ''), ['GPTBot', 'Google-CloudVertexBot', 'Google-Extended', 'GoogleOther', 'Claude-SearchBot', 'Claude-User', 'ClaudeBot', 'Claude-Web', 'anthropic-ai', 'Perplexity-User', 'PerplexityBot', 'CCBot', 'meta-externalagent', 'Bytespider', 'TikTokSpider', 'cohere-ai', 'Diffbot', 'omgili', 'Webzio-Extended', 'Timpibot', 'Amazonbot', 'PetalBot', 'Brightbot', 'OAI-SearchBot', 'Applebot-Extended', 'ChatGPT-User', 'meta-externalfetcher', 'Meta-ExternalFetcher', 'DuckAssistBot', 'MistralAI-User', 'Manus-User', 'Google-NotebookLM', 'Shap-User', 'desktop\\.hog\\.dev', 'mobile\\.hog\\.dev', 'agent\\.hog\\.dev', 'cloud\\.hog\\.dev', 'Applebot/', 'Googlebot', 'bingbot', 'Bingbot', 'YandexBot', 'Baiduspider', 'DuckDuckBot', 'Slurp', 'Yeti/', 'YisouSpider', 'Sogou web spider', 'pageburst', '360Spider', 'AdsBot-Google', 'Google-InspectionTool', 'Google-Food', 'AhrefsSiteAudit', 'AhrefsBot', 'Barkrowler', 'SemrushBot', 'SERankingBacklinksBot', 'MJ12bot', 'DotBot', 'Lighthouse', 'MeltwaterNews', 'FacebookBot', 'facebookexternalhit', 'Twitterbot', 'LinkedInBot', 'Pinterest', 'Slackbot', 'Slack-ImgProxy', 'TelegramBot', 'WhatsApp', 'GoogleImageProxy', 'Iframely', 'Pingdom', 'UptimeRobot', 'Site24x7', 'StatusCake', 'Datadog', 'GrafanaSyntheticMonitoring', 'DMBrowser', 'DigitalOcean Uptime Probe', 'curl/', 'Wget', 'python-requests', 'axios', 'node-fetch', 'Go-http-client', 'okhttp', 'Apache-HttpClient', 'libwww-perl', 'Scrapy', 'PlayStore-Google', 'Amazon CloudFront', 'AmazonProductDiscovery', 'Google-BusinessLinkVerification', 'Storebot-Google', 'Google-Read-Aloud', 'Google-Appointments', 'Google-Actions', 'OneTrust', 'Chrome Privacy Preserving Prefetch Proxy', 'Mozlila/', 'HeadlessChrome', 'PhantomJS', 'Puppeteer', 'Playwright', 'Selenium', '^$']), 0)))
+ and(equals(event, '$pageview'), not(notEquals(multiMatchAnyIndex(ifNull(properties.$user_agent, ''), ['GPTBot', 'Google-CloudVertexBot', 'Google-Extended', 'GoogleOther', 'Claude-SearchBot', 'Claude-User', 'ClaudeBot', 'Claude-Web', 'anthropic-ai', 'Perplexity-User', 'PerplexityBot', 'CCBot', 'meta-externalagent', 'Bytespider', 'TikTokSpider', 'cohere-ai', 'Diffbot', 'omgili', 'Webzio-Extended', 'Timpibot', 'Amazonbot', 'PetalBot', 'Brightbot', 'OAI-SearchBot', 'Applebot-Extended', 'ChatGPT-User', 'meta-externalfetcher', 'Meta-ExternalFetcher', 'DuckAssistBot', 'MistralAI-User', 'Manus-User', 'Google-NotebookLM', 'Shap-User', 'desktop\\.hog\\.dev', 'mobile\\.hog\\.dev', 'agent\\.hog\\.dev', 'cloud\\.hog\\.dev', 'Applebot/', 'Googlebot', 'bingbot', 'Bingbot', 'YandexBot', 'Baiduspider', 'DuckDuckBot', 'Slurp', 'Yeti/', 'YisouSpider', 'Sogou web spider', 'pageburst', '360Spider', 'AdsBot-Google', 'Google-InspectionTool', 'Google-Food', 'AhrefsSiteAudit', 'AhrefsBot', 'Barkrowler', 'SemrushBot', 'SERankingBacklinksBot', 'MJ12bot', 'DotBot', 'Lighthouse', 'MeltwaterNews', 'FacebookBot', 'facebookexternalhit', 'Twitterbot', 'LinkedInBot', 'Pinterest', 'Slackbot', 'Slack-ImgProxy', 'TelegramBot', 'WhatsApp', 'GoogleImageProxy', 'Iframely', 'Pingdom', 'UptimeRobot', 'Site24x7', 'StatusCake', 'Datadog', 'GrafanaSyntheticMonitoring', 'DMBrowser', 'DigitalOcean Uptime Probe', 'curl/', 'Wget', 'python-requests', 'axios', 'node-fetch', 'Go-http-client', 'okhttp', 'Apache-HttpClient', 'libwww-perl', 'Scrapy', 'PlayStore-Google', 'Amazon CloudFront', 'AmazonProductDiscovery', 'Google-BusinessLinkVerification', 'Storebot-Google', 'Google-Read-Aloud', 'Google-Appointments', 'Google-Actions', 'OneTrust', 'Chrome Privacy Preserving Prefetch Proxy', 'Mozlila/', 'HeadlessChrome', 'PhantomJS', 'Puppeteer', 'Playwright', 'Selenium', '^$']), 0)))
ORDER BY
- user_agent ASC
+ user_agent ASC
LIMIT 100
'''
# ---
@@ -31,31 +31,31 @@
'''
-- ClickHouse
SELECT
- if(ifNull(equals(multiMatchAnyIndex(ifNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', ''), %(hogql_val_1)s), [%(hogql_val_2)s, %(hogql_val_3)s, %(hogql_val_4)s, %(hogql_val_5)s, %(hogql_val_6)s, %(hogql_val_7)s, %(hogql_val_8)s, %(hogql_val_9)s, %(hogql_val_10)s, %(hogql_val_11)s, %(hogql_val_12)s, %(hogql_val_13)s, %(hogql_val_14)s, %(hogql_val_15)s, %(hogql_val_16)s, %(hogql_val_17)s, %(hogql_val_18)s, %(hogql_val_19)s, %(hogql_val_20)s, %(hogql_val_21)s, %(hogql_val_22)s, %(hogql_val_23)s, %(hogql_val_24)s, %(hogql_val_25)s, %(hogql_val_26)s, %(hogql_val_27)s, %(hogql_val_28)s, %(hogql_val_29)s, %(hogql_val_30)s, %(hogql_val_31)s, %(hogql_val_32)s, %(hogql_val_33)s, %(hogql_val_34)s, %(hogql_val_35)s, %(hogql_val_36)s, %(hogql_val_37)s, %(hogql_val_38)s, %(hogql_val_39)s, %(hogql_val_40)s, %(hogql_val_41)s, %(hogql_val_42)s, %(hogql_val_43)s, %(hogql_val_44)s, %(hogql_val_45)s, %(hogql_val_46)s, %(hogql_val_47)s, %(hogql_val_48)s, %(hogql_val_49)s, %(hogql_val_50)s, %(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, %(hogql_val_59)s, %(hogql_val_60)s, %(hogql_val_61)s, %(hogql_val_62)s, %(hogql_val_63)s, %(hogql_val_64)s, %(hogql_val_65)s, %(hogql_val_66)s, %(hogql_val_67)s, %(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, %(hogql_val_79)s, %(hogql_val_80)s, %(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, %(hogql_val_91)s, %(hogql_val_92)s, %(hogql_val_93)s, %(hogql_val_94)s, %(hogql_val_95)s, %(hogql_val_96)s, %(hogql_val_97)s, %(hogql_val_98)s, %(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, %(hogql_val_107)s, %(hogql_val_108)s, %(hogql_val_109)s]), 0), 0), %(hogql_val_110)s, [%(hogql_val_111)s, %(hogql_val_112)s, %(hogql_val_113)s, %(hogql_val_114)s, %(hogql_val_115)s, %(hogql_val_116)s, %(hogql_val_117)s, %(hogql_val_118)s, %(hogql_val_119)s, %(hogql_val_120)s, %(hogql_val_121)s, %(hogql_val_122)s, %(hogql_val_123)s, %(hogql_val_124)s, %(hogql_val_125)s, %(hogql_val_126)s, %(hogql_val_127)s, %(hogql_val_128)s, %(hogql_val_129)s, %(hogql_val_130)s, %(hogql_val_131)s, %(hogql_val_132)s, %(hogql_val_133)s, %(hogql_val_134)s, %(hogql_val_135)s, %(hogql_val_136)s, %(hogql_val_137)s, %(hogql_val_138)s, %(hogql_val_139)s, %(hogql_val_140)s, %(hogql_val_141)s, %(hogql_val_142)s, %(hogql_val_143)s, %(hogql_val_144)s, %(hogql_val_145)s, %(hogql_val_146)s, %(hogql_val_147)s, %(hogql_val_148)s, %(hogql_val_149)s, %(hogql_val_150)s, %(hogql_val_151)s, %(hogql_val_152)s, %(hogql_val_153)s, %(hogql_val_154)s, %(hogql_val_155)s, %(hogql_val_156)s, %(hogql_val_157)s, %(hogql_val_158)s, %(hogql_val_159)s, %(hogql_val_160)s, %(hogql_val_161)s, %(hogql_val_162)s, %(hogql_val_163)s, %(hogql_val_164)s, %(hogql_val_165)s, %(hogql_val_166)s, %(hogql_val_167)s, %(hogql_val_168)s, %(hogql_val_169)s, %(hogql_val_170)s, %(hogql_val_171)s, %(hogql_val_172)s, %(hogql_val_173)s, %(hogql_val_174)s, %(hogql_val_175)s, %(hogql_val_176)s, %(hogql_val_177)s, %(hogql_val_178)s, %(hogql_val_179)s, %(hogql_val_180)s, %(hogql_val_181)s, %(hogql_val_182)s, %(hogql_val_183)s, %(hogql_val_184)s, %(hogql_val_185)s, %(hogql_val_186)s, %(hogql_val_187)s, %(hogql_val_188)s, %(hogql_val_189)s, %(hogql_val_190)s, %(hogql_val_191)s, %(hogql_val_192)s, %(hogql_val_193)s, %(hogql_val_194)s, %(hogql_val_195)s, %(hogql_val_196)s, %(hogql_val_197)s, %(hogql_val_198)s, %(hogql_val_199)s, %(hogql_val_200)s, %(hogql_val_201)s, %(hogql_val_202)s, %(hogql_val_203)s, %(hogql_val_204)s, %(hogql_val_205)s, %(hogql_val_206)s, %(hogql_val_207)s, %(hogql_val_208)s, %(hogql_val_209)s, %(hogql_val_210)s, %(hogql_val_211)s, %(hogql_val_212)s, %(hogql_val_213)s, %(hogql_val_214)s, %(hogql_val_215)s, %(hogql_val_216)s, %(hogql_val_217)s, %(hogql_val_218)s][multiMatchAnyIndex(ifNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_219)s), ''), 'null'), '^"|"$', ''), %(hogql_val_220)s), [%(hogql_val_221)s, %(hogql_val_222)s, %(hogql_val_223)s, %(hogql_val_224)s, %(hogql_val_225)s, %(hogql_val_226)s, %(hogql_val_227)s, %(hogql_val_228)s, %(hogql_val_229)s, %(hogql_val_230)s, %(hogql_val_231)s, %(hogql_val_232)s, %(hogql_val_233)s, %(hogql_val_234)s, %(hogql_val_235)s, %(hogql_val_236)s, %(hogql_val_237)s, %(hogql_val_238)s, %(hogql_val_239)s, %(hogql_val_240)s, %(hogql_val_241)s, %(hogql_val_242)s, %(hogql_val_243)s, %(hogql_val_244)s, %(hogql_val_245)s, %(hogql_val_246)s, %(hogql_val_247)s, %(hogql_val_248)s, %(hogql_val_249)s, %(hogql_val_250)s, %(hogql_val_251)s, %(hogql_val_252)s, %(hogql_val_253)s, %(hogql_val_254)s, %(hogql_val_255)s, %(hogql_val_256)s, %(hogql_val_257)s, %(hogql_val_258)s, %(hogql_val_259)s, %(hogql_val_260)s, %(hogql_val_261)s, %(hogql_val_262)s, %(hogql_val_263)s, %(hogql_val_264)s, %(hogql_val_265)s, %(hogql_val_266)s, %(hogql_val_267)s, %(hogql_val_268)s, %(hogql_val_269)s, %(hogql_val_270)s, %(hogql_val_271)s, %(hogql_val_272)s, %(hogql_val_273)s, %(hogql_val_274)s, %(hogql_val_275)s, %(hogql_val_276)s, %(hogql_val_277)s, %(hogql_val_278)s, %(hogql_val_279)s, %(hogql_val_280)s, %(hogql_val_281)s, %(hogql_val_282)s, %(hogql_val_283)s, %(hogql_val_284)s, %(hogql_val_285)s, %(hogql_val_286)s, %(hogql_val_287)s, %(hogql_val_288)s, %(hogql_val_289)s, %(hogql_val_290)s, %(hogql_val_291)s, %(hogql_val_292)s, %(hogql_val_293)s, %(hogql_val_294)s, %(hogql_val_295)s, %(hogql_val_296)s, %(hogql_val_297)s, %(hogql_val_298)s, %(hogql_val_299)s, %(hogql_val_300)s, %(hogql_val_301)s, %(hogql_val_302)s, %(hogql_val_303)s, %(hogql_val_304)s, %(hogql_val_305)s, %(hogql_val_306)s, %(hogql_val_307)s, %(hogql_val_308)s, %(hogql_val_309)s, %(hogql_val_310)s, %(hogql_val_311)s, %(hogql_val_312)s, %(hogql_val_313)s, %(hogql_val_314)s, %(hogql_val_315)s, %(hogql_val_316)s, %(hogql_val_317)s, %(hogql_val_318)s, %(hogql_val_319)s, %(hogql_val_320)s, %(hogql_val_321)s, %(hogql_val_322)s, %(hogql_val_323)s, %(hogql_val_324)s, %(hogql_val_325)s, %(hogql_val_326)s, %(hogql_val_327)s, %(hogql_val_328)s])]) AS bot_name,
- count() AS count
+ if(ifNull(equals(multiMatchAnyIndex(ifNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', ''), %(hogql_val_1)s), [%(hogql_val_2)s, %(hogql_val_3)s, %(hogql_val_4)s, %(hogql_val_5)s, %(hogql_val_6)s, %(hogql_val_7)s, %(hogql_val_8)s, %(hogql_val_9)s, %(hogql_val_10)s, %(hogql_val_11)s, %(hogql_val_12)s, %(hogql_val_13)s, %(hogql_val_14)s, %(hogql_val_15)s, %(hogql_val_16)s, %(hogql_val_17)s, %(hogql_val_18)s, %(hogql_val_19)s, %(hogql_val_20)s, %(hogql_val_21)s, %(hogql_val_22)s, %(hogql_val_23)s, %(hogql_val_24)s, %(hogql_val_25)s, %(hogql_val_26)s, %(hogql_val_27)s, %(hogql_val_28)s, %(hogql_val_29)s, %(hogql_val_30)s, %(hogql_val_31)s, %(hogql_val_32)s, %(hogql_val_33)s, %(hogql_val_34)s, %(hogql_val_35)s, %(hogql_val_36)s, %(hogql_val_37)s, %(hogql_val_38)s, %(hogql_val_39)s, %(hogql_val_40)s, %(hogql_val_41)s, %(hogql_val_42)s, %(hogql_val_43)s, %(hogql_val_44)s, %(hogql_val_45)s, %(hogql_val_46)s, %(hogql_val_47)s, %(hogql_val_48)s, %(hogql_val_49)s, %(hogql_val_50)s, %(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, %(hogql_val_59)s, %(hogql_val_60)s, %(hogql_val_61)s, %(hogql_val_62)s, %(hogql_val_63)s, %(hogql_val_64)s, %(hogql_val_65)s, %(hogql_val_66)s, %(hogql_val_67)s, %(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, %(hogql_val_79)s, %(hogql_val_80)s, %(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, %(hogql_val_91)s, %(hogql_val_92)s, %(hogql_val_93)s, %(hogql_val_94)s, %(hogql_val_95)s, %(hogql_val_96)s, %(hogql_val_97)s, %(hogql_val_98)s, %(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, %(hogql_val_107)s, %(hogql_val_108)s, %(hogql_val_109)s]), 0), 0), %(hogql_val_110)s, [%(hogql_val_111)s, %(hogql_val_112)s, %(hogql_val_113)s, %(hogql_val_114)s, %(hogql_val_115)s, %(hogql_val_116)s, %(hogql_val_117)s, %(hogql_val_118)s, %(hogql_val_119)s, %(hogql_val_120)s, %(hogql_val_121)s, %(hogql_val_122)s, %(hogql_val_123)s, %(hogql_val_124)s, %(hogql_val_125)s, %(hogql_val_126)s, %(hogql_val_127)s, %(hogql_val_128)s, %(hogql_val_129)s, %(hogql_val_130)s, %(hogql_val_131)s, %(hogql_val_132)s, %(hogql_val_133)s, %(hogql_val_134)s, %(hogql_val_135)s, %(hogql_val_136)s, %(hogql_val_137)s, %(hogql_val_138)s, %(hogql_val_139)s, %(hogql_val_140)s, %(hogql_val_141)s, %(hogql_val_142)s, %(hogql_val_143)s, %(hogql_val_144)s, %(hogql_val_145)s, %(hogql_val_146)s, %(hogql_val_147)s, %(hogql_val_148)s, %(hogql_val_149)s, %(hogql_val_150)s, %(hogql_val_151)s, %(hogql_val_152)s, %(hogql_val_153)s, %(hogql_val_154)s, %(hogql_val_155)s, %(hogql_val_156)s, %(hogql_val_157)s, %(hogql_val_158)s, %(hogql_val_159)s, %(hogql_val_160)s, %(hogql_val_161)s, %(hogql_val_162)s, %(hogql_val_163)s, %(hogql_val_164)s, %(hogql_val_165)s, %(hogql_val_166)s, %(hogql_val_167)s, %(hogql_val_168)s, %(hogql_val_169)s, %(hogql_val_170)s, %(hogql_val_171)s, %(hogql_val_172)s, %(hogql_val_173)s, %(hogql_val_174)s, %(hogql_val_175)s, %(hogql_val_176)s, %(hogql_val_177)s, %(hogql_val_178)s, %(hogql_val_179)s, %(hogql_val_180)s, %(hogql_val_181)s, %(hogql_val_182)s, %(hogql_val_183)s, %(hogql_val_184)s, %(hogql_val_185)s, %(hogql_val_186)s, %(hogql_val_187)s, %(hogql_val_188)s, %(hogql_val_189)s, %(hogql_val_190)s, %(hogql_val_191)s, %(hogql_val_192)s, %(hogql_val_193)s, %(hogql_val_194)s, %(hogql_val_195)s, %(hogql_val_196)s, %(hogql_val_197)s, %(hogql_val_198)s, %(hogql_val_199)s, %(hogql_val_200)s, %(hogql_val_201)s, %(hogql_val_202)s, %(hogql_val_203)s, %(hogql_val_204)s, %(hogql_val_205)s, %(hogql_val_206)s, %(hogql_val_207)s, %(hogql_val_208)s, %(hogql_val_209)s, %(hogql_val_210)s, %(hogql_val_211)s, %(hogql_val_212)s, %(hogql_val_213)s, %(hogql_val_214)s, %(hogql_val_215)s, %(hogql_val_216)s, %(hogql_val_217)s, %(hogql_val_218)s][multiMatchAnyIndex(ifNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_219)s), ''), 'null'), '^"|"$', ''), %(hogql_val_220)s), [%(hogql_val_221)s, %(hogql_val_222)s, %(hogql_val_223)s, %(hogql_val_224)s, %(hogql_val_225)s, %(hogql_val_226)s, %(hogql_val_227)s, %(hogql_val_228)s, %(hogql_val_229)s, %(hogql_val_230)s, %(hogql_val_231)s, %(hogql_val_232)s, %(hogql_val_233)s, %(hogql_val_234)s, %(hogql_val_235)s, %(hogql_val_236)s, %(hogql_val_237)s, %(hogql_val_238)s, %(hogql_val_239)s, %(hogql_val_240)s, %(hogql_val_241)s, %(hogql_val_242)s, %(hogql_val_243)s, %(hogql_val_244)s, %(hogql_val_245)s, %(hogql_val_246)s, %(hogql_val_247)s, %(hogql_val_248)s, %(hogql_val_249)s, %(hogql_val_250)s, %(hogql_val_251)s, %(hogql_val_252)s, %(hogql_val_253)s, %(hogql_val_254)s, %(hogql_val_255)s, %(hogql_val_256)s, %(hogql_val_257)s, %(hogql_val_258)s, %(hogql_val_259)s, %(hogql_val_260)s, %(hogql_val_261)s, %(hogql_val_262)s, %(hogql_val_263)s, %(hogql_val_264)s, %(hogql_val_265)s, %(hogql_val_266)s, %(hogql_val_267)s, %(hogql_val_268)s, %(hogql_val_269)s, %(hogql_val_270)s, %(hogql_val_271)s, %(hogql_val_272)s, %(hogql_val_273)s, %(hogql_val_274)s, %(hogql_val_275)s, %(hogql_val_276)s, %(hogql_val_277)s, %(hogql_val_278)s, %(hogql_val_279)s, %(hogql_val_280)s, %(hogql_val_281)s, %(hogql_val_282)s, %(hogql_val_283)s, %(hogql_val_284)s, %(hogql_val_285)s, %(hogql_val_286)s, %(hogql_val_287)s, %(hogql_val_288)s, %(hogql_val_289)s, %(hogql_val_290)s, %(hogql_val_291)s, %(hogql_val_292)s, %(hogql_val_293)s, %(hogql_val_294)s, %(hogql_val_295)s, %(hogql_val_296)s, %(hogql_val_297)s, %(hogql_val_298)s, %(hogql_val_299)s, %(hogql_val_300)s, %(hogql_val_301)s, %(hogql_val_302)s, %(hogql_val_303)s, %(hogql_val_304)s, %(hogql_val_305)s, %(hogql_val_306)s, %(hogql_val_307)s, %(hogql_val_308)s, %(hogql_val_309)s, %(hogql_val_310)s, %(hogql_val_311)s, %(hogql_val_312)s, %(hogql_val_313)s, %(hogql_val_314)s, %(hogql_val_315)s, %(hogql_val_316)s, %(hogql_val_317)s, %(hogql_val_318)s, %(hogql_val_319)s, %(hogql_val_320)s, %(hogql_val_321)s, %(hogql_val_322)s, %(hogql_val_323)s, %(hogql_val_324)s, %(hogql_val_325)s, %(hogql_val_326)s, %(hogql_val_327)s, %(hogql_val_328)s])]) AS bot_name,
+ count() AS count
FROM
- events
+ events
WHERE
- and(equals(events.team_id, 99999), equals(events.event, %(hogql_val_329)s))
+ and(equals(events.team_id, 99999), equals(events.event, %(hogql_val_329)s))
GROUP BY
- bot_name
+ bot_name
ORDER BY
- bot_name ASC
+ bot_name ASC
LIMIT 100
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0, transform_null_in=1, optimize_min_equality_disjunction_chain_length=4294967295, allow_experimental_join_condition=1, use_hive_partitioning=0
-- HogQL
SELECT
- if(equals(multiMatchAnyIndex(ifNull(properties.$user_agent, ''), ['GPTBot', 'Google-CloudVertexBot', 'Google-Extended', 'GoogleOther', 'Claude-SearchBot', 'Claude-User', 'ClaudeBot', 'Claude-Web', 'anthropic-ai', 'Perplexity-User', 'PerplexityBot', 'CCBot', 'meta-externalagent', 'Bytespider', 'TikTokSpider', 'cohere-ai', 'Diffbot', 'omgili', 'Webzio-Extended', 'Timpibot', 'Amazonbot', 'PetalBot', 'Brightbot', 'OAI-SearchBot', 'Applebot-Extended', 'ChatGPT-User', 'meta-externalfetcher', 'Meta-ExternalFetcher', 'DuckAssistBot', 'MistralAI-User', 'Manus-User', 'Google-NotebookLM', 'Shap-User', 'desktop\\.hog\\.dev', 'mobile\\.hog\\.dev', 'agent\\.hog\\.dev', 'cloud\\.hog\\.dev', 'Applebot/', 'Googlebot', 'bingbot', 'Bingbot', 'YandexBot', 'Baiduspider', 'DuckDuckBot', 'Slurp', 'Yeti/', 'YisouSpider', 'Sogou web spider', 'pageburst', '360Spider', 'AdsBot-Google', 'Google-InspectionTool', 'Google-Food', 'AhrefsSiteAudit', 'AhrefsBot', 'Barkrowler', 'SemrushBot', 'SERankingBacklinksBot', 'MJ12bot', 'DotBot', 'Lighthouse', 'MeltwaterNews', 'FacebookBot', 'facebookexternalhit', 'Twitterbot', 'LinkedInBot', 'Pinterest', 'Slackbot', 'Slack-ImgProxy', 'TelegramBot', 'WhatsApp', 'GoogleImageProxy', 'Iframely', 'Pingdom', 'UptimeRobot', 'Site24x7', 'StatusCake', 'Datadog', 'GrafanaSyntheticMonitoring', 'DMBrowser', 'DigitalOcean Uptime Probe', 'curl/', 'Wget', 'python-requests', 'axios', 'node-fetch', 'Go-http-client', 'okhttp', 'Apache-HttpClient', 'libwww-perl', 'Scrapy', 'PlayStore-Google', 'Amazon CloudFront', 'AmazonProductDiscovery', 'Google-BusinessLinkVerification', 'Storebot-Google', 'Google-Read-Aloud', 'Google-Appointments', 'Google-Actions', 'OneTrust', 'Chrome Privacy Preserving Prefetch Proxy', 'Mozlila/', 'HeadlessChrome', 'PhantomJS', 'Puppeteer', 'Playwright', 'Selenium', '^$']), 0), '', ['GPTBot', 'Google Cloud Vertex', 'Google AI', 'GoogleOther', 'Claude Search', 'Claude User', 'Claude', 'Claude Web', 'Anthropic', 'Perplexity User', 'Perplexity', 'Common Crawl', 'Meta AI', 'ByteDance', 'TikTok AI', 'Cohere', 'Diffbot', 'Webz.io', 'Webz.io Extended', 'Timpi', 'Amazon', 'Petal', 'Brightbot', 'OpenAI Search', 'Apple AI', 'ChatGPT', 'Meta Fetcher', 'Meta Fetcher', 'DuckDuckGo AI', 'Mistral AI', 'Manus', 'NotebookLM', 'Shap', 'PostHog Code Desktop', 'PostHog Code Mobile', 'PostHog Code Agent', 'PostHog Code Cloud', 'Applebot', 'Googlebot', 'Bingbot', 'Bingbot', 'Yandex', 'Baidu', 'DuckDuckGo', 'Yahoo', 'Naver', 'Yisou', 'Sogou', 'Pageburst', '360 Spider', 'Google Ads', 'Google Inspection', 'Google Food', 'Ahrefs Site Audit', 'Ahrefs', 'Barkrowler', 'Semrush', 'SE Ranking', 'Majestic', 'Moz', 'Lighthouse', 'Meltwater', 'Facebook Bot', 'Facebook', 'Twitter', 'LinkedIn', 'Pinterest', 'Slack', 'Slack Image Proxy', 'Telegram', 'WhatsApp', 'Google Image Proxy', 'Iframely', 'Pingdom', 'UptimeRobot', 'Site24x7', 'StatusCake', 'Datadog', 'Grafana Synthetic', 'Doctom Monitor', 'DigitalOcean Uptime', 'curl', 'Wget', 'Python Requests', 'Axios', 'Node Fetch', 'Go HTTP', 'OkHttp', 'Apache HTTP', 'LWP', 'Scrapy', 'Google Play Store', 'Amazon CloudFront', 'Amazon Product Discovery', 'Google Business Link', 'Google Storebot', 'Google Read Aloud', 'Google Appointments', 'Google Actions', 'OneTrust', 'Chrome Prefetch Proxy', 'Mozlila Typo Bot', 'Headless Chrome', 'PhantomJS', 'Puppeteer', 'Playwright', 'Selenium', ''][multiMatchAnyIndex(ifNull(properties.$user_agent, ''), ['GPTBot', 'Google-CloudVertexBot', 'Google-Extended', 'GoogleOther', 'Claude-SearchBot', 'Claude-User', 'ClaudeBot', 'Claude-Web', 'anthropic-ai', 'Perplexity-User', 'PerplexityBot', 'CCBot', 'meta-externalagent', 'Bytespider', 'TikTokSpider', 'cohere-ai', 'Diffbot', 'omgili', 'Webzio-Extended', 'Timpibot', 'Amazonbot', 'PetalBot', 'Brightbot', 'OAI-SearchBot', 'Applebot-Extended', 'ChatGPT-User', 'meta-externalfetcher', 'Meta-ExternalFetcher', 'DuckAssistBot', 'MistralAI-User', 'Manus-User', 'Google-NotebookLM', 'Shap-User', 'desktop\\.hog\\.dev', 'mobile\\.hog\\.dev', 'agent\\.hog\\.dev', 'cloud\\.hog\\.dev', 'Applebot/', 'Googlebot', 'bingbot', 'Bingbot', 'YandexBot', 'Baiduspider', 'DuckDuckBot', 'Slurp', 'Yeti/', 'YisouSpider', 'Sogou web spider', 'pageburst', '360Spider', 'AdsBot-Google', 'Google-InspectionTool', 'Google-Food', 'AhrefsSiteAudit', 'AhrefsBot', 'Barkrowler', 'SemrushBot', 'SERankingBacklinksBot', 'MJ12bot', 'DotBot', 'Lighthouse', 'MeltwaterNews', 'FacebookBot', 'facebookexternalhit', 'Twitterbot', 'LinkedInBot', 'Pinterest', 'Slackbot', 'Slack-ImgProxy', 'TelegramBot', 'WhatsApp', 'GoogleImageProxy', 'Iframely', 'Pingdom', 'UptimeRobot', 'Site24x7', 'StatusCake', 'Datadog', 'GrafanaSyntheticMonitoring', 'DMBrowser', 'DigitalOcean Uptime Probe', 'curl/', 'Wget', 'python-requests', 'axios', 'node-fetch', 'Go-http-client', 'okhttp', 'Apache-HttpClient', 'libwww-perl', 'Scrapy', 'PlayStore-Google', 'Amazon CloudFront', 'AmazonProductDiscovery', 'Google-BusinessLinkVerification', 'Storebot-Google', 'Google-Read-Aloud', 'Google-Appointments', 'Google-Actions', 'OneTrust', 'Chrome Privacy Preserving Prefetch Proxy', 'Mozlila/', 'HeadlessChrome', 'PhantomJS', 'Puppeteer', 'Playwright', 'Selenium', '^$'])]) AS bot_name,
- count() AS count
+ if(equals(multiMatchAnyIndex(ifNull(properties.$user_agent, ''), ['GPTBot', 'Google-CloudVertexBot', 'Google-Extended', 'GoogleOther', 'Claude-SearchBot', 'Claude-User', 'ClaudeBot', 'Claude-Web', 'anthropic-ai', 'Perplexity-User', 'PerplexityBot', 'CCBot', 'meta-externalagent', 'Bytespider', 'TikTokSpider', 'cohere-ai', 'Diffbot', 'omgili', 'Webzio-Extended', 'Timpibot', 'Amazonbot', 'PetalBot', 'Brightbot', 'OAI-SearchBot', 'Applebot-Extended', 'ChatGPT-User', 'meta-externalfetcher', 'Meta-ExternalFetcher', 'DuckAssistBot', 'MistralAI-User', 'Manus-User', 'Google-NotebookLM', 'Shap-User', 'desktop\\.hog\\.dev', 'mobile\\.hog\\.dev', 'agent\\.hog\\.dev', 'cloud\\.hog\\.dev', 'Applebot/', 'Googlebot', 'bingbot', 'Bingbot', 'YandexBot', 'Baiduspider', 'DuckDuckBot', 'Slurp', 'Yeti/', 'YisouSpider', 'Sogou web spider', 'pageburst', '360Spider', 'AdsBot-Google', 'Google-InspectionTool', 'Google-Food', 'AhrefsSiteAudit', 'AhrefsBot', 'Barkrowler', 'SemrushBot', 'SERankingBacklinksBot', 'MJ12bot', 'DotBot', 'Lighthouse', 'MeltwaterNews', 'FacebookBot', 'facebookexternalhit', 'Twitterbot', 'LinkedInBot', 'Pinterest', 'Slackbot', 'Slack-ImgProxy', 'TelegramBot', 'WhatsApp', 'GoogleImageProxy', 'Iframely', 'Pingdom', 'UptimeRobot', 'Site24x7', 'StatusCake', 'Datadog', 'GrafanaSyntheticMonitoring', 'DMBrowser', 'DigitalOcean Uptime Probe', 'curl/', 'Wget', 'python-requests', 'axios', 'node-fetch', 'Go-http-client', 'okhttp', 'Apache-HttpClient', 'libwww-perl', 'Scrapy', 'PlayStore-Google', 'Amazon CloudFront', 'AmazonProductDiscovery', 'Google-BusinessLinkVerification', 'Storebot-Google', 'Google-Read-Aloud', 'Google-Appointments', 'Google-Actions', 'OneTrust', 'Chrome Privacy Preserving Prefetch Proxy', 'Mozlila/', 'HeadlessChrome', 'PhantomJS', 'Puppeteer', 'Playwright', 'Selenium', '^$']), 0), '', ['GPTBot', 'Google Cloud Vertex', 'Google AI', 'GoogleOther', 'Claude Search', 'Claude User', 'Claude', 'Claude Web', 'Anthropic', 'Perplexity User', 'Perplexity', 'Common Crawl', 'Meta AI', 'ByteDance', 'TikTok AI', 'Cohere', 'Diffbot', 'Webz.io', 'Webz.io Extended', 'Timpi', 'Amazon', 'Petal', 'Brightbot', 'OpenAI Search', 'Apple AI', 'ChatGPT', 'Meta Fetcher', 'Meta Fetcher', 'DuckDuckGo AI', 'Mistral AI', 'Manus', 'NotebookLM', 'Shap', 'PostHog Code Desktop', 'PostHog Code Mobile', 'PostHog Code Agent', 'PostHog Code Cloud', 'Applebot', 'Googlebot', 'Bingbot', 'Bingbot', 'Yandex', 'Baidu', 'DuckDuckGo', 'Yahoo', 'Naver', 'Yisou', 'Sogou', 'Pageburst', '360 Spider', 'Google Ads', 'Google Inspection', 'Google Food', 'Ahrefs Site Audit', 'Ahrefs', 'Barkrowler', 'Semrush', 'SE Ranking', 'Majestic', 'Moz', 'Lighthouse', 'Meltwater', 'Facebook Bot', 'Facebook', 'Twitter', 'LinkedIn', 'Pinterest', 'Slack', 'Slack Image Proxy', 'Telegram', 'WhatsApp', 'Google Image Proxy', 'Iframely', 'Pingdom', 'UptimeRobot', 'Site24x7', 'StatusCake', 'Datadog', 'Grafana Synthetic', 'Doctom Monitor', 'DigitalOcean Uptime', 'curl', 'Wget', 'Python Requests', 'Axios', 'Node Fetch', 'Go HTTP', 'OkHttp', 'Apache HTTP', 'LWP', 'Scrapy', 'Google Play Store', 'Amazon CloudFront', 'Amazon Product Discovery', 'Google Business Link', 'Google Storebot', 'Google Read Aloud', 'Google Appointments', 'Google Actions', 'OneTrust', 'Chrome Prefetch Proxy', 'Mozlila Typo Bot', 'Headless Chrome', 'PhantomJS', 'Puppeteer', 'Playwright', 'Selenium', ''][multiMatchAnyIndex(ifNull(properties.$user_agent, ''), ['GPTBot', 'Google-CloudVertexBot', 'Google-Extended', 'GoogleOther', 'Claude-SearchBot', 'Claude-User', 'ClaudeBot', 'Claude-Web', 'anthropic-ai', 'Perplexity-User', 'PerplexityBot', 'CCBot', 'meta-externalagent', 'Bytespider', 'TikTokSpider', 'cohere-ai', 'Diffbot', 'omgili', 'Webzio-Extended', 'Timpibot', 'Amazonbot', 'PetalBot', 'Brightbot', 'OAI-SearchBot', 'Applebot-Extended', 'ChatGPT-User', 'meta-externalfetcher', 'Meta-ExternalFetcher', 'DuckAssistBot', 'MistralAI-User', 'Manus-User', 'Google-NotebookLM', 'Shap-User', 'desktop\\.hog\\.dev', 'mobile\\.hog\\.dev', 'agent\\.hog\\.dev', 'cloud\\.hog\\.dev', 'Applebot/', 'Googlebot', 'bingbot', 'Bingbot', 'YandexBot', 'Baiduspider', 'DuckDuckBot', 'Slurp', 'Yeti/', 'YisouSpider', 'Sogou web spider', 'pageburst', '360Spider', 'AdsBot-Google', 'Google-InspectionTool', 'Google-Food', 'AhrefsSiteAudit', 'AhrefsBot', 'Barkrowler', 'SemrushBot', 'SERankingBacklinksBot', 'MJ12bot', 'DotBot', 'Lighthouse', 'MeltwaterNews', 'FacebookBot', 'facebookexternalhit', 'Twitterbot', 'LinkedInBot', 'Pinterest', 'Slackbot', 'Slack-ImgProxy', 'TelegramBot', 'WhatsApp', 'GoogleImageProxy', 'Iframely', 'Pingdom', 'UptimeRobot', 'Site24x7', 'StatusCake', 'Datadog', 'GrafanaSyntheticMonitoring', 'DMBrowser', 'DigitalOcean Uptime Probe', 'curl/', 'Wget', 'python-requests', 'axios', 'node-fetch', 'Go-http-client', 'okhttp', 'Apache-HttpClient', 'libwww-perl', 'Scrapy', 'PlayStore-Google', 'Amazon CloudFront', 'AmazonProductDiscovery', 'Google-BusinessLinkVerification', 'Storebot-Google', 'Google-Read-Aloud', 'Google-Appointments', 'Google-Actions', 'OneTrust', 'Chrome Privacy Preserving Prefetch Proxy', 'Mozlila/', 'HeadlessChrome', 'PhantomJS', 'Puppeteer', 'Playwright', 'Selenium', '^$'])]) AS bot_name,
+ count() AS count
FROM
- events
+ events
WHERE
- equals(event, '$pageview')
+ equals(event, '$pageview')
GROUP BY
- bot_name
+ bot_name
ORDER BY
- bot_name ASC
+ bot_name ASC
LIMIT 100
'''
# ---
@@ -63,31 +63,31 @@
'''
-- ClickHouse
SELECT
- if(ifNull(equals(multiMatchAnyIndex(ifNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', ''), %(hogql_val_1)s), [%(hogql_val_2)s, %(hogql_val_3)s, %(hogql_val_4)s, %(hogql_val_5)s, %(hogql_val_6)s, %(hogql_val_7)s, %(hogql_val_8)s, %(hogql_val_9)s, %(hogql_val_10)s, %(hogql_val_11)s, %(hogql_val_12)s, %(hogql_val_13)s, %(hogql_val_14)s, %(hogql_val_15)s, %(hogql_val_16)s, %(hogql_val_17)s, %(hogql_val_18)s, %(hogql_val_19)s, %(hogql_val_20)s, %(hogql_val_21)s, %(hogql_val_22)s, %(hogql_val_23)s, %(hogql_val_24)s, %(hogql_val_25)s, %(hogql_val_26)s, %(hogql_val_27)s, %(hogql_val_28)s, %(hogql_val_29)s, %(hogql_val_30)s, %(hogql_val_31)s, %(hogql_val_32)s, %(hogql_val_33)s, %(hogql_val_34)s, %(hogql_val_35)s, %(hogql_val_36)s, %(hogql_val_37)s, %(hogql_val_38)s, %(hogql_val_39)s, %(hogql_val_40)s, %(hogql_val_41)s, %(hogql_val_42)s, %(hogql_val_43)s, %(hogql_val_44)s, %(hogql_val_45)s, %(hogql_val_46)s, %(hogql_val_47)s, %(hogql_val_48)s, %(hogql_val_49)s, %(hogql_val_50)s, %(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, %(hogql_val_59)s, %(hogql_val_60)s, %(hogql_val_61)s, %(hogql_val_62)s, %(hogql_val_63)s, %(hogql_val_64)s, %(hogql_val_65)s, %(hogql_val_66)s, %(hogql_val_67)s, %(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, %(hogql_val_79)s, %(hogql_val_80)s, %(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, %(hogql_val_91)s, %(hogql_val_92)s, %(hogql_val_93)s, %(hogql_val_94)s, %(hogql_val_95)s, %(hogql_val_96)s, %(hogql_val_97)s, %(hogql_val_98)s, %(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, %(hogql_val_107)s, %(hogql_val_108)s, %(hogql_val_109)s]), 0), 0), %(hogql_val_110)s, [%(hogql_val_111)s, %(hogql_val_112)s, %(hogql_val_113)s, %(hogql_val_114)s, %(hogql_val_115)s, %(hogql_val_116)s, %(hogql_val_117)s, %(hogql_val_118)s, %(hogql_val_119)s, %(hogql_val_120)s, %(hogql_val_121)s, %(hogql_val_122)s, %(hogql_val_123)s, %(hogql_val_124)s, %(hogql_val_125)s, %(hogql_val_126)s, %(hogql_val_127)s, %(hogql_val_128)s, %(hogql_val_129)s, %(hogql_val_130)s, %(hogql_val_131)s, %(hogql_val_132)s, %(hogql_val_133)s, %(hogql_val_134)s, %(hogql_val_135)s, %(hogql_val_136)s, %(hogql_val_137)s, %(hogql_val_138)s, %(hogql_val_139)s, %(hogql_val_140)s, %(hogql_val_141)s, %(hogql_val_142)s, %(hogql_val_143)s, %(hogql_val_144)s, %(hogql_val_145)s, %(hogql_val_146)s, %(hogql_val_147)s, %(hogql_val_148)s, %(hogql_val_149)s, %(hogql_val_150)s, %(hogql_val_151)s, %(hogql_val_152)s, %(hogql_val_153)s, %(hogql_val_154)s, %(hogql_val_155)s, %(hogql_val_156)s, %(hogql_val_157)s, %(hogql_val_158)s, %(hogql_val_159)s, %(hogql_val_160)s, %(hogql_val_161)s, %(hogql_val_162)s, %(hogql_val_163)s, %(hogql_val_164)s, %(hogql_val_165)s, %(hogql_val_166)s, %(hogql_val_167)s, %(hogql_val_168)s, %(hogql_val_169)s, %(hogql_val_170)s, %(hogql_val_171)s, %(hogql_val_172)s, %(hogql_val_173)s, %(hogql_val_174)s, %(hogql_val_175)s, %(hogql_val_176)s, %(hogql_val_177)s, %(hogql_val_178)s, %(hogql_val_179)s, %(hogql_val_180)s, %(hogql_val_181)s, %(hogql_val_182)s, %(hogql_val_183)s, %(hogql_val_184)s, %(hogql_val_185)s, %(hogql_val_186)s, %(hogql_val_187)s, %(hogql_val_188)s, %(hogql_val_189)s, %(hogql_val_190)s, %(hogql_val_191)s, %(hogql_val_192)s, %(hogql_val_193)s, %(hogql_val_194)s, %(hogql_val_195)s, %(hogql_val_196)s, %(hogql_val_197)s, %(hogql_val_198)s, %(hogql_val_199)s, %(hogql_val_200)s, %(hogql_val_201)s, %(hogql_val_202)s, %(hogql_val_203)s, %(hogql_val_204)s, %(hogql_val_205)s, %(hogql_val_206)s, %(hogql_val_207)s, %(hogql_val_208)s, %(hogql_val_209)s, %(hogql_val_210)s, %(hogql_val_211)s, %(hogql_val_212)s, %(hogql_val_213)s, %(hogql_val_214)s, %(hogql_val_215)s, %(hogql_val_216)s, %(hogql_val_217)s, %(hogql_val_218)s][multiMatchAnyIndex(ifNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_219)s), ''), 'null'), '^"|"$', ''), %(hogql_val_220)s), [%(hogql_val_221)s, %(hogql_val_222)s, %(hogql_val_223)s, %(hogql_val_224)s, %(hogql_val_225)s, %(hogql_val_226)s, %(hogql_val_227)s, %(hogql_val_228)s, %(hogql_val_229)s, %(hogql_val_230)s, %(hogql_val_231)s, %(hogql_val_232)s, %(hogql_val_233)s, %(hogql_val_234)s, %(hogql_val_235)s, %(hogql_val_236)s, %(hogql_val_237)s, %(hogql_val_238)s, %(hogql_val_239)s, %(hogql_val_240)s, %(hogql_val_241)s, %(hogql_val_242)s, %(hogql_val_243)s, %(hogql_val_244)s, %(hogql_val_245)s, %(hogql_val_246)s, %(hogql_val_247)s, %(hogql_val_248)s, %(hogql_val_249)s, %(hogql_val_250)s, %(hogql_val_251)s, %(hogql_val_252)s, %(hogql_val_253)s, %(hogql_val_254)s, %(hogql_val_255)s, %(hogql_val_256)s, %(hogql_val_257)s, %(hogql_val_258)s, %(hogql_val_259)s, %(hogql_val_260)s, %(hogql_val_261)s, %(hogql_val_262)s, %(hogql_val_263)s, %(hogql_val_264)s, %(hogql_val_265)s, %(hogql_val_266)s, %(hogql_val_267)s, %(hogql_val_268)s, %(hogql_val_269)s, %(hogql_val_270)s, %(hogql_val_271)s, %(hogql_val_272)s, %(hogql_val_273)s, %(hogql_val_274)s, %(hogql_val_275)s, %(hogql_val_276)s, %(hogql_val_277)s, %(hogql_val_278)s, %(hogql_val_279)s, %(hogql_val_280)s, %(hogql_val_281)s, %(hogql_val_282)s, %(hogql_val_283)s, %(hogql_val_284)s, %(hogql_val_285)s, %(hogql_val_286)s, %(hogql_val_287)s, %(hogql_val_288)s, %(hogql_val_289)s, %(hogql_val_290)s, %(hogql_val_291)s, %(hogql_val_292)s, %(hogql_val_293)s, %(hogql_val_294)s, %(hogql_val_295)s, %(hogql_val_296)s, %(hogql_val_297)s, %(hogql_val_298)s, %(hogql_val_299)s, %(hogql_val_300)s, %(hogql_val_301)s, %(hogql_val_302)s, %(hogql_val_303)s, %(hogql_val_304)s, %(hogql_val_305)s, %(hogql_val_306)s, %(hogql_val_307)s, %(hogql_val_308)s, %(hogql_val_309)s, %(hogql_val_310)s, %(hogql_val_311)s, %(hogql_val_312)s, %(hogql_val_313)s, %(hogql_val_314)s, %(hogql_val_315)s, %(hogql_val_316)s, %(hogql_val_317)s, %(hogql_val_318)s, %(hogql_val_319)s, %(hogql_val_320)s, %(hogql_val_321)s, %(hogql_val_322)s, %(hogql_val_323)s, %(hogql_val_324)s, %(hogql_val_325)s, %(hogql_val_326)s, %(hogql_val_327)s, %(hogql_val_328)s])]) AS bot_type,
- count() AS count
+ if(ifNull(equals(multiMatchAnyIndex(ifNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', ''), %(hogql_val_1)s), [%(hogql_val_2)s, %(hogql_val_3)s, %(hogql_val_4)s, %(hogql_val_5)s, %(hogql_val_6)s, %(hogql_val_7)s, %(hogql_val_8)s, %(hogql_val_9)s, %(hogql_val_10)s, %(hogql_val_11)s, %(hogql_val_12)s, %(hogql_val_13)s, %(hogql_val_14)s, %(hogql_val_15)s, %(hogql_val_16)s, %(hogql_val_17)s, %(hogql_val_18)s, %(hogql_val_19)s, %(hogql_val_20)s, %(hogql_val_21)s, %(hogql_val_22)s, %(hogql_val_23)s, %(hogql_val_24)s, %(hogql_val_25)s, %(hogql_val_26)s, %(hogql_val_27)s, %(hogql_val_28)s, %(hogql_val_29)s, %(hogql_val_30)s, %(hogql_val_31)s, %(hogql_val_32)s, %(hogql_val_33)s, %(hogql_val_34)s, %(hogql_val_35)s, %(hogql_val_36)s, %(hogql_val_37)s, %(hogql_val_38)s, %(hogql_val_39)s, %(hogql_val_40)s, %(hogql_val_41)s, %(hogql_val_42)s, %(hogql_val_43)s, %(hogql_val_44)s, %(hogql_val_45)s, %(hogql_val_46)s, %(hogql_val_47)s, %(hogql_val_48)s, %(hogql_val_49)s, %(hogql_val_50)s, %(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, %(hogql_val_59)s, %(hogql_val_60)s, %(hogql_val_61)s, %(hogql_val_62)s, %(hogql_val_63)s, %(hogql_val_64)s, %(hogql_val_65)s, %(hogql_val_66)s, %(hogql_val_67)s, %(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, %(hogql_val_79)s, %(hogql_val_80)s, %(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, %(hogql_val_91)s, %(hogql_val_92)s, %(hogql_val_93)s, %(hogql_val_94)s, %(hogql_val_95)s, %(hogql_val_96)s, %(hogql_val_97)s, %(hogql_val_98)s, %(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, %(hogql_val_107)s, %(hogql_val_108)s, %(hogql_val_109)s]), 0), 0), %(hogql_val_110)s, [%(hogql_val_111)s, %(hogql_val_112)s, %(hogql_val_113)s, %(hogql_val_114)s, %(hogql_val_115)s, %(hogql_val_116)s, %(hogql_val_117)s, %(hogql_val_118)s, %(hogql_val_119)s, %(hogql_val_120)s, %(hogql_val_121)s, %(hogql_val_122)s, %(hogql_val_123)s, %(hogql_val_124)s, %(hogql_val_125)s, %(hogql_val_126)s, %(hogql_val_127)s, %(hogql_val_128)s, %(hogql_val_129)s, %(hogql_val_130)s, %(hogql_val_131)s, %(hogql_val_132)s, %(hogql_val_133)s, %(hogql_val_134)s, %(hogql_val_135)s, %(hogql_val_136)s, %(hogql_val_137)s, %(hogql_val_138)s, %(hogql_val_139)s, %(hogql_val_140)s, %(hogql_val_141)s, %(hogql_val_142)s, %(hogql_val_143)s, %(hogql_val_144)s, %(hogql_val_145)s, %(hogql_val_146)s, %(hogql_val_147)s, %(hogql_val_148)s, %(hogql_val_149)s, %(hogql_val_150)s, %(hogql_val_151)s, %(hogql_val_152)s, %(hogql_val_153)s, %(hogql_val_154)s, %(hogql_val_155)s, %(hogql_val_156)s, %(hogql_val_157)s, %(hogql_val_158)s, %(hogql_val_159)s, %(hogql_val_160)s, %(hogql_val_161)s, %(hogql_val_162)s, %(hogql_val_163)s, %(hogql_val_164)s, %(hogql_val_165)s, %(hogql_val_166)s, %(hogql_val_167)s, %(hogql_val_168)s, %(hogql_val_169)s, %(hogql_val_170)s, %(hogql_val_171)s, %(hogql_val_172)s, %(hogql_val_173)s, %(hogql_val_174)s, %(hogql_val_175)s, %(hogql_val_176)s, %(hogql_val_177)s, %(hogql_val_178)s, %(hogql_val_179)s, %(hogql_val_180)s, %(hogql_val_181)s, %(hogql_val_182)s, %(hogql_val_183)s, %(hogql_val_184)s, %(hogql_val_185)s, %(hogql_val_186)s, %(hogql_val_187)s, %(hogql_val_188)s, %(hogql_val_189)s, %(hogql_val_190)s, %(hogql_val_191)s, %(hogql_val_192)s, %(hogql_val_193)s, %(hogql_val_194)s, %(hogql_val_195)s, %(hogql_val_196)s, %(hogql_val_197)s, %(hogql_val_198)s, %(hogql_val_199)s, %(hogql_val_200)s, %(hogql_val_201)s, %(hogql_val_202)s, %(hogql_val_203)s, %(hogql_val_204)s, %(hogql_val_205)s, %(hogql_val_206)s, %(hogql_val_207)s, %(hogql_val_208)s, %(hogql_val_209)s, %(hogql_val_210)s, %(hogql_val_211)s, %(hogql_val_212)s, %(hogql_val_213)s, %(hogql_val_214)s, %(hogql_val_215)s, %(hogql_val_216)s, %(hogql_val_217)s, %(hogql_val_218)s][multiMatchAnyIndex(ifNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_219)s), ''), 'null'), '^"|"$', ''), %(hogql_val_220)s), [%(hogql_val_221)s, %(hogql_val_222)s, %(hogql_val_223)s, %(hogql_val_224)s, %(hogql_val_225)s, %(hogql_val_226)s, %(hogql_val_227)s, %(hogql_val_228)s, %(hogql_val_229)s, %(hogql_val_230)s, %(hogql_val_231)s, %(hogql_val_232)s, %(hogql_val_233)s, %(hogql_val_234)s, %(hogql_val_235)s, %(hogql_val_236)s, %(hogql_val_237)s, %(hogql_val_238)s, %(hogql_val_239)s, %(hogql_val_240)s, %(hogql_val_241)s, %(hogql_val_242)s, %(hogql_val_243)s, %(hogql_val_244)s, %(hogql_val_245)s, %(hogql_val_246)s, %(hogql_val_247)s, %(hogql_val_248)s, %(hogql_val_249)s, %(hogql_val_250)s, %(hogql_val_251)s, %(hogql_val_252)s, %(hogql_val_253)s, %(hogql_val_254)s, %(hogql_val_255)s, %(hogql_val_256)s, %(hogql_val_257)s, %(hogql_val_258)s, %(hogql_val_259)s, %(hogql_val_260)s, %(hogql_val_261)s, %(hogql_val_262)s, %(hogql_val_263)s, %(hogql_val_264)s, %(hogql_val_265)s, %(hogql_val_266)s, %(hogql_val_267)s, %(hogql_val_268)s, %(hogql_val_269)s, %(hogql_val_270)s, %(hogql_val_271)s, %(hogql_val_272)s, %(hogql_val_273)s, %(hogql_val_274)s, %(hogql_val_275)s, %(hogql_val_276)s, %(hogql_val_277)s, %(hogql_val_278)s, %(hogql_val_279)s, %(hogql_val_280)s, %(hogql_val_281)s, %(hogql_val_282)s, %(hogql_val_283)s, %(hogql_val_284)s, %(hogql_val_285)s, %(hogql_val_286)s, %(hogql_val_287)s, %(hogql_val_288)s, %(hogql_val_289)s, %(hogql_val_290)s, %(hogql_val_291)s, %(hogql_val_292)s, %(hogql_val_293)s, %(hogql_val_294)s, %(hogql_val_295)s, %(hogql_val_296)s, %(hogql_val_297)s, %(hogql_val_298)s, %(hogql_val_299)s, %(hogql_val_300)s, %(hogql_val_301)s, %(hogql_val_302)s, %(hogql_val_303)s, %(hogql_val_304)s, %(hogql_val_305)s, %(hogql_val_306)s, %(hogql_val_307)s, %(hogql_val_308)s, %(hogql_val_309)s, %(hogql_val_310)s, %(hogql_val_311)s, %(hogql_val_312)s, %(hogql_val_313)s, %(hogql_val_314)s, %(hogql_val_315)s, %(hogql_val_316)s, %(hogql_val_317)s, %(hogql_val_318)s, %(hogql_val_319)s, %(hogql_val_320)s, %(hogql_val_321)s, %(hogql_val_322)s, %(hogql_val_323)s, %(hogql_val_324)s, %(hogql_val_325)s, %(hogql_val_326)s, %(hogql_val_327)s, %(hogql_val_328)s])]) AS bot_type,
+ count() AS count
FROM
- events
+ events
WHERE
- and(equals(events.team_id, 99999), equals(events.event, %(hogql_val_329)s))
+ and(equals(events.team_id, 99999), equals(events.event, %(hogql_val_329)s))
GROUP BY
- bot_type
+ bot_type
ORDER BY
- bot_type ASC
+ bot_type ASC
LIMIT 100
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0, transform_null_in=1, optimize_min_equality_disjunction_chain_length=4294967295, allow_experimental_join_condition=1, use_hive_partitioning=0
-- HogQL
SELECT
- if(equals(multiMatchAnyIndex(ifNull(properties.$user_agent, ''), ['GPTBot', 'Google-CloudVertexBot', 'Google-Extended', 'GoogleOther', 'Claude-SearchBot', 'Claude-User', 'ClaudeBot', 'Claude-Web', 'anthropic-ai', 'Perplexity-User', 'PerplexityBot', 'CCBot', 'meta-externalagent', 'Bytespider', 'TikTokSpider', 'cohere-ai', 'Diffbot', 'omgili', 'Webzio-Extended', 'Timpibot', 'Amazonbot', 'PetalBot', 'Brightbot', 'OAI-SearchBot', 'Applebot-Extended', 'ChatGPT-User', 'meta-externalfetcher', 'Meta-ExternalFetcher', 'DuckAssistBot', 'MistralAI-User', 'Manus-User', 'Google-NotebookLM', 'Shap-User', 'desktop\\.hog\\.dev', 'mobile\\.hog\\.dev', 'agent\\.hog\\.dev', 'cloud\\.hog\\.dev', 'Applebot/', 'Googlebot', 'bingbot', 'Bingbot', 'YandexBot', 'Baiduspider', 'DuckDuckBot', 'Slurp', 'Yeti/', 'YisouSpider', 'Sogou web spider', 'pageburst', '360Spider', 'AdsBot-Google', 'Google-InspectionTool', 'Google-Food', 'AhrefsSiteAudit', 'AhrefsBot', 'Barkrowler', 'SemrushBot', 'SERankingBacklinksBot', 'MJ12bot', 'DotBot', 'Lighthouse', 'MeltwaterNews', 'FacebookBot', 'facebookexternalhit', 'Twitterbot', 'LinkedInBot', 'Pinterest', 'Slackbot', 'Slack-ImgProxy', 'TelegramBot', 'WhatsApp', 'GoogleImageProxy', 'Iframely', 'Pingdom', 'UptimeRobot', 'Site24x7', 'StatusCake', 'Datadog', 'GrafanaSyntheticMonitoring', 'DMBrowser', 'DigitalOcean Uptime Probe', 'curl/', 'Wget', 'python-requests', 'axios', 'node-fetch', 'Go-http-client', 'okhttp', 'Apache-HttpClient', 'libwww-perl', 'Scrapy', 'PlayStore-Google', 'Amazon CloudFront', 'AmazonProductDiscovery', 'Google-BusinessLinkVerification', 'Storebot-Google', 'Google-Read-Aloud', 'Google-Appointments', 'Google-Actions', 'OneTrust', 'Chrome Privacy Preserving Prefetch Proxy', 'Mozlila/', 'HeadlessChrome', 'PhantomJS', 'Puppeteer', 'Playwright', 'Selenium', '^$']), 0), '', ['ai_crawler', 'ai_crawler', 'ai_crawler', 'ai_crawler', 'ai_search', 'ai_assistant', 'ai_crawler', 'ai_crawler', 'ai_crawler', 'ai_assistant', 'ai_search', 'ai_crawler', 'ai_crawler', 'ai_crawler', 'ai_crawler', 'ai_crawler', 'ai_crawler', 'ai_crawler', 'ai_crawler', 'ai_crawler', 'ai_crawler', 'ai_crawler', 'ai_crawler', 'ai_search', 'ai_search', 'ai_assistant', 'ai_assistant', 'ai_assistant', 'ai_assistant', 'ai_assistant', 'ai_assistant', 'ai_assistant', 'ai_assistant', 'ai_assistant', 'ai_assistant', 'ai_assistant', 'ai_assistant', 'ai_search', 'search_crawler', 'search_crawler', 'search_crawler', 'search_crawler', 'search_crawler', 'search_crawler', 'search_crawler', 'search_crawler', 'search_crawler', 'search_crawler', 'search_crawler', 'search_crawler', 'search_crawler', 'search_crawler', 'search_crawler', 'seo_crawler', 'seo_crawler', 'seo_crawler', 'seo_crawler', 'seo_crawler', 'seo_crawler', 'seo_crawler', 'seo_crawler', 'seo_crawler', 'social_crawler', 'social_crawler', 'social_crawler', 'social_crawler', 'social_crawler', 'social_crawler', 'social_crawler', 'social_crawler', 'social_crawler', 'social_crawler', 'social_crawler', 'monitoring', 'monitoring', 'monitoring', 'monitoring', 'monitoring', 'monitoring', 'monitoring', 'monitoring', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'headless_browser', 'headless_browser', 'headless_browser', 'headless_browser', 'headless_browser', 'headless_browser', 'no_user_agent'][multiMatchAnyIndex(ifNull(properties.$user_agent, ''), ['GPTBot', 'Google-CloudVertexBot', 'Google-Extended', 'GoogleOther', 'Claude-SearchBot', 'Claude-User', 'ClaudeBot', 'Claude-Web', 'anthropic-ai', 'Perplexity-User', 'PerplexityBot', 'CCBot', 'meta-externalagent', 'Bytespider', 'TikTokSpider', 'cohere-ai', 'Diffbot', 'omgili', 'Webzio-Extended', 'Timpibot', 'Amazonbot', 'PetalBot', 'Brightbot', 'OAI-SearchBot', 'Applebot-Extended', 'ChatGPT-User', 'meta-externalfetcher', 'Meta-ExternalFetcher', 'DuckAssistBot', 'MistralAI-User', 'Manus-User', 'Google-NotebookLM', 'Shap-User', 'desktop\\.hog\\.dev', 'mobile\\.hog\\.dev', 'agent\\.hog\\.dev', 'cloud\\.hog\\.dev', 'Applebot/', 'Googlebot', 'bingbot', 'Bingbot', 'YandexBot', 'Baiduspider', 'DuckDuckBot', 'Slurp', 'Yeti/', 'YisouSpider', 'Sogou web spider', 'pageburst', '360Spider', 'AdsBot-Google', 'Google-InspectionTool', 'Google-Food', 'AhrefsSiteAudit', 'AhrefsBot', 'Barkrowler', 'SemrushBot', 'SERankingBacklinksBot', 'MJ12bot', 'DotBot', 'Lighthouse', 'MeltwaterNews', 'FacebookBot', 'facebookexternalhit', 'Twitterbot', 'LinkedInBot', 'Pinterest', 'Slackbot', 'Slack-ImgProxy', 'TelegramBot', 'WhatsApp', 'GoogleImageProxy', 'Iframely', 'Pingdom', 'UptimeRobot', 'Site24x7', 'StatusCake', 'Datadog', 'GrafanaSyntheticMonitoring', 'DMBrowser', 'DigitalOcean Uptime Probe', 'curl/', 'Wget', 'python-requests', 'axios', 'node-fetch', 'Go-http-client', 'okhttp', 'Apache-HttpClient', 'libwww-perl', 'Scrapy', 'PlayStore-Google', 'Amazon CloudFront', 'AmazonProductDiscovery', 'Google-BusinessLinkVerification', 'Storebot-Google', 'Google-Read-Aloud', 'Google-Appointments', 'Google-Actions', 'OneTrust', 'Chrome Privacy Preserving Prefetch Proxy', 'Mozlila/', 'HeadlessChrome', 'PhantomJS', 'Puppeteer', 'Playwright', 'Selenium', '^$'])]) AS bot_type,
- count() AS count
+ if(equals(multiMatchAnyIndex(ifNull(properties.$user_agent, ''), ['GPTBot', 'Google-CloudVertexBot', 'Google-Extended', 'GoogleOther', 'Claude-SearchBot', 'Claude-User', 'ClaudeBot', 'Claude-Web', 'anthropic-ai', 'Perplexity-User', 'PerplexityBot', 'CCBot', 'meta-externalagent', 'Bytespider', 'TikTokSpider', 'cohere-ai', 'Diffbot', 'omgili', 'Webzio-Extended', 'Timpibot', 'Amazonbot', 'PetalBot', 'Brightbot', 'OAI-SearchBot', 'Applebot-Extended', 'ChatGPT-User', 'meta-externalfetcher', 'Meta-ExternalFetcher', 'DuckAssistBot', 'MistralAI-User', 'Manus-User', 'Google-NotebookLM', 'Shap-User', 'desktop\\.hog\\.dev', 'mobile\\.hog\\.dev', 'agent\\.hog\\.dev', 'cloud\\.hog\\.dev', 'Applebot/', 'Googlebot', 'bingbot', 'Bingbot', 'YandexBot', 'Baiduspider', 'DuckDuckBot', 'Slurp', 'Yeti/', 'YisouSpider', 'Sogou web spider', 'pageburst', '360Spider', 'AdsBot-Google', 'Google-InspectionTool', 'Google-Food', 'AhrefsSiteAudit', 'AhrefsBot', 'Barkrowler', 'SemrushBot', 'SERankingBacklinksBot', 'MJ12bot', 'DotBot', 'Lighthouse', 'MeltwaterNews', 'FacebookBot', 'facebookexternalhit', 'Twitterbot', 'LinkedInBot', 'Pinterest', 'Slackbot', 'Slack-ImgProxy', 'TelegramBot', 'WhatsApp', 'GoogleImageProxy', 'Iframely', 'Pingdom', 'UptimeRobot', 'Site24x7', 'StatusCake', 'Datadog', 'GrafanaSyntheticMonitoring', 'DMBrowser', 'DigitalOcean Uptime Probe', 'curl/', 'Wget', 'python-requests', 'axios', 'node-fetch', 'Go-http-client', 'okhttp', 'Apache-HttpClient', 'libwww-perl', 'Scrapy', 'PlayStore-Google', 'Amazon CloudFront', 'AmazonProductDiscovery', 'Google-BusinessLinkVerification', 'Storebot-Google', 'Google-Read-Aloud', 'Google-Appointments', 'Google-Actions', 'OneTrust', 'Chrome Privacy Preserving Prefetch Proxy', 'Mozlila/', 'HeadlessChrome', 'PhantomJS', 'Puppeteer', 'Playwright', 'Selenium', '^$']), 0), '', ['ai_crawler', 'ai_crawler', 'ai_crawler', 'ai_crawler', 'ai_search', 'ai_assistant', 'ai_crawler', 'ai_crawler', 'ai_crawler', 'ai_assistant', 'ai_search', 'ai_crawler', 'ai_crawler', 'ai_crawler', 'ai_crawler', 'ai_crawler', 'ai_crawler', 'ai_crawler', 'ai_crawler', 'ai_crawler', 'ai_crawler', 'ai_crawler', 'ai_crawler', 'ai_search', 'ai_search', 'ai_assistant', 'ai_assistant', 'ai_assistant', 'ai_assistant', 'ai_assistant', 'ai_assistant', 'ai_assistant', 'ai_assistant', 'ai_assistant', 'ai_assistant', 'ai_assistant', 'ai_assistant', 'ai_search', 'search_crawler', 'search_crawler', 'search_crawler', 'search_crawler', 'search_crawler', 'search_crawler', 'search_crawler', 'search_crawler', 'search_crawler', 'search_crawler', 'search_crawler', 'search_crawler', 'search_crawler', 'search_crawler', 'search_crawler', 'seo_crawler', 'seo_crawler', 'seo_crawler', 'seo_crawler', 'seo_crawler', 'seo_crawler', 'seo_crawler', 'seo_crawler', 'seo_crawler', 'social_crawler', 'social_crawler', 'social_crawler', 'social_crawler', 'social_crawler', 'social_crawler', 'social_crawler', 'social_crawler', 'social_crawler', 'social_crawler', 'social_crawler', 'monitoring', 'monitoring', 'monitoring', 'monitoring', 'monitoring', 'monitoring', 'monitoring', 'monitoring', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'headless_browser', 'headless_browser', 'headless_browser', 'headless_browser', 'headless_browser', 'headless_browser', 'no_user_agent'][multiMatchAnyIndex(ifNull(properties.$user_agent, ''), ['GPTBot', 'Google-CloudVertexBot', 'Google-Extended', 'GoogleOther', 'Claude-SearchBot', 'Claude-User', 'ClaudeBot', 'Claude-Web', 'anthropic-ai', 'Perplexity-User', 'PerplexityBot', 'CCBot', 'meta-externalagent', 'Bytespider', 'TikTokSpider', 'cohere-ai', 'Diffbot', 'omgili', 'Webzio-Extended', 'Timpibot', 'Amazonbot', 'PetalBot', 'Brightbot', 'OAI-SearchBot', 'Applebot-Extended', 'ChatGPT-User', 'meta-externalfetcher', 'Meta-ExternalFetcher', 'DuckAssistBot', 'MistralAI-User', 'Manus-User', 'Google-NotebookLM', 'Shap-User', 'desktop\\.hog\\.dev', 'mobile\\.hog\\.dev', 'agent\\.hog\\.dev', 'cloud\\.hog\\.dev', 'Applebot/', 'Googlebot', 'bingbot', 'Bingbot', 'YandexBot', 'Baiduspider', 'DuckDuckBot', 'Slurp', 'Yeti/', 'YisouSpider', 'Sogou web spider', 'pageburst', '360Spider', 'AdsBot-Google', 'Google-InspectionTool', 'Google-Food', 'AhrefsSiteAudit', 'AhrefsBot', 'Barkrowler', 'SemrushBot', 'SERankingBacklinksBot', 'MJ12bot', 'DotBot', 'Lighthouse', 'MeltwaterNews', 'FacebookBot', 'facebookexternalhit', 'Twitterbot', 'LinkedInBot', 'Pinterest', 'Slackbot', 'Slack-ImgProxy', 'TelegramBot', 'WhatsApp', 'GoogleImageProxy', 'Iframely', 'Pingdom', 'UptimeRobot', 'Site24x7', 'StatusCake', 'Datadog', 'GrafanaSyntheticMonitoring', 'DMBrowser', 'DigitalOcean Uptime Probe', 'curl/', 'Wget', 'python-requests', 'axios', 'node-fetch', 'Go-http-client', 'okhttp', 'Apache-HttpClient', 'libwww-perl', 'Scrapy', 'PlayStore-Google', 'Amazon CloudFront', 'AmazonProductDiscovery', 'Google-BusinessLinkVerification', 'Storebot-Google', 'Google-Read-Aloud', 'Google-Appointments', 'Google-Actions', 'OneTrust', 'Chrome Privacy Preserving Prefetch Proxy', 'Mozlila/', 'HeadlessChrome', 'PhantomJS', 'Puppeteer', 'Playwright', 'Selenium', '^$'])]) AS bot_type,
+ count() AS count
FROM
- events
+ events
WHERE
- equals(event, '$pageview')
+ equals(event, '$pageview')
GROUP BY
- bot_type
+ bot_type
ORDER BY
- bot_type ASC
+ bot_type ASC
LIMIT 100
'''
# ---
@@ -95,31 +95,31 @@
'''
-- ClickHouse
SELECT
- if(ifNull(equals(multiMatchAnyIndex(ifNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', ''), %(hogql_val_1)s), [%(hogql_val_2)s, %(hogql_val_3)s, %(hogql_val_4)s, %(hogql_val_5)s, %(hogql_val_6)s, %(hogql_val_7)s, %(hogql_val_8)s, %(hogql_val_9)s, %(hogql_val_10)s, %(hogql_val_11)s, %(hogql_val_12)s, %(hogql_val_13)s, %(hogql_val_14)s, %(hogql_val_15)s, %(hogql_val_16)s, %(hogql_val_17)s, %(hogql_val_18)s, %(hogql_val_19)s, %(hogql_val_20)s, %(hogql_val_21)s, %(hogql_val_22)s, %(hogql_val_23)s, %(hogql_val_24)s, %(hogql_val_25)s, %(hogql_val_26)s, %(hogql_val_27)s, %(hogql_val_28)s, %(hogql_val_29)s, %(hogql_val_30)s, %(hogql_val_31)s, %(hogql_val_32)s, %(hogql_val_33)s, %(hogql_val_34)s, %(hogql_val_35)s, %(hogql_val_36)s, %(hogql_val_37)s, %(hogql_val_38)s, %(hogql_val_39)s, %(hogql_val_40)s, %(hogql_val_41)s, %(hogql_val_42)s, %(hogql_val_43)s, %(hogql_val_44)s, %(hogql_val_45)s, %(hogql_val_46)s, %(hogql_val_47)s, %(hogql_val_48)s, %(hogql_val_49)s, %(hogql_val_50)s, %(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, %(hogql_val_59)s, %(hogql_val_60)s, %(hogql_val_61)s, %(hogql_val_62)s, %(hogql_val_63)s, %(hogql_val_64)s, %(hogql_val_65)s, %(hogql_val_66)s, %(hogql_val_67)s, %(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, %(hogql_val_79)s, %(hogql_val_80)s, %(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, %(hogql_val_91)s, %(hogql_val_92)s, %(hogql_val_93)s, %(hogql_val_94)s, %(hogql_val_95)s, %(hogql_val_96)s, %(hogql_val_97)s, %(hogql_val_98)s, %(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, %(hogql_val_107)s, %(hogql_val_108)s, %(hogql_val_109)s]), 0), 0), %(hogql_val_110)s, [%(hogql_val_111)s, %(hogql_val_112)s, %(hogql_val_113)s, %(hogql_val_114)s, %(hogql_val_115)s, %(hogql_val_116)s, %(hogql_val_117)s, %(hogql_val_118)s, %(hogql_val_119)s, %(hogql_val_120)s, %(hogql_val_121)s, %(hogql_val_122)s, %(hogql_val_123)s, %(hogql_val_124)s, %(hogql_val_125)s, %(hogql_val_126)s, %(hogql_val_127)s, %(hogql_val_128)s, %(hogql_val_129)s, %(hogql_val_130)s, %(hogql_val_131)s, %(hogql_val_132)s, %(hogql_val_133)s, %(hogql_val_134)s, %(hogql_val_135)s, %(hogql_val_136)s, %(hogql_val_137)s, %(hogql_val_138)s, %(hogql_val_139)s, %(hogql_val_140)s, %(hogql_val_141)s, %(hogql_val_142)s, %(hogql_val_143)s, %(hogql_val_144)s, %(hogql_val_145)s, %(hogql_val_146)s, %(hogql_val_147)s, %(hogql_val_148)s, %(hogql_val_149)s, %(hogql_val_150)s, %(hogql_val_151)s, %(hogql_val_152)s, %(hogql_val_153)s, %(hogql_val_154)s, %(hogql_val_155)s, %(hogql_val_156)s, %(hogql_val_157)s, %(hogql_val_158)s, %(hogql_val_159)s, %(hogql_val_160)s, %(hogql_val_161)s, %(hogql_val_162)s, %(hogql_val_163)s, %(hogql_val_164)s, %(hogql_val_165)s, %(hogql_val_166)s, %(hogql_val_167)s, %(hogql_val_168)s, %(hogql_val_169)s, %(hogql_val_170)s, %(hogql_val_171)s, %(hogql_val_172)s, %(hogql_val_173)s, %(hogql_val_174)s, %(hogql_val_175)s, %(hogql_val_176)s, %(hogql_val_177)s, %(hogql_val_178)s, %(hogql_val_179)s, %(hogql_val_180)s, %(hogql_val_181)s, %(hogql_val_182)s, %(hogql_val_183)s, %(hogql_val_184)s, %(hogql_val_185)s, %(hogql_val_186)s, %(hogql_val_187)s, %(hogql_val_188)s, %(hogql_val_189)s, %(hogql_val_190)s, %(hogql_val_191)s, %(hogql_val_192)s, %(hogql_val_193)s, %(hogql_val_194)s, %(hogql_val_195)s, %(hogql_val_196)s, %(hogql_val_197)s, %(hogql_val_198)s, %(hogql_val_199)s, %(hogql_val_200)s, %(hogql_val_201)s, %(hogql_val_202)s, %(hogql_val_203)s, %(hogql_val_204)s, %(hogql_val_205)s, %(hogql_val_206)s, %(hogql_val_207)s, %(hogql_val_208)s, %(hogql_val_209)s, %(hogql_val_210)s, %(hogql_val_211)s, %(hogql_val_212)s, %(hogql_val_213)s, %(hogql_val_214)s, %(hogql_val_215)s, %(hogql_val_216)s, %(hogql_val_217)s, %(hogql_val_218)s][multiMatchAnyIndex(ifNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_219)s), ''), 'null'), '^"|"$', ''), %(hogql_val_220)s), [%(hogql_val_221)s, %(hogql_val_222)s, %(hogql_val_223)s, %(hogql_val_224)s, %(hogql_val_225)s, %(hogql_val_226)s, %(hogql_val_227)s, %(hogql_val_228)s, %(hogql_val_229)s, %(hogql_val_230)s, %(hogql_val_231)s, %(hogql_val_232)s, %(hogql_val_233)s, %(hogql_val_234)s, %(hogql_val_235)s, %(hogql_val_236)s, %(hogql_val_237)s, %(hogql_val_238)s, %(hogql_val_239)s, %(hogql_val_240)s, %(hogql_val_241)s, %(hogql_val_242)s, %(hogql_val_243)s, %(hogql_val_244)s, %(hogql_val_245)s, %(hogql_val_246)s, %(hogql_val_247)s, %(hogql_val_248)s, %(hogql_val_249)s, %(hogql_val_250)s, %(hogql_val_251)s, %(hogql_val_252)s, %(hogql_val_253)s, %(hogql_val_254)s, %(hogql_val_255)s, %(hogql_val_256)s, %(hogql_val_257)s, %(hogql_val_258)s, %(hogql_val_259)s, %(hogql_val_260)s, %(hogql_val_261)s, %(hogql_val_262)s, %(hogql_val_263)s, %(hogql_val_264)s, %(hogql_val_265)s, %(hogql_val_266)s, %(hogql_val_267)s, %(hogql_val_268)s, %(hogql_val_269)s, %(hogql_val_270)s, %(hogql_val_271)s, %(hogql_val_272)s, %(hogql_val_273)s, %(hogql_val_274)s, %(hogql_val_275)s, %(hogql_val_276)s, %(hogql_val_277)s, %(hogql_val_278)s, %(hogql_val_279)s, %(hogql_val_280)s, %(hogql_val_281)s, %(hogql_val_282)s, %(hogql_val_283)s, %(hogql_val_284)s, %(hogql_val_285)s, %(hogql_val_286)s, %(hogql_val_287)s, %(hogql_val_288)s, %(hogql_val_289)s, %(hogql_val_290)s, %(hogql_val_291)s, %(hogql_val_292)s, %(hogql_val_293)s, %(hogql_val_294)s, %(hogql_val_295)s, %(hogql_val_296)s, %(hogql_val_297)s, %(hogql_val_298)s, %(hogql_val_299)s, %(hogql_val_300)s, %(hogql_val_301)s, %(hogql_val_302)s, %(hogql_val_303)s, %(hogql_val_304)s, %(hogql_val_305)s, %(hogql_val_306)s, %(hogql_val_307)s, %(hogql_val_308)s, %(hogql_val_309)s, %(hogql_val_310)s, %(hogql_val_311)s, %(hogql_val_312)s, %(hogql_val_313)s, %(hogql_val_314)s, %(hogql_val_315)s, %(hogql_val_316)s, %(hogql_val_317)s, %(hogql_val_318)s, %(hogql_val_319)s, %(hogql_val_320)s, %(hogql_val_321)s, %(hogql_val_322)s, %(hogql_val_323)s, %(hogql_val_324)s, %(hogql_val_325)s, %(hogql_val_326)s, %(hogql_val_327)s, %(hogql_val_328)s])]) AS category,
- count() AS count
+ if(ifNull(equals(multiMatchAnyIndex(ifNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', ''), %(hogql_val_1)s), [%(hogql_val_2)s, %(hogql_val_3)s, %(hogql_val_4)s, %(hogql_val_5)s, %(hogql_val_6)s, %(hogql_val_7)s, %(hogql_val_8)s, %(hogql_val_9)s, %(hogql_val_10)s, %(hogql_val_11)s, %(hogql_val_12)s, %(hogql_val_13)s, %(hogql_val_14)s, %(hogql_val_15)s, %(hogql_val_16)s, %(hogql_val_17)s, %(hogql_val_18)s, %(hogql_val_19)s, %(hogql_val_20)s, %(hogql_val_21)s, %(hogql_val_22)s, %(hogql_val_23)s, %(hogql_val_24)s, %(hogql_val_25)s, %(hogql_val_26)s, %(hogql_val_27)s, %(hogql_val_28)s, %(hogql_val_29)s, %(hogql_val_30)s, %(hogql_val_31)s, %(hogql_val_32)s, %(hogql_val_33)s, %(hogql_val_34)s, %(hogql_val_35)s, %(hogql_val_36)s, %(hogql_val_37)s, %(hogql_val_38)s, %(hogql_val_39)s, %(hogql_val_40)s, %(hogql_val_41)s, %(hogql_val_42)s, %(hogql_val_43)s, %(hogql_val_44)s, %(hogql_val_45)s, %(hogql_val_46)s, %(hogql_val_47)s, %(hogql_val_48)s, %(hogql_val_49)s, %(hogql_val_50)s, %(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, %(hogql_val_59)s, %(hogql_val_60)s, %(hogql_val_61)s, %(hogql_val_62)s, %(hogql_val_63)s, %(hogql_val_64)s, %(hogql_val_65)s, %(hogql_val_66)s, %(hogql_val_67)s, %(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, %(hogql_val_79)s, %(hogql_val_80)s, %(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, %(hogql_val_91)s, %(hogql_val_92)s, %(hogql_val_93)s, %(hogql_val_94)s, %(hogql_val_95)s, %(hogql_val_96)s, %(hogql_val_97)s, %(hogql_val_98)s, %(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, %(hogql_val_107)s, %(hogql_val_108)s, %(hogql_val_109)s]), 0), 0), %(hogql_val_110)s, [%(hogql_val_111)s, %(hogql_val_112)s, %(hogql_val_113)s, %(hogql_val_114)s, %(hogql_val_115)s, %(hogql_val_116)s, %(hogql_val_117)s, %(hogql_val_118)s, %(hogql_val_119)s, %(hogql_val_120)s, %(hogql_val_121)s, %(hogql_val_122)s, %(hogql_val_123)s, %(hogql_val_124)s, %(hogql_val_125)s, %(hogql_val_126)s, %(hogql_val_127)s, %(hogql_val_128)s, %(hogql_val_129)s, %(hogql_val_130)s, %(hogql_val_131)s, %(hogql_val_132)s, %(hogql_val_133)s, %(hogql_val_134)s, %(hogql_val_135)s, %(hogql_val_136)s, %(hogql_val_137)s, %(hogql_val_138)s, %(hogql_val_139)s, %(hogql_val_140)s, %(hogql_val_141)s, %(hogql_val_142)s, %(hogql_val_143)s, %(hogql_val_144)s, %(hogql_val_145)s, %(hogql_val_146)s, %(hogql_val_147)s, %(hogql_val_148)s, %(hogql_val_149)s, %(hogql_val_150)s, %(hogql_val_151)s, %(hogql_val_152)s, %(hogql_val_153)s, %(hogql_val_154)s, %(hogql_val_155)s, %(hogql_val_156)s, %(hogql_val_157)s, %(hogql_val_158)s, %(hogql_val_159)s, %(hogql_val_160)s, %(hogql_val_161)s, %(hogql_val_162)s, %(hogql_val_163)s, %(hogql_val_164)s, %(hogql_val_165)s, %(hogql_val_166)s, %(hogql_val_167)s, %(hogql_val_168)s, %(hogql_val_169)s, %(hogql_val_170)s, %(hogql_val_171)s, %(hogql_val_172)s, %(hogql_val_173)s, %(hogql_val_174)s, %(hogql_val_175)s, %(hogql_val_176)s, %(hogql_val_177)s, %(hogql_val_178)s, %(hogql_val_179)s, %(hogql_val_180)s, %(hogql_val_181)s, %(hogql_val_182)s, %(hogql_val_183)s, %(hogql_val_184)s, %(hogql_val_185)s, %(hogql_val_186)s, %(hogql_val_187)s, %(hogql_val_188)s, %(hogql_val_189)s, %(hogql_val_190)s, %(hogql_val_191)s, %(hogql_val_192)s, %(hogql_val_193)s, %(hogql_val_194)s, %(hogql_val_195)s, %(hogql_val_196)s, %(hogql_val_197)s, %(hogql_val_198)s, %(hogql_val_199)s, %(hogql_val_200)s, %(hogql_val_201)s, %(hogql_val_202)s, %(hogql_val_203)s, %(hogql_val_204)s, %(hogql_val_205)s, %(hogql_val_206)s, %(hogql_val_207)s, %(hogql_val_208)s, %(hogql_val_209)s, %(hogql_val_210)s, %(hogql_val_211)s, %(hogql_val_212)s, %(hogql_val_213)s, %(hogql_val_214)s, %(hogql_val_215)s, %(hogql_val_216)s, %(hogql_val_217)s, %(hogql_val_218)s][multiMatchAnyIndex(ifNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_219)s), ''), 'null'), '^"|"$', ''), %(hogql_val_220)s), [%(hogql_val_221)s, %(hogql_val_222)s, %(hogql_val_223)s, %(hogql_val_224)s, %(hogql_val_225)s, %(hogql_val_226)s, %(hogql_val_227)s, %(hogql_val_228)s, %(hogql_val_229)s, %(hogql_val_230)s, %(hogql_val_231)s, %(hogql_val_232)s, %(hogql_val_233)s, %(hogql_val_234)s, %(hogql_val_235)s, %(hogql_val_236)s, %(hogql_val_237)s, %(hogql_val_238)s, %(hogql_val_239)s, %(hogql_val_240)s, %(hogql_val_241)s, %(hogql_val_242)s, %(hogql_val_243)s, %(hogql_val_244)s, %(hogql_val_245)s, %(hogql_val_246)s, %(hogql_val_247)s, %(hogql_val_248)s, %(hogql_val_249)s, %(hogql_val_250)s, %(hogql_val_251)s, %(hogql_val_252)s, %(hogql_val_253)s, %(hogql_val_254)s, %(hogql_val_255)s, %(hogql_val_256)s, %(hogql_val_257)s, %(hogql_val_258)s, %(hogql_val_259)s, %(hogql_val_260)s, %(hogql_val_261)s, %(hogql_val_262)s, %(hogql_val_263)s, %(hogql_val_264)s, %(hogql_val_265)s, %(hogql_val_266)s, %(hogql_val_267)s, %(hogql_val_268)s, %(hogql_val_269)s, %(hogql_val_270)s, %(hogql_val_271)s, %(hogql_val_272)s, %(hogql_val_273)s, %(hogql_val_274)s, %(hogql_val_275)s, %(hogql_val_276)s, %(hogql_val_277)s, %(hogql_val_278)s, %(hogql_val_279)s, %(hogql_val_280)s, %(hogql_val_281)s, %(hogql_val_282)s, %(hogql_val_283)s, %(hogql_val_284)s, %(hogql_val_285)s, %(hogql_val_286)s, %(hogql_val_287)s, %(hogql_val_288)s, %(hogql_val_289)s, %(hogql_val_290)s, %(hogql_val_291)s, %(hogql_val_292)s, %(hogql_val_293)s, %(hogql_val_294)s, %(hogql_val_295)s, %(hogql_val_296)s, %(hogql_val_297)s, %(hogql_val_298)s, %(hogql_val_299)s, %(hogql_val_300)s, %(hogql_val_301)s, %(hogql_val_302)s, %(hogql_val_303)s, %(hogql_val_304)s, %(hogql_val_305)s, %(hogql_val_306)s, %(hogql_val_307)s, %(hogql_val_308)s, %(hogql_val_309)s, %(hogql_val_310)s, %(hogql_val_311)s, %(hogql_val_312)s, %(hogql_val_313)s, %(hogql_val_314)s, %(hogql_val_315)s, %(hogql_val_316)s, %(hogql_val_317)s, %(hogql_val_318)s, %(hogql_val_319)s, %(hogql_val_320)s, %(hogql_val_321)s, %(hogql_val_322)s, %(hogql_val_323)s, %(hogql_val_324)s, %(hogql_val_325)s, %(hogql_val_326)s, %(hogql_val_327)s, %(hogql_val_328)s])]) AS category,
+ count() AS count
FROM
- events
+ events
WHERE
- and(equals(events.team_id, 99999), equals(events.event, %(hogql_val_329)s))
+ and(equals(events.team_id, 99999), equals(events.event, %(hogql_val_329)s))
GROUP BY
- category
+ category
ORDER BY
- category ASC
+ category ASC
LIMIT 100
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0, transform_null_in=1, optimize_min_equality_disjunction_chain_length=4294967295, allow_experimental_join_condition=1, use_hive_partitioning=0
-- HogQL
SELECT
- if(equals(multiMatchAnyIndex(ifNull(properties.$user_agent, ''), ['GPTBot', 'Google-CloudVertexBot', 'Google-Extended', 'GoogleOther', 'Claude-SearchBot', 'Claude-User', 'ClaudeBot', 'Claude-Web', 'anthropic-ai', 'Perplexity-User', 'PerplexityBot', 'CCBot', 'meta-externalagent', 'Bytespider', 'TikTokSpider', 'cohere-ai', 'Diffbot', 'omgili', 'Webzio-Extended', 'Timpibot', 'Amazonbot', 'PetalBot', 'Brightbot', 'OAI-SearchBot', 'Applebot-Extended', 'ChatGPT-User', 'meta-externalfetcher', 'Meta-ExternalFetcher', 'DuckAssistBot', 'MistralAI-User', 'Manus-User', 'Google-NotebookLM', 'Shap-User', 'desktop\\.hog\\.dev', 'mobile\\.hog\\.dev', 'agent\\.hog\\.dev', 'cloud\\.hog\\.dev', 'Applebot/', 'Googlebot', 'bingbot', 'Bingbot', 'YandexBot', 'Baiduspider', 'DuckDuckBot', 'Slurp', 'Yeti/', 'YisouSpider', 'Sogou web spider', 'pageburst', '360Spider', 'AdsBot-Google', 'Google-InspectionTool', 'Google-Food', 'AhrefsSiteAudit', 'AhrefsBot', 'Barkrowler', 'SemrushBot', 'SERankingBacklinksBot', 'MJ12bot', 'DotBot', 'Lighthouse', 'MeltwaterNews', 'FacebookBot', 'facebookexternalhit', 'Twitterbot', 'LinkedInBot', 'Pinterest', 'Slackbot', 'Slack-ImgProxy', 'TelegramBot', 'WhatsApp', 'GoogleImageProxy', 'Iframely', 'Pingdom', 'UptimeRobot', 'Site24x7', 'StatusCake', 'Datadog', 'GrafanaSyntheticMonitoring', 'DMBrowser', 'DigitalOcean Uptime Probe', 'curl/', 'Wget', 'python-requests', 'axios', 'node-fetch', 'Go-http-client', 'okhttp', 'Apache-HttpClient', 'libwww-perl', 'Scrapy', 'PlayStore-Google', 'Amazon CloudFront', 'AmazonProductDiscovery', 'Google-BusinessLinkVerification', 'Storebot-Google', 'Google-Read-Aloud', 'Google-Appointments', 'Google-Actions', 'OneTrust', 'Chrome Privacy Preserving Prefetch Proxy', 'Mozlila/', 'HeadlessChrome', 'PhantomJS', 'Puppeteer', 'Playwright', 'Selenium', '^$']), 0), 'regular', ['ai_crawler', 'ai_crawler', 'ai_crawler', 'ai_crawler', 'ai_search', 'ai_assistant', 'ai_crawler', 'ai_crawler', 'ai_crawler', 'ai_assistant', 'ai_search', 'ai_crawler', 'ai_crawler', 'ai_crawler', 'ai_crawler', 'ai_crawler', 'ai_crawler', 'ai_crawler', 'ai_crawler', 'ai_crawler', 'ai_crawler', 'ai_crawler', 'ai_crawler', 'ai_search', 'ai_search', 'ai_assistant', 'ai_assistant', 'ai_assistant', 'ai_assistant', 'ai_assistant', 'ai_assistant', 'ai_assistant', 'ai_assistant', 'ai_assistant', 'ai_assistant', 'ai_assistant', 'ai_assistant', 'ai_search', 'search_crawler', 'search_crawler', 'search_crawler', 'search_crawler', 'search_crawler', 'search_crawler', 'search_crawler', 'search_crawler', 'search_crawler', 'search_crawler', 'search_crawler', 'search_crawler', 'search_crawler', 'search_crawler', 'search_crawler', 'seo_crawler', 'seo_crawler', 'seo_crawler', 'seo_crawler', 'seo_crawler', 'seo_crawler', 'seo_crawler', 'seo_crawler', 'seo_crawler', 'social_crawler', 'social_crawler', 'social_crawler', 'social_crawler', 'social_crawler', 'social_crawler', 'social_crawler', 'social_crawler', 'social_crawler', 'social_crawler', 'social_crawler', 'monitoring', 'monitoring', 'monitoring', 'monitoring', 'monitoring', 'monitoring', 'monitoring', 'monitoring', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'headless_browser', 'headless_browser', 'headless_browser', 'headless_browser', 'headless_browser', 'headless_browser', 'no_user_agent'][multiMatchAnyIndex(ifNull(properties.$user_agent, ''), ['GPTBot', 'Google-CloudVertexBot', 'Google-Extended', 'GoogleOther', 'Claude-SearchBot', 'Claude-User', 'ClaudeBot', 'Claude-Web', 'anthropic-ai', 'Perplexity-User', 'PerplexityBot', 'CCBot', 'meta-externalagent', 'Bytespider', 'TikTokSpider', 'cohere-ai', 'Diffbot', 'omgili', 'Webzio-Extended', 'Timpibot', 'Amazonbot', 'PetalBot', 'Brightbot', 'OAI-SearchBot', 'Applebot-Extended', 'ChatGPT-User', 'meta-externalfetcher', 'Meta-ExternalFetcher', 'DuckAssistBot', 'MistralAI-User', 'Manus-User', 'Google-NotebookLM', 'Shap-User', 'desktop\\.hog\\.dev', 'mobile\\.hog\\.dev', 'agent\\.hog\\.dev', 'cloud\\.hog\\.dev', 'Applebot/', 'Googlebot', 'bingbot', 'Bingbot', 'YandexBot', 'Baiduspider', 'DuckDuckBot', 'Slurp', 'Yeti/', 'YisouSpider', 'Sogou web spider', 'pageburst', '360Spider', 'AdsBot-Google', 'Google-InspectionTool', 'Google-Food', 'AhrefsSiteAudit', 'AhrefsBot', 'Barkrowler', 'SemrushBot', 'SERankingBacklinksBot', 'MJ12bot', 'DotBot', 'Lighthouse', 'MeltwaterNews', 'FacebookBot', 'facebookexternalhit', 'Twitterbot', 'LinkedInBot', 'Pinterest', 'Slackbot', 'Slack-ImgProxy', 'TelegramBot', 'WhatsApp', 'GoogleImageProxy', 'Iframely', 'Pingdom', 'UptimeRobot', 'Site24x7', 'StatusCake', 'Datadog', 'GrafanaSyntheticMonitoring', 'DMBrowser', 'DigitalOcean Uptime Probe', 'curl/', 'Wget', 'python-requests', 'axios', 'node-fetch', 'Go-http-client', 'okhttp', 'Apache-HttpClient', 'libwww-perl', 'Scrapy', 'PlayStore-Google', 'Amazon CloudFront', 'AmazonProductDiscovery', 'Google-BusinessLinkVerification', 'Storebot-Google', 'Google-Read-Aloud', 'Google-Appointments', 'Google-Actions', 'OneTrust', 'Chrome Privacy Preserving Prefetch Proxy', 'Mozlila/', 'HeadlessChrome', 'PhantomJS', 'Puppeteer', 'Playwright', 'Selenium', '^$'])]) AS category,
- count() AS count
+ if(equals(multiMatchAnyIndex(ifNull(properties.$user_agent, ''), ['GPTBot', 'Google-CloudVertexBot', 'Google-Extended', 'GoogleOther', 'Claude-SearchBot', 'Claude-User', 'ClaudeBot', 'Claude-Web', 'anthropic-ai', 'Perplexity-User', 'PerplexityBot', 'CCBot', 'meta-externalagent', 'Bytespider', 'TikTokSpider', 'cohere-ai', 'Diffbot', 'omgili', 'Webzio-Extended', 'Timpibot', 'Amazonbot', 'PetalBot', 'Brightbot', 'OAI-SearchBot', 'Applebot-Extended', 'ChatGPT-User', 'meta-externalfetcher', 'Meta-ExternalFetcher', 'DuckAssistBot', 'MistralAI-User', 'Manus-User', 'Google-NotebookLM', 'Shap-User', 'desktop\\.hog\\.dev', 'mobile\\.hog\\.dev', 'agent\\.hog\\.dev', 'cloud\\.hog\\.dev', 'Applebot/', 'Googlebot', 'bingbot', 'Bingbot', 'YandexBot', 'Baiduspider', 'DuckDuckBot', 'Slurp', 'Yeti/', 'YisouSpider', 'Sogou web spider', 'pageburst', '360Spider', 'AdsBot-Google', 'Google-InspectionTool', 'Google-Food', 'AhrefsSiteAudit', 'AhrefsBot', 'Barkrowler', 'SemrushBot', 'SERankingBacklinksBot', 'MJ12bot', 'DotBot', 'Lighthouse', 'MeltwaterNews', 'FacebookBot', 'facebookexternalhit', 'Twitterbot', 'LinkedInBot', 'Pinterest', 'Slackbot', 'Slack-ImgProxy', 'TelegramBot', 'WhatsApp', 'GoogleImageProxy', 'Iframely', 'Pingdom', 'UptimeRobot', 'Site24x7', 'StatusCake', 'Datadog', 'GrafanaSyntheticMonitoring', 'DMBrowser', 'DigitalOcean Uptime Probe', 'curl/', 'Wget', 'python-requests', 'axios', 'node-fetch', 'Go-http-client', 'okhttp', 'Apache-HttpClient', 'libwww-perl', 'Scrapy', 'PlayStore-Google', 'Amazon CloudFront', 'AmazonProductDiscovery', 'Google-BusinessLinkVerification', 'Storebot-Google', 'Google-Read-Aloud', 'Google-Appointments', 'Google-Actions', 'OneTrust', 'Chrome Privacy Preserving Prefetch Proxy', 'Mozlila/', 'HeadlessChrome', 'PhantomJS', 'Puppeteer', 'Playwright', 'Selenium', '^$']), 0), 'regular', ['ai_crawler', 'ai_crawler', 'ai_crawler', 'ai_crawler', 'ai_search', 'ai_assistant', 'ai_crawler', 'ai_crawler', 'ai_crawler', 'ai_assistant', 'ai_search', 'ai_crawler', 'ai_crawler', 'ai_crawler', 'ai_crawler', 'ai_crawler', 'ai_crawler', 'ai_crawler', 'ai_crawler', 'ai_crawler', 'ai_crawler', 'ai_crawler', 'ai_crawler', 'ai_search', 'ai_search', 'ai_assistant', 'ai_assistant', 'ai_assistant', 'ai_assistant', 'ai_assistant', 'ai_assistant', 'ai_assistant', 'ai_assistant', 'ai_assistant', 'ai_assistant', 'ai_assistant', 'ai_assistant', 'ai_search', 'search_crawler', 'search_crawler', 'search_crawler', 'search_crawler', 'search_crawler', 'search_crawler', 'search_crawler', 'search_crawler', 'search_crawler', 'search_crawler', 'search_crawler', 'search_crawler', 'search_crawler', 'search_crawler', 'search_crawler', 'seo_crawler', 'seo_crawler', 'seo_crawler', 'seo_crawler', 'seo_crawler', 'seo_crawler', 'seo_crawler', 'seo_crawler', 'seo_crawler', 'social_crawler', 'social_crawler', 'social_crawler', 'social_crawler', 'social_crawler', 'social_crawler', 'social_crawler', 'social_crawler', 'social_crawler', 'social_crawler', 'social_crawler', 'monitoring', 'monitoring', 'monitoring', 'monitoring', 'monitoring', 'monitoring', 'monitoring', 'monitoring', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'http_client', 'headless_browser', 'headless_browser', 'headless_browser', 'headless_browser', 'headless_browser', 'headless_browser', 'no_user_agent'][multiMatchAnyIndex(ifNull(properties.$user_agent, ''), ['GPTBot', 'Google-CloudVertexBot', 'Google-Extended', 'GoogleOther', 'Claude-SearchBot', 'Claude-User', 'ClaudeBot', 'Claude-Web', 'anthropic-ai', 'Perplexity-User', 'PerplexityBot', 'CCBot', 'meta-externalagent', 'Bytespider', 'TikTokSpider', 'cohere-ai', 'Diffbot', 'omgili', 'Webzio-Extended', 'Timpibot', 'Amazonbot', 'PetalBot', 'Brightbot', 'OAI-SearchBot', 'Applebot-Extended', 'ChatGPT-User', 'meta-externalfetcher', 'Meta-ExternalFetcher', 'DuckAssistBot', 'MistralAI-User', 'Manus-User', 'Google-NotebookLM', 'Shap-User', 'desktop\\.hog\\.dev', 'mobile\\.hog\\.dev', 'agent\\.hog\\.dev', 'cloud\\.hog\\.dev', 'Applebot/', 'Googlebot', 'bingbot', 'Bingbot', 'YandexBot', 'Baiduspider', 'DuckDuckBot', 'Slurp', 'Yeti/', 'YisouSpider', 'Sogou web spider', 'pageburst', '360Spider', 'AdsBot-Google', 'Google-InspectionTool', 'Google-Food', 'AhrefsSiteAudit', 'AhrefsBot', 'Barkrowler', 'SemrushBot', 'SERankingBacklinksBot', 'MJ12bot', 'DotBot', 'Lighthouse', 'MeltwaterNews', 'FacebookBot', 'facebookexternalhit', 'Twitterbot', 'LinkedInBot', 'Pinterest', 'Slackbot', 'Slack-ImgProxy', 'TelegramBot', 'WhatsApp', 'GoogleImageProxy', 'Iframely', 'Pingdom', 'UptimeRobot', 'Site24x7', 'StatusCake', 'Datadog', 'GrafanaSyntheticMonitoring', 'DMBrowser', 'DigitalOcean Uptime Probe', 'curl/', 'Wget', 'python-requests', 'axios', 'node-fetch', 'Go-http-client', 'okhttp', 'Apache-HttpClient', 'libwww-perl', 'Scrapy', 'PlayStore-Google', 'Amazon CloudFront', 'AmazonProductDiscovery', 'Google-BusinessLinkVerification', 'Storebot-Google', 'Google-Read-Aloud', 'Google-Appointments', 'Google-Actions', 'OneTrust', 'Chrome Privacy Preserving Prefetch Proxy', 'Mozlila/', 'HeadlessChrome', 'PhantomJS', 'Puppeteer', 'Playwright', 'Selenium', '^$'])]) AS category,
+ count() AS count
FROM
- events
+ events
WHERE
- equals(event, '$pageview')
+ equals(event, '$pageview')
GROUP BY
- category
+ category
ORDER BY
- category ASC
+ category ASC
LIMIT 100
'''
# ---
@@ -127,31 +127,31 @@
'''
-- ClickHouse
SELECT
- if(ifNull(equals(multiMatchAnyIndex(ifNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', ''), %(hogql_val_1)s), [%(hogql_val_2)s, %(hogql_val_3)s, %(hogql_val_4)s, %(hogql_val_5)s, %(hogql_val_6)s, %(hogql_val_7)s, %(hogql_val_8)s, %(hogql_val_9)s, %(hogql_val_10)s, %(hogql_val_11)s, %(hogql_val_12)s, %(hogql_val_13)s, %(hogql_val_14)s, %(hogql_val_15)s, %(hogql_val_16)s, %(hogql_val_17)s, %(hogql_val_18)s, %(hogql_val_19)s, %(hogql_val_20)s, %(hogql_val_21)s, %(hogql_val_22)s, %(hogql_val_23)s, %(hogql_val_24)s, %(hogql_val_25)s, %(hogql_val_26)s, %(hogql_val_27)s, %(hogql_val_28)s, %(hogql_val_29)s, %(hogql_val_30)s, %(hogql_val_31)s, %(hogql_val_32)s, %(hogql_val_33)s, %(hogql_val_34)s, %(hogql_val_35)s, %(hogql_val_36)s, %(hogql_val_37)s, %(hogql_val_38)s, %(hogql_val_39)s, %(hogql_val_40)s, %(hogql_val_41)s, %(hogql_val_42)s, %(hogql_val_43)s, %(hogql_val_44)s, %(hogql_val_45)s, %(hogql_val_46)s, %(hogql_val_47)s, %(hogql_val_48)s, %(hogql_val_49)s, %(hogql_val_50)s, %(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, %(hogql_val_59)s, %(hogql_val_60)s, %(hogql_val_61)s, %(hogql_val_62)s, %(hogql_val_63)s, %(hogql_val_64)s, %(hogql_val_65)s, %(hogql_val_66)s, %(hogql_val_67)s, %(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, %(hogql_val_79)s, %(hogql_val_80)s, %(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, %(hogql_val_91)s, %(hogql_val_92)s, %(hogql_val_93)s, %(hogql_val_94)s, %(hogql_val_95)s, %(hogql_val_96)s, %(hogql_val_97)s, %(hogql_val_98)s, %(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, %(hogql_val_107)s, %(hogql_val_108)s, %(hogql_val_109)s]), 0), 0), %(hogql_val_110)s, [%(hogql_val_111)s, %(hogql_val_112)s, %(hogql_val_113)s, %(hogql_val_114)s, %(hogql_val_115)s, %(hogql_val_116)s, %(hogql_val_117)s, %(hogql_val_118)s, %(hogql_val_119)s, %(hogql_val_120)s, %(hogql_val_121)s, %(hogql_val_122)s, %(hogql_val_123)s, %(hogql_val_124)s, %(hogql_val_125)s, %(hogql_val_126)s, %(hogql_val_127)s, %(hogql_val_128)s, %(hogql_val_129)s, %(hogql_val_130)s, %(hogql_val_131)s, %(hogql_val_132)s, %(hogql_val_133)s, %(hogql_val_134)s, %(hogql_val_135)s, %(hogql_val_136)s, %(hogql_val_137)s, %(hogql_val_138)s, %(hogql_val_139)s, %(hogql_val_140)s, %(hogql_val_141)s, %(hogql_val_142)s, %(hogql_val_143)s, %(hogql_val_144)s, %(hogql_val_145)s, %(hogql_val_146)s, %(hogql_val_147)s, %(hogql_val_148)s, %(hogql_val_149)s, %(hogql_val_150)s, %(hogql_val_151)s, %(hogql_val_152)s, %(hogql_val_153)s, %(hogql_val_154)s, %(hogql_val_155)s, %(hogql_val_156)s, %(hogql_val_157)s, %(hogql_val_158)s, %(hogql_val_159)s, %(hogql_val_160)s, %(hogql_val_161)s, %(hogql_val_162)s, %(hogql_val_163)s, %(hogql_val_164)s, %(hogql_val_165)s, %(hogql_val_166)s, %(hogql_val_167)s, %(hogql_val_168)s, %(hogql_val_169)s, %(hogql_val_170)s, %(hogql_val_171)s, %(hogql_val_172)s, %(hogql_val_173)s, %(hogql_val_174)s, %(hogql_val_175)s, %(hogql_val_176)s, %(hogql_val_177)s, %(hogql_val_178)s, %(hogql_val_179)s, %(hogql_val_180)s, %(hogql_val_181)s, %(hogql_val_182)s, %(hogql_val_183)s, %(hogql_val_184)s, %(hogql_val_185)s, %(hogql_val_186)s, %(hogql_val_187)s, %(hogql_val_188)s, %(hogql_val_189)s, %(hogql_val_190)s, %(hogql_val_191)s, %(hogql_val_192)s, %(hogql_val_193)s, %(hogql_val_194)s, %(hogql_val_195)s, %(hogql_val_196)s, %(hogql_val_197)s, %(hogql_val_198)s, %(hogql_val_199)s, %(hogql_val_200)s, %(hogql_val_201)s, %(hogql_val_202)s, %(hogql_val_203)s, %(hogql_val_204)s, %(hogql_val_205)s, %(hogql_val_206)s, %(hogql_val_207)s, %(hogql_val_208)s, %(hogql_val_209)s, %(hogql_val_210)s, %(hogql_val_211)s, %(hogql_val_212)s, %(hogql_val_213)s, %(hogql_val_214)s, %(hogql_val_215)s, %(hogql_val_216)s, %(hogql_val_217)s, %(hogql_val_218)s][multiMatchAnyIndex(ifNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_219)s), ''), 'null'), '^"|"$', ''), %(hogql_val_220)s), [%(hogql_val_221)s, %(hogql_val_222)s, %(hogql_val_223)s, %(hogql_val_224)s, %(hogql_val_225)s, %(hogql_val_226)s, %(hogql_val_227)s, %(hogql_val_228)s, %(hogql_val_229)s, %(hogql_val_230)s, %(hogql_val_231)s, %(hogql_val_232)s, %(hogql_val_233)s, %(hogql_val_234)s, %(hogql_val_235)s, %(hogql_val_236)s, %(hogql_val_237)s, %(hogql_val_238)s, %(hogql_val_239)s, %(hogql_val_240)s, %(hogql_val_241)s, %(hogql_val_242)s, %(hogql_val_243)s, %(hogql_val_244)s, %(hogql_val_245)s, %(hogql_val_246)s, %(hogql_val_247)s, %(hogql_val_248)s, %(hogql_val_249)s, %(hogql_val_250)s, %(hogql_val_251)s, %(hogql_val_252)s, %(hogql_val_253)s, %(hogql_val_254)s, %(hogql_val_255)s, %(hogql_val_256)s, %(hogql_val_257)s, %(hogql_val_258)s, %(hogql_val_259)s, %(hogql_val_260)s, %(hogql_val_261)s, %(hogql_val_262)s, %(hogql_val_263)s, %(hogql_val_264)s, %(hogql_val_265)s, %(hogql_val_266)s, %(hogql_val_267)s, %(hogql_val_268)s, %(hogql_val_269)s, %(hogql_val_270)s, %(hogql_val_271)s, %(hogql_val_272)s, %(hogql_val_273)s, %(hogql_val_274)s, %(hogql_val_275)s, %(hogql_val_276)s, %(hogql_val_277)s, %(hogql_val_278)s, %(hogql_val_279)s, %(hogql_val_280)s, %(hogql_val_281)s, %(hogql_val_282)s, %(hogql_val_283)s, %(hogql_val_284)s, %(hogql_val_285)s, %(hogql_val_286)s, %(hogql_val_287)s, %(hogql_val_288)s, %(hogql_val_289)s, %(hogql_val_290)s, %(hogql_val_291)s, %(hogql_val_292)s, %(hogql_val_293)s, %(hogql_val_294)s, %(hogql_val_295)s, %(hogql_val_296)s, %(hogql_val_297)s, %(hogql_val_298)s, %(hogql_val_299)s, %(hogql_val_300)s, %(hogql_val_301)s, %(hogql_val_302)s, %(hogql_val_303)s, %(hogql_val_304)s, %(hogql_val_305)s, %(hogql_val_306)s, %(hogql_val_307)s, %(hogql_val_308)s, %(hogql_val_309)s, %(hogql_val_310)s, %(hogql_val_311)s, %(hogql_val_312)s, %(hogql_val_313)s, %(hogql_val_314)s, %(hogql_val_315)s, %(hogql_val_316)s, %(hogql_val_317)s, %(hogql_val_318)s, %(hogql_val_319)s, %(hogql_val_320)s, %(hogql_val_321)s, %(hogql_val_322)s, %(hogql_val_323)s, %(hogql_val_324)s, %(hogql_val_325)s, %(hogql_val_326)s, %(hogql_val_327)s, %(hogql_val_328)s])]) AS traffic_type,
- count() AS count
+ if(ifNull(equals(multiMatchAnyIndex(ifNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', ''), %(hogql_val_1)s), [%(hogql_val_2)s, %(hogql_val_3)s, %(hogql_val_4)s, %(hogql_val_5)s, %(hogql_val_6)s, %(hogql_val_7)s, %(hogql_val_8)s, %(hogql_val_9)s, %(hogql_val_10)s, %(hogql_val_11)s, %(hogql_val_12)s, %(hogql_val_13)s, %(hogql_val_14)s, %(hogql_val_15)s, %(hogql_val_16)s, %(hogql_val_17)s, %(hogql_val_18)s, %(hogql_val_19)s, %(hogql_val_20)s, %(hogql_val_21)s, %(hogql_val_22)s, %(hogql_val_23)s, %(hogql_val_24)s, %(hogql_val_25)s, %(hogql_val_26)s, %(hogql_val_27)s, %(hogql_val_28)s, %(hogql_val_29)s, %(hogql_val_30)s, %(hogql_val_31)s, %(hogql_val_32)s, %(hogql_val_33)s, %(hogql_val_34)s, %(hogql_val_35)s, %(hogql_val_36)s, %(hogql_val_37)s, %(hogql_val_38)s, %(hogql_val_39)s, %(hogql_val_40)s, %(hogql_val_41)s, %(hogql_val_42)s, %(hogql_val_43)s, %(hogql_val_44)s, %(hogql_val_45)s, %(hogql_val_46)s, %(hogql_val_47)s, %(hogql_val_48)s, %(hogql_val_49)s, %(hogql_val_50)s, %(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, %(hogql_val_59)s, %(hogql_val_60)s, %(hogql_val_61)s, %(hogql_val_62)s, %(hogql_val_63)s, %(hogql_val_64)s, %(hogql_val_65)s, %(hogql_val_66)s, %(hogql_val_67)s, %(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, %(hogql_val_79)s, %(hogql_val_80)s, %(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, %(hogql_val_91)s, %(hogql_val_92)s, %(hogql_val_93)s, %(hogql_val_94)s, %(hogql_val_95)s, %(hogql_val_96)s, %(hogql_val_97)s, %(hogql_val_98)s, %(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, %(hogql_val_107)s, %(hogql_val_108)s, %(hogql_val_109)s]), 0), 0), %(hogql_val_110)s, [%(hogql_val_111)s, %(hogql_val_112)s, %(hogql_val_113)s, %(hogql_val_114)s, %(hogql_val_115)s, %(hogql_val_116)s, %(hogql_val_117)s, %(hogql_val_118)s, %(hogql_val_119)s, %(hogql_val_120)s, %(hogql_val_121)s, %(hogql_val_122)s, %(hogql_val_123)s, %(hogql_val_124)s, %(hogql_val_125)s, %(hogql_val_126)s, %(hogql_val_127)s, %(hogql_val_128)s, %(hogql_val_129)s, %(hogql_val_130)s, %(hogql_val_131)s, %(hogql_val_132)s, %(hogql_val_133)s, %(hogql_val_134)s, %(hogql_val_135)s, %(hogql_val_136)s, %(hogql_val_137)s, %(hogql_val_138)s, %(hogql_val_139)s, %(hogql_val_140)s, %(hogql_val_141)s, %(hogql_val_142)s, %(hogql_val_143)s, %(hogql_val_144)s, %(hogql_val_145)s, %(hogql_val_146)s, %(hogql_val_147)s, %(hogql_val_148)s, %(hogql_val_149)s, %(hogql_val_150)s, %(hogql_val_151)s, %(hogql_val_152)s, %(hogql_val_153)s, %(hogql_val_154)s, %(hogql_val_155)s, %(hogql_val_156)s, %(hogql_val_157)s, %(hogql_val_158)s, %(hogql_val_159)s, %(hogql_val_160)s, %(hogql_val_161)s, %(hogql_val_162)s, %(hogql_val_163)s, %(hogql_val_164)s, %(hogql_val_165)s, %(hogql_val_166)s, %(hogql_val_167)s, %(hogql_val_168)s, %(hogql_val_169)s, %(hogql_val_170)s, %(hogql_val_171)s, %(hogql_val_172)s, %(hogql_val_173)s, %(hogql_val_174)s, %(hogql_val_175)s, %(hogql_val_176)s, %(hogql_val_177)s, %(hogql_val_178)s, %(hogql_val_179)s, %(hogql_val_180)s, %(hogql_val_181)s, %(hogql_val_182)s, %(hogql_val_183)s, %(hogql_val_184)s, %(hogql_val_185)s, %(hogql_val_186)s, %(hogql_val_187)s, %(hogql_val_188)s, %(hogql_val_189)s, %(hogql_val_190)s, %(hogql_val_191)s, %(hogql_val_192)s, %(hogql_val_193)s, %(hogql_val_194)s, %(hogql_val_195)s, %(hogql_val_196)s, %(hogql_val_197)s, %(hogql_val_198)s, %(hogql_val_199)s, %(hogql_val_200)s, %(hogql_val_201)s, %(hogql_val_202)s, %(hogql_val_203)s, %(hogql_val_204)s, %(hogql_val_205)s, %(hogql_val_206)s, %(hogql_val_207)s, %(hogql_val_208)s, %(hogql_val_209)s, %(hogql_val_210)s, %(hogql_val_211)s, %(hogql_val_212)s, %(hogql_val_213)s, %(hogql_val_214)s, %(hogql_val_215)s, %(hogql_val_216)s, %(hogql_val_217)s, %(hogql_val_218)s][multiMatchAnyIndex(ifNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_219)s), ''), 'null'), '^"|"$', ''), %(hogql_val_220)s), [%(hogql_val_221)s, %(hogql_val_222)s, %(hogql_val_223)s, %(hogql_val_224)s, %(hogql_val_225)s, %(hogql_val_226)s, %(hogql_val_227)s, %(hogql_val_228)s, %(hogql_val_229)s, %(hogql_val_230)s, %(hogql_val_231)s, %(hogql_val_232)s, %(hogql_val_233)s, %(hogql_val_234)s, %(hogql_val_235)s, %(hogql_val_236)s, %(hogql_val_237)s, %(hogql_val_238)s, %(hogql_val_239)s, %(hogql_val_240)s, %(hogql_val_241)s, %(hogql_val_242)s, %(hogql_val_243)s, %(hogql_val_244)s, %(hogql_val_245)s, %(hogql_val_246)s, %(hogql_val_247)s, %(hogql_val_248)s, %(hogql_val_249)s, %(hogql_val_250)s, %(hogql_val_251)s, %(hogql_val_252)s, %(hogql_val_253)s, %(hogql_val_254)s, %(hogql_val_255)s, %(hogql_val_256)s, %(hogql_val_257)s, %(hogql_val_258)s, %(hogql_val_259)s, %(hogql_val_260)s, %(hogql_val_261)s, %(hogql_val_262)s, %(hogql_val_263)s, %(hogql_val_264)s, %(hogql_val_265)s, %(hogql_val_266)s, %(hogql_val_267)s, %(hogql_val_268)s, %(hogql_val_269)s, %(hogql_val_270)s, %(hogql_val_271)s, %(hogql_val_272)s, %(hogql_val_273)s, %(hogql_val_274)s, %(hogql_val_275)s, %(hogql_val_276)s, %(hogql_val_277)s, %(hogql_val_278)s, %(hogql_val_279)s, %(hogql_val_280)s, %(hogql_val_281)s, %(hogql_val_282)s, %(hogql_val_283)s, %(hogql_val_284)s, %(hogql_val_285)s, %(hogql_val_286)s, %(hogql_val_287)s, %(hogql_val_288)s, %(hogql_val_289)s, %(hogql_val_290)s, %(hogql_val_291)s, %(hogql_val_292)s, %(hogql_val_293)s, %(hogql_val_294)s, %(hogql_val_295)s, %(hogql_val_296)s, %(hogql_val_297)s, %(hogql_val_298)s, %(hogql_val_299)s, %(hogql_val_300)s, %(hogql_val_301)s, %(hogql_val_302)s, %(hogql_val_303)s, %(hogql_val_304)s, %(hogql_val_305)s, %(hogql_val_306)s, %(hogql_val_307)s, %(hogql_val_308)s, %(hogql_val_309)s, %(hogql_val_310)s, %(hogql_val_311)s, %(hogql_val_312)s, %(hogql_val_313)s, %(hogql_val_314)s, %(hogql_val_315)s, %(hogql_val_316)s, %(hogql_val_317)s, %(hogql_val_318)s, %(hogql_val_319)s, %(hogql_val_320)s, %(hogql_val_321)s, %(hogql_val_322)s, %(hogql_val_323)s, %(hogql_val_324)s, %(hogql_val_325)s, %(hogql_val_326)s, %(hogql_val_327)s, %(hogql_val_328)s])]) AS traffic_type,
+ count() AS count
FROM
- events
+ events
WHERE
- and(equals(events.team_id, 99999), equals(events.event, %(hogql_val_329)s))
+ and(equals(events.team_id, 99999), equals(events.event, %(hogql_val_329)s))
GROUP BY
- traffic_type
+ traffic_type
ORDER BY
- traffic_type ASC
+ traffic_type ASC
LIMIT 100
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0, transform_null_in=1, optimize_min_equality_disjunction_chain_length=4294967295, allow_experimental_join_condition=1, use_hive_partitioning=0
-- HogQL
SELECT
- if(equals(multiMatchAnyIndex(ifNull(properties.$user_agent, ''), ['GPTBot', 'Google-CloudVertexBot', 'Google-Extended', 'GoogleOther', 'Claude-SearchBot', 'Claude-User', 'ClaudeBot', 'Claude-Web', 'anthropic-ai', 'Perplexity-User', 'PerplexityBot', 'CCBot', 'meta-externalagent', 'Bytespider', 'TikTokSpider', 'cohere-ai', 'Diffbot', 'omgili', 'Webzio-Extended', 'Timpibot', 'Amazonbot', 'PetalBot', 'Brightbot', 'OAI-SearchBot', 'Applebot-Extended', 'ChatGPT-User', 'meta-externalfetcher', 'Meta-ExternalFetcher', 'DuckAssistBot', 'MistralAI-User', 'Manus-User', 'Google-NotebookLM', 'Shap-User', 'desktop\\.hog\\.dev', 'mobile\\.hog\\.dev', 'agent\\.hog\\.dev', 'cloud\\.hog\\.dev', 'Applebot/', 'Googlebot', 'bingbot', 'Bingbot', 'YandexBot', 'Baiduspider', 'DuckDuckBot', 'Slurp', 'Yeti/', 'YisouSpider', 'Sogou web spider', 'pageburst', '360Spider', 'AdsBot-Google', 'Google-InspectionTool', 'Google-Food', 'AhrefsSiteAudit', 'AhrefsBot', 'Barkrowler', 'SemrushBot', 'SERankingBacklinksBot', 'MJ12bot', 'DotBot', 'Lighthouse', 'MeltwaterNews', 'FacebookBot', 'facebookexternalhit', 'Twitterbot', 'LinkedInBot', 'Pinterest', 'Slackbot', 'Slack-ImgProxy', 'TelegramBot', 'WhatsApp', 'GoogleImageProxy', 'Iframely', 'Pingdom', 'UptimeRobot', 'Site24x7', 'StatusCake', 'Datadog', 'GrafanaSyntheticMonitoring', 'DMBrowser', 'DigitalOcean Uptime Probe', 'curl/', 'Wget', 'python-requests', 'axios', 'node-fetch', 'Go-http-client', 'okhttp', 'Apache-HttpClient', 'libwww-perl', 'Scrapy', 'PlayStore-Google', 'Amazon CloudFront', 'AmazonProductDiscovery', 'Google-BusinessLinkVerification', 'Storebot-Google', 'Google-Read-Aloud', 'Google-Appointments', 'Google-Actions', 'OneTrust', 'Chrome Privacy Preserving Prefetch Proxy', 'Mozlila/', 'HeadlessChrome', 'PhantomJS', 'Puppeteer', 'Playwright', 'Selenium', '^$']), 0), 'Regular', ['AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Automation', 'Automation', 'Automation', 'Automation', 'Automation', 'Automation', 'Automation', 'Automation', 'Automation', 'Automation', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Automation', 'Automation', 'Automation', 'Automation', 'Automation', 'Automation', 'Automation', 'Automation'][multiMatchAnyIndex(ifNull(properties.$user_agent, ''), ['GPTBot', 'Google-CloudVertexBot', 'Google-Extended', 'GoogleOther', 'Claude-SearchBot', 'Claude-User', 'ClaudeBot', 'Claude-Web', 'anthropic-ai', 'Perplexity-User', 'PerplexityBot', 'CCBot', 'meta-externalagent', 'Bytespider', 'TikTokSpider', 'cohere-ai', 'Diffbot', 'omgili', 'Webzio-Extended', 'Timpibot', 'Amazonbot', 'PetalBot', 'Brightbot', 'OAI-SearchBot', 'Applebot-Extended', 'ChatGPT-User', 'meta-externalfetcher', 'Meta-ExternalFetcher', 'DuckAssistBot', 'MistralAI-User', 'Manus-User', 'Google-NotebookLM', 'Shap-User', 'desktop\\.hog\\.dev', 'mobile\\.hog\\.dev', 'agent\\.hog\\.dev', 'cloud\\.hog\\.dev', 'Applebot/', 'Googlebot', 'bingbot', 'Bingbot', 'YandexBot', 'Baiduspider', 'DuckDuckBot', 'Slurp', 'Yeti/', 'YisouSpider', 'Sogou web spider', 'pageburst', '360Spider', 'AdsBot-Google', 'Google-InspectionTool', 'Google-Food', 'AhrefsSiteAudit', 'AhrefsBot', 'Barkrowler', 'SemrushBot', 'SERankingBacklinksBot', 'MJ12bot', 'DotBot', 'Lighthouse', 'MeltwaterNews', 'FacebookBot', 'facebookexternalhit', 'Twitterbot', 'LinkedInBot', 'Pinterest', 'Slackbot', 'Slack-ImgProxy', 'TelegramBot', 'WhatsApp', 'GoogleImageProxy', 'Iframely', 'Pingdom', 'UptimeRobot', 'Site24x7', 'StatusCake', 'Datadog', 'GrafanaSyntheticMonitoring', 'DMBrowser', 'DigitalOcean Uptime Probe', 'curl/', 'Wget', 'python-requests', 'axios', 'node-fetch', 'Go-http-client', 'okhttp', 'Apache-HttpClient', 'libwww-perl', 'Scrapy', 'PlayStore-Google', 'Amazon CloudFront', 'AmazonProductDiscovery', 'Google-BusinessLinkVerification', 'Storebot-Google', 'Google-Read-Aloud', 'Google-Appointments', 'Google-Actions', 'OneTrust', 'Chrome Privacy Preserving Prefetch Proxy', 'Mozlila/', 'HeadlessChrome', 'PhantomJS', 'Puppeteer', 'Playwright', 'Selenium', '^$'])]) AS traffic_type,
- count() AS count
+ if(equals(multiMatchAnyIndex(ifNull(properties.$user_agent, ''), ['GPTBot', 'Google-CloudVertexBot', 'Google-Extended', 'GoogleOther', 'Claude-SearchBot', 'Claude-User', 'ClaudeBot', 'Claude-Web', 'anthropic-ai', 'Perplexity-User', 'PerplexityBot', 'CCBot', 'meta-externalagent', 'Bytespider', 'TikTokSpider', 'cohere-ai', 'Diffbot', 'omgili', 'Webzio-Extended', 'Timpibot', 'Amazonbot', 'PetalBot', 'Brightbot', 'OAI-SearchBot', 'Applebot-Extended', 'ChatGPT-User', 'meta-externalfetcher', 'Meta-ExternalFetcher', 'DuckAssistBot', 'MistralAI-User', 'Manus-User', 'Google-NotebookLM', 'Shap-User', 'desktop\\.hog\\.dev', 'mobile\\.hog\\.dev', 'agent\\.hog\\.dev', 'cloud\\.hog\\.dev', 'Applebot/', 'Googlebot', 'bingbot', 'Bingbot', 'YandexBot', 'Baiduspider', 'DuckDuckBot', 'Slurp', 'Yeti/', 'YisouSpider', 'Sogou web spider', 'pageburst', '360Spider', 'AdsBot-Google', 'Google-InspectionTool', 'Google-Food', 'AhrefsSiteAudit', 'AhrefsBot', 'Barkrowler', 'SemrushBot', 'SERankingBacklinksBot', 'MJ12bot', 'DotBot', 'Lighthouse', 'MeltwaterNews', 'FacebookBot', 'facebookexternalhit', 'Twitterbot', 'LinkedInBot', 'Pinterest', 'Slackbot', 'Slack-ImgProxy', 'TelegramBot', 'WhatsApp', 'GoogleImageProxy', 'Iframely', 'Pingdom', 'UptimeRobot', 'Site24x7', 'StatusCake', 'Datadog', 'GrafanaSyntheticMonitoring', 'DMBrowser', 'DigitalOcean Uptime Probe', 'curl/', 'Wget', 'python-requests', 'axios', 'node-fetch', 'Go-http-client', 'okhttp', 'Apache-HttpClient', 'libwww-perl', 'Scrapy', 'PlayStore-Google', 'Amazon CloudFront', 'AmazonProductDiscovery', 'Google-BusinessLinkVerification', 'Storebot-Google', 'Google-Read-Aloud', 'Google-Appointments', 'Google-Actions', 'OneTrust', 'Chrome Privacy Preserving Prefetch Proxy', 'Mozlila/', 'HeadlessChrome', 'PhantomJS', 'Puppeteer', 'Playwright', 'Selenium', '^$']), 0), 'Regular', ['AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'AI Agent', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Automation', 'Automation', 'Automation', 'Automation', 'Automation', 'Automation', 'Automation', 'Automation', 'Automation', 'Automation', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Bot', 'Automation', 'Automation', 'Automation', 'Automation', 'Automation', 'Automation', 'Automation', 'Automation'][multiMatchAnyIndex(ifNull(properties.$user_agent, ''), ['GPTBot', 'Google-CloudVertexBot', 'Google-Extended', 'GoogleOther', 'Claude-SearchBot', 'Claude-User', 'ClaudeBot', 'Claude-Web', 'anthropic-ai', 'Perplexity-User', 'PerplexityBot', 'CCBot', 'meta-externalagent', 'Bytespider', 'TikTokSpider', 'cohere-ai', 'Diffbot', 'omgili', 'Webzio-Extended', 'Timpibot', 'Amazonbot', 'PetalBot', 'Brightbot', 'OAI-SearchBot', 'Applebot-Extended', 'ChatGPT-User', 'meta-externalfetcher', 'Meta-ExternalFetcher', 'DuckAssistBot', 'MistralAI-User', 'Manus-User', 'Google-NotebookLM', 'Shap-User', 'desktop\\.hog\\.dev', 'mobile\\.hog\\.dev', 'agent\\.hog\\.dev', 'cloud\\.hog\\.dev', 'Applebot/', 'Googlebot', 'bingbot', 'Bingbot', 'YandexBot', 'Baiduspider', 'DuckDuckBot', 'Slurp', 'Yeti/', 'YisouSpider', 'Sogou web spider', 'pageburst', '360Spider', 'AdsBot-Google', 'Google-InspectionTool', 'Google-Food', 'AhrefsSiteAudit', 'AhrefsBot', 'Barkrowler', 'SemrushBot', 'SERankingBacklinksBot', 'MJ12bot', 'DotBot', 'Lighthouse', 'MeltwaterNews', 'FacebookBot', 'facebookexternalhit', 'Twitterbot', 'LinkedInBot', 'Pinterest', 'Slackbot', 'Slack-ImgProxy', 'TelegramBot', 'WhatsApp', 'GoogleImageProxy', 'Iframely', 'Pingdom', 'UptimeRobot', 'Site24x7', 'StatusCake', 'Datadog', 'GrafanaSyntheticMonitoring', 'DMBrowser', 'DigitalOcean Uptime Probe', 'curl/', 'Wget', 'python-requests', 'axios', 'node-fetch', 'Go-http-client', 'okhttp', 'Apache-HttpClient', 'libwww-perl', 'Scrapy', 'PlayStore-Google', 'Amazon CloudFront', 'AmazonProductDiscovery', 'Google-BusinessLinkVerification', 'Storebot-Google', 'Google-Read-Aloud', 'Google-Appointments', 'Google-Actions', 'OneTrust', 'Chrome Privacy Preserving Prefetch Proxy', 'Mozlila/', 'HeadlessChrome', 'PhantomJS', 'Puppeteer', 'Playwright', 'Selenium', '^$'])]) AS traffic_type,
+ count() AS count
FROM
- events
+ events
WHERE
- equals(event, '$pageview')
+ equals(event, '$pageview')
GROUP BY
- traffic_type
+ traffic_type
ORDER BY
- traffic_type ASC
+ traffic_type ASC
LIMIT 100
'''
# ---
@@ -159,31 +159,31 @@
'''
-- ClickHouse
SELECT
- ifNull(notEquals(multiMatchAnyIndex(ifNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', ''), %(hogql_val_1)s), [%(hogql_val_2)s, %(hogql_val_3)s, %(hogql_val_4)s, %(hogql_val_5)s, %(hogql_val_6)s, %(hogql_val_7)s, %(hogql_val_8)s, %(hogql_val_9)s, %(hogql_val_10)s, %(hogql_val_11)s, %(hogql_val_12)s, %(hogql_val_13)s, %(hogql_val_14)s, %(hogql_val_15)s, %(hogql_val_16)s, %(hogql_val_17)s, %(hogql_val_18)s, %(hogql_val_19)s, %(hogql_val_20)s, %(hogql_val_21)s, %(hogql_val_22)s, %(hogql_val_23)s, %(hogql_val_24)s, %(hogql_val_25)s, %(hogql_val_26)s, %(hogql_val_27)s, %(hogql_val_28)s, %(hogql_val_29)s, %(hogql_val_30)s, %(hogql_val_31)s, %(hogql_val_32)s, %(hogql_val_33)s, %(hogql_val_34)s, %(hogql_val_35)s, %(hogql_val_36)s, %(hogql_val_37)s, %(hogql_val_38)s, %(hogql_val_39)s, %(hogql_val_40)s, %(hogql_val_41)s, %(hogql_val_42)s, %(hogql_val_43)s, %(hogql_val_44)s, %(hogql_val_45)s, %(hogql_val_46)s, %(hogql_val_47)s, %(hogql_val_48)s, %(hogql_val_49)s, %(hogql_val_50)s, %(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, %(hogql_val_59)s, %(hogql_val_60)s, %(hogql_val_61)s, %(hogql_val_62)s, %(hogql_val_63)s, %(hogql_val_64)s, %(hogql_val_65)s, %(hogql_val_66)s, %(hogql_val_67)s, %(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, %(hogql_val_79)s, %(hogql_val_80)s, %(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, %(hogql_val_91)s, %(hogql_val_92)s, %(hogql_val_93)s, %(hogql_val_94)s, %(hogql_val_95)s, %(hogql_val_96)s, %(hogql_val_97)s, %(hogql_val_98)s, %(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, %(hogql_val_107)s, %(hogql_val_108)s, %(hogql_val_109)s]), 0), 1) AS is_bot,
- count() AS count
+ ifNull(notEquals(multiMatchAnyIndex(ifNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_0)s), ''), 'null'), '^"|"$', ''), %(hogql_val_1)s), [%(hogql_val_2)s, %(hogql_val_3)s, %(hogql_val_4)s, %(hogql_val_5)s, %(hogql_val_6)s, %(hogql_val_7)s, %(hogql_val_8)s, %(hogql_val_9)s, %(hogql_val_10)s, %(hogql_val_11)s, %(hogql_val_12)s, %(hogql_val_13)s, %(hogql_val_14)s, %(hogql_val_15)s, %(hogql_val_16)s, %(hogql_val_17)s, %(hogql_val_18)s, %(hogql_val_19)s, %(hogql_val_20)s, %(hogql_val_21)s, %(hogql_val_22)s, %(hogql_val_23)s, %(hogql_val_24)s, %(hogql_val_25)s, %(hogql_val_26)s, %(hogql_val_27)s, %(hogql_val_28)s, %(hogql_val_29)s, %(hogql_val_30)s, %(hogql_val_31)s, %(hogql_val_32)s, %(hogql_val_33)s, %(hogql_val_34)s, %(hogql_val_35)s, %(hogql_val_36)s, %(hogql_val_37)s, %(hogql_val_38)s, %(hogql_val_39)s, %(hogql_val_40)s, %(hogql_val_41)s, %(hogql_val_42)s, %(hogql_val_43)s, %(hogql_val_44)s, %(hogql_val_45)s, %(hogql_val_46)s, %(hogql_val_47)s, %(hogql_val_48)s, %(hogql_val_49)s, %(hogql_val_50)s, %(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, %(hogql_val_59)s, %(hogql_val_60)s, %(hogql_val_61)s, %(hogql_val_62)s, %(hogql_val_63)s, %(hogql_val_64)s, %(hogql_val_65)s, %(hogql_val_66)s, %(hogql_val_67)s, %(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, %(hogql_val_79)s, %(hogql_val_80)s, %(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, %(hogql_val_91)s, %(hogql_val_92)s, %(hogql_val_93)s, %(hogql_val_94)s, %(hogql_val_95)s, %(hogql_val_96)s, %(hogql_val_97)s, %(hogql_val_98)s, %(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, %(hogql_val_107)s, %(hogql_val_108)s, %(hogql_val_109)s]), 0), 1) AS is_bot,
+ count() AS count
FROM
- events
+ events
WHERE
- and(equals(events.team_id, 99999), equals(events.event, %(hogql_val_110)s))
+ and(equals(events.team_id, 99999), equals(events.event, %(hogql_val_110)s))
GROUP BY
- is_bot
+ is_bot
ORDER BY
- is_bot ASC
+ is_bot ASC
LIMIT 100
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0, transform_null_in=1, optimize_min_equality_disjunction_chain_length=4294967295, allow_experimental_join_condition=1, use_hive_partitioning=0
-- HogQL
SELECT
- notEquals(multiMatchAnyIndex(ifNull(properties.$user_agent, ''), ['GPTBot', 'Google-CloudVertexBot', 'Google-Extended', 'GoogleOther', 'Claude-SearchBot', 'Claude-User', 'ClaudeBot', 'Claude-Web', 'anthropic-ai', 'Perplexity-User', 'PerplexityBot', 'CCBot', 'meta-externalagent', 'Bytespider', 'TikTokSpider', 'cohere-ai', 'Diffbot', 'omgili', 'Webzio-Extended', 'Timpibot', 'Amazonbot', 'PetalBot', 'Brightbot', 'OAI-SearchBot', 'Applebot-Extended', 'ChatGPT-User', 'meta-externalfetcher', 'Meta-ExternalFetcher', 'DuckAssistBot', 'MistralAI-User', 'Manus-User', 'Google-NotebookLM', 'Shap-User', 'desktop\\.hog\\.dev', 'mobile\\.hog\\.dev', 'agent\\.hog\\.dev', 'cloud\\.hog\\.dev', 'Applebot/', 'Googlebot', 'bingbot', 'Bingbot', 'YandexBot', 'Baiduspider', 'DuckDuckBot', 'Slurp', 'Yeti/', 'YisouSpider', 'Sogou web spider', 'pageburst', '360Spider', 'AdsBot-Google', 'Google-InspectionTool', 'Google-Food', 'AhrefsSiteAudit', 'AhrefsBot', 'Barkrowler', 'SemrushBot', 'SERankingBacklinksBot', 'MJ12bot', 'DotBot', 'Lighthouse', 'MeltwaterNews', 'FacebookBot', 'facebookexternalhit', 'Twitterbot', 'LinkedInBot', 'Pinterest', 'Slackbot', 'Slack-ImgProxy', 'TelegramBot', 'WhatsApp', 'GoogleImageProxy', 'Iframely', 'Pingdom', 'UptimeRobot', 'Site24x7', 'StatusCake', 'Datadog', 'GrafanaSyntheticMonitoring', 'DMBrowser', 'DigitalOcean Uptime Probe', 'curl/', 'Wget', 'python-requests', 'axios', 'node-fetch', 'Go-http-client', 'okhttp', 'Apache-HttpClient', 'libwww-perl', 'Scrapy', 'PlayStore-Google', 'Amazon CloudFront', 'AmazonProductDiscovery', 'Google-BusinessLinkVerification', 'Storebot-Google', 'Google-Read-Aloud', 'Google-Appointments', 'Google-Actions', 'OneTrust', 'Chrome Privacy Preserving Prefetch Proxy', 'Mozlila/', 'HeadlessChrome', 'PhantomJS', 'Puppeteer', 'Playwright', 'Selenium', '^$']), 0) AS is_bot,
- count() AS count
+ notEquals(multiMatchAnyIndex(ifNull(properties.$user_agent, ''), ['GPTBot', 'Google-CloudVertexBot', 'Google-Extended', 'GoogleOther', 'Claude-SearchBot', 'Claude-User', 'ClaudeBot', 'Claude-Web', 'anthropic-ai', 'Perplexity-User', 'PerplexityBot', 'CCBot', 'meta-externalagent', 'Bytespider', 'TikTokSpider', 'cohere-ai', 'Diffbot', 'omgili', 'Webzio-Extended', 'Timpibot', 'Amazonbot', 'PetalBot', 'Brightbot', 'OAI-SearchBot', 'Applebot-Extended', 'ChatGPT-User', 'meta-externalfetcher', 'Meta-ExternalFetcher', 'DuckAssistBot', 'MistralAI-User', 'Manus-User', 'Google-NotebookLM', 'Shap-User', 'desktop\\.hog\\.dev', 'mobile\\.hog\\.dev', 'agent\\.hog\\.dev', 'cloud\\.hog\\.dev', 'Applebot/', 'Googlebot', 'bingbot', 'Bingbot', 'YandexBot', 'Baiduspider', 'DuckDuckBot', 'Slurp', 'Yeti/', 'YisouSpider', 'Sogou web spider', 'pageburst', '360Spider', 'AdsBot-Google', 'Google-InspectionTool', 'Google-Food', 'AhrefsSiteAudit', 'AhrefsBot', 'Barkrowler', 'SemrushBot', 'SERankingBacklinksBot', 'MJ12bot', 'DotBot', 'Lighthouse', 'MeltwaterNews', 'FacebookBot', 'facebookexternalhit', 'Twitterbot', 'LinkedInBot', 'Pinterest', 'Slackbot', 'Slack-ImgProxy', 'TelegramBot', 'WhatsApp', 'GoogleImageProxy', 'Iframely', 'Pingdom', 'UptimeRobot', 'Site24x7', 'StatusCake', 'Datadog', 'GrafanaSyntheticMonitoring', 'DMBrowser', 'DigitalOcean Uptime Probe', 'curl/', 'Wget', 'python-requests', 'axios', 'node-fetch', 'Go-http-client', 'okhttp', 'Apache-HttpClient', 'libwww-perl', 'Scrapy', 'PlayStore-Google', 'Amazon CloudFront', 'AmazonProductDiscovery', 'Google-BusinessLinkVerification', 'Storebot-Google', 'Google-Read-Aloud', 'Google-Appointments', 'Google-Actions', 'OneTrust', 'Chrome Privacy Preserving Prefetch Proxy', 'Mozlila/', 'HeadlessChrome', 'PhantomJS', 'Puppeteer', 'Playwright', 'Selenium', '^$']), 0) AS is_bot,
+ count() AS count
FROM
- events
+ events
WHERE
- equals(event, '$pageview')
+ equals(event, '$pageview')
GROUP BY
- is_bot
+ is_bot
ORDER BY
- is_bot ASC
+ is_bot ASC
LIMIT 100
'''
# ---