Fix a multi-touch marketing attribution mart using dbt + DuckDB/Snowflake
harbor run -t snowflake-labs/dbt-fix-multi-touch-attributionharbor download snowflake-labs/dbt-fix-multi-touch-attributionYou are an analytics engineer rebuilding a multi-touch marketing attribution mart. The warehouse contains GA4-style sessions/events data and a reference dbt project. Create a new dbt model that outputs daily attribution metrics for five attribution models, channel grouping, and assist metrics.
/app/database/retail.duckdb/app/dbt_models_duckdb/app/dbt_models_snowflake/app/dbt_project with schema analytics.This task supports both DuckDB and Snowflake. Run echo $DB_TYPE in a shell BEFORE you write any code or spawn any subagents — the live env determines which backend the verifier runs against. Do NOT assume a default from this prose. Both /app/dbt_models_duckdb/ and /app/dbt_models_snowflake/ exist on disk; the verifier only checks the project matching the live $DB_TYPE.
DB_TYPE=duckdb$DUCKDB_PATH (default: /app/database/retail.duckdb)DB_TYPE=snowflakeSNOWFLAKE_ACCOUNTSNOWFLAKE_USERSNOWFLAKE_PASSWORDSNOWFLAKE_DATABASE - The clone database to useSNOWFLAKE_SCHEMASNOWFLAKE_WAREHOUSESNOWFLAKE_ROLE (optional)Note: For Snowflake, the entrypoint automatically creates a clone database and sets SNOWFLAKE_DATABASE. The clone is destroyed when the task completes.
You must configure dbt to connect to the database:
profiles.yml in the dbt project directory with profile name retail_dw_masterDBT_PROFILES_DIR environment variable to the directory containing profiles.ymlConfigure with type: duckdb and the database path from $DUCKDB_PATH.
Configure with type: snowflake using password authentication:
Create rpt_multi_touch_attribution_fixed.sql in /app/dbt_project/models/marts/marketing/ that materializes analytics.rpt_multi_touch_attribution_fixed.
| Column | Type | Description |
|---|---|---|
attribution_date |
DATE | Session date (from session_start) |
channel |
STRING | utm_source, default direct |
medium |
STRING | utm_medium, default none |
campaign |
STRING | utm_campaign, default none |
channel_group |
STRING | One of: Direct, Paid, Organic, Referral |
attribution_model |
STRING | One of: last_touch, first_touch, linear, time_decay, position_based |
sessions |
INTEGER | Distinct session_id count |
conversions |
INTEGER | Sessions where is_converted = true |
attributed_revenue |
NUMERIC(12,2) | Sum purchase revenue for converted sessions |
conversion_rate |
NUMERIC | conversions/sessions, in [0,1], 4 decimals; 0 when sessions=0 |
assist_sessions |
INTEGER | Count of distinct sessions that are assists (see Business Rules) |
session_id, visitor_id, customer_id, session_start, is_converted, utm_source, utm_medium, utm_campaignevent_name, event_valuet2_event_name, t2_event_value -- prefer for revenue when availableUse adapter.get_relation / adapter.get_columns_in_relation to detect int_sessions_events_joined and t2_event_value.
CAST(session_start AS DATE) BETWEEN '2025-10-02'::DATE AND '2025-12-31'::DATE (inclusive). Do not use CURRENT_DATE or INTERVAL.channel: COALESCE(utm_source, 'direct') (treat missing as direct)medium: COALESCE(utm_medium, 'none'); campaign: COALESCE(utm_campaign, 'none')LOWER(TRIM(COALESCE(...,''))) for comparisons):
Paid: LOWER(TRIM(COALESCE(utm_medium,''))) IN (cpc,ppc,paid,cpm)Organic: LOWER(TRIM(COALESCE(utm_medium,''))) = organicDirect: LOWER(TRIM(COALESCE(utm_source,''))) IN ('',direct) AND LOWER(TRIM(COALESCE(utm_medium,''))) IN ('',none,(none))Referral: elseCOUNT(DISTINCT session_id)is_converted = trueevent_value (or t2_event_value from int_sessions_events_joined if available) for converted sessions; round to 2 decimalsconversions / sessions rounded to 4 decimals; 0 when sessions=0; must be in [0,1]visitor_id; if NULL, use session_id as fallback: COALESCE(visitor_id, session_id)For each (attribution_date, channel, medium, campaign): count distinct session_id where that session is in at least one converting path and is not the last touch of that path.
visitor_id) with session_start <= C.session_start within the window. The last touch is C. All other touches are assists.session_start) in the window. All of that user's conversions and revenue go to the first session's channel/medium/campaign.visitor_id, session_start <= C.session_start). For each touch: days_before_conversion = (C's session_start date - touch's session_start date) in days. Weight = POW(2, -days_before_conversion / 7.0) (7-day half-life). Normalize weights to sum 1 per path; attribute that conversion and its revenue in proportion. When days_before_conversion=0, weight=1.session_start (1=first, N=last). N=1 -> 100% to the only touch; N=2 -> 50% each; N>=3 -> 40% first, 40% last, 20% split evenly among the (N-2) middle touches: each middle gets 20/(N-2)./app/dbt_project/models/marts/marketing/rpt_multi_touch_attribution_fixed.sqldbt run --select rpt_multi_touch_attribution_fixed must succeed and create analytics.rpt_multi_touch_attribution_fixed.attribution_model values.channel_group and assist_sessions.dbt run --select stg_ga__sessions stg_ga__events int_sessions_events_joined in the dbt project directory (DuckDB: /app/dbt_models_duckdb, Snowflake: /app/dbt_models_snowflake).main schema for staged data.DATEDIFF('day', start, end) which works on both backends::DATE casts or date subtraction operators