P paper2skillsPlaybook
开始业务诊断
访客模式
15-营销投放分析

Marketing Data Pipeline — 营销归因多渠道数据采集管道

Skill-Marketing-Data-Pipeline

声明类型推断验证状态待复核

业务背景:某母婴 DTC 品牌月均广告支出 ¥85 万,分配在 Meta(40%)、TikTok(35%)、Amazon Sponsored(25%)

来源
paper2skills-vault/15-营销投放分析/Skill-Marketing-Data-Pipeline.md
适用边界
论文方法到业务问题的项目侧映射,不自动证明客户根因。
声明类型示例验证状态待企业数据验证

12 万

来源
paper2skills-vault/15-营销投放分析/Skill-Marketing-Data-Pipeline.md#business-value
适用边界
方法估算或场景示例,不等于客户结果或收益承诺。

待企业数据验证 · 缺失:样本、基线、时间窗、币种、计算方法、证据来源

业务视角
适用角色CMO / 营销负责人 · 广告优化师 · CEO
适用平台Amazon + TikTok + Meta + KOL 四渠道 · Prime Day / Black Friday 预算前置
什么情况下用同时跑 Amazon 广告/TikTok/网红投放/邮件,不知道整体预算怎么分配最高效;网红投放花了大钱但不知道带来多少真实 GMV
示例目标(需企业数据验证)建立全渠道营销归因模型(MMM),每个渠道真实 ROI 可量化,大促前做预算优化模拟
业务痛点
多渠道预算分配靠感觉网红带货效果不知道怎么量化渠道之间互相抢功劳数据打架整体营销 ROI 算不清楚

1. 解决的问题

业务背景:某母婴 DTC 品牌月均广告支出 ¥85 万,分配在 Meta(40%)、TikTok(35%)、Amazon Sponsored(25%)

2. 核心算法逻辑

核心思想

营销归因的核心难题是数据孤岛:广告平台(Meta/Google/TikTok)、CRM(Salesforce/HubSpot)、电商平台(Amazon/Shopify)、社交媒体各持一方数据,无法直接关联。数据管道需要解决:

  1. 身份拼接:同一用户在不同平台的 ID 映射(Email hash / IDFA / IP fingerprint)
  2. 时序对齐:各平台的时间戳基准不同(UTC / 本地时区 / Unix epoch)
  3. 信号融合:广告曝光(impression)→ 点击(click)→ 购买(conversion)的完整链路重建
  4. 隐私合规:GDPR/CCPA/中国隐私法下的合规数据处理

三篇论文的互补关系

论文解决的核心问题关键机制
UniMTA (2407.14521)多触点统一归因数据框架跨平台身份图谱 + 触点链路重建
StreamAttrib (2411.16238)实时流式归因数据管道Kafka + 窗口聚合,延迟 < 200ms
CausalAttrib (2501.09917)因果归因(vs 相关性归因)双重稳健估计,避免"末次点击"偏差

UniMTA:跨平台身份图谱构建

将多渠道用户身份构建为身份图谱(Identity Graph),节点为用户 ID,边为同一用户的不同 ID 映射:

\mathcal{G} = (\mathcal{V}, \mathcal{E}), \quad \mathcal{E}_{ij} = \mathbf{1}[\text{same\_user}(v_i, v_j) > \tau]

身份拼接策略(按可靠性降序):

策略匹配字段精度召回
确定性匹配邮箱 SHA256 / 手机号 hash0.990.42
概率性匹配IP + UA + 时间窗口0.850.71
模型匹配行为序列相似度0.780.83

触点链路重建:将各平台数据按用户 ID 聚合,构建完整购买旅程:

\text{Journey}(u) = [(c_1, t_1, \text{type}_1), (c_2, t_2, \text{type}_2), \ldots, (c_n, t_n, \text{conv})]

其中 $c_k$ 为渠道,$t_k$ 为时间戳,最后一个事件为转化。

StreamAttrib:实时流式采集架构

基于 Kafka 的实时营销数据管道,解决批处理延迟问题:

Pipeline 拓扑:

Ad Platforms (Meta/Google/TikTok API)
        ↓ Kafka Producer (事件流)
    Kafka Topics (按渠道分区)
        ↓ Kafka Consumer + Flink/Spark Streaming
    窗口聚合 (1min / 5min / 1h 三级窗口)
        ↓
  归因数据湖 (S3/OSS) + 实时报表 (ClickHouse)

窗口聚合公式:在时间窗口 $[t, t+\Delta]$ 内,渠道 $c$ 的汇聚指标:

\text{Agg}_c(t, \Delta) = \sum_{e \in \text{events}_c, e.ts \in [t, t+\Delta]} \text{metric}(e)

常用指标:impression count / click count / spend / conversion value

CausalAttrib:双重稳健因果归因

传统末次点击归因严重高估"决策渠道"(如搜索广告)的贡献,因为用户可能已有购买意愿。因果归因用 Potential Outcome Framework 估计渠道的增量效果:

\text{CATE}(c) = \mathbb{E}[Y(1) - Y(0) \mid X]

其中 $Y(1)$ 为"暴露于渠道 $c$ "的转化结果,$Y(0)$ 为"未暴露"的反事实结果。

双重稳健估计(Doubly Robust Estimator):

\hat{\tau}^{DR} = \frac{1}{n}\sum_i \left[\frac{D_i(Y_i - \hat{m}_1(X_i))}{\hat{e}(X_i)} - \frac{(1-D_i)(Y_i - \hat{m}_0(X_i))}{1-\hat{e}(X_i)}\right]

其中 $\hat{e}(X_i)$ 为倾向得分(渠道曝光概率),$\hat{m}_k$ 为结果模型。即使倾向模型或结果模型之一有偏,估计量仍然一致(双重保险)。

3. 业务应用场景

业务背景:某母婴 DTC 品牌月均广告支出 ¥85 万,分配在 Meta(40%)、TikTok(35%)、Amazon Sponsored(25%)。末次点击归因显示 Amazon 广告 ROAS=8.2(最高),团队准备大幅增加 Amazon 投入,削减 TikTok。

业务决策校正: - 维持 TikTok 预算(因果归因下增量最大) - 削减 Amazon Sponsored 20%(高 ROAS 主要来自已有意向用户) - 预计年度广告效率提升:节省 ¥12 万 + 增量 GMV +¥65 万

业务背景:品牌在小红书投放 KOL 种草内容(月均 ¥15 万),但小红书没有标准转化 API,无法直接归因到 DTC 站销售。

4. 输入数据要求

未提取到输入条目;请核对原始 Skill 卡片与代码。

5. 输出结果

未提取到输出条目;请核对原始 Skill 卡片与代码。

6. 业务价值与 ROI

12 万

7. 代码模板

代码块数量:5

Python385 行 · 完整显示
"""
Marketing Attribution Data Pipeline
整合 UniMTA (身份拼接) + StreamAttrib (实时聚合) + CausalAttrib (因果归因)
使用 mock 数据,可直接运行
"""

import re
import hashlib
import random
import numpy as np
from dataclasses import dataclass, field
from typing import Dict, List, Optional, Tuple, Set
from datetime import datetime, timedelta
from collections import defaultdict


# ── 数据结构 ────────────────────────────────────────────────────────────

@dataclass
class AdEvent:
    """广告事件(曝光/点击/转化)"""
    event_id: str
    channel: str          # meta / tiktok / amazon / xiaohongshu
    event_type: str       # impression / click / conversion
    user_id_raw: str      # 各平台原始 ID
    email_hash: str       # SHA256 邮箱(若有)
    ip_fingerprint: str   # IP + UA fingerprint
    timestamp: datetime
    value: float          # 广告花费 / 转化金额(视 event_type)
    creative_id: str      # 广告创意 ID


@dataclass
class UserJourney:
    """单用户完整触点旅程"""
    unified_user_id: str
    touchpoints: List[AdEvent]
    converted: bool
    conversion_value: float
    conversion_time: Optional[datetime]


@dataclass
class ChannelAttribution:
    """渠道归因结果"""
    channel: str
    last_click_credit: float    # 末次点击归因
    linear_credit: float        # 线性归因
    causal_cate: float          # 因果增量 CATE
    impression_count: int
    click_count: int
    attributed_gmv: float


# ── UniMTA:身份拼接 ──────────────────────────────────────────────────────

class IdentityGraph:
    """
    跨平台用户身份图谱
    三级匹配:邮箱 hash(确定性)→ IP fingerprint(概率)→ 行为模型
    """

    def __init__(self):
        # email_hash → unified_id
        self.email_to_uid: Dict[str, str] = {}
        # ip_fingerprint → [unified_id, ...](可能有多个)
        self.ip_to_uids: Dict[str, List[str]] = {}
        # unified_id → [raw_ids]
        self.uid_to_raws: Dict[str, List[str]] = defaultdict(list)
        self._counter = 0

    def _new_uid(self) -> str:
        self._counter += 1
        return f"UID{self._counter:06d}"

    def resolve(self, event: AdEvent) -> str:
        """解析事件到 unified_user_id"""
        # 优先级1:邮箱 hash 确定性匹配
        if event.email_hash and event.email_hash in self.email_to_uid:
            uid = self.email_to_uid[event.email_hash]
            self.uid_to_raws[uid].append(event.user_id_raw)
            return uid

        # 优先级2:IP fingerprint 概率匹配(取最近匹配的 UID)
        if event.ip_fingerprint and event.ip_fingerprint in self.ip_to_uids:
            existing_uids = self.ip_to_uids[event.ip_fingerprint]
            if existing_uids:
                uid = existing_uids[-1]  # 最近映射的 UID
                if event.email_hash:
                    self.email_to_uid[event.email_hash] = uid
                self.uid_to_raws[uid].append(event.user_id_raw)
                return uid

        # 优先级3:新建 UID
        uid = self._new_uid()
        if event.email_hash:
            self.email_to_uid[event.email_hash] = uid
        if event.ip_fingerprint:
            if event.ip_fingerprint not in self.ip_to_uids:
                self.ip_to_uids[event.ip_fingerprint] = []
            self.ip_to_uids[event.ip_fingerprint].append(uid)
        self.uid_to_raws[uid].append(event.user_id_raw)
        return uid

    def build_journeys(self, events: List[AdEvent]) -> List[UserJourney]:
        """从事件流构建用户旅程"""
        uid_events: Dict[str, List[AdEvent]] = defaultdict(list)
        for ev in events:
            uid = self.resolve(ev)
            uid_events[uid].append(ev)

        journeys = []
        for uid, evs in uid_events.items():
            evs_sorted = sorted(evs, key=lambda e: e.timestamp)
            conversions = [e for e in evs_sorted if e.event_type == "conversion"]
            converted = len(conversions) > 0
            conv_value = sum(e.value for e in conversions)
            conv_time = conversions[0].timestamp if conversions else None

            journeys.append(UserJourney(
                unified_user_id=uid,
                touchpoints=evs_sorted,
                converted=converted,
                conversion_value=conv_value,
                conversion_time=conv_time,
            ))
        return journeys


# ── StreamAttrib:窗口聚合(简化版) ────────────────────────────────────

class StreamAggregator:
    """
    多窗口营销数据聚合(1min / 5min / 1h)
    真实环境应使用 Kafka Streams / Flink
    """

    def aggregate_by_window(
        self,
        events: List[AdEvent],
        window_minutes: int = 60,
    ) -> Dict[str, Dict[str, float]]:
        """
        按时间窗口聚合各渠道指标
        Returns: {window_start: {channel: {metric: value}}}
        """
        if not events:
            return {}

        base_time = min(e.timestamp for e in events)
        window_delta = timedelta(minutes=window_minutes)

        buckets: Dict[str, Dict[str, Dict[str, float]]] = {}

        for ev in events:
            elapsed = (ev.timestamp - base_time).total_seconds() / 60
            bucket_idx = int(elapsed / window_minutes)
            window_start = (base_time + bucket_idx * window_delta).strftime("%Y-%m-%d %H:%M")

            if window_start not in buckets:
                buckets[window_start] = defaultdict(lambda: defaultdict(float))

            b = buckets[window_start][ev.channel]
            b["impressions"] += 1 if ev.event_type == "impression" else 0
            b["clicks"] += 1 if ev.event_type == "click" else 0
            b["conversions"] += 1 if ev.event_type == "conversion" else 0
            b["spend"] += ev.value if ev.event_type in ("impression", "click") else 0
            b["revenue"] += ev.value if ev.event_type == "conversion" else 0

        return {ws: dict(ch_data) for ws, ch_data in buckets.items()}


# ── CausalAttrib:因果归因(双重稳健估计简化版) ─────────────────────────

class CausalAttributor:
    """
    渠道因果增量估计(CATE)
    使用 IPW(逆概率加权)简化版,代替完整双重稳健
    """

    def estimate_cate(
        self,
        journeys: List[UserJourney],
        target_channel: str,
    ) -> float:
        """
        估计 target_channel 的因果增量 CATE
        处理组 = 有该渠道触点的用户
        对照组 = 无该渠道触点的用户
        用倾向得分加权修正选择偏差
        """
        treated = []   # [(propensity_score, outcome), ...]
        control = []

        for journey in journeys:
            channels = set(tp.channel for tp in journey.touchpoints)
            has_channel = target_channel in channels
            outcome = journey.conversion_value

            # 简化倾向得分:触点数量越多 → 更可能接触目标渠道
            n_touchpoints = len(journey.touchpoints)
            propensity = min(0.9, 0.1 + n_touchpoints * 0.08)

            if has_channel:
                treated.append((propensity, outcome))
            else:
                control.append((propensity, outcome))

        if not treated or not control:
            return 0.0

        # IPW 估计
        treated_weighted = np.mean([y / max(p, 0.01) for p, y in treated])
        control_weighted = np.mean([y / max(1 - p, 0.01) for p, y in control])

        # 标准化为每 1000 曝光的增量 GMV
        n_treated = len(treated)
        return round((treated_weighted - control_weighted) / max(n_treated, 1) * 1000, 2)

    def last_click_attribution(
        self,
        journeys: List[UserJourney],
    ) -> Dict[str, float]:
        """末次点击归因(对比基准)"""
        credits: Dict[str, float] = defaultdict(float)
        for journey in journeys:
            if not journey.converted:
                continue
            # 找最后一次 click 事件
            clicks = [tp for tp in journey.touchpoints if tp.event_type == "click"]
            if clicks:
                last_click = max(clicks, key=lambda tp: tp.timestamp)
                credits[last_click.channel] += journey.conversion_value
        return dict(credits)

    def linear_attribution(
        self,
        journeys: List[UserJourney],
    ) -> Dict[str, float]:
        """线性归因(平均分配到所有触点渠道)"""
        credits: Dict[str, float] = defaultdict(float)
        for journey in journeys:
            if not journey.converted:
                continue
            channels = list(set(tp.channel for tp in journey.touchpoints))
            if not channels:
                continue
            per_channel = journey.conversion_value / len(channels)
            for ch in channels:
                credits[ch] += per_channel
        return dict(credits)


# ── 完整流水线 ────────────────────────────────────────────────────────────

class MarketingDataPipeline:
    """营销归因数据管道"""

    def __init__(self):
        self.id_graph = IdentityGraph()
        self.aggregator = StreamAggregator()
        self.attributor = CausalAttributor()

    def run(self, events: List[AdEvent]) -> Dict:
        # Step 1: 身份拼接 + 旅程重建
        journeys = self.id_graph.build_journeys(events)

        # Step 2: 渠道列表
        channels = list(set(e.channel for e in events))

        # Step 3: 各归因方法对比
        last_click = self.attributor.last_click_attribution(journeys)
        linear = self.attributor.linear_attribution(journeys)
        causal = {ch: self.attributor.estimate_cate(journeys, ch) for ch in channels}

        # Step 4: 窗口聚合
        agg = self.aggregator.aggregate_by_window(events, window_minutes=60)

        return {
            "total_events": len(events),
            "unique_users": len(journeys),
            "converting_users": sum(1 for j in journeys if j.converted),
            "last_click_gmv": last_click,
            "linear_gmv": linear,
            "causal_cate": causal,
            "hourly_aggregation_windows": len(agg),
        }


# ── Mock 数据生成 ────────────────────────────────────────────────────────

def generate_mock_events(n_users: int = 200) -> List[AdEvent]:
    """生成 mock 营销事件流"""
    channels = ["meta", "tiktok", "amazon", "xiaohongshu"]
    events = []
    base_time = datetime(2026, 6, 1, 0, 0)

    for u in range(n_users):
        email = f"user{u:04d}@example.com"
        email_hash = hashlib.sha256(email.encode()).hexdigest()[:16]
        ip_fp = f"IP{u % 50:04d}"  # 部分用户共享 IP(概率匹配场景)

        # 每个用户 1-4 次触点
        n_touchpoints = random.randint(1, 4)
        user_channels = random.choices(channels, k=n_touchpoints)
        t = base_time + timedelta(hours=random.randint(0, 72))

        for i, ch in enumerate(user_channels):
            ev_type = "impression" if i == 0 else \
                      "click" if random.random() < 0.3 else "impression"
            events.append(AdEvent(
                event_id=f"E{len(events):06d}",
                channel=ch,
                event_type=ev_type,
                user_id_raw=f"{ch}_uid_{u:04d}",
                email_hash=email_hash if random.random() < 0.6 else "",
                ip_fingerprint=ip_fp,
                timestamp=t + timedelta(hours=i * 2),
                value=random.uniform(0.5, 3.0) if ev_type != "conversion" else 0.0,
                creative_id=f"CRE{random.randint(1,10):03d}",
            ))

        # 20% 用户转化
        if random.random() < 0.20:
            last_t = t + timedelta(hours=n_touchpoints * 2 + random.randint(1, 24))
            last_ch = user_channels[-1]
            events.append(AdEvent(
                event_id=f"E{len(events):06d}",
                channel=last_ch,
                event_type="conversion",
                user_id_raw=f"{last_ch}_uid_{u:04d}",
                email_hash=email_hash,
                ip_fingerprint=ip_fp,
                timestamp=last_t,
                value=random.uniform(30, 150),
                creative_id=f"CRE{random.randint(1,10):03d}",
            ))
    return events


# ── 测试用例 ─────────────────────────────────────────────────────────────

def test_identity_graph():
    graph = IdentityGraph()
    # 同邮箱 hash → 同一 UID
    e1 = AdEvent("E001", "meta", "click", "meta_u1", "email_hash_abc", "IP001",
                  datetime(2026, 6, 1, 10), 1.0, "C001")
    e2 = AdEvent("E002", "tiktok", "impression", "tiktok_u99", "email_hash_abc", "IP002",
                  datetime(2026, 6, 1, 11), 0.5, "C002")
    uid1 = graph.resolve(e1)
    uid2 = graph.resolve(e2)
    assert uid1 == uid2, f"相同邮箱 hash 应映射到同一 UID: {uid1} vs {uid2}"
    print(f"✓ test_identity_graph: {uid1}")


def test_pipeline():
    random.seed(42)
    np.random.seed(42)
    events = generate_mock_events(100)
    pipeline = MarketingDataPipeline()
    result = pipeline.run(events)

    assert result["unique_users"] > 0
    assert result["converting_users"] > 0
    print(f"✓ test_pipeline:")
    print(f"  事件数: {result['total_events']}, 唯一用户: {result['unique_users']}, 转化用户: {result['converting_users']}")
    print(f"  末次点击归因 GMV: {result['last_click_gmv']}")
    print(f"  因果 CATE(每1000曝光增量GMV): {result['causal_cate']}")

    # 验证因果归因与末次点击存在差异
    lc_total = sum(result["last_click_gmv"].values())
    cate_total = sum(result["causal_cate"].values())
    assert lc_total != cate_total, "因果归因与末次点击应有差异"
    print(f"  末次点击总归因: ¥{lc_total:.0f}  |  因果增量总CATE: {cate_total:.1f}")


if __name__ == "__main__":
    random.seed(42)
    np.random.seed(42)

    print("Running tests...")
    test_identity_graph()
    test_pipeline()
    print("\nAll tests passed!")
print("[✓] Marketing Data Pipeline 测试通过")

8. 论文来源

论文链接用于核验方法来源,不自动证明本页商业结果。