Skip to content

Functions

Functions are JavaScript code that runs on every event in transit - between your app and destinations. Use them to filter unwanted events, transform data shapes, or enrich events with external data.

What Functions Can Do

CapabilityExample
FilterDrop bot traffic, test events, internal users
TransformRename fields, change event structure, normalize data
EnrichAdd geolocation, company data, or third-party API lookups
RouteSend different events to different warehouse tables

Writing a Function

Functions are JavaScript that receives an event and returns a modified event (or "drop" to filter it out).

Basic Transform

// Rename event properties to match your warehouse schema
function transform(event, ctx) {
if (event.type === 'track') {
event.properties.source = event.properties.utm_source;
delete event.properties.utm_source;
}
return event;
}

Filter Events

// Drop events from internal users
function transform(event, ctx) {
const internalDomains = ['graph8.com', 'yourcompany.com'];
const email = event.traits?.email || event.properties?.email || '';
if (internalDomains.some(d => email.endsWith(`@${d}`))) {
return 'drop'; // magic string — event is discarded
}
return event;
}

Enrich with External API

// Add company data from a third-party API
async function transform(event, ctx) {
const email = event.traits?.email;
if (!email) return event;
const domain = email.split('@')[1];
const resp = await ctx.fetch(`https://api.example.com/company/${domain}`);
if (resp.ok) {
const company = await resp.json();
event.traits.company_name = company.name;
event.traits.company_size = company.employee_count;
event.traits.industry = company.industry;
}
return event;
}

Strip PII

// Remove sensitive fields before sending to warehouse
function transform(event, ctx) {
delete event.traits?.phone;
delete event.traits?.address;
delete event.properties?.credit_card;
// Hash email for analytics (keep original for CRM)
if (event.traits?.email) {
event.traits.email_hash = ctx.crypto.sha256(event.traits.email);
}
return event;
}

Route to Custom Tables

// Send different events to different warehouse tables
function transform(event, ctx) {
if (event.event === 'purchase') {
event.JITSU_TABLE_NAME = 'purchases';
} else if (event.event === 'page') {
event.JITSU_TABLE_NAME = 'pageviews';
}
return event;
}

Available APIs

Functions run in a sandboxed environment with these APIs:

APIUsageDescription
ctx.fetch(url, options)HTTP requestsCall external APIs (enrichment, validation, webhooks)
ctx.storePersistent storageKey-value store that persists between function calls
ctx.logLoggingWrite to Live Events log for debugging
ctx.geoGeolocationIP-based location data (city, country, region)
ctx.uaUser agentParsed browser and device information
ctx.headersHTTP headersOriginal request headers
ctx.getWarehouseWarehouse queriesQuery your data warehouse from within a function
ctx.sourceSource metadataInformation about the originating stream
ctx.destinationDestination metadataTarget destination details

Managing Functions

  1. Go to Connections > Functions in your graph8 dashboard
  2. Click + Create Function
  3. Write your JavaScript in the code editor
  4. Test with sample events using the built-in debugger
  5. Attach to a stream-destination link

Functions include a built-in editor with syntax highlighting and a debugger that lets you test with sample event payloads.

Return Values

ReturnWhat Happens
event (modified)Modified event is sent to destination
New objectReplaces the original event entirely
"drop"Event is discarded (not sent to any destination)
Array of objectsOne event becomes multiple events
NothingOriginal event is sent unchanged