One of the key features of RudderStack Transformations is the ability to reuse the transformation code in other transformations using the Libraries feature.

Adding a library

To add a new library, follow these steps:

  1. In the RudderStack dashboard, go to Enhance > Transformations and click the Libraries tab, as shown:
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 import a function rudderEmail from the Is Rudder Email library to filter events that don't have the email address containing the RudderStack domain.

The rudderEmail function is as follows:

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

The following code snippet demonstrates how you can implement this 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

To delete a library, go to Enhance > Transformations and click the Libraries tab. Click the Delete button next to the library you want to delete, as shown:

Deleting a library
You cannot delete a referenced library.

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