Functions
JavaScript that runs on every event a pipe carries: transform it, filter it out, or enrich it before delivery
Function types#
Choose the type when you create a function. It cannot be changed later.
| Type | What it does |
|---|---|
| Transform | Reshapes the event and passes it on. |
| Filter | Decides whether the event continues. Dropping an event counts as success, not failure. |
| Enrich | Adds fields to the event without changing what is already there. |
Some integrations, such as Zid, install managed functions for you. Your team can add its own next to them.
Write a function#
- Open Functions and select Write a function.
- Enter a Name, a Slug (the machine name; it follows the name until you change it) and an optional Description of what it changes and why.
- Pick the Type.
- Write the code and select Save function. After saving, only the code can be changed.
A function receives one event and returns it, returns a changed copy, or returns nothing to drop it:
export default async function (event, { log }) {
// Return the event you want the next stage to receive
return event
}Examples#
// Rename a legacy property before it reaches the warehouse
export default async function (event) {
const properties = { ...event.properties }
if (properties.orderId && !properties.transaction_id) {
properties.transaction_id = properties.orderId
delete properties.orderId
}
return { ...event, properties }
}// Drop internal test traffic
export default async function (event) {
const pageUrl = event.context?.page?.url || ""
if (pageUrl.includes("staging.") || event.properties?.test === true) return
return event
}// Tag every event with the store region
export default async function (event) {
return { ...event, store_region: "us-west" }
}Test a function#
Once a function is saved, its page runs the code against a sample event. Nothing is ingested and nothing is delivered. Edit the sample event, and the test environment variables, to cover the cases you care about:
{
"type": "track",
"event": "purchase",
"userId": "user_1234",
"properties": { "transaction_id": "order_98765", "value": 313, "currency": "USD" },
"context": { "page": { "url": "https://shop.example.com/checkout/thanks" } }
}
Attach a function to a pipe#
Open the pipe and go to its Functions tab. A function runs on every event that pipe carries. Function errors appear in Monitoring under Errors.