---
name: "attribution-ledger"
description: "Build an attribution ledger: write attribution rules first (lookback window, touch definition, meeting-sourced definition), log every touch from every channel with account, campaign, date and cost, stitch touches to opportunities by account and date, credit first touch, last touch and meeting-sourced side by side, report untracked opportunities, and compute cost per opportunity by campaign. Use when pipeline credit is disputed or cost per opportunity is unknown."
---

# Attribution ledger

## Goal
A plain, auditable record of what happened before each opportunity, and costs that tie back to it. The ledger tells you what the evidence shows, not what makes a channel look good. Part of GTM engineering. Fed by: multi-channel-orchestrator, signal-detection-engine, website-visitor-deanonymizer, inbound-lead-router, reply-classifier. Feeds: experiment-harness, icp-scoring-model, signal-detection-engine (weights).

## When to use
- Marketing and sales both claim the same pipeline.
- Budget decisions rest on guesses.
- Reports change when filters change.

## Inputs
- Touch records from every channel with an account domain.
- Opportunities from the CRM: account, created date, amount, first meeting date, stage.
- Cost: cost per touch or spend by campaign.
- Agreement on the rules.

## Data model
`touches`: `touch_id, date, account_domain, contact_id, channel, campaign, cost`
`opps`: `opp_id, account_domain, created_date, amount, first_meeting_date, stage, owner`
`stitched`: `opp_id, touches_in_window, first_touch_campaign, last_touch_campaign, meeting_sourced_campaign, path`
`report`: per campaign: `touches, cost, opps_first, opps_last, opps_meeting, pipeline_meeting, cost_per_opp_meeting`

## Step 1. Write the rules down
Before any query:
- What is a touch? List the types.
- Lookback window (for example 90 days, set from your sales cycle).
- First touch: earliest in the window. Last touch: latest before creation.
- Meeting-sourced: the latest touch on or before the first meeting date and inside the window.
- What happens with opportunities that have no tracked touch: they are reported as untracked.
Get sales and marketing to agree in writing. Put the date on the page.

## Step 2. Log touches
Every channel writes to one table: sequences, calls, LinkedIn tasks, ad clicks, events, webinars, content downloads. Each row has a unique `touch_id`, an account domain, a campaign name and a cost (zero if unknown, with a note). Remove duplicates by id.

## Step 3. Stitch
Run `scripts/ledger.py stitch touches.csv opps.csv --lookback 90`. For each opportunity the script finds touches on that account inside the window and returns the path (channels in order), the first-touch campaign, the last-touch campaign and the meeting-sourced campaign.

## Step 4. Report
Run `scripts/ledger.py report touches.csv opps.csv --lookback 90`. For each campaign you get touches, cost, opportunities credited under each rule, pipeline for meeting-sourced opportunities and cost per opportunity (cost divided by meeting-sourced opportunities). Always show the untracked row.

## Step 5. Read it honestly
- Compare the three views. A campaign that wins only under one rule needs a second look.
- Do not compare cost per opportunity when counts are very small.
- Do not use this to claim cause. It shows association. Use holdouts (experiment-harness) for cause.
- Report the untracked share every time.

## Step 6. Close the loop
Feed results back: signal weights (signal-detection-engine), ICP weights (icp-scoring-model), channel budgets, sequence changes. Log the decision and the date.

## Tool options
A warehouse with scheduled models is best. A CRM with campaign and touch objects, a spreadsheet for small volumes, or a script like this one. Attribution products can help with web journeys. Keep the rules and the raw touches in your own hands whatever you use.

## Output
`stitched.csv`, `report.csv` and the signed rules page.

## Success metrics
- Share of opportunities with at least one tracked touch.
- Cost per meeting-sourced opportunity by campaign, with counts.
- Agreement between the three views.
- Time from month end to report.

## Guardrails
- Show counts, not only ratios.
- Keep personal data to what attribution needs. Use account-level joins where you can.
- Respect consent and retention rules for touch data.
- Do not change rules to improve a number. Change them in writing, with a date, for everyone.
- Never present attribution as proof of cause.
