Build multi-layer marketing campaign ROI analysis model from scratch
harbor run -t snowflake-labs/dbt-campaign-roi-analysisharbor download snowflake-labs/dbt-campaign-roi-analysisThe marketing team needs a comprehensive ROI analysis model that attributes revenue to campaigns using multi-touch attribution logic. They want to understand which campaigns and channels are most effective at driving conversions.
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:
All models must materialize in the main schema on Snowflake. Configure your dbt profile with schema: main. You must also override the generate_schema_name macro so that dbt places all models directly in the target schema defined in profiles.yml, ignoring any custom schema config from subdirectory placement. Create macros/generate_schema_name.sql with:
{% macro generate_schema_name(custom_schema_name, node) -%}
{{ default_schema }}
{%- endmacro %}
This ensures models in models/staging/marketing/ still land in main, not main_marketing.
/app/dbt_models_duckdb/app/dbt_models_snowflakemodels/staging/marketing/stg_marketing__campaigns.sqlmodels/intermediate/marketing/int_marketing__attributed_conversions.sqlmodels/marts/marketing/mart_marketing__campaign_roi.sqlmacros/calculate_roas.sqlExplore the following tables to understand available columns:
Create a multi-layer model architecture to analyze campaign ROI:
stg_marketing__campaigns)Clean and prepare campaign data:
Required Columns:
campaign_idcampaign_namechannel (uppercase)start_dateend_datetotal_budgettotal_spend (sum of SPEND from CAMPAIGN_PERFORMANCE)total_impressions (sum from CAMPAIGN_PERFORMANCE)total_clicks (sum from CAMPAIGN_PERFORMANCE)is_active (1 if STATUS = 'ACTIVE', else 0)Aggregate performance metrics per campaign from CAMPAIGN_PERFORMANCE table.
int_marketing__attributed_conversions)Implement multi-touch attribution logic:
Attribution Rules (per conversion, may create multiple rows):
Required Columns:
conversion_idcampaign_id (campaign receiving attribution for this row)customer_idsession_id (converting session)conversion_value (from CONVERSIONS.VALUE)order_value (from orders table: grand_total)attribution_type (text: 'first_touch', 'last_touch', 'multi_touch')attribution_weight (numeric: 0.5 for first/last only, 1.0 for multi-touch)attributed_revenue (order_value * attribution_weight)Join Logic:
mart_marketing__campaign_roi)Calculate ROI metrics and performance tiers:
Base Metrics:
campaign_idcampaign_namechanneltotal_budgettotal_spendtotal_impressionstotal_clickstotal_attributed_conversions (count of distinct conversions)total_attributed_revenue (sum of attributed_revenue)Efficiency Metrics (use calculate_roas macro):
roas (Return on Ad Spend: attributed_revenue / spend)ctr (Click-Through Rate: clicks / impressions * 100)cpc (Cost Per Click: spend / clicks)cpa (Cost Per Acquisition: spend / conversions)revenue_per_impression (attributed_revenue / impressions)Percentile Rankings (use PERCENT_RANK):
roas_percentile (higher ROAS = higher percentile)cpa_percentile (lower CPA = higher percentile - inverted!)ctr_percentile (higher CTR = higher percentile)ROI Performance Tier (roi_tier):
Waterfall logic (check in order):
| Tier | Criteria |
|---|---|
| high_performer | roas_percentile >= 0.75 AND roas >= 3.0 AND cpa < 50 |
| profitable | roas_percentile >= 0.60 OR roas >= 2.0 |
| break_even | roas >= 1.0 OR roas_percentile >= 0.40 |
| underperforming | All others |
NULL Handling: Treat NULL percentiles as 0, NULL ROAS as 0, NULL CPA as 999.
Campaign Effectiveness Index (0-100):
A composite score combining:
| Factor | Weight | Calculation |
|---|---|---|
| ROAS normalized | 40% | min(1.0, max(0, roas / 5.0)) |
| CTR normalized | 25% | min(1.0, max(0, ctr / 5.0)) |
| CPA efficiency | 20% | 1.0 - min(1.0, max(0, cpa / 100.0)) |
| Revenue/impression ratio | 15% | min(1.0, max(0, revenue_per_impression / median_revenue_per_impression)) |
Formula: (factor1 * 0.40 + factor2 * 0.25 + factor3 * 0.20 + factor4 * 0.15) * 100
Final score bounded 0-100.
Channel Peer Comparison Metrics:
Compare campaigns within the same channel:
| Column | Description |
|---|---|
| channel_roi_rank | Rank within channel by ROAS (best = 1) |
| channel_peer_count | Total campaigns in this channel |
| above_channel_avg_roas | 1 if roas > avg for channel, else 0 |
calculate_roasCreate a reusable macro for ROAS calculation:
{% macro calculate_roas(revenue, spend, min_spend_threshold=1) %}
-- Return ROAS (revenue / spend)
-- If spend < min_spend_threshold, return 0
-- Handle division by zero
{% endmacro %}
Required columns:
campaign_idcampaign_namechanneltotal_budgettotal_spendtotal_impressionstotal_clickstotal_attributed_conversionstotal_attributed_revenueroasctrcpccparevenue_per_impressionroas_percentilecpa_percentilectr_percentileroi_tiercampaign_effectiveness_indexchannel_roi_rankchannel_peer_countabove_channel_avg_roas