API Reference
Complete reference for the window.TrafficCampaign API and the digitalData schema.
Methods
| Method | Returns | Description |
|---|---|---|
getDigitalData() | Object | Full digitalData object with traffic, campaigns, UTMs |
isAvailable() | boolean | Whether digitalData has been populated |
getParams() | Object | Raw UTM parameters only |
refresh() | Promise<Object> | Force re-fetch from Service Worker |
sendToGA(id) | void | Push traffic data to Google Analytics |
enableDebug() | void | Enable console debug logging |
diagnose() | Promise<Object> | SW health check + data dump |
getDigitalData()
const data = TrafficCampaign.getDigitalData(); // Returns: { trafficDetail: { source: "google", medium: "cpc", campaign: "spring_sale", channel: "Paid Search", provider: "googleAds" }, campaigns: { campaignProvider: { provider: "googleAds", parameter: "gclid", value: "abc123..." }, isCampaign: true }, params: { utm_source: "google", utm_medium: "cpc", gclid: "abc123..." } }
sendToGA(measurementId)
// Pushes campaign data to Google Analytics 4 TrafficCampaign.sendToGA('G-XXXXXXXXXX'); // This fires: gtag('event', 'campaign_detected', { traffic_source: "google", traffic_medium: "cpc", send_to: 'G-XXXXXXXXXX' });
Events
// Listen for when digitalData is ready window.addEventListener('trafficcampaign:ready', (e) => { const digitalData = e.detail; console.log('Source:', digitalData.trafficDetail.source); console.log('Channel:', digitalData.trafficDetail.channel); });
Window Globals
| Global | Type | Description |
|---|---|---|
window.__PRESERVED_DIGITALDATA__ | Object | Full digitalData (UTMs, traffic, campaigns) |
window.__DIGITALDATA_TIMESTAMP__ | number | Unix timestamp when data was processed |
window.__DIGITALDATA_SOURCE__ | string | "service-worker" or "fallback" |
window.TrafficCampaign | Object | Public SDK API |
digitalData Schema
The SDK follows the Event-Driven Data Layer (EDDL) pattern. The digitalData object is vendor-agnostic and can be consumed by any analytics tool (GA4, Adobe, Piwik, etc).
trafficDetail
| Field | Type | Example | Description |
|---|---|---|---|
source | string | "google" | Traffic origin |
medium | string | "cpc" | Marketing channel type |
campaign | string | "spring_sale" | Campaign name |
channel | string | "Paid Search" | Grouped channel classification |
provider | string | "googleAds" | Ad platform (60+ supported) |
Supported Ad Platforms (60+)
Search & Display
- Google Ads (
gclid) - Microsoft Ads (
msclkid) - Criteo, Taboola, Outbrain
Social
- Meta/Facebook (
fbclid) - TikTok (
ttclid) - Pinterest, Snapchat, Reddit, LinkedIn
Deep Dive
⚙️ Service Worker
Fetch interception, TrafficDetector (70+ providers), MacroDetector (9 platforms), IndexedDB persistence, and HTML injection.
🔌 Loader
SW registration with retry logic, capability detection, fallback capture, and the TrafficCampaign public API.
🛡️ Sanitization
7-layer XSS protection with js-xss, prototype pollution blocking, and safe JSON injection for script contexts.