Fix an email attribution dbt model that broke due to upstream tracking changes
harbor run -t snowflake-labs/dbt-fix-email-attributionharbor download snowflake-labs/dbt-fix-email-attributionYou are an analytics engineer tasked with rebuilding a broken email attribution reporting model. The original model failed due to upstream schema changes in the GA4 data pipeline. Your goal is to create a robust, production-ready dbt model that accurately tracks email marketing performance.
Build a dbt model rpt_email_attribution_fixed.sql that generates a daily attribution report for email marketing campaigns. The model must be placed in /app/dbt_project/models/marts/marketing/ and materialize to the analytics schema.
The final table analytics.rpt_email_attribution_fixed must contain exactly these columns:
| Column | Type | Description |
|---|---|---|
attribution_date |
DATE | Date of the session (from session_start) |
channel |
VARCHAR | Marketing channel (utm_source, default: 'direct') |
medium |
VARCHAR | Marketing medium (utm_medium, default: 'none') |
campaign |
VARCHAR | Campaign name (utm_campaign, default: 'none') |
sessions |
BIGINT | Count of distinct sessions |
conversions |
BIGINT | Count of sessions that converted |
attributed_revenue |
DECIMAL(12,2) | Total revenue attributed to these sessions |
conversion_rate |
DECIMAL(10,4) | Conversion rate (conversions/sessions), between 0 and 1 |
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 dbt_projectDBT_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:
main.stg_ga__sessions tablemain.int_sessions_events_joined if available (check for t2_event_value column), otherwise fall back to main.stg_ga__eventsdbt run --select stg_ga__sessions stg_ga__events int_sessions_events_joined first/app/dbt_transforms//app/dbt_models_snowflake/session_start date is on or after 2025-10-04 (fixed cutoff)CAST(session_start AS DATE) >= DATE '2025-10-04'CAST(session_start AS DATE) <= DATE '2026-01-02'channel: Use utm_source, if NULL or empty string, default to 'direct'medium: Use utm_medium, if NULL or empty string, default to 'none'campaign: Use utm_campaign, if NULL or empty string, default to 'none'session_id valuessession_id valuesattribution_date, channel, medium, campaignis_converted = truetrue, '1', 'true', 1, etc.is_converted = true)event_value from purchase events (event_name = 'purchase')int_sessions_events_joined, use t2_event_value and t2_event_nameROUND(..., 2)GREATEST(..., 0))conversions / sessionsROUND(..., 4)sessions = 0, set conversion_rate = 0.0DECIMAL(10,4) for precision/app/dbt_project/
├── dbt_project.yml
└── models/
└── marts/
└── marketing/
└── rpt_email_attribution_fixed.sql
dbt_projectanalyticstableadapter.get_relation() to check table availabilityadapter.get_columns_in_relation() to check for specific columnsDISTINCT appropriately to avoid duplicatesHAVING clause to filter out zero-session rowsThe solution must pass all 10 test cases that validate:
COALESCE and CASE statements for robust NULL handlingWHERE 1=0 pattern)