Overview
TheAdTracker class manages VAST tracking pixels using a fire-and-forget strategy. It uses navigator.sendBeacon() when available, falls back to fetch() with keepalive, and ultimately to image beacons for maximum compatibility.
Features
- Fire-and-forget: No waiting for responses, no error handling required
- Beacon API: Uses
sendBeacon()for reliable delivery even on page unload - Automatic fallback: Falls back to
fetch(keepalive)and image beacons - Macro replacement: Replaces VAST macros in tracking URLs
- Deduplication: Prevents duplicate event firing with once-only tracking
Constructor
Array of tracking events from VAST (use
VASTParser.aggregateTrackingEvents())Configuration object (all fields optional)
Methods
track()
Track a specific VAST event type.eventType: TrackingEventType- The VAST event type to trackonce?: boolean- Only fire once per event type (default:true)
start- Ad startedfirstQuartile- 25% progressmidpoint- 50% progressthirdQuartile- 75% progresscomplete- 100% progresspause- Playback pausedresume- Playback resumedskip- Ad skippedmute- Audio mutedunmute- Audio unmutedcreativeView- Creative viewedclick- Ad clicked- And more (see TrackingEventType)
- Groups events by type and fires all matching URLs
- Skips duplicates if
onceis true (default) - Replaces macros before firing
- Fires and forgets (no error handling)
firePixel()
Fire a single tracking pixel (fire-and-forget).url: string- Tracking URL to fire
- sendBeacon: If platform supports it (most reliable)
- fetch(keepalive): If sendBeacon fails or unavailable
- Image beacon: Final fallback for maximum compatibility
fireImpressions()
Fire impression pixels.impressionUrls: string[]- Array of impression URLs
play() succeeds).
fireError()
Fire error tracking with error code.errorUrls: string[]- Array of error tracking URLserrorCode: number- VAST error code
[ERRORCODE] macro with the provided code.
updateMacroContext()
Update macro context for URL replacement.context: Partial<MacroContext>- Macro values to merge into context
assetUri- Media file URL (replaces[ASSETURI])adPlayhead- Current ad time (replaces[ADPLAYHEAD])contentPlayhead- Current content time (replaces[CONTENTPLAYHEAD])errorCode- Error code (replaces[ERRORCODE])cachebusting- Random number (replaces[CACHEBUSTING])timestamp- Unix timestamp (replaces[TIMESTAMP])
Macro replacement happens automatically before firing pixels. Update the context as values change (e.g., update
adPlayhead on timeupdate).reset()
Reset fired events (for replay scenarios).Example Usage
Basic Tracking
With Video Player
Error Tracking
Custom Pixel Firing
Replay Scenario
Beacon Methods
sendBeacon()
Most reliable method - guaranteed delivery even on page unload:PlatformAdapter.capabilities.sendBeacon
fetch(keepalive)
Fallback when sendBeacon unavailable:PlatformAdapter.capabilities.fetchKeepalive
Image Beacon
Final fallback for maximum compatibility:Macro Replacement
The tracker automatically replaces VAST macros before firing pixels:[ASSETURI]- Media file URL[ADPLAYHEAD]- Current ad time in seconds[CONTENTPLAYHEAD]- Current content time[ERRORCODE]- VAST error code[CACHEBUSTING]- Random number (auto-generated)[TIMESTAMP]- Unix timestamp (auto-generated)
Platform Compatibility
The tracker handles platform-specific quirks:Beacon API Support
Detected via PlatformAdapter:- Tizen: Full support
- WebOS: Full support
- FireTV: Full support
- Roku: Limited (uses fetch fallback)
- Generic browsers: Depends on browser version
Keepalive Support
Most modern platforms supportfetch(keepalive) as a fallback.
Image Beacon Fallback
Works universally on all platforms.Related Types
See Also
- AdgentSDK - Main player class
- VASTParser - VAST parsing
- replaceMacros - Macro replacement utility
- PlatformAdapter - Platform detection
