RudderStack's Transformations feature lets you reuse the transformation code in other transformations using Libraries.

Currently, RudderStack supports writing transformation libraries only in JavaScript. Python support will be added soon.

Adding a library

  1. In the RudderStack dashboard, go to Enhance > Transformations and click the Libraries tab.
Adding a library
  1. Click New library.
  2. Add the library Name, Description and write the function you want to reuse across different transformations. You can also add multiple functions in a single library, as shown:
Adding a library
  1. Click Run Test to ensure the library code has the correct syntax.

Using libraries in transformations

To use the libraries in your existing transformations, refer to the Import Library Name option in the RudderStack dashboard, as shown:

Using libraries in transformations
RudderStack converts the library name into camel case without spaces; this becomes your library handle which you can use across multiple transformations. For example, if your library name is Sample Transformation Library, then the library handle would be sampleTransformationLibrary.

You can then use the library in a transformation with a simple import statement. Refer to the below use case for more information.

Use case

Suppose you want to filter the events that don't contain an email address with the RudderStack domain. To do so, you can import a rudderEmail function from the transformation library isRudderEmail.

The rudderEmail function containing the filtering logic is shown below:

export function rudderEmail(email) {
return /@(rudderlabs|rudderstack)| \+ruddertest/.test(email);
}

The following code snippet demonstrates how you can import this function in a transformation:

import { rudderEmail } from "isRudderEmail";
export function transformEvent(event) {
const email =
event.context && event.context.traits && event.context.traits.email;
if (email) {
if (!rudderEmail(email)) return;
}
return event;
}

On clicking Run Test, a sample event not containing the RudderStack email domain is filtered out, as shown:

Filtering out events using libraries in transformation

Importing multiple functions from a single library

The following snippets highlight how to properly import functions from a library:

// ---------------
import { getLoss } from "getFinanceData";
// OR
import { getLoss, getRevenue, getProfit } from "getFinanceData";
import {
getLoss,
getRevenue,
getProfit
} from "getFinanceData";
// For default getPrice import
import getPrice, { getRevenue, getProfit } from "getFinanceData";
// alias imports
import getPrice as gp, { getRevenue as gr, getProfit } from "getFinanceData";
// usage: gp(event), gr(event), getProfit(ev)
import * as GFD from "getFinanceData";
// usage: GFD.getRevenue(ev), GFD.getProfit(ev)
// for default import: GFD.default(ev)

The following snippets highlight the incorrect way of importing the library functions:

// -----------------
import * from "getFinanceData";
getPrice(ev)
// OR
import getPrice as gp from "getFinanceData";
getPrice(ev)

Deleting a library

You cannot delete a library that is referenced or imported in a transformation.

To delete a library, go to Enhance > Transformations > Libraries and click the Delete button next to the library that you want to delete.



Contact us

For more information on the topics covered on this page, email us or start a conversation in our Slack community.

On this page