: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(marketing-analytics): reusable touchpoint precompute (#60726)
Javier Bahamondes3761d37e6a3b
diff --git a/posthog/clickhouse/migrations/0271_marketing_touchpoints_preaggregated.py b/posthog/clickhouse/migrations/0271_marketing_touchpoints_preaggregated.pynew file mode 100644index 000000000000..8718c9701956--- /dev/null+++ b/posthog/clickhouse/migrations/0271_marketing_touchpoints_preaggregated.py@@ -0,0 +1,17 @@+from posthog.clickhouse.client.connection import NodeRole+from posthog.clickhouse.client.migration_tools import run_sql_with_exceptions+from posthog.clickhouse.preaggregation.marketing_touchpoints_sql import (+ DISTRIBUTED_MARKETING_TOUCHPOINTS_TABLE_SQL,+ SHARDED_MARKETING_TOUCHPOINTS_TABLE_SQL,+)++operations = [+ run_sql_with_exceptions(+ SHARDED_MARKETING_TOUCHPOINTS_TABLE_SQL(),+ node_roles=[NodeRole.AUX],+ ),+ run_sql_with_exceptions(+ DISTRIBUTED_MARKETING_TOUCHPOINTS_TABLE_SQL(),+ node_roles=[NodeRole.AUX, NodeRole.DATA],+ ),+]diff --git a/posthog/clickhouse/migrations/max_migration.txt b/posthog/clickhouse/migrations/max_migration.txtindex c36dd7acdc01..21d6967d8d1f 100644--- a/posthog/clickhouse/migrations/max_migration.txt+++ b/posthog/clickhouse/migrations/max_migration.txt@@ -1 +1 @@-0270_property_values_drop_mv_length_filter+0271_marketing_touchpoints_preaggregateddiff --git a/posthog/clickhouse/preaggregation/marketing_touchpoints_sql.py b/posthog/clickhouse/preaggregation/marketing_touchpoints_sql.pynew file mode 100644index 000000000000..ee22837f2b79--- /dev/null+++ b/posthog/clickhouse/preaggregation/marketing_touchpoints_sql.py@@ -0,0 +1,89 @@+# Reusable, attribution-config-agnostic touchpoint precompute: one row per UTM-tagged+# pageview (team, person, timestamp + the tracked UTM dimensions). Unlike+# conversion_goal_attributed_preaggregated (which caches per-goal attribution RESULTS,+# keyed by job_id = goal+mode+window), this caches the raw touchpoints — the same job is+# shared across every attribution mode, window and goal, which all attribute at read time.++from django.conf import settings++from posthog.clickhouse.preaggregation.conversion_goal_attributed_sql import (+ CONVERSION_GOAL_ATTRIBUTED_TRACKED_FIELD_NAMES,+)+from posthog.clickhouse.table_engines import Distributed, ReplacingMergeTree++TABLE_BASE_NAME = "marketing_touchpoints_preaggregated"++# Shared with the attributed table so both stay in lockstep with TRACKED_FIELDS.+MARKETING_TOUCHPOINTS_TRACKED_FIELD_NAMES: list[str] = CONVERSION_GOAL_ATTRIBUTED_TRACKED_FIELD_NAMES+++def DISTRIBUTED_MARKETING_TOUCHPOINTS_TABLE():+ return TABLE_BASE_NAME+++def SHARDED_MARKETING_TOUCHPOINTS_TABLE():+ return f"sharded_{TABLE_BASE_NAME}"+++def SHARDED_MARKETING_TOUCHPOINTS_TABLE_ENGINE():+ return ReplacingMergeTree(TABLE_BASE_NAME, ver="computed_at")+++def _touchpoint_field_columns() -> str:+ """Render one ``{name}_name String,`` per tracked field."""+ return "\n ".join(f"{name}_name String," for name in MARKETING_TOUCHPOINTS_TRACKED_FIELD_NAMES)+++MARKETING_TOUCHPOINTS_TABLE_BASE_SQL = f"""+CREATE TABLE IF NOT EXISTS {{table_name}}+(+ team_id Int64,+ job_id UUID,++ person_id UUID,+ touchpoint_timestamp DateTime64(6, 'UTC'),++ {_touchpoint_field_columns()}++ computed_at DateTime64(6, 'UTC') DEFAULT now(),+ expires_at Date DEFAULT today() + INTERVAL 7 DAY+) ENGINE = {{engine}}+"""+++def SHARDED_MARKETING_TOUCHPOINTS_TABLE_SQL():+ return (+ MARKETING_TOUCHPOINTS_TABLE_BASE_SQL+ + """+PARTITION BY toYYYYMMDD(expires_at)+ORDER BY (team_id, job_id, person_id, touchpoint_timestamp)+TTL expires_at+SETTINGS index_granularity=8192, ttl_only_drop_parts = 1+"""+ ).format(+ table_name=SHARDED_MARKETING_TOUCHPOINTS_TABLE(),+ engine=SHARDED_MARKETING_TOUCHPOINTS_TABLE_ENGINE(),+ )+++def DISTRIBUTED_MARKETING_TOUCHPOINTS_TABLE_SQL():+ return MARKETING_TOUCHPOINTS_TABLE_BASE_SQL.format(+ table_name=DISTRIBUTED_MARKETING_TOUCHPOINTS_TABLE(),+ engine=Distributed(+ data_table=SHARDED_MARKETING_TOUCHPOINTS_TABLE(),+ sharding_key="cityHash64(person_id)",+ cluster=settings.CLICKHOUSE_AUX_CLUSTER,+ ),+ )+++def DROP_MARKETING_TOUCHPOINTS_TABLE_SQL():+ return f"DROP TABLE IF EXISTS {DISTRIBUTED_MARKETING_TOUCHPOINTS_TABLE()}"+++def DROP_SHARDED_MARKETING_TOUCHPOINTS_TABLE_SQL():+ return f"DROP TABLE IF EXISTS {SHARDED_MARKETING_TOUCHPOINTS_TABLE()} SYNC"+++def TRUNCATE_MARKETING_TOUCHPOINTS_TABLE_SQL():+ return f"TRUNCATE TABLE IF EXISTS {SHARDED_MARKETING_TOUCHPOINTS_TABLE()}"diff --git a/posthog/clickhouse/schema.py b/posthog/clickhouse/schema.pyindex cd9976a54f73..496b64b00d15 100644--- a/posthog/clickhouse/schema.py+++ b/posthog/clickhouse/schema.py@@ -45,6 +45,10 @@ from posthog.clickhouse.preaggregation.experiment_metric_events_sql import (DISTRIBUTED_EXPERIMENT_METRIC_EVENTS_TABLE_SQL,SHARDED_EXPERIMENT_METRIC_EVENTS_TABLE_SQL,)+from posthog.clickhouse.preaggregation.marketing_touchpoints_sql import (+ DISTRIBUTED_MARKETING_TOUCHPOINTS_TABLE_SQL,+ SHARDED_MARKETING_TOUCHPOINTS_TABLE_SQL,+)from posthog.clickhouse.preaggregation.sql import (DISTRIBUTED_PREAGGREGATION_RESULTS_TABLE_SQL,SHARDED_PREAGGREGATION_RESULTS_TABLE_SQL,@@ -315,6 +319,7 @@ CREATE_MERGETREE_TABLE_QUERIES = (SHARDED_EXPERIMENT_EXPOSURES_TABLE_SQL,SHARDED_EXPERIMENT_METRIC_EVENTS_TABLE_SQL,SHARDED_CONVERSION_GOAL_ATTRIBUTED_TABLE_SQL,+ SHARDED_MARKETING_TOUCHPOINTS_TABLE_SQL,SHARDED_WEB_OVERVIEW_PREAGGREGATED_TABLE_SQL,SHARDED_WEB_STATS_PREAGGREGATED_TABLE_SQL,SHARDED_WEB_VITALS_PATHS_PREAGGREGATED_TABLE_SQL,@@ -375,6 +380,7 @@ CREATE_DISTRIBUTED_TABLE_QUERIES = (DISTRIBUTED_EXPERIMENT_EXPOSURES_TABLE_SQL,DISTRIBUTED_EXPERIMENT_METRIC_EVENTS_TABLE_SQL,DISTRIBUTED_CONVERSION_GOAL_ATTRIBUTED_TABLE_SQL,+ DISTRIBUTED_MARKETING_TOUCHPOINTS_TABLE_SQL,DISTRIBUTED_WEB_OVERVIEW_PREAGGREGATED_TABLE_SQL,DISTRIBUTED_WEB_STATS_PREAGGREGATED_TABLE_SQL,DISTRIBUTED_WEB_VITALS_PATHS_PREAGGREGATED_TABLE_SQL,diff --git a/posthog/clickhouse/test/__snapshots__/test_schema.ambr b/posthog/clickhouse/test/__snapshots__/test_schema.ambrindex ef0d754eeaf7..e0abbf4cb92a 100644--- a/posthog/clickhouse/test/__snapshots__/test_schema.ambr+++ b/posthog/clickhouse/test/__snapshots__/test_schema.ambr@@ -2886,6 +2886,33 @@'''# ---+# name: test_create_table_query[marketing_touchpoints_preaggregated]+ '''++ CREATE TABLE IF NOT EXISTS marketing_touchpoints_preaggregated+ (+ team_id Int64,+ job_id UUID,++ person_id UUID,+ touchpoint_timestamp DateTime64(6, 'UTC'),++ campaign_name String,+ source_name String,+ medium_name String,+ content_name String,+ term_name String,+ referring_domain_name String,+ gclid_name String,+ fbclid_name String,+ gad_source_name String,++ computed_at DateTime64(6, 'UTC') DEFAULT now(),+ expires_at Date DEFAULT today() + INTERVAL 7 DAY+ ) ENGINE = Distributed('aux', 'posthog_test', 'sharded_marketing_touchpoints_preaggregated', cityHash64(person_id))++ '''+# ---# name: test_create_table_query[metric_attributes]'''@@ -5539,6 +5566,38 @@'''# ---+# name: test_create_table_query[sharded_marketing_touchpoints_preaggregated]+ '''++ CREATE TABLE IF NOT EXISTS sharded_marketing_touchpoints_preaggregated+ (+ team_id Int64,+ job_id UUID,++ person_id UUID,+ touchpoint_timestamp DateTime64(6, 'UTC'),++ campaign_name String,+ source_name String,+ medium_name String,+ content_name String,+ term_name String,+ referring_domain_name String,+ gclid_name String,+ fbclid_name String,+ gad_source_name String,++ computed_at DateTime64(6, 'UTC') DEFAULT now(),+ expires_at Date DEFAULT today() + INTERVAL 7 DAY+ ) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_noshard/posthog.marketing_touchpoints_preaggregated', '{replica}-{shard}', computed_at)++ PARTITION BY toYYYYMMDD(expires_at)+ ORDER BY (team_id, job_id, person_id, touchpoint_timestamp)+ TTL expires_at+ SETTINGS index_granularity=8192, ttl_only_drop_parts = 1++ '''+# ---# name: test_create_table_query[sharded_performance_events]'''@@ -9019,6 +9078,38 @@'''# ---+# name: test_create_table_query_replicated_and_storage[sharded_marketing_touchpoints_preaggregated]+ '''++ CREATE TABLE IF NOT EXISTS sharded_marketing_touchpoints_preaggregated+ (+ team_id Int64,+ job_id UUID,++ person_id UUID,+ touchpoint_timestamp DateTime64(6, 'UTC'),++ campaign_name String,+ source_name String,+ medium_name String,+ content_name String,+ term_name String,+ referring_domain_name String,+ gclid_name String,+ fbclid_name String,+ gad_source_name String,++ computed_at DateTime64(6, 'UTC') DEFAULT now(),+ expires_at Date DEFAULT today() + INTERVAL 7 DAY+ ) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_noshard/posthog.marketing_touchpoints_preaggregated', '{replica}-{shard}', computed_at)++ PARTITION BY toYYYYMMDD(expires_at)+ ORDER BY (team_id, job_id, person_id, touchpoint_timestamp)+ TTL expires_at+ SETTINGS index_granularity=8192, ttl_only_drop_parts = 1++ '''+# ---# name: test_create_table_query_replicated_and_storage[sharded_performance_events]'''diff --git a/posthog/hogql/database/database.py b/posthog/hogql/database/database.pyindex 43a1c8adfbac..c1d1383a925e 100644--- a/posthog/hogql/database/database.py+++ b/posthog/hogql/database/database.py@@ -100,6 +100,7 @@ from posthog.hogql.database.schema.log_entries import (ReplayConsoleLogsLogEntriesTable,)from posthog.hogql.database.schema.logs import LogAttributesTable, LogsKafkaMetricsTable, LogsTable+from posthog.hogql.database.schema.marketing_touchpoints_preaggregated import MarketingTouchpointsPreaggregatedTablefrom posthog.hogql.database.schema.metrics import MetricAttributesTable, MetricsKafkaMetricsTable, MetricsTablefrom posthog.hogql.database.schema.numbers import NumbersTablefrom posthog.hogql.database.schema.person_distinct_id_overrides import (@@ -310,6 +311,10 @@ def build_database_root_node(*, include_posthog_tables: bool = True) -> TableNodname="conversion_goal_attributed_preaggregated",table=ConversionGoalAttributedPreaggregatedTable(),),+ "marketing_touchpoints_preaggregated": TableNode(+ name="marketing_touchpoints_preaggregated",+ table=MarketingTouchpointsPreaggregatedTable(),+ ),"web_stats_paths_preaggregated": TableNode(name="web_stats_paths_preaggregated", table=WebStatsPathsPreaggregatedTable()),diff --git a/posthog/hogql/database/schema/marketing_touchpoints_preaggregated.py b/posthog/hogql/database/schema/marketing_touchpoints_preaggregated.pynew file mode 100644index 000000000000..cadc73578ff9--- /dev/null+++ b/posthog/hogql/database/schema/marketing_touchpoints_preaggregated.py@@ -0,0 +1,45 @@+from pydantic import Field++from posthog.hogql.constants import HogQLQuerySettings+from posthog.hogql.database.models import (+ DateDatabaseField,+ DateTimeDatabaseField,+ FieldOrTable,+ IntegerDatabaseField,+ StringDatabaseField,+ Table,+)++from posthog.clickhouse.preaggregation.marketing_touchpoints_sql import (+ DISTRIBUTED_MARKETING_TOUCHPOINTS_TABLE,+ MARKETING_TOUCHPOINTS_TRACKED_FIELD_NAMES,+)+++def _build_fields() -> dict[str, FieldOrTable]:+ fields: dict[str, FieldOrTable] = {+ "team_id": IntegerDatabaseField(name="team_id"),+ "job_id": StringDatabaseField(name="job_id"),+ "person_id": StringDatabaseField(name="person_id"),+ "touchpoint_timestamp": DateTimeDatabaseField(name="touchpoint_timestamp"),+ }+ for tracked_name in MARKETING_TOUCHPOINTS_TRACKED_FIELD_NAMES:+ col = f"{tracked_name}_name"+ fields[col] = StringDatabaseField(name=col)+ fields["computed_at"] = DateTimeDatabaseField(name="computed_at")+ fields["expires_at"] = DateDatabaseField(name="expires_at")+ return fields+++class MarketingTouchpointsPreaggregatedTable(Table):+ top_level_settings: HogQLQuerySettings | None = Field(+ default_factory=lambda: HogQLQuerySettings(load_balancing="in_order")+ )++ fields: dict[str, FieldOrTable] = _build_fields()++ def to_printed_clickhouse(self, context):+ return DISTRIBUTED_MARKETING_TOUCHPOINTS_TABLE()++ def to_printed_hogql(self):+ return "marketing_touchpoints_preaggregated"diff --git a/products/analytics_platform/backend/lazy_computation/lazy_computation_executor.py b/products/analytics_platform/backend/lazy_computation/lazy_computation_executor.pyindex 000ef61959a5..3816e5052a43 100644--- a/products/analytics_platform/backend/lazy_computation/lazy_computation_executor.py+++ b/products/analytics_platform/backend/lazy_computation/lazy_computation_executor.py@@ -313,6 +313,7 @@ class LazyComputationTable(StrEnum):EXPERIMENT_EXPOSURES_PREAGGREGATED = "experiment_exposures_preaggregated"EXPERIMENT_METRIC_EVENTS_PREAGGREGATED = "experiment_metric_events_preaggregated"CONVERSION_GOAL_ATTRIBUTED_PREAGGREGATED = "conversion_goal_attributed_preaggregated"+ MARKETING_TOUCHPOINTS_PREAGGREGATED = "marketing_touchpoints_preaggregated"WEB_OVERVIEW_PREAGGREGATED = "web_overview_preaggregated"WEB_STATS_PREAGGREGATED = "web_stats_preaggregated"WEB_STATS_PATHS_PREAGGREGATED = "web_stats_paths_preaggregated"@@ -328,6 +329,7 @@ _DATE_EXPIRES_AT_TABLES: set[LazyComputationTable] = {LazyComputationTable.EXPERIMENT_EXPOSURES_PREAGGREGATED,LazyComputationTable.EXPERIMENT_METRIC_EVENTS_PREAGGREGATED,LazyComputationTable.CONVERSION_GOAL_ATTRIBUTED_PREAGGREGATED,+ LazyComputationTable.MARKETING_TOUCHPOINTS_PREAGGREGATED,}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.ambrindex 68c51e6041e6..e974f5633497 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@@ -19,21 +19,110 @@(SELECTperson_id,- campaign_name,- source_name,- medium_name,- content_name,- term_name,- referring_domain_name,- gclid_name,- fbclid_name,- gad_source_name,+ 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(conversion_value, touchpoint_weight) AS conversion_value+ 1 AS conversion_valueFROM- 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+ (+ 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+ c.person_id AS person_id,+ c.conversion_timestamps AS conversion_timestamps,+ c.conversion_math_values AS conversion_math_values,+ c.conversion_campaigns AS conversion_campaigns,+ c.conversion_sources AS conversion_sources,+ c.conversion_mediums AS conversion_mediums,+ c.conversion_contents AS conversion_contents,+ c.conversion_terms AS conversion_terms,+ c.conversion_referring_domains AS conversion_referring_domains,+ c.conversion_gclids AS conversion_gclids,+ c.conversion_fbclids AS conversion_fbclids,+ c.conversion_gad_sources AS conversion_gad_sources,+ t.utm_timestamps AS utm_timestamps,+ t.utm_campaigns AS utm_campaigns,+ t.utm_sources AS utm_sources,+ t.utm_mediums AS utm_mediums,+ t.utm_contents AS utm_contents,+ t.utm_terms AS utm_terms,+ t.utm_referring_domains AS utm_referring_domains,+ t.utm_gclids AS utm_gclids,+ t.utm_fbclids AS utm_fbclids,+ t.utm_gad_sources 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+ FROM+ events+ WHERE+ and(equals(events.event, 'purchase'), greaterOrEquals(events.timestamp, toDateTime('2023-01-01 00:00:00.000000')), lessOrEquals(events.timestamp, toDateTime('2023-01-31 23:59:59.999999')))+ GROUP BY+ events.person_id+ HAVING+ greater(length(conversion_timestamps), 0)) AS c+ LEFT JOIN (+ SELECT+ person_id,+ groupArray(toUnixTimestamp(touchpoint_timestamp)) AS utm_timestamps,+ arrayFilter(x -> notEmpty(x), groupArray(campaign_name)) AS utm_campaigns,+ arrayFilter(x -> notEmpty(x), groupArray(source_name)) AS utm_sources,+ arrayFilter(x -> notEmpty(x), groupArray(medium_name)) AS utm_mediums,+ arrayFilter(x -> notEmpty(x), groupArray(content_name)) AS utm_contents,+ arrayFilter(x -> notEmpty(x), groupArray(term_name)) AS utm_terms,+ arrayFilter(x -> notEmpty(x), groupArray(referring_domain_name)) AS utm_referring_domains,+ arrayFilter(x -> notEmpty(x), groupArray(gclid_name)) AS utm_gclids,+ arrayFilter(x -> notEmpty(x), groupArray(fbclid_name)) AS utm_fbclids,+ arrayFilter(x -> notEmpty(x), groupArray(gad_source_name)) AS utm_gad_sources+ FROM+ marketing_touchpoints_preaggregated+ WHERE+ and(in(job_id, tuple('00000000-0000-0000-0000-000000000001')), equals(team_id, 420))+ GROUP BY+ person_id) AS t ON equals(c.person_id, t.person_id))+ ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversionsGROUP BYcampaign,id,@@ -65,21 +154,110 @@(SELECTperson_id,- campaign_name,- source_name,- medium_name,- content_name,- term_name,- referring_domain_name,- gclid_name,- fbclid_name,- gad_source_name,+ 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(conversion_value, touchpoint_weight) AS conversion_value+ 1 AS conversion_valueFROM- 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+ (+ 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+ c.person_id AS person_id,+ c.conversion_timestamps AS conversion_timestamps,+ c.conversion_math_values AS conversion_math_values,+ c.conversion_campaigns AS conversion_campaigns,+ c.conversion_sources AS conversion_sources,+ c.conversion_mediums AS conversion_mediums,+ c.conversion_contents AS conversion_contents,+ c.conversion_terms AS conversion_terms,+ c.conversion_referring_domains AS conversion_referring_domains,+ c.conversion_gclids AS conversion_gclids,+ c.conversion_fbclids AS conversion_fbclids,+ c.conversion_gad_sources AS conversion_gad_sources,+ t.utm_timestamps AS utm_timestamps,+ t.utm_campaigns AS utm_campaigns,+ t.utm_sources AS utm_sources,+ t.utm_mediums AS utm_mediums,+ t.utm_contents AS utm_contents,+ t.utm_terms AS utm_terms,+ t.utm_referring_domains AS utm_referring_domains,+ t.utm_gclids AS utm_gclids,+ t.utm_fbclids AS utm_fbclids,+ t.utm_gad_sources 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+ FROM+ events+ WHERE+ and(equals(events.event, 'purchase'), greaterOrEquals(events.timestamp, toDateTime('2023-01-01 00:00:00.000000')), lessOrEquals(events.timestamp, toDateTime('2023-01-31 23:59:59.999999')))+ GROUP BY+ events.person_id+ HAVING+ greater(length(conversion_timestamps), 0)) AS c+ LEFT JOIN (+ SELECT+ person_id,+ groupArray(toUnixTimestamp(touchpoint_timestamp)) AS utm_timestamps,+ arrayFilter(x -> notEmpty(x), groupArray(campaign_name)) AS utm_campaigns,+ arrayFilter(x -> notEmpty(x), groupArray(source_name)) AS utm_sources,+ arrayFilter(x -> notEmpty(x), groupArray(medium_name)) AS utm_mediums,+ arrayFilter(x -> notEmpty(x), groupArray(content_name)) AS utm_contents,+ arrayFilter(x -> notEmpty(x), groupArray(term_name)) AS utm_terms,+ arrayFilter(x -> notEmpty(x), groupArray(referring_domain_name)) AS utm_referring_domains,+ arrayFilter(x -> notEmpty(x), groupArray(gclid_name)) AS utm_gclids,+ arrayFilter(x -> notEmpty(x), groupArray(fbclid_name)) AS utm_fbclids,+ arrayFilter(x -> notEmpty(x), groupArray(gad_source_name)) AS utm_gad_sources+ FROM+ marketing_touchpoints_preaggregated+ WHERE+ and(in(job_id, tuple('00000000-0000-0000-0000-000000000001')), equals(team_id, 420))+ GROUP BY+ person_id) AS t ON equals(c.person_id, t.person_id))+ ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversionsGROUP BYcampaign,id,@@ -111,21 +289,110 @@(SELECTperson_id,- campaign_name,- source_name,- medium_name,- content_name,- term_name,- referring_domain_name,- gclid_name,- fbclid_name,- gad_source_name,+ 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(conversion_value, touchpoint_weight) AS conversion_value+ 1 AS conversion_valueFROM- 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+ (+ 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+ c.person_id AS person_id,+ c.conversion_timestamps AS conversion_timestamps,+ c.conversion_math_values AS conversion_math_values,+ c.conversion_campaigns AS conversion_campaigns,+ c.conversion_sources AS conversion_sources,+ c.conversion_mediums AS conversion_mediums,+ c.conversion_contents AS conversion_contents,+ c.conversion_terms AS conversion_terms,+ c.conversion_referring_domains AS conversion_referring_domains,+ c.conversion_gclids AS conversion_gclids,+ c.conversion_fbclids AS conversion_fbclids,+ c.conversion_gad_sources AS conversion_gad_sources,+ t.utm_timestamps AS utm_timestamps,+ t.utm_campaigns AS utm_campaigns,+ t.utm_sources AS utm_sources,+ t.utm_mediums AS utm_mediums,+ t.utm_contents AS utm_contents,+ t.utm_terms AS utm_terms,+ t.utm_referring_domains AS utm_referring_domains,+ t.utm_gclids AS utm_gclids,+ t.utm_fbclids AS utm_fbclids,+ t.utm_gad_sources AS utm_gad_sources+ FROM+ (+ SELECT+ events.person_id,+ arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'conversion'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps,+ arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'conversion'), toFloat(1), 0))) AS conversion_math_values,+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'conversion'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns,+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'conversion'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources,+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'conversion'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums,+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'conversion'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents,+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'conversion'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms,+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'conversion'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains,+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'conversion'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids,+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'conversion'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids,+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'conversion'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources+ FROM+ events+ WHERE+ and(equals(events.event, 'conversion'), greaterOrEquals(events.timestamp, toDateTime('2023-01-01 00:00:00.000000')), lessOrEquals(events.timestamp, toDateTime('2023-01-31 23:59:59.999999')))+ GROUP BY+ events.person_id+ HAVING+ greater(length(conversion_timestamps), 0)) AS c+ LEFT JOIN (+ SELECT+ person_id,+ groupArray(toUnixTimestamp(touchpoint_timestamp)) AS utm_timestamps,+ arrayFilter(x -> notEmpty(x), groupArray(campaign_name)) AS utm_campaigns,+ arrayFilter(x -> notEmpty(x), groupArray(source_name)) AS utm_sources,+ arrayFilter(x -> notEmpty(x), groupArray(medium_name)) AS utm_mediums,+ arrayFilter(x -> notEmpty(x), groupArray(content_name)) AS utm_contents,+ arrayFilter(x -> notEmpty(x), groupArray(term_name)) AS utm_terms,+ arrayFilter(x -> notEmpty(x), groupArray(referring_domain_name)) AS utm_referring_domains,+ arrayFilter(x -> notEmpty(x), groupArray(gclid_name)) AS utm_gclids,+ arrayFilter(x -> notEmpty(x), groupArray(fbclid_name)) AS utm_fbclids,+ arrayFilter(x -> notEmpty(x), groupArray(gad_source_name)) AS utm_gad_sources+ FROM+ marketing_touchpoints_preaggregated+ WHERE+ and(in(job_id, tuple('00000000-0000-0000-0000-000000000001')), equals(team_id, 420))+ GROUP BY+ person_id) AS t ON equals(c.person_id, t.person_id))+ ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversionsGROUP BYcampaign,id,@@ -157,21 +424,110 @@(SELECTperson_id,- campaign_name,- source_name,- medium_name,- content_name,- term_name,- referring_domain_name,- gclid_name,- fbclid_name,- gad_source_name,+ 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(conversion_value, touchpoint_weight) AS conversion_value+ 1 AS conversion_valueFROM- 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+ (+ 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+ c.person_id AS person_id,+ c.conversion_timestamps AS conversion_timestamps,+ c.conversion_math_values AS conversion_math_values,+ c.conversion_campaigns AS conversion_campaigns,+ c.conversion_sources AS conversion_sources,+ c.conversion_mediums AS conversion_mediums,+ c.conversion_contents AS conversion_contents,+ c.conversion_terms AS conversion_terms,+ c.conversion_referring_domains AS conversion_referring_domains,+ c.conversion_gclids AS conversion_gclids,+ c.conversion_fbclids AS conversion_fbclids,+ c.conversion_gad_sources AS conversion_gad_sources,+ t.utm_timestamps AS utm_timestamps,+ t.utm_campaigns AS utm_campaigns,+ t.utm_sources AS utm_sources,+ t.utm_mediums AS utm_mediums,+ t.utm_contents AS utm_contents,+ t.utm_terms AS utm_terms,+ t.utm_referring_domains AS utm_referring_domains,+ t.utm_gclids AS utm_gclids,+ t.utm_fbclids AS utm_fbclids,+ t.utm_gad_sources 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+ FROM+ events+ WHERE+ and(equals(events.event, 'purchase'), greaterOrEquals(events.timestamp, toDateTime('2023-01-01 00:00:00.000000')), lessOrEquals(events.timestamp, toDateTime('2023-01-31 23:59:59.999999')))+ GROUP BY+ events.person_id+ HAVING+ greater(length(conversion_timestamps), 0)) AS c+ LEFT JOIN (+ SELECT+ person_id,+ groupArray(toUnixTimestamp(touchpoint_timestamp)) AS utm_timestamps,+ arrayFilter(x -> notEmpty(x), groupArray(campaign_name)) AS utm_campaigns,+ arrayFilter(x -> notEmpty(x), groupArray(source_name)) AS utm_sources,+ arrayFilter(x -> notEmpty(x), groupArray(medium_name)) AS utm_mediums,+ arrayFilter(x -> notEmpty(x), groupArray(content_name)) AS utm_contents,+ arrayFilter(x -> notEmpty(x), groupArray(term_name)) AS utm_terms,+ arrayFilter(x -> notEmpty(x), groupArray(referring_domain_name)) AS utm_referring_domains,+ arrayFilter(x -> notEmpty(x), groupArray(gclid_name)) AS utm_gclids,+ arrayFilter(x -> notEmpty(x), groupArray(fbclid_name)) AS utm_fbclids,+ arrayFilter(x -> notEmpty(x), groupArray(gad_source_name)) AS utm_gad_sources+ FROM+ marketing_touchpoints_preaggregated+ WHERE+ and(in(job_id, tuple('00000000-0000-0000-0000-000000000001')), equals(team_id, 420))+ GROUP BY+ person_id) AS t ON equals(c.person_id, t.person_id))+ ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversionsGROUP BYcampaign,id,@@ -207,21 +563,110 @@(SELECTperson_id,- campaign_name,- source_name,- medium_name,- content_name,- term_name,- referring_domain_name,- gclid_name,- fbclid_name,- gad_source_name,+ 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(conversion_value, touchpoint_weight) AS conversion_value+ 1 AS conversion_valueFROM- 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+ (+ 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+ c.person_id AS person_id,+ c.conversion_timestamps AS conversion_timestamps,+ c.conversion_math_values AS conversion_math_values,+ c.conversion_campaigns AS conversion_campaigns,+ c.conversion_sources AS conversion_sources,+ c.conversion_mediums AS conversion_mediums,+ c.conversion_contents AS conversion_contents,+ c.conversion_terms AS conversion_terms,+ c.conversion_referring_domains AS conversion_referring_domains,+ c.conversion_gclids AS conversion_gclids,+ c.conversion_fbclids AS conversion_fbclids,+ c.conversion_gad_sources AS conversion_gad_sources,+ t.utm_timestamps AS utm_timestamps,+ t.utm_campaigns AS utm_campaigns,+ t.utm_sources AS utm_sources,+ t.utm_mediums AS utm_mediums,+ t.utm_contents AS utm_contents,+ t.utm_terms AS utm_terms,+ t.utm_referring_domains AS utm_referring_domains,+ t.utm_gclids AS utm_gclids,+ t.utm_fbclids AS utm_fbclids,+ t.utm_gad_sources 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+ FROM+ events+ WHERE+ and(equals(events.event, 'purchase'), greaterOrEquals(events.timestamp, toDateTime('2023-01-01 00:00:00.000000')), lessOrEquals(events.timestamp, toDateTime('2023-01-31 23:59:59.999999')))+ GROUP BY+ events.person_id+ HAVING+ greater(length(conversion_timestamps), 0)) AS c+ LEFT JOIN (+ SELECT+ person_id,+ groupArray(toUnixTimestamp(touchpoint_timestamp)) AS utm_timestamps,+ arrayFilter(x -> notEmpty(x), groupArray(campaign_name)) AS utm_campaigns,+ arrayFilter(x -> notEmpty(x), groupArray(source_name)) AS utm_sources,+ arrayFilter(x -> notEmpty(x), groupArray(medium_name)) AS utm_mediums,+ arrayFilter(x -> notEmpty(x), groupArray(content_name)) AS utm_contents,+ arrayFilter(x -> notEmpty(x), groupArray(term_name)) AS utm_terms,+ arrayFilter(x -> notEmpty(x), groupArray(referring_domain_name)) AS utm_referring_domains,+ arrayFilter(x -> notEmpty(x), groupArray(gclid_name)) AS utm_gclids,+ arrayFilter(x -> notEmpty(x), groupArray(fbclid_name)) AS utm_fbclids,+ arrayFilter(x -> notEmpty(x), groupArray(gad_source_name)) AS utm_gad_sources+ FROM+ marketing_touchpoints_preaggregated+ WHERE+ and(in(job_id, tuple('00000000-0000-0000-0000-000000000001')), equals(team_id, 420))+ GROUP BY+ person_id) AS t ON equals(c.person_id, t.person_id))+ ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversionsGROUP BYcampaign,id,@@ -238,21 +683,110 @@(SELECTperson_id,- campaign_name,- source_name,- medium_name,- content_name,- term_name,- referring_domain_name,- gclid_name,- fbclid_name,- gad_source_name,+ 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(conversion_value, touchpoint_weight) AS conversion_value+ 1 AS conversion_valueFROM- 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+ (+ 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+ c.person_id AS person_id,+ c.conversion_timestamps AS conversion_timestamps,+ c.conversion_math_values AS conversion_math_values,+ c.conversion_campaigns AS conversion_campaigns,+ c.conversion_sources AS conversion_sources,+ c.conversion_mediums AS conversion_mediums,+ c.conversion_contents AS conversion_contents,+ c.conversion_terms AS conversion_terms,+ c.conversion_referring_domains AS conversion_referring_domains,+ c.conversion_gclids AS conversion_gclids,+ c.conversion_fbclids AS conversion_fbclids,+ c.conversion_gad_sources AS conversion_gad_sources,+ t.utm_timestamps AS utm_timestamps,+ t.utm_campaigns AS utm_campaigns,+ t.utm_sources AS utm_sources,+ t.utm_mediums AS utm_mediums,+ t.utm_contents AS utm_contents,+ t.utm_terms AS utm_terms,+ t.utm_referring_domains AS utm_referring_domains,+ t.utm_gclids AS utm_gclids,+ t.utm_fbclids AS utm_fbclids,+ t.utm_gad_sources AS utm_gad_sources+ 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+ FROM+ events+ WHERE+ and(equals(events.event, 'sign_up'), greaterOrEquals(events.timestamp, toDateTime('2023-01-01 00:00:00.000000')), lessOrEquals(events.timestamp, toDateTime('2023-01-31 23:59:59.999999')))+ GROUP BY+ events.person_id+ HAVING+ greater(length(conversion_timestamps), 0)) AS c+ LEFT JOIN (+ SELECT+ person_id,+ groupArray(toUnixTimestamp(touchpoint_timestamp)) AS utm_timestamps,+ arrayFilter(x -> notEmpty(x), groupArray(campaign_name)) AS utm_campaigns,+ arrayFilter(x -> notEmpty(x), groupArray(source_name)) AS utm_sources,+ arrayFilter(x -> notEmpty(x), groupArray(medium_name)) AS utm_mediums,+ arrayFilter(x -> notEmpty(x), groupArray(content_name)) AS utm_contents,+ arrayFilter(x -> notEmpty(x), groupArray(term_name)) AS utm_terms,+ arrayFilter(x -> notEmpty(x), groupArray(referring_domain_name)) AS utm_referring_domains,+ arrayFilter(x -> notEmpty(x), groupArray(gclid_name)) AS utm_gclids,+ arrayFilter(x -> notEmpty(x), groupArray(fbclid_name)) AS utm_fbclids,+ arrayFilter(x -> notEmpty(x), groupArray(gad_source_name)) AS utm_gad_sources+ FROM+ marketing_touchpoints_preaggregated+ WHERE+ and(in(job_id, tuple('00000000-0000-0000-0000-000000000001')), equals(team_id, 420))+ GROUP BY+ person_id) AS t ON equals(c.person_id, t.person_id))+ ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversionsGROUP BYcampaign,id,@@ -269,21 +803,110 @@(SELECTperson_id,- campaign_name,- source_name,- medium_name,- content_name,- term_name,- referring_domain_name,- gclid_name,- fbclid_name,- gad_source_name,+ 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(conversion_value, touchpoint_weight) AS conversion_value+ 1 AS conversion_valueFROM- 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+ (+ 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+ c.person_id AS person_id,+ c.conversion_timestamps AS conversion_timestamps,+ c.conversion_math_values AS conversion_math_values,+ c.conversion_campaigns AS conversion_campaigns,+ c.conversion_sources AS conversion_sources,+ c.conversion_mediums AS conversion_mediums,+ c.conversion_contents AS conversion_contents,+ c.conversion_terms AS conversion_terms,+ c.conversion_referring_domains AS conversion_referring_domains,+ c.conversion_gclids AS conversion_gclids,+ c.conversion_fbclids AS conversion_fbclids,+ c.conversion_gad_sources AS conversion_gad_sources,+ t.utm_timestamps AS utm_timestamps,+ t.utm_campaigns AS utm_campaigns,+ t.utm_sources AS utm_sources,+ t.utm_mediums AS utm_mediums,+ t.utm_contents AS utm_contents,+ t.utm_terms AS utm_terms,+ t.utm_referring_domains AS utm_referring_domains,+ t.utm_gclids AS utm_gclids,+ t.utm_fbclids AS utm_fbclids,+ t.utm_gad_sources AS utm_gad_sources+ FROM+ (+ SELECT+ events.person_id,+ arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'login'), toUnixTimestamp(events.timestamp), 0))) AS conversion_timestamps,+ arrayFilter(x -> greater(x, 0), groupArray(if(equals(events.event, 'login'), toFloat(1), 0))) AS conversion_math_values,+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'login'), toString(ifNull(events.properties.utm_campaign, '')), ''))) AS conversion_campaigns,+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'login'), toString(ifNull(events.properties.utm_source, '')), ''))) AS conversion_sources,+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'login'), toString(ifNull(events.properties.utm_medium, '')), ''))) AS conversion_mediums,+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'login'), toString(ifNull(events.properties.utm_content, '')), ''))) AS conversion_contents,+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'login'), toString(ifNull(events.properties.utm_term, '')), ''))) AS conversion_terms,+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'login'), toString(ifNull(events.properties.$referring_domain, '')), ''))) AS conversion_referring_domains,+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'login'), toString(ifNull(events.properties.$gclid, '')), ''))) AS conversion_gclids,+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'login'), toString(ifNull(events.properties.$fbclid, '')), ''))) AS conversion_fbclids,+ arrayFilter(x -> notEmpty(toString(x)), groupArray(if(equals(events.event, 'login'), toString(ifNull(events.properties.$gad_source, '')), ''))) AS conversion_gad_sources+ FROM+ events+ WHERE+ and(equals(events.event, 'login'), greaterOrEquals(events.timestamp, toDateTime('2023-01-01 00:00:00.000000')), lessOrEquals(events.timestamp, toDateTime('2023-01-31 23:59:59.999999')))+ GROUP BY+ events.person_id+ HAVING+ greater(length(conversion_timestamps), 0)) AS c+ LEFT JOIN (+ SELECT+ person_id,+ groupArray(toUnixTimestamp(touchpoint_timestamp)) AS utm_timestamps,+ arrayFilter(x -> notEmpty(x), groupArray(campaign_name)) AS utm_campaigns,+ arrayFilter(x -> notEmpty(x), groupArray(source_name)) AS utm_sources,+ arrayFilter(x -> notEmpty(x), groupArray(medium_name)) AS utm_mediums,+ arrayFilter(x -> notEmpty(x), groupArray(content_name)) AS utm_contents,+ arrayFilter(x -> notEmpty(x), groupArray(term_name)) AS utm_terms,+ arrayFilter(x -> notEmpty(x), groupArray(referring_domain_name)) AS utm_referring_domains,+ arrayFilter(x -> notEmpty(x), groupArray(gclid_name)) AS utm_gclids,+ arrayFilter(x -> notEmpty(x), groupArray(fbclid_name)) AS utm_fbclids,+ arrayFilter(x -> notEmpty(x), groupArray(gad_source_name)) AS utm_gad_sources+ FROM+ marketing_touchpoints_preaggregated+ WHERE+ and(in(job_id, tuple('00000000-0000-0000-0000-000000000001')), equals(team_id, 420))+ GROUP BY+ person_id) AS t ON equals(c.person_id, t.person_id))+ ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversionsGROUP BYcampaign,id,@@ -317,21 +940,110 @@(SELECTperson_id,- campaign_name,- source_name,- medium_name,- content_name,- term_name,- referring_domain_name,- gclid_name,- fbclid_name,- gad_source_name,+ 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(conversion_value, touchpoint_weight) AS conversion_value+ 1 AS conversion_valueFROM- 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+ (+ 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+ c.person_id AS person_id,+ c.conversion_timestamps AS conversion_timestamps,+ c.conversion_math_values AS conversion_math_values,+ c.conversion_campaigns AS conversion_campaigns,+ c.conversion_sources AS conversion_sources,+ c.conversion_mediums AS conversion_mediums,+ c.conversion_contents AS conversion_contents,+ c.conversion_terms AS conversion_terms,+ c.conversion_referring_domains AS conversion_referring_domains,+ c.conversion_gclids AS conversion_gclids,+ c.conversion_fbclids AS conversion_fbclids,+ c.conversion_gad_sources AS conversion_gad_sources,+ t.utm_timestamps AS utm_timestamps,+ t.utm_campaigns AS utm_campaigns,+ t.utm_sources AS utm_sources,+ t.utm_mediums AS utm_mediums,+ t.utm_contents AS utm_contents,+ t.utm_terms AS utm_terms,+ t.utm_referring_domains AS utm_referring_domains,+ t.utm_gclids AS utm_gclids,+ t.utm_fbclids AS utm_fbclids,+ t.utm_gad_sources AS utm_gad_sources+ 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+ FROM+ events+ WHERE+ and(equals(events.event, 'sign_up'), greaterOrEquals(events.timestamp, toDateTime('2023-01-01 00:00:00.000000')), lessOrEquals(events.timestamp, toDateTime('2023-01-31 23:59:59.999999')))+ GROUP BY+ events.person_id+ HAVING+ greater(length(conversion_timestamps), 0)) AS c+ LEFT JOIN (+ SELECT+ person_id,+ groupArray(toUnixTimestamp(touchpoint_timestamp)) AS utm_timestamps,+ arrayFilter(x -> notEmpty(x), groupArray(campaign_name)) AS utm_campaigns,+ arrayFilter(x -> notEmpty(x), groupArray(source_name)) AS utm_sources,+ arrayFilter(x -> notEmpty(x), groupArray(medium_name)) AS utm_mediums,+ arrayFilter(x -> notEmpty(x), groupArray(content_name)) AS utm_contents,+ arrayFilter(x -> notEmpty(x), groupArray(term_name)) AS utm_terms,+ arrayFilter(x -> notEmpty(x), groupArray(referring_domain_name)) AS utm_referring_domains,+ arrayFilter(x -> notEmpty(x), groupArray(gclid_name)) AS utm_gclids,+ arrayFilter(x -> notEmpty(x), groupArray(fbclid_name)) AS utm_fbclids,+ arrayFilter(x -> notEmpty(x), groupArray(gad_source_name)) AS utm_gad_sources+ FROM+ marketing_touchpoints_preaggregated+ WHERE+ and(in(job_id, tuple('00000000-0000-0000-0000-000000000001')), equals(team_id, 420))+ GROUP BY+ person_id) AS t ON equals(c.person_id, t.person_id))+ ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversionsGROUP BYcampaign,id,@@ -347,21 +1059,110 @@(SELECTperson_id,- campaign_name,- source_name,- medium_name,- content_name,- term_name,- referring_domain_name,- gclid_name,- fbclid_name,- gad_source_name,+ 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(conversion_value, touchpoint_weight) AS conversion_value+ 1 AS conversion_valueFROM- 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+ (+ 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+ c.person_id AS person_id,+ c.conversion_timestamps AS conversion_timestamps,+ c.conversion_math_values AS conversion_math_values,+ c.conversion_campaigns AS conversion_campaigns,+ c.conversion_sources AS conversion_sources,+ c.conversion_mediums AS conversion_mediums,+ c.conversion_contents AS conversion_contents,+ c.conversion_terms AS conversion_terms,+ c.conversion_referring_domains AS conversion_referring_domains,+ c.conversion_gclids AS conversion_gclids,+ c.conversion_fbclids AS conversion_fbclids,+ c.conversion_gad_sources AS conversion_gad_sources,+ t.utm_timestamps AS utm_timestamps,+ t.utm_campaigns AS utm_campaigns,+ t.utm_sources AS utm_sources,+ t.utm_mediums AS utm_mediums,+ t.utm_contents AS utm_contents,+ t.utm_terms AS utm_terms,+ t.utm_referring_domains AS utm_referring_domains,+ t.utm_gclids AS utm_gclids,+ t.utm_fbclids AS utm_fbclids,+ t.utm_gad_sources 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+ FROM+ events+ WHERE+ and(equals(events.event, 'purchase'), greaterOrEquals(events.timestamp, toDateTime('2023-01-01 00:00:00.000000')), lessOrEquals(events.timestamp, toDateTime('2023-01-31 23:59:59.999999')))+ GROUP BY+ events.person_id+ HAVING+ greater(length(conversion_timestamps), 0)) AS c+ LEFT JOIN (+ SELECT+ person_id,+ groupArray(toUnixTimestamp(touchpoint_timestamp)) AS utm_timestamps,+ arrayFilter(x -> notEmpty(x), groupArray(campaign_name)) AS utm_campaigns,+ arrayFilter(x -> notEmpty(x), groupArray(source_name)) AS utm_sources,+ arrayFilter(x -> notEmpty(x), groupArray(medium_name)) AS utm_mediums,+ arrayFilter(x -> notEmpty(x), groupArray(content_name)) AS utm_contents,+ arrayFilter(x -> notEmpty(x), groupArray(term_name)) AS utm_terms,+ arrayFilter(x -> notEmpty(x), groupArray(referring_domain_name)) AS utm_referring_domains,+ arrayFilter(x -> notEmpty(x), groupArray(gclid_name)) AS utm_gclids,+ arrayFilter(x -> notEmpty(x), groupArray(fbclid_name)) AS utm_fbclids,+ arrayFilter(x -> notEmpty(x), groupArray(gad_source_name)) AS utm_gad_sources+ FROM+ marketing_touchpoints_preaggregated+ WHERE+ and(in(job_id, tuple('00000000-0000-0000-0000-000000000001')), equals(team_id, 420))+ GROUP BY+ person_id) AS t ON equals(c.person_id, t.person_id))+ ARRAY JOIN arrayEnumerate(conversion_timestamps) AS i)) AS attributed_conversionsGROUP BYcampaign,id,diff --git a/products/marketing_analytics/backend/hogql_queries/conversion_goal_processor.py b/products/marketing_analytics/backend/hogql_queries/conversion_goal_processor.pyindex 24758ede0fdf..042e3a96f4ee 100644--- a/products/marketing_analytics/backend/hogql_queries/conversion_goal_processor.py+++ b/products/marketing_analytics/backend/hogql_queries/conversion_goal_processor.py@@ -2,7 +2,7 @@ import mathimport uuidfrom collections.abc import Sequencefrom dataclasses import dataclass-from datetime import datetime+from datetime import datetime, timedeltafrom typing import ClassVar, Optional, Unionimport structlog@@ -26,6 +26,10 @@ from posthog.models import PropertyDefinition, Team, Userfrom products.access_control.backend.property_access_control import get_restricted_property_namesfrom products.actions.backend.models.action import Action+from products.analytics_platform.backend.lazy_computation.lazy_computation_executor import (+ LazyComputationTable,+ ensure_precomputed,+)from .adapters.factory import MarketingSourceFactoryfrom .marketing_analytics_config import MarketingAnalyticsConfig@@ -86,6 +90,61 @@ TRACKED_FIELDS: list[TrackedField] = []+def build_touchpoints_precompute_query() -> ast.SelectQuery:+ """Config-agnostic touchpoint precompute: one row per UTM-tagged pageview, independent of any+ goal, attribution mode or window. Every attribution query reuses the same materialized rows+ (identical query hash → one shared lazy-computation job per team); attribution happens at read+ time. Columns are aliased to the marketing_touchpoints_preaggregated schema — the lazy framework+ prepends team_id/job_id and appends expires_at, and resolves the time_window placeholders per job.+ """++ def _prop_to_string(event_property: str) -> ast.Expr:+ return ast.Call(+ name="toString",+ args=[+ ast.Call(+ name="ifNull",+ args=[ast.Field(chain=["events", "properties", event_property]), ast.Constant(value="")],+ )+ ],+ )++ select_columns: list[ast.Expr] = [+ ast.Alias(alias="person_id", expr=ast.Field(chain=["events", "person_id"])),+ ast.Alias(alias="touchpoint_timestamp", expr=ast.Field(chain=["events", "timestamp"])),+ ]+ for tracked in TRACKED_FIELDS:+ select_columns.append(ast.Alias(alias=tracked.attributed_name, expr=_prop_to_string(tracked.event_property)))++ # Mirror the fallback's touchpoint definition (_build_pageview_event_filter): a UTM pageview+ # requires BOTH campaign and source non-empty — keep these in lockstep.+ return ast.SelectQuery(+ select=select_columns,+ select_from=ast.JoinExpr(table=ast.Field(chain=["events"])),+ where=ast.And(+ exprs=[+ ast.CompareOperation(+ left=ast.Field(chain=["events", "event"]),+ op=ast.CompareOperationOp.Eq,+ right=ast.Constant(value="$pageview"),+ ),+ ast.CompareOperation(+ left=ast.Field(chain=["events", "timestamp"]),+ op=ast.CompareOperationOp.GtEq,+ right=ast.Placeholder(expr=ast.Field(chain=["time_window_min"])),+ ),+ ast.CompareOperation(+ left=ast.Field(chain=["events", "timestamp"]),+ op=ast.CompareOperationOp.LtEq,+ right=ast.Placeholder(expr=ast.Field(chain=["time_window_max"])),+ ),+ ast.Call(name="notEmpty", args=[_prop_to_string("utm_campaign")]),+ ast.Call(name="notEmpty", args=[_prop_to_string("utm_source")]),+ ]+ ),+ )++@dataclassclass ConversionGoalProcessor:"""@@ -294,9 +353,9 @@ class ConversionGoalProcessor:# `_should_use_precompute` returns False unless both dates are set; narrow for mypy.assert date_from is not None and date_to is not Nonetry:- precomputed_attribution = self._build_attributed_source_from_precompute(date_from, date_to)- if precomputed_attribution is not None:- return self._build_final_aggregation_query(precomputed_attribution)+ precomputed = self._build_attribution_from_touchpoints_precompute(date_from, date_to)+ if precomputed is not None:+ return precomputedexcept Exception:CONVERSION_GOAL_PRECOMPUTE_FALLBACK_COUNTER.inc()logger.exception(@@ -326,6 +385,11 @@ class ConversionGoalProcessor:for prop in self.goal.properties or []:if prop.type in ("person", "cohort"):return False+ # The shared touchpoints precompute is config-agnostic: build_touchpoints_precompute_query()+ # always materializes the default UTM property names. A goal that remaps any tracked field via+ # schema_map would read mismatched columns on the conversion side, so use the direct path.+ if any(self._resolve_field_name(field) != field.event_property for field in TRACKED_FIELDS):+ return Falseif self._precompute_properties_restricted_for_user():return Falsereturn True@@ -360,11 +424,6 @@ class ConversionGoalProcessor:Returns None if jobs are not ready — caller falls back to direct path."""- from products.analytics_platform.backend.lazy_computation.lazy_computation_executor import (- LazyComputationTable,- ensure_precomputed,- )-insert_select = self.get_attributed_query_for_precomputation()result = ensure_precomputed(@@ -425,6 +484,170 @@ class ConversionGoalProcessor:array_join = self._build_single_touch_array_join_subquery(array_collection, attribution_window_seconds)return self._build_single_touch_attribution_subquery(array_join, for_precompute=True)+ def _build_attribution_from_touchpoints_precompute(+ self, date_from: datetime, date_to: datetime+ ) -> Optional[ast.SelectQuery]:+ """Reusable-precompute read path: ensure the config-agnostic touchpoints are materialized,+ then attribute at read time by feeding a touchpoint-sourced array collection through the+ existing pipeline (all modes). Returns None if jobs aren't ready — caller falls back.+ """+ window = timedelta(days=self.config.attribution_window_days)+ result = ensure_precomputed(+ team=self.team,+ insert_query=build_touchpoints_precompute_query(),+ time_range_start=date_from - window,+ time_range_end=date_to,+ ttl_seconds={"0d": 15 * 60, "1d": 60 * 60, "7d": 24 * 60 * 60, "default": 7 * 24 * 60 * 60},+ table=LazyComputationTable.MARKETING_TOUCHPOINTS_PREAGGREGATED,+ )+ if not result.ready:+ return None++ array_collection = self._build_touchpoint_sourced_array_collection(result.job_ids, date_from, date_to)+ return self.build_attribution_pipeline(array_collection)++ def _build_touchpoint_sourced_array_collection(+ self, job_ids: Sequence[str | uuid.UUID], date_from: datetime, date_to: datetime+ ) -> ast.SelectQuery:+ """Reusable-precompute analogue of build_array_collection_query: identical per-person array+ contract, but touchpoint arrays come from the precomputed marketing_touchpoints table (joined+ on person_id) instead of an inline UTM-pageview scan. Conversion arrays still come live from+ events, so build_attribution_pipeline runs unchanged for every attribution mode.+ """+ conversion_event = self.goal.event if self.goal.kind == "EventsNode" else None+ conversions = self._build_conversion_only_arrays(conversion_event, date_from, date_to)+ touchpoints = self._build_touchpoint_arrays_from_table(job_ids)++ select_columns: list[ast.Expr] = []+ for col in ("person_id", "conversion_timestamps", "conversion_math_values"):+ select_columns.append(ast.Alias(alias=col, expr=ast.Field(chain=["c", col])))+ for field in TRACKED_FIELDS:+ select_columns.append(+ ast.Alias(alias=field.conversion_array, expr=ast.Field(chain=["c", field.conversion_array]))+ )+ # LEFT JOIN: organic conversions (no matching person in the touchpoints table) get empty+ # touchpoint arrays (ClickHouse fills missing Array columns with []), handled downstream.+ select_columns.append(ast.Alias(alias="utm_timestamps", expr=ast.Field(chain=["t", "utm_timestamps"])))+ for field in TRACKED_FIELDS:+ select_columns.append(ast.Alias(alias=field.utm_array, expr=ast.Field(chain=["t", field.utm_array])))++ return ast.SelectQuery(+ select=select_columns,+ select_from=ast.JoinExpr(+ table=conversions,+ alias="c",+ next_join=ast.JoinExpr(+ table=touchpoints,+ alias="t",+ join_type="LEFT JOIN",+ constraint=ast.JoinConstraint(+ expr=ast.CompareOperation(+ op=ast.CompareOperationOp.Eq,+ left=ast.Field(chain=["c", "person_id"]),+ right=ast.Field(chain=["t", "person_id"]),+ ),+ constraint_type="ON",+ ),+ ),+ ),+ )++ def _build_conversion_only_arrays(+ self, conversion_event: Optional[str], date_from: datetime, date_to: datetime+ ) -> ast.SelectQuery:+ """Per-person conversion arrays from events (no touchpoint scan) — the live half of the+ touchpoint-sourced array collection. Mirrors the conversion columns of build_array_collection_query.+ """+ select_columns: list[ast.Expr] = [+ ast.Field(chain=["events", "person_id"]),+ self._build_conversion_timestamps_array(conversion_event),+ self._build_conversion_math_values_array(conversion_event),+ ]+ for field in TRACKED_FIELDS:+ select_columns.append(+ self._build_conversion_utm_array(+ field.conversion_array, conversion_event, self._resolve_field_name(field)+ )+ )++ where_exprs: list[ast.Expr] = [+ self._build_conversion_event_condition(conversion_event),+ ast.CompareOperation(+ left=ast.Field(chain=["events", "timestamp"]),+ op=ast.CompareOperationOp.GtEq,+ right=ast.Constant(value=date_from),+ ),+ ast.CompareOperation(+ left=ast.Field(chain=["events", "timestamp"]),+ op=ast.CompareOperationOp.LtEq,+ right=ast.Constant(value=date_to),+ ),+ ]+ where_exprs = add_conversion_goal_property_filters(where_exprs, self.goal, self.team)++ return ast.SelectQuery(+ select=select_columns,+ select_from=ast.JoinExpr(table=ast.Field(chain=["events"])),+ where=ast.And(exprs=where_exprs),+ group_by=[ast.Field(chain=["events", "person_id"])],+ having=ast.CompareOperation(+ left=ast.Call(name="length", args=[ast.Field(chain=["conversion_timestamps"])]),+ op=ast.CompareOperationOp.Gt,+ right=ast.Constant(value=0),+ ),+ )++ def _build_touchpoint_arrays_from_table(self, job_ids: Sequence[str | uuid.UUID]) -> ast.SelectQuery:+ """Per-person touchpoint arrays (utm_timestamps + per-field UTM arrays) read from the+ precomputed marketing_touchpoints table, matching the array shape build_array_collection_query+ produces from a UTM-pageview scan.+ """+ select_columns: list[ast.Expr] = [+ ast.Field(chain=["person_id"]),+ ast.Alias(+ alias="utm_timestamps",+ expr=ast.Call(+ name="groupArray",+ args=[ast.Call(name="toUnixTimestamp", args=[ast.Field(chain=["touchpoint_timestamp"])])],+ ),+ ),+ ]+ for field in TRACKED_FIELDS:+ select_columns.append(+ ast.Alias(+ alias=field.utm_array,+ expr=ast.Call(+ name="arrayFilter",+ args=[+ ast.Lambda(args=["x"], expr=ast.Call(name="notEmpty", args=[ast.Field(chain=["x"])])),+ ast.Call(name="groupArray", args=[ast.Field(chain=[field.attributed_name])]),+ ],+ ),+ )+ )++ return ast.SelectQuery(+ select=select_columns,+ select_from=ast.JoinExpr(table=ast.Field(chain=["posthog", "marketing_touchpoints_preaggregated"])),+ where=ast.And(+ exprs=[+ ast.Call(+ name="in",+ args=[+ ast.Field(chain=["job_id"]),+ ast.Tuple(exprs=[ast.Constant(value=str(jid)) for jid in job_ids]),+ ],+ ),+ ast.CompareOperation(+ left=ast.Field(chain=["team_id"]),+ op=ast.CompareOperationOp.Eq,+ right=ast.Constant(value=self.team.pk),+ ),+ ]+ ),+ group_by=[ast.Field(chain=["person_id"])],+ )+def build_attributed_source_from_precomputed(self,job_ids: Sequence[str | uuid.UUID],diff --git a/products/marketing_analytics/backend/hogql_queries/test_conversion_goal_precompute_e2e.py b/products/marketing_analytics/backend/hogql_queries/test_conversion_goal_precompute_e2e.pyindex c0d7c52df453..a245232d419c 100644--- a/products/marketing_analytics/backend/hogql_queries/test_conversion_goal_precompute_e2e.py+++ b/products/marketing_analytics/backend/hogql_queries/test_conversion_goal_precompute_e2e.py@@ -15,6 +15,7 @@ from posthog.clickhouse.client.execute import sync_executefrom posthog.clickhouse.preaggregation.conversion_goal_attributed_sql import (TRUNCATE_CONVERSION_GOAL_ATTRIBUTED_TABLE_SQL,)+from posthog.clickhouse.preaggregation.marketing_touchpoints_sql import TRUNCATE_MARKETING_TOUCHPOINTS_TABLE_SQLfrom products.analytics_platform.backend.models.preaggregation_job import PreaggregationJobfrom products.marketing_analytics.backend.hogql_queries.conversion_goal_processor import ConversionGoalProcessor@@ -33,6 +34,7 @@ class TestConversionGoalPrecomputeEquivalence(ClickhouseTestMixin, APIBaseTest):def _clean_preaggregation_data(self):sync_execute(TRUNCATE_CONVERSION_GOAL_ATTRIBUTED_TABLE_SQL())+ sync_execute(TRUNCATE_MARKETING_TOUCHPOINTS_TABLE_SQL())PreaggregationJob.objects.all().delete()def _seed_events(self):diff --git a/products/marketing_analytics/backend/hogql_queries/test_marketing_touchpoints_precompute.py b/products/marketing_analytics/backend/hogql_queries/test_marketing_touchpoints_precompute.pynew file mode 100644index 000000000000..0f6ad0ed2b7c--- /dev/null+++ b/products/marketing_analytics/backend/hogql_queries/test_marketing_touchpoints_precompute.py@@ -0,0 +1,108 @@+from datetime import UTC, datetime++from posthog.test.base import APIBaseTest, ClickhouseTestMixin, _create_event, _create_person, flush_persons_and_events++from django.test import override_settings++from posthog.clickhouse.client.execute import sync_execute+from posthog.clickhouse.preaggregation.marketing_touchpoints_sql import (+ DISTRIBUTED_MARKETING_TOUCHPOINTS_TABLE,+ TRUNCATE_MARKETING_TOUCHPOINTS_TABLE_SQL,+)++from products.analytics_platform.backend.lazy_computation.lazy_computation_executor import (+ LazyComputationTable,+ ensure_precomputed,+)+from products.analytics_platform.backend.models.preaggregation_job import PreaggregationJob+from products.marketing_analytics.backend.hogql_queries.conversion_goal_processor import (+ build_touchpoints_precompute_query,+)+++@override_settings(IN_UNIT_TESTING=True)+class TestMarketingTouchpointsPrecompute(ClickhouseTestMixin, APIBaseTest):+ def setUp(self):+ super().setUp()+ self._clean()++ def tearDown(self):+ self._clean()+ super().tearDown()++ def _clean(self):+ sync_execute(TRUNCATE_MARKETING_TOUCHPOINTS_TABLE_SQL())+ PreaggregationJob.objects.all().delete()++ def _seed(self):+ for distinct_id in ("u1", "u2", "u3"):+ _create_person(distinct_ids=[distinct_id], team=self.team)++ # u1: two UTM-tagged pageviews → two touchpoints.+ for day, (campaign, source) in [(3, ("spring", "google")), (6, ("summer", "facebook"))]:+ _create_event(+ team=self.team,+ event="$pageview",+ distinct_id="u1",+ timestamp=datetime(2025, 1, day, 10, tzinfo=UTC),+ properties={"utm_campaign": campaign, "utm_source": source, "utm_medium": "cpc"},+ )++ # u2: one UTM-tagged pageview + one pageview with no UTM (excluded).+ _create_event(+ team=self.team,+ event="$pageview",+ distinct_id="u2",+ timestamp=datetime(2025, 1, 5, 10, tzinfo=UTC),+ properties={"utm_campaign": "winter", "utm_source": "bing"},+ )+ _create_event(+ team=self.team,+ event="$pageview",+ distinct_id="u2",+ timestamp=datetime(2025, 1, 7, 10, tzinfo=UTC),+ properties={},+ )++ # u3: a non-pageview event carrying UTM (excluded — only $pageview is a touchpoint).+ _create_event(+ team=self.team,+ event="purchase",+ distinct_id="u3",+ timestamp=datetime(2025, 1, 8, 10, tzinfo=UTC),+ properties={"utm_campaign": "x", "utm_source": "y"},+ )+ flush_persons_and_events()++ def _ensure(self):+ return ensure_precomputed(+ team=self.team,+ insert_query=build_touchpoints_precompute_query(),+ time_range_start=datetime(2025, 1, 1, tzinfo=UTC),+ time_range_end=datetime(2025, 1, 31, tzinfo=UTC),+ ttl_seconds=3600,+ table=LazyComputationTable.MARKETING_TOUCHPOINTS_PREAGGREGATED,+ )++ def _count_touchpoints(self) -> int:+ rows = sync_execute(+ f"SELECT count() FROM {DISTRIBUTED_MARKETING_TOUCHPOINTS_TABLE()} WHERE team_id = %(team_id)s",+ {"team_id": self.team.pk},+ )+ return rows[0][0]++ def test_touchpoints_materialize_through_framework(self):+ self._seed()+ result = self._ensure()+ assert result.ready+ # Three UTM-tagged pageviews; the no-UTM pageview and the purchase are excluded.+ assert self._count_touchpoints() == 3++ def test_config_agnostic_query_reuses_one_job(self):+ self._seed()+ first = self._ensure()+ second = self._ensure()+ assert first.ready and second.ready+ # Identical (config-agnostic) query hash → the second call reuses the same job(s), no re-materialize.+ assert set(first.job_ids) == set(second.job_ids)+ assert self._count_touchpoints() == 3