RudderStack's Transformations feature lets you reuse the transformation code in other transformations using Libraries.
Adding a library
- In the RudderStack dashboard, go to Enhance > Transformations and click the Libraries tab.
- 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 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:
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 > 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.