JavaScript is a script that runs in the browser to execute various actions. Some common use cases for a JavaScript action block include:

  • Performing mathematical calculations.
  • Dynamically updating variable values.
  • Modifying strings.
  • Triggering and tracking events in Google Analytics.

Select Javascript action block in Replycx bot flow.

Maths Calculations:

Here are some code snippets you can use to perform mathematical calculations within the chatbot flow:

Parameter Snippets Description
Summation (Addition) const additionResult = num1 + num2;
  • Num 1 and Num 2 can be any number or a variable containing a numeric value.
  • The result will be stored in the additionResult variable.
Subtraction (Minus) const additionResult = num1 - num2;
  • Num 1 and Num 2 can be any numeric value or a variable containing a number.
  • The result will be saved in the additionResult variable.
Division (Divide) const additionResult = num1 / num2;
  • Num 1 and Num 2 can be any numeric value or a variable containing a stored number.
  • The result will be stored in the 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

Rendering values into variables:

Here are some snippets you can use to dynamically render values in variables.

Parameter Snippet Example
Set Variable (Conversation)

wn.setConversationVariable(“variable_name”,”variable_value”);

Or

wn.setConversationVariables({“variable_name”:”variable_value”})

wn.setConversationVariable(“name_wn”,”ABC”);

Or

wn.setConversationVariables({“t1”:”123”})

Set Variable (Contact)

wn.setContactVariable(variable_name”,”variable_value”);

Or

wn.setContactVariables({“variable_name”:”variable_value”})

wn.setContactVariable(“name_wn”,”ABC”);

Or

wn.setContactVariables({“t1”:”123”})

String Modification:

Here are some snippets you can use to determine the length of a string or extract a portion of its value.

Parameter Snippet Example
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

Triggering day-based flow:

Here is a snippet you can use to trigger the flow based on a specific day.

Parameter Snippet Description
Get current date const currentDate = new Date(); Helps retrieve today's date.
Check if From date should be more than To date

isFromDateBeforeToDate(fromDate, toDate) {

const fromDateObj = new Date(fromDate);

const toDateObj = new Date(toDate);

return fromDateObj < toDateObj;

}

Helps to trigger the day-specific flow.

Triggering and monitoring events in Google Analytics or any other analytics tool:

Here is a snippet you can use to log or track events in Google Analytics or any other analytics tool.

Tool Snippet Example
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", "Replycx_Test", {

"send_to": "123",

"name": "Test",

"phone": "123456789",

});

JS execution in async

JavaScript can be run asynchronously, allowing the bot to continue the conversation without waiting for the script to complete. This results in a smoother and faster chat experience.

How to enable:
Go to JavaScript block → Enable "Execute asynchronously".

Was this page helpful?