Like Jitsu? Give us a star on ā­ GitHub!

šŸ“œ Configuration

Configuration UI

šŸ‘©ā€šŸ”¬ Extending Jitsu

Overview
Destination Extensions
Source Extensions
API Specs

Jitsu Internals

JavaScript Functions

Jitsu supports JavaScript language to dynamically evaluate certain destination parameters based on received event. E.g., this applies to Table Names and Filters and WebHook url and JSON body calculation. Jitsu supports JavaScript functions alongside GO text template ā€“ it automatically detects language based on provided syntax.

See also JavaScript Transform for full event transformation using JavaScript

Difference with Go text/template

It is important to understand differences between two approaches:

Go text/template ā€“ text templates engine that uses incoming event data to produce text result based on provided template. Nonetheless, that templates can be easily used for creating JSON objects.

JavaScript Functions ā€“ you can use full power of JavaScript language to decide what to do with incoming event, modify it or build entirely new object for your destinations.

Implementation Details

JavaScript code runs in a context where Jitsu put incoming event as global variables event and short convenient synonyms $ or _

//all 3 expressions are equivalent:
event.event_type
$.event_type
_.event_type //this one better suits Template literals `Welcome, ${_.user?.name}!`

Jitsu automatically converts returned value to the type expected by underlying destination property. E.g.,

  • text string ā€“ for Table Name and HTTP URL
  • JSON ā€“ for HTTP JSON Body

With javascript you can simply return Object where JSON is expected - it will be automatically converted

return {
	"blocks": [
		{
			"type": "header",
			"text": {
				"type": "plain_text",
				"text": "šŸ¤˜" + ` ${_.metric_type} from ${_.user?.email}`,
				"emoji": true
			}
		},
		{
			"type": "section",
			"text": {
				"type": "mrkdwn",
				"text": `User ${_.user?.email} has sent *${_.metric_type}* in <https://example.com?email=${encodeURIComponent(_.user?.email)}>`
			}
		},
		{
			"type": "section",
			"text": {
				"type": "mrkdwn",
				"text": "*Data*:```" +  JSON.stringify(_, null, 4) + "```"
			}
		}
	]
}

Hints & Tips

You can omit return statement for simple expressions

//table name expression that skips events without user's email
$.user?.email && $.metric_type

You can use more complicated things like flow control and loops statements in your code. Also, you can modify provided event object and that will reflect on what actually be sent to destination

//simple Data Masking: filter event from all email occurrences
var emailRegexp = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/
function removeEmails(obj) {
	for (const key in obj) {
		if (typeof obj[key] === "object") {
			removeEmails(obj[key])
		} else if (typeof obj[key] === "string" && obj[key].match(emailRegexp)) {
			delete obj[key]
		}
	}
}
removeEmails($)
return "events_without_emails"