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:
- In the RudderStack dashboard, go to Enhance > Transformations and click the Libraries tab, as shown:
- Click New library.
- 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:
- 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:
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:
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 importimport getPrice, { getRevenue, getProfit } from "getFinanceData";
// alias importsimport 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:
Contact us
For more information on the topics covered on this page, email us or start a conversation in our Slack community.