Use prebuilt transformations to implement specific use cases on your event data.
11 minute read
RudderStack’s transformation templates provide prebuilt JavaScript functions that you can use to create transformations and implement use cases on your event data. These include data cleaning and enrichment, event filtering and sampling, PII management, and more.
Currently, transformation templates are available in JavaScript only.
Python support (applicable for Growth/Enterprise customers) is coming soon.
Using transformation templates
To use transformation templates, go to Collect > Transformations and click Create Transformation.
Under Transformation templates, select the prebuilt template depending on your use case. To write a transformation from scratch, click Custom transformation.
PII management and privacy
Use the following transformation templates to easily apply rules for PII management.
IP Anonymization
This transformation changes the last octet of the IP address to zero. For example, the IP 12.214.31.144 is transformed to 12.214.31.0. This protects the privacy rights of your users and reduces the risk of accidentally disclosing their IP addresses.
This transformation replaces or masks sensitive personal data, for example, email, birthday, or social security number, reducing the risk of accidentally disclosing Personally Identifiable Information (PII). You can specify the properties to be masked or replaced and define the masking logic in the code.
The following transformation masks a social security number with zeros:
Use the following templates for your event filtering and sampling use cases.
Allowlist
This transformation allows events to reach a downstream destination only if a specific property contains certain values.
Some examples of how you can use this template are:
Allow only certain event names, like Product Added or Order Completed.
Send certain event types to a particular destination, like identify or track.
Send only the events that have a tracking plan linked to the source.
The allowlist transformation is shown below:
exportfunctiontransformEvent(event,metadata){constproperty=event.event;// Edit property; here it is event name
constallowlist=["<VALUE>","<OTHER_VALUE>"];// Edit allowlist contents
if(!property||!allowlist.includes(property))return;returnevent;}
Denylist
This transformation blocks the events from reaching a downstream destination if a specific property contains certain values. For example, you can block all Product Added and Order Completed events or block certain event types (identify, track, etc.).
The denylist transformation is shown below:
exportfunctiontransformEvent(event,metadata){constproperty=event.event;// Edit property, here it is event name
constdenylist=["<VALUE>","<OTHER_VALUE>"];// Edit denylist contents
if(property&&denylist.includes(property))return;returnevent;}
Event Sampling
This transformation sends only a subset of events to a downstream destination. You can anonymize the events and select a random subset from the anonymous events; this allows you to work with a small, manageable amount of data in the destination.
Use the following templates for your data enrichment use cases.
Geolocation Enrichment
This transformation enriches events with the geolocation data using an IP-to-geolocation API. This lets you easily query your events based on the geolocation data, for example, country or city.
The transformation is shown below:
exportasyncfunctiontransformEvent(event,metadata){if(event.request_ip){try{constres=awaitfetchV2("<YOUR_API_ENDPOINT>"+event.request_ip);// Use your paid IP-to-geolocation API endpoint.
event.context.geolocation=res.body;}catch{}}returnevent;}
Parse User Agent
This transformation lets you add the user’s browser, engine, OS, device, and CPU-related information to the events. Adding this user metadata to your user events is helpful when you want to perform downstream actions later, for example, filter events coming from mobile devices.
This template uses the standard RudderStack library User Agent Parser which you can use for your custom transformations.
This transformation enriches the events by fetching user data like location, employment, or social media details using the Clearbit API and their email address. It is helpful when sending events with additional user metadata to various downstream destinations.
This transformation lets you programmatically extract the query parameters from a URL. For example, in the URL rudderstack.com/transformations?publish=true, this transformation identifies the query parameter publish.
This transformation lets you use the localized version of the Apple device model present in your event’s context.device.model. It converts the standard Apple device model information (for example, iPhone 13,1) into a human-readable format (iPhone 12 Mini).
This transformation template uses the localizeAppleDeviceModel RudderStack library which leverages the device model mappings based on open source information. If you find any discrepancies, open an issue in the rudder-libraries GitHub repository.
Use the following templates for your data cleaning use cases.
Remove Null Properties
This transformation removes all event properties with null values; this is helpful when you want to reduce the number of unnecessary fields generated in downstream destinations.
This transformation renames the properties to conform with the appropriate naming convention expected by the downstream destination, for example, first_name to firstName.
When using the Auth0 source to generate events from your website, the userId is prefixed with characters containing Auth0 followed by a pipe (|). This transformation removes any Auth0 userId prefixes.
Use the following templates to transform events sent to your webhook destinations:
Dynamic Headers
This transformation sets the HTTP headers for your webhook destinations dynamically. It lets you set the headers based on the values in your data.
The transformation is shown below:
exportfunctiontransformEvent(event,metadata){event.header={Authorization:"Basic <credentials>",// Change headers and values
header_2:"value"};returnevent;}
Dynamic Path
This transformation dynamically appends to the URL specified in your webhook destination configuration. This lets you send events to different paths based on the values in your event data. For example, you can send identify events to only to the /users URL path and the group events only to /organizations.
The following transformation appends a /search?email=${email} path to the webhook URL based on the email property present in the event:
exportfunctiontransformEvent(event,metadata){constemail=event.context?.traits?.email;// Change property
if(email)event.appendPath=`/search?email=${email}`;// Change property and appendPath
returnevent;}
Community contributions
The following transformation templates are contributed by the RudderStack community:
Filter Bot Traffic
This transformation lets you identify and filter events from potential bots by checking an event’s user agent against a list of known bot user agents obtained from crawler-user-agents.json.
exportfunctiontransformEvent(event,metadata){constlower_user_agent=event["context"]["userAgent"]constknown_bot_filter_list=['bot','crawler','spider','feedfetcher-google','mediapartners-google','apis-google','slurp','python-urllib','python-requests','aiohttp','httpx','libwww-perl','httpunit','nutch','go-http-client','biglotron','teoma','convera','gigablast','ia_archiver','webmon ','httrack','grub.org','netresearchserver','speedy','fluffy','findlink','panscient','ips-agent','yanga','yandex','yadirectfetcher','cyberpatrol','postrank','page2rss','linkdex','ezooms','heritrix','findthatfile','europarchive.org','mappydata','eright','apercite','aboundex','summify','ec2linkfinder','facebookexternalhit','yeti','retrevopageanalyzer','sogou','wotbox','ichiro','drupact','coccoc','integromedb','siteexplorer.info','proximic','changedetection','cc metadata scaper','g00g1e.net','binlar','a6-indexer','admantx','megaindex','ltx71','bubing','qwantify','lipperhey','addthis','metauri','scrapy','capsulechecker','sonic','sysomos','trove','deadlinkchecker','slack-imgproxy','embedly','iskanie','skypeuripreview','google-adwords-instant','whatsapp','electricmonk','yahoo link preview','xenu link sleuth','pcore-http','appinsights','phantomjs','jetslide','newsharecounts','tineye','linkarchiver','digg deeper','snacktory','okhttp','nuzzel','omgili','pocketparser','um-ln','muckrack','netcraftsurveyagent','appengine-google','jetty','upflow','thinklab','traackr.com','twurly','mastodon','http_get','brandverity','check_http','ezid','genieo','meltwaternews','moreover','scoutjet','seoscanners','hatena','google web preview','adscanner','netvibes','baidu-yunguance','btwebclient','disqus','feedly','fever','flamingo_searchengine','flipboardproxy','g2 web services','vkshare','siteimprove.com','dareboost','feedspot','seokicks','tracemyfile','zgrab','pr-cy.ru','datafeedwatch','zabbix','google-xrawler','axios','amazon cloudfront','pulsepoint','cloudflare-alwaysonline','google-structured-data-testing-tool','wordupinfosearch','webdatastats','httpurlconnection','outbrain','w3c_validator','w3c-checklink','w3c-mobileok','w3c_i18n-checker','feedvalidator','w3c_css_validator','w3c_unicorn','google-physicalweb','blackboard','bazqux','twingly','rivva','dataprovider.com','theoldreader.com','anyevent','nmap scripting engine','2ip.ru','clickagy','google favicon','hubspot','chrome-lighthouse','headlesschrome','simplescraper','fedoraplanet','friendica','nextcloud','tiny tiny rss','datanyze','google-site-verification','trendsmapresolver','tweetedtimes','gwene','simplepie','searchatlas','superfeedr','freewebmonitoring sitechecker','pandalytics','seewithkids','cincraw','freshrss','google-certificates-bridge','viber','evc-batch','virustotal','uptime-kuma','feedbin','snap url preview service','ruxitsynthetic','google-read-aloud','mediapartners','wget','wget','ahrefsgot','ahrefssiteaudit','wesee:search','y!j','collection@infegy.com','deusu','bingpreview','daum','pingdom','barkrowler','yak','ning','ahc','apache-httpclient','buck','newspaper','sentry','fetch','miniflux','validator.nu','grouphigh','checkmarknetwork','www.uptime.com','mixnodecache','domains project','pagepeeker','vigil','php-curl-class','ptst','seostar.co']if(known_bot_filter_list.map((bot_name)=>lower_user_agent.includes(bot_name)).some((elem)=>!!elem)){return}else{returnevent}}
Timestamp Conversion
This template lets you convert an ISO-8601 timestamp into local date and time based on the user’s timezone.
This site uses cookies to improve your experience while you navigate through the website. Out of
these
cookies, the cookies that are categorized as necessary are stored on your browser as they are as
essential
for the working of basic functionalities of the website. We also use third-party cookies that
help
us
analyze and understand how you use this website. These cookies will be stored in your browser
only
with
your
consent. You also have the option to opt-out of these cookies. But opting out of some of these
cookies
may
have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This
category only includes cookies that ensures basic functionalities and security
features of the website. These cookies do not store any personal information.
This site uses cookies to improve your experience. If you want to
learn more about cookies and why we use them, visit our cookie
policy. We'll assume you're ok with this, but you can opt-out if you wish Cookie Settings.