This article contains example code snippets to get you started creating JS nodes in your call flows.
The JS node is a powerful tool. It lets you add a snippet of JavaScript to do things like processes tags and call information. Unlike parsing JS parsing steps in ring tree targets, JS nodes in call flows are not functions, and do not require the code to return anything. Advanced JS functionality is not supported.
The JS node supports the ES5 version of JavaScript.
Note: The JS node is included in your subscription, but is not available by default. Contact Ringba support to enable JS node for your Ringba account.
This article contains the following examples:
- Example 1: Tag a call when it goes through a specific route
- Example 2: Add leading zeros to months and days
- Example 3: Check if tag exists and add missing tag
- Example 4: Separate a birth date into separate tags
- Example 5: Generate a random age in a range
- Example 6: Isolate the Trusted Form certificate ID
- Example 7: Generate UNIX epoch timestamps
- Example 8: Changing the date format
Example 1: Tag a call when it goes through a specific route
The following snippet lets you add a tag to a call to capture the caller's route through the call flow.
upsertTag('user', 'ivr_input', '1');
You add this JS node after a router node. When you define your different routes, you can use this node to attach the route the call chose as a tag to the call.
Example 2: Add leading zeros to months and days
Some ring tree targets (RTTs) require that one-digit numbers for months and days have a leading zero. If you receive the month or day without the leading zero, you can use this JS node to add it.
var month = getTag('User', 'month');
if (month < 10) {
upsertTag('JS', 'month, 0 + month);
} else {
upsertTag('JS', 'month', month);
}
var day = getTag('User', 'day');
if (day < 10) {
upsertTag('JS', 'day', 0 + day);
} else {
upsertTag('JS', 'day', day);
}
Example 3: Check if tag exists and add missing tag
If a required tag is missing, you can use this JS node to add a default value so the call can proceed without the required tag. In this example, the code checks whether the User:registration_lead_id tag exists. If it doesn't exist, the code inserts the value 123.
var leadid = getTag('User', 'registration_lead_id');
if (leadid == null) {
upsertTag('User', 'registration_lead_id', 123);
}
Example 4: Separate a birth date into separate tags
The following JS code receives a birth date in the format MM/DD/YYYY and separates it into separate tags for month, day, and year.
var olddate2 = getTag('user', 'dob')
var olddate = getTag('user', 'DateofBirth');
if (olddate2 !== null) {
var monthRT = olddate2.substring(0,2);
var dayRT = olddate2.substring(3,5);
var yearRT = olddate2.substring(6,10);
} else if (olddate !== null) {
var monthRT = olddate.substring(0,2);
var dayRT = olddate.substring(3,5);
var yearRT = olddate.substring(6,10);
};
upsertTag('user', 'monthRT', monthRT);
upsertTag('user', 'dayRT', dayRT);
upsertTag('user', 'yearRT', yearRT);
Example 5: Generate a random age in a range
When you need a specific age, but all you have is an age range, you can use the JS node to choose a random number from the range to assign as the age. The following example chooses a random number between 65 and 75.
function randomAgeMaker(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
var randomAge = randomAgeMaker(65, 75);
upsertTag('user', 'age', randomAge);
Example 6: Isolate the Trusted Form certificate ID
You may receive a full Trusted Form certificate URL when all you really need with the certificate ID. For example, the following URL contains the certificate ID:
https://cert.trustedform.com/7c194b77bd8974f988fca7129xx9x999x99x999x
But you want only the last part:
7c194b77bd8974f988fca7129xx9x999x99x999x
The following example accepts the URL in the user:trusted_form tag and generates a new tag, user:tfcertificateid, with only the certificate ID:
var originaltfURL = getTag('user', 'trusted_form');
var tfcertificateid = originaltfURL.substring(29,69);
upsertTag('user', 'tfcertificateid', tfcertificateid);
Example 7: Generate UNIX epoch timestamps
The following example generates a UNIX epoch timestamp and adds it to the call in a tag called [tag:JS:epoch]:
upsertTag('JS', 'epoch',Math.floor(Date.now() / 1000));
Example 8: Changing the date format
In this example, the JS node changes the format of the date/time of the call from the original ISO format to UNIX timestamp.
var date = getTag('Date','ISODate');
var dateObject = new Date(date);
var epochTime = dateObject.getTime();
upsertTag('User','epoch',epochTime);
You could add a JS node to your call flow after a gather node to get the ISO value from the date tag, calculate the UNIX timestamp version, and send the new date/time value with the call.