服务端追踪与数据回传
High Contrast
Dark Mode
Light Mode
Sepia
Forest
2 min read356 words

服务端追踪与数据回传

浏览器端 Pixel 越来越不可靠——ITP、广告拦截器、隐私法规让客户端追踪丢失 20-40% 的数据。服务端追踪(Server-Side Tracking)是下一代广告效果衡量的基础。

客户端 vs 服务端追踪

graph LR subgraph 客户端追踪 A1[用户浏览器] --> B1[JavaScript Pixel] B1 --> C1[广告平台] end subgraph 服务端追踪 A2[用户浏览器] --> B2[你的服务器] B2 --> C2[Conversions API] C2 --> D2[广告平台] end style B1 fill:#ffcdd2,stroke:#e53935,stroke-width:2px style B2 fill:#c8e6c9,stroke:#43a047,stroke-width:2px

服务端事件管理器

from dataclasses import dataclass, field
from datetime import datetime
from enum import Enum
import hashlib
class EventType(Enum):
PAGE_VIEW = "PageView"
ADD_TO_CART = "AddToCart"
PURCHASE = "Purchase"
LEAD = "Lead"
COMPLETE_REGISTRATION = "CompleteRegistration"
@dataclass
class ServerEvent:
"""服务端转化事件"""
event_type: EventType
event_time: str
user_email_hash: str     # SHA-256 哈希
user_phone_hash: str     # SHA-256 哈希
source_url: str
value: float = 0.0
currency: str = "USD"
order_id: str = ""
event_id: str = ""       # 去重用
class ServerSideTracker:
"""服务端追踪管理器"""
def __init__(self, pixel_id: str):
self.pixel_id = pixel_id
self.event_queue: list[dict] = []
@staticmethod
def hash_pii(value: str) -> str:
"""PII 数据哈希——不传明文"""
normalized = value.strip().lower()
return hashlib.sha256(normalized.encode()).hexdigest()
def track(
self,
event_type: EventType,
email: str = "",
phone: str = "",
value: float = 0.0,
currency: str = "USD",
source_url: str = "",
order_id: str = "",
) -> ServerEvent:
"""记录服务端事件"""
event = ServerEvent(
event_type=event_type,
event_time=datetime.utcnow().isoformat() + "Z",
user_email_hash=self.hash_pii(email) if email else "",
user_phone_hash=self.hash_pii(phone) if phone else "",
source_url=source_url,
value=value,
currency=currency,
order_id=order_id,
event_id=f"{order_id}_{event_type.value}",
)
self.event_queue.append(self._to_payload(event))
return event
def _to_payload(self, event: ServerEvent) -> dict:
"""转换为 API 有效负载格式"""
payload = {
"event_name": event.event_type.value,
"event_time": event.event_time,
"event_id": event.event_id,
"event_source_url": event.source_url,
"user_data": {},
"custom_data": {},
}
if event.user_email_hash:
payload["user_data"]["em"] = event.user_email_hash
if event.user_phone_hash:
payload["user_data"]["ph"] = event.user_phone_hash
if event.value > 0:
payload["custom_data"]["value"] = event.value
payload["custom_data"]["currency"] = event.currency
if event.order_id:
payload["custom_data"]["order_id"] = event.order_id
return payload
def flush(self) -> dict:
"""批量发送事件到 Conversions API"""
batch = self.event_queue.copy()
self.event_queue.clear()
return {
"pixel_id": self.pixel_id,
"events": batch,
"count": len(batch),
# 实际使用时 POST 到广告平台 API
}
# 使用示例:电商购买事件
tracker = ServerSideTracker(pixel_id="PIXEL_123")
tracker.track(
EventType.PURCHASE,
email="customer@example.com",
phone="+60123456789",
value=299.00,
currency="MYR",
source_url="https://shop.example.com/thanks",
order_id="ORD-20240115-001",
)
result = tracker.flush()
print(f"发送 {result['count']} 个事件到 Pixel {result['pixel_id']}")

追踪方案对比

方案 数据完整度 受 ITP 影响 受拦截器影响 实现复杂度
客户端 Pixel ~60-70% ✅ 严重 ✅ 严重
服务端追踪 ~85-95% ❌ 不影响 ❌ 不影响
混合模式 ~90-98% 部分影响 部分影响 中高
数据清洁室 ~95%+ ❌ 不影响 ❌ 不影响

各平台 CAPI 对接

平台 API 名称 数据字段 去重机制
Meta Conversions API em/ph/fbc/fbp event_id 匹配
Google Enhanced Conversions email/phone order_id 匹配
TikTok Events API email/phone/ttclid event_id 匹配
Snapchat CAPI email/phone/scid uuid 去重

本章小结

下一章:ROAS 优化与出价策略