Data and analytics

Marketing attribution pipeline in dbt and BigQuery

Daily dbt models for cost per order by channel with first and last touch attribution, a Monday spreadsheet export, and source freshness alerts.

dbtbigquerysql

Built 1 time, downloaded 0 times.

Install from your terminal

Run this in your project folder. It signs you in, shows what changes and asks before it overwrites a file.

npx claude-lab add marketing-attribution-pipeline-in-dbt-and-bigquery-f58d43
CLAUDE.md
# Marketing attribution pipeline

## Purpose

Daily attribution tables for a small ecommerce brand. The founders read a Monday spreadsheet export. There is no BI tool.

## Stack

- dbt Core with the dbt-bigquery adapter
- BigQuery datasets: `raw`, `staging`, `marts`, `exports`
- Dev datasets are named `dbt_YOURNAME`
- Project ID comes from the env var `DBT_BQ_PROJECT` (placeholder: `YOUR_PROJECT_ID`)

## Layout

- `models/staging`: one `stg_` model per source. Rename and cast only.
- `models/intermediate`: `int_` models for touchpoints and orders joins.
- `models/marts`: `fct_` and `dim_` tables.
- `models/exports`: spreadsheet-ready views.

## Sources

Three ad platforms, shop orders, and email clicks. Use placeholder names until the user fills in the real ones:

- `PLATFORM_A`, `PLATFORM_B`, `PLATFORM_C`: spend by day, campaign, channel
- Shop orders: order id, customer key, created_at, revenue, refunds
- Email clicks: click time, customer key, campaign

Column lists and open questions live in `SPEC.md`. If a column is unknown, add it to Open questions. Do not guess.

## Commands

```
dbt deps
dbt build --select state:modified+ --defer --state prod-artifacts
dbt build --select +fct_cost_per_order_daily
dbt source freshness
dbt docs generate
```

## Targets

- Always run against the `dev` target.
- Never run against `prod` unless the user asks.
- Prod runs happen only from the scheduler.

## Definitions

- Cost per order = total spend / distinct non-cancelled orders, per day, per channel.
- First touch and last touch use the lookback window var `attribution_window_days` (default 30).
- Each order gets one row per attribution model.

## Conventions

- All dates are UTC.
- A single `reporting_timezone` var is applied only in exports.
- Money is `NUMERIC` in the shop currency.
- Names are `snake_case`.
- Every model has a unique and not_null test on its grain.

## Gotchas

- Ad platforms restate spend for 1 to 3 days. Use incremental models with `insert_overwrite` and a 3 day lookback.
- Orders with no touchpoint go to channel `direct_or_unknown`. Never drop them.
- Divide with `safe_divide`.
- Email clicks are not proof of attribution without a bot filter.

## Cost rules

- On large marts, use `partition_by` date and `cluster_by` channel.
- Before full refreshes, run `dbt compile` and a BigQuery dry run to check bytes.
- Never use `--full-refresh` without asking.

## Secrets

- Never commit service account keys or `profiles.yml`.
- Use env vars and placeholders such as `YOUR_PROJECT_ID`.

## Workflow

1. Explore the relevant models and sources.
2. Plan the change.
3. Make the change.
4. Run the `verify` skill before saying done.

Use `/clear` between unrelated tasks.

## Review and specs

@REVIEW.md
@SPEC.md
Use for my project