For the complete documentation index, see llms.txt. This page is also available as Markdown.

Functions

Functions are small pieces of JavaScript that run on each event before Chord sends it to a server destination. Use them to change an event, add to it, split it or drop it.

Some things functions are good for:

  • Filtering: drop events you don't want, such as test traffic or internal visits.

  • Normalizing: make event data consistent, for example turn page_view, pageview and pageView into one name.

  • Enriching: add data from another service, such as a customer's loyalty tier.

  • Routing: send an event to a different table based on its properties.

Functions only run for server destinations. They don't run for browser destinations, which send events straight from the visitor's browser. See Browser and server destinations.

For changes that don't need code, such as renaming a property, use property mappings. Mappings run before functions.

What a function looks like

Every function has this shape:

export default async function(event, context) {
  // Your logic here
  return event; // or null to drop the event
}

event is the event being processed. It usually has these fields:

  • event: the event name, such as "Order Completed".

  • type: the event type, such as track, page or identify.

  • userId and anonymousId: who did it.

  • timestamp: when it happened, in ISO 8601 format.

  • properties: the event's own data.

  • context: details such as the page, IP address and browser.

context gives your function a few helpers:

  • context.log.debug, info, warn and error write to the function's logs.

  • context.fetch(url, options) makes web requests, using the standard Fetch API.

  • context.store is a key-value store that keeps data between runs.

What a function returns

  • An object: this becomes the event Chord sends on.

  • An array of objects: each object becomes its own event. Only the last function in a pipeline can return an array.

  • null, false or []: Chord drops the event.

  • Nothing: Chord uses the original event, with any changes you made to it.

Create a function

  1. In Chord, go to CDP and open the Functions tab.

  2. Click Create Your First Function, or + Function if you already have some.

  3. Click the function name at the top left, type a name and click the check mark.

  4. Write your code in the editor.

  5. Click Save.

To edit a function, click the pencil icon next to it in the Actions column. To delete one, open the three-dot menu in the Actions column and click the trash icon.

Test and debug

The editor has a Run button that runs your function on sample data. Two tabs show what happened:

  • Logs shows anything you wrote with context.log. Use it to print event contents and trace your logic.

  • Last Run Result shows what the function returned. You can see whether it dropped the event, changed it or passed it on, along with any errors.

Add a function to a connection

Functions run on connections between a source and a destination.

  1. On the Overview tab, open the destination's menu and click View Connected Streams.

  2. Click the pencil icon next to the connection.

  3. Under Functions pipeline, add functions from your library. They run in the order you list them.

  4. Click Save changes.

You can also set environment variables on a connection, below the function library. Read them in your code like this:

Persistent storage

The store object keeps data between runs. Use it for things like rate limits, caching and keeping state across events.

The expiry (TTL) can be a number of seconds, text such as "30s", "5m", "2h" or "7d", or "inf" for no expiry.

Here is a full example:

Write for an empty store

The store can be empty at any time. A key might not exist, might have expired, or the store might be briefly unavailable. When that happens, your function keeps running. get() returns undefined, and set() and del() do nothing. Chord logs a warning.

Always handle a missing value:

Keep your keys unique

Every function in your workspace shares the same store. One function can read, change or delete another's keys. Start each key with a prefix for its function:

If you run two versions of a function, such as development and production, add an environment variable to the prefix:

Examples

Drop events with no user ID

Only forward some events

Add data from an outside service

Errors

A function can throw a specific error to tell Chord what to do with the event. Each error changes whether the event is sent, whether Chord retries, and whether Chord sends a notification.

What the function does
Event sent to the destination
Retried
Notification

return null, return false or return []

No

No

No

throw new Error(...)

Yes

No

Yes

throw new NoRetryError(...)

No

No

Yes

throw new RetryError(...)

Yes, with whatever the function had done so far

Yes

Yes

throw new RetryError(..., { drop: true })

No

Yes

Yes

To drop an event on purpose, return a value. Don't throw an error. A returned null makes no notification. See Errors and retries for what happens after a retry.

Drop an event on purpose. Return null, false or an empty array. Chord records the event as dropped, logs no error and sends no notification.

Standard Error. Use this for a bug or surprise in your code. The event still goes on to the destination, but Chord records the error and sends a notification. If you want events to keep flowing without a notification, catch the error in your code instead.

NoRetryError. Use this when the event itself has a problem that retrying can't fix, such as a missing required field. Chord drops the event and sends a notification. Don't use it just to filter events.

RetryError. Use this for a temporary problem, such as an outside service being down. Chord retries the event later and sends a notification.

By default, Chord still sends the event on with whatever data was ready when the error happened. The retry then runs the function again, so the destination can get the event twice. To hold the event back until the retry works, pass { drop: true }:

Last updated

Was this helpful?