# Lead Analytics — Pipeline Data Analysis & Reporting

Analyze lead pipeline data to extract insights, measure campaign effectiveness, and identify optimization opportunities.

## When This Skill Activates

Trigger phrases: "analyze leads", "campaign metrics", "pipeline analytics", "lead data", "conversion analysis", "which campaigns performed best", "show me the numbers"

## Data Sources

### Primary: Lead Pipeline Data
- Campaign results: `~/.lead_pipeline/data/campaign_*.json`
- GDPR audit logs: `~/.lead_pipeline/data/audit_*.json`
- Deduplication state: `~/.lead_pipeline/data/dedup_state.json`
- Send logs from `EmailSender.get_send_log()`

### Secondary: CSV Exports
- Exported via `lead_pipeline export <campaign_id> --format csv`
- Manual CRM data dumps

## Analysis Types

### 1. Campaign Performance Analysis
```markdown
## Campaign Performance: [Campaign ID]

### Funnel Metrics
| Stage | Count | Conversion | Drop-off |
|-------|-------|-----------|----------|
| Scraped | [N] | 100% | — |
| Enriched | [N] | [%] | [%] lost |
| Email Drafted | [N] | [%] | [%] lost |
| Sent | [N] | [%] | [%] lost |
| Failed | [N] | — | [%] error |

### Enrichment Quality
| Metric | Value |
|--------|-------|
| Email found rate | [%] |
| Phone found rate | [%] |
| LinkedIn found rate | [%] |
| Website found rate | [%] |
| Avg contacts per lead | [N] |

### Source Performance
| Registry Source | Leads | Enrichment Rate | Send Rate |
|----------------|-------|-----------------|-----------|
| UK Companies House | [N] | [%] | [%] |
| Qatar QFC | [N] | [%] | [%] |
| Qatar Chamber | [N] | [%] | [%] |

### Search Engine Effectiveness
| Engine | Leads Enriched | Emails Found | Quality Score |
|--------|---------------|--------------|---------------|
| Tavily | [N] | [N] | [High/Med/Low] |
| Google Custom | [N] | [N] | [High/Med/Low] |
| DuckDuckGo | [N] | [N] | [High/Med/Low] |
```

### 2. Cross-Campaign Comparison
```markdown
## Campaign Comparison

| Metric | Campaign A | Campaign B | Delta |
|--------|-----------|-----------|-------|
| Total leads | [N] | [N] | [+/-] |
| Enrichment rate | [%] | [%] | [+/-] |
| Send success rate | [%] | [%] | [+/-] |
| Reply rate | [%] | [%] | [+/-] |
| Best source | [Source] | [Source] | — |
| AI provider | [Model] | [Model] | — |
```

### 3. Trend Analysis
- Pipeline growth: leads per week/month
- Conversion rate trends over time
- Source quality degradation or improvement
- Seasonal patterns in enrichment success

### 4. Optimization Recommendations
Based on data analysis, recommend:
- **Best performing sources** — where to focus scraping
- **Optimal batch sizes** — based on enrichment bottlenecks
- **Best AI provider** — which model produces better emails
- **Timing patterns** — when sends have highest success
- **Data quality improvements** — which enrichment gaps to address

## Visualization Support

When creating reports, suggest or generate:
- Funnel charts for conversion stages
- Bar charts for source comparison
- Line charts for trends over time
- Tables for detailed breakdowns

Use Python with matplotlib/plotly for visualization when the user wants charts.

## Integration with Lead Pipeline

- Read campaign data using `LeadStore.load_leads(campaign_id)`
- Parse `CampaignResult` objects for metrics
- Cross-reference `GDPRAuditEntry` for compliance tracking
- Use `Lead.status`, `Lead.data_sources`, `Lead.error` for analysis
- Reference `Company.source` (RegistrySource enum) for source analysis

## Key Rules
- Base all insights on actual data — never fabricate metrics
- Always show sample sizes — "5% conversion" means different things for 10 vs 10,000 leads
- Flag statistical insignificance when sample sizes are too small
- Recommend A/B tests when data is inconclusive
- Present both good and bad news — honest analysis drives improvement
