---
name: "crm-hygiene-pipeline"
description: "Build a CRM hygiene pipeline: back up the data, dedupe contacts by exact email then fuzzy name inside one company domain with explicit survivor rules, match every lead to an account by email domain then company name with a confidence value, mark records fresh, stale or dead from verification and activity dates, apply changes in small logged batches, and schedule the run. Use when records are duplicated, leads have no account, or the database is old."
---

# CRM hygiene pipeline

## Goal
A database people trust: one record per person, every lead on the right account, and old records marked so they stop distorting reports and sends. Part of GTM engineering. Fed by: waterfall-enrichment-router, inbound-lead-router (new leads). Feeds: icp-scoring-model, inbound-lead-router, attribution-ledger, multi-channel-orchestrator.

## When to use
- Duplicates confuse reps and double-count leads.
- Routing or reporting breaks because leads have no account.
- Old records push up bounce rates and distort conversion numbers.

## Inputs
- Contacts and accounts exports with ids, names, emails, domains, owners, created dates, last activity and last verified dates.
- The survivor rules (which record wins, which fields are kept).
- Decay windows.
- Access to apply changes through the CRM import or API, with a sandbox if one exists.

## Data model
`contacts`: `contact_id, name, email, company, title, owner, created, last_activity, last_verified`
`accounts`: `account_id, name, domain, parent_account_id, owner`
Additions the pipeline writes: `dup_of, dup_reason, account_id, match_method, match_confidence, record_status, next_action`
`change_log`: `timestamp, record_id, field, old_value, new_value, reason, batch_id`

## Step 1. Back up and audit
Export everything and store it read-only. Run the three checks without applying anything. Report: duplicate rate, share of contacts with no account, share stale, share dead. Show 10 examples of each.

## Step 2. Dedupe
Run `scripts/crm_hygiene.py dedupe contacts.csv`.
- Exact match on lowercase, trimmed email.
- Fuzzy match on name only inside the same email domain, and only above a high similarity (the script uses 0.9). These are marked `name_match_same_domain` and go to review, not to automatic merge.
- Skip role mailboxes (info, sales, support, hello and similar) as people.
- Choose a survivor by the rules: newest activity, then most filled fields, with missing fields copied from the loser and nothing overwritten.

## Step 3. Match leads to accounts
Run `scripts/crm_hygiene.py match contacts.csv accounts.csv`.
1. Email domain equals the account domain or is a subdomain of it: method `domain`, confidence high.
2. Free-mail domains (gmail and similar) never match by domain.
3. Normalised company name (lowercase, punctuation and legal suffixes removed) with high string similarity: method `name`, confidence medium.
4. Everything else goes to a manual queue. Do not force a match.
Keep a domain alias table for acquisitions and rebrands.

## Step 4. Decay
Run `scripts/crm_hygiene.py decay contacts.csv --stale-days 180 --dead-days 365`.
- Fresh: verified or active inside the stale window.
- Stale: past the stale window. Next action: re-verify email and title through enrichment.
- Dead: past the dead window, or hard-bounced. Next action: archive and suppress from sends.
Use the later of `last_verified` and `last_activity`. A reply, a meeting or a verified enrichment resets the clock.

## Step 5. Apply safely
- Apply a small batch first (20 to 50 records), read 10 back, and show before and after.
- Apply the rest in batches, log every field change with the old value, and stop on errors.
- Archive instead of delete, with a retention date.
- Never change owner on active deals without telling the owner.

## Step 6. Schedule
Run weekly on new and changed records, and monthly on the full set. Report the share of clean records, duplicates created in the last week and where they came from (form, import, integration). Fix sources, not just symptoms.

## Tool options
The CRM's own duplicate management, import tools and workflows for applying changes. A script (like the one here) for audits and previews. A data-matching tool or warehouse job at larger scale. A spreadsheet is fine for a first audit.

## Output
`contacts_deduped.csv`, `contacts_matched.csv`, `contacts_status.csv` and the change log.

## Success metrics
- Duplicate rate over time.
- Share of leads with an account at creation.
- Share of sends going to records verified in the window.
- Bounce rate on emails to fresh records versus stale ones.
- Time from lead creation to a correct account match.

## Guardrails
- Back up before bulk changes. Keep the old values so a merge can be undone.
- Ambiguous matches go to review, not to automatic merge.
- Honour deletion and opt-out requests, and keep a suppression list that survives merges and imports.
- Keep personal data to what you need, and keep a retention date.
- Do not scrape private data to fill gaps.
