Code Destination
The code destination is a built-in, platform-agnostic destination that executes custom JavaScript code strings. It provides a lightweight alternative to tag managers like GTM, allowing you to run arbitrary code in response to events without external dependencies.
Setup
Use code: true to enable the built-in code destination:
Configuration Reference
Settings
| Property | Type | Description |
|---|---|---|
init | string | Code to run once when the destination initializes |
on | string | Code to run on lifecycle events (consent, etc.) |
push | string | Default code to run for each event |
pushBatch | string | Default code to run for batched events |
Mapping
Event-specific code can override settings via mapping:
| Property | Type | Description |
|---|---|---|
push | string | Code to run for this specific event |
pushBatch | string | Code to run for batched events |
Context Variables
Each code string has access to specific variables:
init
context.collector- The collector instancecontext.config- Destination configurationcontext.env- Environment variablescontext.logger- Scoped logger instance
push
event- The WalkerOS event objectcontext.collector- The collector instancecontext.config- Destination configurationcontext.data- Transformed event data (from mapping)context.env- Environment variablescontext.logger- Scoped logger instancecontext.mapping- The event mapping rule
pushBatch
batch.key- The batch key (event name)batch.events- Array of events in the batchbatch.data- Array of transformed datacontext.collector- The collector instancecontext.config- Destination configurationcontext.env- Environment variablescontext.logger- Scoped logger instancecontext.mapping- The event mapping rule
on
type- The event type ('consent','ready', etc.)context.collector- The collector instancecontext.config- Destination configurationcontext.data- Event-specific datacontext.env- Environment variablescontext.logger- Scoped logger instance
Examples
Basic Logging
API Calls
Consent Handling
Event-Specific Overrides
Use mapping to override the default push code for specific events:
Batched Events
Error Handling
All code execution is wrapped in try-catch blocks. Errors are logged using the destination's scoped logger and don't affect other destinations or event processing.
Security Considerations
The code destination uses new Function() to execute code strings. This is
similar to eval() and should only be used with trusted code. Never execute
user-provided code strings directly.
For production environments, consider:
- Only using code strings defined in your source code
- Validating and sanitizing any dynamic configuration
- Using Content Security Policy headers where appropriate