# Javascript

{% hint style="info" %}
Available on the **PREMIUM** plan and above.
{% endhint %}

Javascript is basically a piece of code that can be run on the browser to perform an action. Some of the cases where a Javascript action block can be used are:&#x20;

* Performing maths calculations.
* Dynamically rendering the values of the variables.&#x20;
* String modification.
* Firing & tracking events in Google Analytics.

<figure><img src="https://360969599-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsrMxU8nO3RjusUiYuXBB%2Fuploads%2F1OxjkKaNPzEQJdLYOOw3%2FCleanShot%202025-12-29%20at%2011.40.48.png?alt=media&#x26;token=0843f65e-d0f7-47b1-8e41-e5fe4e219430" alt=""><figcaption></figcaption></figure>

You can choose to run the Javascript code on:

* **Client** — The code runs on the user’s device (like their browser or phone).
* **Server** — The code runs on your backend server instead of the user’s device.

You can also **execute your JavaScript code asynchronously** without the bot enforcing a delay on the end user. This results in a smoother experience with improved chat speed.

## Math calculations:&#x20;

Here are few of the snippets that you can use to perform the maths calculations within the chatbot flow:&#x20;

<table data-full-width="true"><thead><tr><th width="204.5946044921875" align="center">Parameter</th><th width="230.865478515625" align="center">Snippet</th><th align="center">Description</th></tr></thead><tbody><tr><td align="center">Summation (Addition)</td><td align="center"><em>const additionResult = num1 + num2;</em></td><td align="center"><ul><li>Num 1 &#x26; Num 2 can be any number or variable which has a number stored in it</li><li>The result will be stored in <em>additionResult</em> variable</li></ul></td></tr><tr><td align="center">Subtraction (Minus)</td><td align="center"><em>const additionResult = num1 - num2;</em></td><td align="center"><ul><li>Num 1 &#x26; Num 2 can be any number or variable which has a number stored in it</li><li>The result will be stored in <em>additionResult</em> variable</li></ul></td></tr><tr><td align="center">Division (Divide)</td><td align="center"><em>const additionResult = num1 / num2;</em></td><td align="center"><ul><li>Num 1 &#x26; Num 2 can be any number or variable which has a number stored in it</li><li>The result will be stored in <em>additionResult</em> variable</li></ul></td></tr><tr><td align="center">Multiplication (Multiple)</td><td align="center"><em>const additionResult = num1 * num2;</em></td><td align="center"><ul><li>Num 1 &#x26; Num 2 can be any number or variable which has a number stored in it</li><li>The result will be stored in <em>additionResult</em> variable</li></ul></td></tr></tbody></table>

## Storing values into variables:&#x20;

Here are few of the snippets that you can use to render values in variables.&#x20;

<table><thead><tr><th width="199.9375" align="center">Parameter</th><th align="center">Snippet</th><th align="center">Example</th></tr></thead><tbody><tr><td align="center">Set Variable (Conversation)</td><td align="center"><p><em>wn.setConversationVariable(“variable_name”,”variable_value”);</em><br></p><p><em>Or</em> <br></p><p><em>WotNot.setConversationVariables({“variable_name”:”variable_value”})</em></p></td><td align="center"><p><em>wn.setConversationVariable(“name_wn”,”ABC”);</em></p><p></p><p><em>Or</em></p><p></p><p><em>WotNot.setConversationVariables({“t1”:”123”})</em></p></td></tr><tr><td align="center">Set Variable (Contact)</td><td align="center"><p><em>wn.setContactVariable(“variable_name”,”variable_value”);</em><br></p><p><em>Or</em> <br></p><p><em>WotNot.setContactVariables({“variable_name”:”variable_value”})</em><br></p></td><td align="center"><p><em>wn.setContactVariable(“name_wn”,”ABC”);</em></p><p></p><p><em>Or</em><br></p><p><em>WotNot.setContactVariables({“t1”:”123”})</em></p></td></tr></tbody></table>

## String modification:&#x20;

Here are few of the snippets that you can use to calculate the length of the variable or get part of the variable.&#x20;

<table><thead><tr><th width="208.66143798828125" align="center">Parameter</th><th width="239.998291015625" align="center">Snippet</th><th align="center">Example</th></tr></thead><tbody><tr><td align="center">Length of String</td><td align="center"><em>const lengthOfString = “variable_name”.length();</em></td><td align="center"><p><em>const lengthOfString = “some_string_content”.length();</em></p><p></p><p><em><strong>//Output: 19</strong></em> </p><p></p><p>Calculates the number of characters passed/present in the value</p><p><br></p></td></tr><tr><td align="center">Get part of a string</td><td align="center"><em>const partOfString = “variable_name”.slice();</em></td><td align="center"><p><em>const partOfString = “some_string_content”.slice(5);</em></p><p><br><strong>Note: We need to pass the starting index from where we need to get the sub-string. The index starts from 0.</strong><br><br><strong>//Output: string_content</strong><br><br>Commonly used to pass the phone number without country code</p></td></tr></tbody></table>

## Fire & Track events in Google Analytics:

Here is the snippet that you can use to push or record events into Google Analytics or any other analytics tool

<table><thead><tr><th width="175.04595947265625" align="center">Tool</th><th width="308.5130615234375" align="center">Snippet </th><th align="center"> Example</th></tr></thead><tbody><tr><td align="center">Google Analytics 4</td><td align="center"><p><em>window.dataLayer = window.dataLayer || [];</em></p><p><em>function gtag() {</em></p><p>  <em>window.dataLayer.push(arguments);</em></p><p><em>}</em></p><p><em>gtag("js", new Date());</em></p><p><em>gtag("config", "YOUR-MEASUREMENT-ID");</em></p><p><em>gtag("event", "YOUR-EVENT-NAME", {</em></p><p>  <em>"send_to": "YOUR-MEASUREMENT-ID"</em></p><p>  <em>// Additional parameters that you may want to pass</em></p><p><em>});</em></p></td><td align="center"><p>window.dataLayer = window.dataLayer || [];</p><p>function gtag() {</p><p>  window.dataLayer.push(arguments);</p><p>}</p><p>gtag("js", new Date());</p><p>gtag("config", "123");</p><p>gtag("event", "WotNot_Test", {</p><p>  "send_to": "123",</p><p>  "name": "Test",</p><p>  "phone": "123456789",</p><p>});</p></td></tr></tbody></table>

## Triggering day-based flow:

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

|                   Parameter                  |                                                                                                                   Snippet                                                                                                                  |                   Description                   |
| :------------------------------------------: | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | :---------------------------------------------: |
|               Get current date               |                                                                                                      *const currentDate = new Date();*                                                                                                     |          Helps to get the today’s date          |
| Check if Fromdate should be more than Todate | <p><em>isFromDateBeforeToDate(fromDate, toDate) {</em></p><p>  <em>const fromDateObj = new Date(fromDate);</em></p><p>  <em>const toDateObj = new Date(toDate);</em></p><p>  <em>return fromDateObj < toDateObj;</em></p><p><em>}</em></p> | Helps to trigger the flow the day specific flow |
