Javascript Enrichment multiple

Hi guys,
Quick question here, im trying to create two JS enrichments, each one of them returns a different payload.

Problem is that is only using one single enrichment.

This is the config:
{
“schema”: “iglu:com.snowplowanalytics.snowplow/javascript_script_config/jsonschema/1-0-0”,
“data”: {
“vendor”: “com.snowplowanalytics.snowplow”,
“name”: “javascript_script_config”,
“enabled”: true,
“parameters”: {
“script”: “BASE64 (Didnt wanted to put the whole code here)”
}
}
}

I thought about renaming the name, but from what i got is mandatory to have the same.

code:
var regex_counter = [{“pattern”: “[^a]fish”}];

function process(event) {
var user_agent = event.getUseragent();
for (const element of regex_counter) {
var regex = new RegExp(element.pattern);
if (regex.test(user_agent)) {
return [{ schema: “iglu:com.company/counter_4/jsonschema/1-0-0”,
data: { counter4Bot: true, counter4BotName: element.pattern } }];
}
}
return [{ schema: “iglu:com.company/counter_4/jsonschema/1-0-0”,
data: { counter4Bot: false, counter4BotName: null } }];
}

Thank you!

You can only have a single JavaScript Enrichment running, this is primarily for performance reasons (and perhaps an architectural one within Enrich too).

If you have mutliple things you want to do in your JavaScript Enrichment, you can make multiple functions and call them both from the process() function, just make sure you merge the contexts together at the end in process() for your return variable.

1 Like

Perfect, I did that and worked. Thanks Paul :sunny: