replaceMacros() function handles VAST macro substitution in tracking URLs, converting placeholder tokens like [TIMESTAMP] into actual values.
Function Signature
URL containing VAST macros to replace
Values to substitute for macros
Supported Macros
The SDK supports 8 VAST macros as defined in the IAB specification:| Macro | Replacement | Notes |
|---|---|---|
[TIMESTAMP] | Current Unix timestamp | Always replaced (e.g., 1704067200000) |
[CACHEBUSTING] | Random cache buster | Always replaced (e.g., x8k2j9f4p) |
[ASSETURI] | Media file URI | URL-encoded |
[CONTENTPLAYHEAD] | Content position | HH:MM:SS.mmm format |
[ADPLAYHEAD] | Ad position | HH:MM:SS.mmm format |
[ERRORCODE] | VAST error code | Integer (e.g., 400) |
[BREAKPOSITION] | Position in break | Integer (e.g., 1) |
[ADTYPE] | Ad type | String (e.g., linear) |
[TIMESTAMP] and [CACHEBUSTING] are always replaced, even if no context is provided.Usage Examples
Basic Usage
With Playhead Context
Error Tracking
Complete Context
Integration with AdTracker
The SDK’sAdTracker class automatically uses replaceMacros() internally:
Playhead Format
Playhead macros ([ADPLAYHEAD] and [CONTENTPLAYHEAD]) use the VAST standard format:
0.5seconds →00:00:00.50065.25seconds →00:01:05.2503661.123seconds →01:01:01.123
Types
VastMacro
MacroContext
Implementation Details
Fromsrc/utils/macros.ts:32-87:
- Macros are replaced using string
.replace()with global regex [ASSETURI]is URL-encoded usingencodeURIComponent()- Playhead values are converted from seconds to
HH:MM:SS.mmmformat - Missing context values leave macros unreplaced
- Replacement is idempotent (can be called multiple times safely)
Best Practices
Always provide context
Pass the relevant context when tracking events to ensure accurate reporting
Test with real URLs
Test macro replacement with your actual tracking URLs during development
Log for debugging
Log final URLs in development to verify macro replacement
Handle encoding
Asset URIs are automatically URL-encoded—don’t encode them manually
See Also
- AdTracker API - Uses macros internally
- Error Handling - Error code macros
- Event Handling - Playhead tracking
