SOURCES GUIDE

Adding User Transformations in RudderStack

A step-by-step guide on adding your custom transformation function in RudderStack

Introduction

With RudderStack, you can now implement your own custom transformation functions that leverage the event data, to implement specific use-cases based on your business requirements. You can find more information on these user transformation functions in our GitHub repository.

In this guide, we walk you through the steps involved in creating and testing your user transformation functions in RudderStack.

Creating a User Transformation

Please follow the steps mentioned below:

Transformations option on the RudderStack dashboard
  • Click on the Create New option as shown:

Create New option on the Transformations page
  • In the following screen, please enter the name you would like to assign to the custom transformation. Enter the transformation function's code in the Transformation window, as shown in the figure below:

  • You need to add your user transformation's code within the transform function in the Transformation window. You can also add other functions and call them from within transform.

You can copy-paste the entire code of any of the functions in this repository into the Transformation window. Do remember to delete the pre-populated transform function in such cases, before pasting your code.

  • You can also test your transformation function by running a test, as shown:

Accessing Metadata

RudderStack injects a function metadata(event) into your transformations. This allows you to access environment variables that will help in customizing your transformations.

metadata() takes the event as input and returns the metadata of the event. Following are the properties, if available, present in the metadata response.

Property name

Description

sourceId

SOURCE ID from RudderStack dashboard (this is different from write key).

Refer to the image below.

destinationId

DESTINATION ID from the RudderStack dashboard

messageId

Unique ID for each event

sessionId

If sessions are enabled, this has the value of session ID

function transform(events) {
events = events.map(ev => {
const meta = metadata(ev);
ev.sourceId = meta.sourceId
return ev;
});
return events
}

External API requests

You can make any external api requests in your transformer functions and use the response to enrich your events data. RudderStack injects an asynchronuous fetch(url) function into your transformations. It makes an api call to the given url and returns the response in JSON format.

Example of how to use fetch function in transformations

async function transform(events) {
const res = await fetch("any_api_endpoint");
events.map(e=>{
e.response = JSON.stringify(res);
return e;
})
return events;
}

Contact Us

If you want to know more about user transformations in RudderStack, feel free to contact us. You can also talk to us on our Discord channel.