JavaScript
Executes a javascript code on the client side.
JavaScript is basically a piece of code that can be run on the browser to perform an action.
JavaScript action block is made up of five things:
Performing math calculations
Dynamically rendering the values of the variables
String modification
Firing & tracking events in Google Analytics
Triggering Day-Based Flow
You can choose to run the JavaScript code on CLIENT / SERVER.
1. Math Calculations
Here are snippets you can use to perform the math calculations within the chatbot flow:
Summation (Addition)
const additionResult = num1 + num2;
Num 1 & Num 2 can be any number or variable which has a number stored in it
The result will be stored in additionResult variable
Subtraction (Minus)
const additionResult = num1 - num2;
Num 1 & Num 2 can be any number or variable which has a number stored in it
The result will be stored in additionResult variable
Division (Divide)
const additionResult = num1 / num2;
Num 1 & Num 2 can be any number or variable which has a number stored in it
The result will be stored in additionResult variable
Multiplication (Multiple)
const additionResult = num1 * num2;
Num 1 & Num 2 can be any number or variable which has a number stored in it
The result will be stored in additionResult variable
2. Rendering Values into Variables
Here are snippets you can use to render values in variables:
Set Variable (Conversation)
wn.setConversationVariable(“variable_name”,”variable_value”);
Or
Ally.setConversationVariables({“variable_name”:”variable_value”})
wn.setConversationVariable(“name_wn”,”ABC”);
Or
Ally.setConversationVariables({“t1”:”123”})
Set Variable (Contact)
wn.setContactVariable(“variable_name”,”variable_value”);
Or
Ally.setContactVariables({“variable_name”:”variable_value”})
wn.setContactVariable(“name_wn”,”ABC”);
Or
Ally.setContactVariables({“t1”:”123”})
3. String Modification
Here are snippets you can use to calculate the length of the variable or get part of the variable:
Length of String
const lengthOfString = “variable_name”.length();
const lengthOfString = “some_string_content”.length();
//Output: 19
Calculates the number of characters passed/present in the value
Get part of a string
const partOfString = “variable_name”.slice();
const partOfString = “some_string_content”.slice(5);
Note: We need to pass the starting index from where we need to get the sub-string. The index starts from 0. //Output: string_content Commonly used to pass the phone number without country code
4. Firing + Tracking Events in Google Analytics or any Analytics Tool
Here is a snippet you can use to push or record events into Google Analytics or any other analytics tool:
Google Analytics 4
window.dataLayer = window.dataLayer || [];
function gtag() {
window.dataLayer.push(arguments);
}
gtag("js", new Date());
gtag("config", "YOUR-MEASUREMENT-ID");
gtag("event", "YOUR-EVENT-NAME", {
"send_to": "YOUR-MEASUREMENT-ID"
// Additional parameters that you may want to pass
});
window.dataLayer = window.dataLayer || [];
function gtag() {
window.dataLayer.push(arguments);
}
gtag("js", new Date());
gtag("config", "123");
gtag("event", "Ally_Test", {
"send_to": "123",
"name": "Test",
"phone": "123456789",
});
5. Triggering Day-Based Flow
Here is a snippet you can use to trigger the flow based on a specific day:
Get current date
const currentDate = new Date();
Helps to get today’s date
Check if Fromdate should be more than Todate
isFromDateBeforeToDate(fromDate, toDate) {
const fromDateObj = new Date(fromDate);
const toDateObj = new Date(toDate);
return fromDateObj < toDateObj;
}
Helps to get a specific day flow
Last updated