Using Javascript Actions


Javascript Actions allow you to write Javascript that executes in a secure, sandboxed Node environment. Javascript Actions have access to the triggering Event, Session, and Profile, as well as context about the Request itself.

Javascript Actions may utilize the node-fetch library to make HTTP requests to external systems, making it possible to do anything from sending a conversion event to an ad network to notifying an external system any just about anything else.

Javascript actions should expose a handler function on the exports object, and the function should accept three arguments.

  • event includes the full event payload and functions to manipulate the Event
  • context includes additional information about the Session and Profile attached to the Event, as well as methods to manipulate each
  • callback is a function that takes no arguments which triggers the end of execution

You must call the callback() method to finish executing the Action

A robust example of a Javascript Action might look like this:

exports.handler = (event, context, callback) => {

    // Set a key/value on the Event's response to the HTTP client
    // Use this to customize things in the browser after an Event is processed
    event.setResponseProperty('property_key', 'property_value');

    // Set a key/value on the Event's "properties" object
    event.setProperty('barfoo', 'foobar');

    // Set a key/value on the Profile's "properties" object
    context.profile.setProperty('awesome', 'sauce');

    // Set a cookie to be returned to the requesting HTTP client
    context.setCookie('cookie_key', 'cookie_value');

    // Make an HTTP request with node-fetch, already included
    fetch('https://api.github.com/users/github')
        .then(res => res.json())
        .then(json => {
            // Set a key/value on the Event's "properties" object
            event.setProperty('payload', json);

            // Finish work and end this Action's execution
            callback();
        });
}

On This Page