Campaign Analytics
Production-grade campaign performance analysis with multi-touch attribution modeling, funnel conversion analysis, and ROI calculation. Three Python CLI tools provide deterministic, repeatable analytics using standard library only -- no external dependencies, no API calls, no ML models.
Input Requirements
All scripts accept a JSON file as positional input argument. See assets/sample_campaign_data.json for complete examples.
Attribution Analyzer
CODEBLOCK0
Funnel Analyzer
CODEBLOCK1
Campaign ROI Calculator
CODEBLOCK2
Input Validation
Before running scripts, verify your JSON is valid and matches the expected schema. Common errors:
- - Missing required keys (e.g.,
journeys, funnel.stages, campaigns) → script exits with a descriptive INLINECODE4 - Mismatched array lengths in funnel data (
stages and counts must be the same length) → raises INLINECODE7 - Non-numeric monetary values in ROI data → raises INLINECODE8
Use python -m json.tool your_file.json to validate JSON syntax before passing it to any script.
Output Formats
All scripts support two output formats via the --format flag:
- -
--format text (default): Human-readable tables and summaries for review - INLINECODE12 : Machine-readable JSON for integrations and pipelines
Typical Analysis Workflow
For a complete campaign review, run the three scripts in sequence:
CODEBLOCK3
Use attribution results to identify top-performing channels, then focus funnel analysis on those channels' segments, and finally validate ROI metrics to prioritize budget reallocation.
How to Use
Attribution Analysis
CODEBLOCK4
Funnel Analysis
CODEBLOCK5
Campaign ROI Calculation
CODEBLOCK6
Scripts
1. attribution_analyzer.py
Implements five industry-standard attribution models to allocate conversion credit across marketing channels:
| Model | Description | Best For |
|---|
| First-Touch | 100% credit to first interaction | Brand awareness campaigns |
| Last-Touch |
100% credit to last interaction | Direct response campaigns |
| Linear | Equal credit to all touchpoints | Balanced multi-channel evaluation |
| Time-Decay | More credit to recent touchpoints | Short sales cycles |
| Position-Based | 40/20/40 split (first/middle/last) | Full-funnel marketing |
2. funnel_analyzer.py
Analyzes conversion funnels to identify bottlenecks and optimization opportunities:
- - Stage-to-stage conversion rates and drop-off percentages
- Automatic bottleneck identification (largest absolute and relative drops)
- Overall funnel conversion rate
- Segment comparison when multiple segments are provided
3. campaignroicalculator.py
Calculates comprehensive ROI metrics with industry benchmarking:
- - ROI: Return on investment percentage
- ROAS: Return on ad spend ratio
- CPA: Cost per acquisition
- CPL: Cost per lead
- CAC: Customer acquisition cost
- CTR: Click-through rate
- CVR: Conversion rate (leads to customers)
- Flags underperforming campaigns against industry benchmarks
Reference Guides
| Guide | Location | Purpose |
|---|
| Attribution Models Guide | INLINECODE13 | Deep dive into 5 models with formulas, pros/cons, selection criteria |
| Campaign Metrics Benchmarks |
references/campaign-metrics-benchmarks.md | Industry benchmarks by channel and vertical for CTR, CPC, CPM, CPA, ROAS |
| Funnel Optimization Framework |
references/funnel-optimization-framework.md | Stage-by-stage optimization strategies, common bottlenecks, best practices |
Best Practices
- 1. Use multiple attribution models -- Compare at least 3 models to triangulate channel value; no single model tells the full story.
- Set appropriate lookback windows -- Match your time-decay half-life to your average sales cycle length.
- Segment your funnels -- Compare segments (channel, cohort, geography) to identify performance drivers.
- Benchmark against your own history first -- Industry benchmarks provide context, but historical data is the most relevant comparison.
- Run ROI analysis at regular intervals -- Weekly for active campaigns, monthly for strategic review.
- Include all costs -- Factor in creative, tooling, and labor costs alongside media spend for accurate ROI.
- Document A/B tests rigorously -- Use the provided template to ensure statistical validity and clear decision criteria.
Limitations
- - No statistical significance testing -- Scripts provide descriptive metrics only; p-value calculations require external tools.
- Standard library only -- No advanced statistical libraries. Suitable for most campaign sizes but not optimized for datasets exceeding 100K journeys.
- Offline analysis -- Scripts analyze static JSON snapshots; no real-time data connections or API integrations.
- Single-currency -- All monetary values assumed to be in the same currency; no currency conversion support.
- Simplified time-decay -- Exponential decay based on configurable half-life; does not account for weekday/weekend or seasonal patterns.
- No cross-device tracking -- Attribution operates on provided journey data as-is; cross-device identity resolution must be handled upstream.
Related Skills
- - analytics-tracking: For setting up tracking. NOT for analyzing data (that's this skill).
- ab-test-setup: For designing experiments to test what analytics reveals.
- marketing-ops: For routing insights to the right execution skill.
- paid-ads: For optimizing ad spend based on analytics findings.
营销活动分析
生产级营销活动效果分析,包含多渠道归因建模、漏斗转化分析和ROI计算。三个Python CLI工具仅使用标准库提供确定性、可重复的分析——无外部依赖、无API调用、无机器学习模型。
输入要求
所有脚本接受JSON文件作为位置输入参数。完整示例请参见assets/samplecampaigndata.json。
归因分析器
json
{
journeys: [
{
journey_id: j1,
touchpoints: [
{channel: organic_search, timestamp: 2025-10-01T10:00:00, interaction: click},
{channel: email, timestamp: 2025-10-05T14:30:00, interaction: open},
{channel: paid_search, timestamp: 2025-10-08T09:15:00, interaction: click}
],
converted: true,
revenue: 500.00
}
]
}
漏斗分析器
json
{
funnel: {
stages: [Awareness, Interest, Consideration, Intent, Purchase],
counts: [10000, 5200, 2800, 1400, 420]
}
}
营销活动ROI计算器
json
{
campaigns: [
{
name: Spring Email Campaign,
channel: email,
spend: 5000.00,
revenue: 25000.00,
impressions: 50000,
clicks: 2500,
leads: 300,
customers: 45
}
]
}
输入验证
运行脚本前,请验证JSON格式正确且符合预期模式。常见错误:
- - 缺少必要键(如journeys、funnel.stages、campaigns)→ 脚本退出并显示描述性KeyError
- 漏斗数据数组长度不匹配(stages和counts必须长度相同)→ 引发ValueError
- ROI数据中的非数字货币值 → 引发TypeError
在将JSON传递给任何脚本前,使用python -m json.tool your_file.json验证JSON语法。
输出格式
所有脚本通过--format标志支持两种输出格式:
- - --format text(默认):人类可读的表格和摘要,便于审阅
- --format json:机器可读的JSON,便于集成和流水线处理
典型分析工作流程
为完成完整的营销活动审查,按顺序运行三个脚本:
bash
步骤1 — 归因:了解哪些渠道驱动转化
python scripts/attribution
analyzer.py campaigndata.json --model time-decay
步骤2 — 漏斗:识别潜在客户在转化路径上的流失点
python scripts/funnel
analyzer.py funneldata.json
步骤3 — ROI:计算盈利能力并与行业标准对标
python scripts/campaign
roicalculator.py campaign_data.json
使用归因结果识别表现最佳的渠道,然后聚焦这些渠道的细分进行漏斗分析,最后验证ROI指标以确定预算重新分配的优先级。
使用方法
归因分析
bash
运行全部5个归因模型
python scripts/attribution
analyzer.py campaigndata.json
运行特定模型
python scripts/attribution
analyzer.py campaigndata.json --model time-decay
JSON输出用于流水线集成
python scripts/attribution
analyzer.py campaigndata.json --format json
自定义时间衰减半衰期(默认:7天)
python scripts/attribution
analyzer.py campaigndata.json --model time-decay --half-life 14
漏斗分析
bash
基础漏斗分析
python scripts/funnel
analyzer.py funneldata.json
JSON输出
python scripts/funnel
analyzer.py funneldata.json --format json
营销活动ROI计算
bash
计算所有营销活动的ROI指标
python scripts/campaign
roicalculator.py campaign_data.json
JSON输出
python scripts/campaign
roicalculator.py campaign_data.json --format json
脚本
1. attribution_analyzer.py
实现五种行业标准归因模型,在营销渠道间分配转化贡献:
| 模型 | 描述 | 最佳适用场景 |
|---|
| 首次接触 | 100%贡献归首次互动 | 品牌知名度活动 |
| 末次接触 |
100%贡献归末次互动 | 直接响应活动 |
| 线性 | 所有接触点均等贡献 | 均衡的多渠道评估 |
| 时间衰减 | 近期接触点获得更多贡献 | 短销售周期 |
| 基于位置 | 40/20/40分配(首次/中间/末次) | 全漏斗营销 |
2. funnel_analyzer.py
分析转化漏斗以识别瓶颈和优化机会:
- - 阶段间转化率和流失百分比
- 自动瓶颈识别(最大绝对和相对流失)
- 整体漏斗转化率
- 提供多个细分时的细分对比
3. campaignroicalculator.py
计算包含行业基准的全面ROI指标:
- - ROI:投资回报率百分比
- ROAS:广告支出回报率
- CPA:每次获取成本
- CPL:每次潜在客户成本
- CAC:客户获取成本
- CTR:点击率
- CVR:转化率(潜在客户到客户)
- 标记表现不佳的营销活动与行业基准对比
参考指南
| 指南 | 位置 | 用途 |
|---|
| 归因模型指南 | references/attribution-models-guide.md | 深入探讨5种模型,包含公式、优缺点、选择标准 |
| 营销活动指标基准 |
references/campaign-metrics-benchmarks.md | 按渠道和垂直行业的CTR、CPC、CPM、CPA、ROAS行业基准 |
| 漏斗优化框架 | references/funnel-optimization-framework.md | 逐阶段优化策略、常见瓶颈、最佳实践 |
最佳实践
- 1. 使用多个归因模型 -- 至少比较3个模型以三角验证渠道价值;没有单一模型能讲述完整故事。
- 设置适当的回溯窗口 -- 将时间衰减半衰期与平均销售周期长度匹配。
- 细分漏斗 -- 比较细分(渠道、同期群、地域)以识别绩效驱动因素。
- 首先与自身历史数据对标 -- 行业基准提供背景,但历史数据是最相关的比较。
- 定期运行ROI分析 -- 活跃活动每周一次,战略审查每月一次。
- 包含所有成本 -- 在媒体支出之外,将创意、工具和人工成本纳入计算以获得准确ROI。
- 严格记录A/B测试 -- 使用提供的模板确保统计有效性和清晰的决策标准。
局限性
- - 无统计显著性检验 -- 脚本仅提供描述性指标;p值计算需要外部工具。
- 仅标准库 -- 无高级统计库。适用于大多数营销活动规模,但未针对超过10万次旅程的数据集进行优化。
- 离线分析 -- 脚本分析静态JSON快照;无实时数据连接或API集成。
- 单一货币 -- 所有货币值假定为同一货币;不支持货币转换。
- 简化时间衰减 -- 基于可配置半衰期的指数衰减;不考虑工作日/周末或季节性模式。
- 无跨设备追踪 -- 归因按原样对提供的旅程数据进行操作;跨设备身份解析需在上游处理。
相关技能
- - analytics-tracking:用于设置追踪。非用于分析数据(此为当前技能)。
- ab-test-setup:用于设计实验以测试分析揭示的内容。
- marketing-ops:用于将洞察路由到正确的执行技能。
- paid-ads:用于基于分析结果优化广告支出。