Skip to main content

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:

Loading...

Configuration Reference

Settings

PropertyTypeDescription
initstringCode to run once when the destination initializes
onstringCode to run on lifecycle events (consent, etc.)
pushstringDefault code to run for each event
pushBatchstringDefault code to run for batched events

Mapping

Event-specific code can override settings via mapping:

PropertyTypeDescription
pushstringCode to run for this specific event
pushBatchstringCode to run for batched events

Context Variables

Each code string has access to specific variables:

init

  • context.collector - The collector instance
  • context.config - Destination configuration
  • context.env - Environment variables
  • context.logger - Scoped logger instance

push

  • event - The WalkerOS event object
  • context.collector - The collector instance
  • context.config - Destination configuration
  • context.data - Transformed event data (from mapping)
  • context.env - Environment variables
  • context.logger - Scoped logger instance
  • context.mapping - The event mapping rule

pushBatch

  • batch.key - The batch key (event name)
  • batch.events - Array of events in the batch
  • batch.data - Array of transformed data
  • context.collector - The collector instance
  • context.config - Destination configuration
  • context.env - Environment variables
  • context.logger - Scoped logger instance
  • context.mapping - The event mapping rule

on

  • type - The event type ('consent', 'ready', etc.)
  • context.collector - The collector instance
  • context.config - Destination configuration
  • context.data - Event-specific data
  • context.env - Environment variables
  • context.logger - Scoped logger instance

Examples

Basic Logging

Loading...

API Calls

Loading...
Loading...

Event-Specific Overrides

Use mapping to override the default push code for specific events:

Loading...

Batched Events

Loading...

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.

Loading...

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
💡 Need Professional Support?
Need professional support with your walkerOS implementation? Check out our services.