> For the complete documentation index, see [llms.txt](https://help.smartico.ai/welcome/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://help.smartico.ai/welcome/products/crm-automation/activities-of-flows/activity-js-code.md).

# Activity: JS Code

Activity "JS Code" provides possibility to execute custom JavaScript code in the browser of the user that is online.

<div data-with-frame="true"><figure><img src="https://77049817-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfS5hl0PiysHtKAKMsQTe%2Fuploads%2F6YROPMrUVfgWGaSRLoJB%2Fr2-003-crop-1-1e01edc1-5a3b2a.png?alt=media" alt=""><figcaption></figcaption></figure></div>

Important to note that in order to be executed, the user should be online on your side, so the activity should be triggered when you are confident that user is on site, for example you can use "Wait for event" with event "User went online", or product specific activities that are done on the web site, e.g. deposit, request for withdrawal, bet, etc.

You can also use "Delivery timeout", in order to overcome cases when user went offline for some period of time, by default 20 seconds. For example:

* You build a campaign that is triggered on the first login during the day and is supposed to execute some JavaScript code.
* There is a chance that right after login, the user is redirected to another page, which means that he was "offline" for a short period of time while navigating to another page.
* Delivery timeout is supposed to handle such cases and deliver the JavaScript code to the user the next time they are online within the defined period.

### Activity Configuration

When adding a **JS Code** activity to a Campaign Flow, configure the following fields:

* **JavaScript code to execute**: The JavaScript snippet to run in the user's browser.
* **Delivery timeout**: The time duration (default: 20 seconds) the system will wait for an offline user to reconnect before marking the execution as timed out.

Some ideas of what you can do with "JS Code"

* Use own JavaScript methods, for example trigger opening of the deposit page
* Use 3rd party libraries APIs, e.g. trigger an event in the Google Analytics or start browser screen recording with HotJar or with Microsoft Clarity, or mark conversion with FaceBook AD API.
* Use [Smartico front-end API (extended integration)](/welcome/technical-guides/front-end-integration/extended-integration.md) to trigger different actions, for example to change an avatar of user - `_smartico.setAvatar("https://somesite.com/avatar.png");`
* Use [Smartico front-end API](https://github.com/smarticoai/public-api/blob/main/docs/classes/WSAPI.md) to do any actions with gamification setup, for example register in the tournament when user does a deposit with code like `_smartico.api.registerInTournament(12345)` or opt-in user in the specific mission with code like `_smartico.api.requestMissionOptIn(12345)`

### Dynamic Tag Replacement

Smartico resolves dynamic tags (such as user attributes, label tags, and event parameters) before sending the JavaScript code to the user's browser.

You can insert tags like `{{state.user_first_name}}` or `{{event.amount}}` directly inside your script. When assigning text values to JavaScript variables, enclose the tag in quotation marks (for example: `const player = "{{state.user_first_name}}";`) so the substituted value forms valid JavaScript syntax.

### Outgoing connections

The JS Code step can have these outgoing connections:

<div data-with-frame="true"><figure><img src="https://77049817-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfS5hl0PiysHtKAKMsQTe%2Fuploads%2FoBJRT8FLNdk5L3DE2kar%2Fdc9cdc2069-js-code-365f03a0-16f9bb.png?alt=media" alt="Outgoing connections"><figcaption><p>Outgoing connections of JS Code in a flow. The numbers match the table below.</p></figcaption></figure></div>

| # | Connection   | When the player goes this way                                                                                                                                                                                    |
| - | ------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 1 | **Then**     | After the script is sent to the player's browser (or queued while the player is offline). It is not sent when the account is not active.                                                                         |
| 2 | **Executed** | The player's browser ran the script without an error.                                                                                                                                                            |
| 3 | **Failed**   | The script threw an uncaught error in the browser, the browser did not report back before the **Delivery timeout** (20 seconds by default; for example the player stayed offline), or the account is not active. |

### Frequently Asked Questions

**What is the difference between the JS Code flow activity, JS Injection in Label Settings, and Label Tag JavaScript functions?**

* **JS Code activity**: Executes in the user's browser in real time when reached during a Campaign Flow.
* **JS Injection (Label Settings)**: Executes once in the browser on page load when the Smartico script initializes on your website.
* **Label Tag JavaScript functions**: Evaluated on the server to generate dynamic text values for tags and templates; they do not have access to browser objects such as window or document.

**What happens if a user is offline when the JS Code activity runs?**

The code delivery remains pending until the user comes online or until the configured **Delivery timeout** expires. If the timeout expires before the user connects, the activity fails with an execution timeout and continues down the **Failed** branch.

**Can I update user profile properties directly from the JS Code activity?**

To update player attributes from a campaign flow, use the dedicated **Update property** flow activity. If you need to trigger updates from client-side JavaScript, use the front-end profile update methods for allowed properties.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://help.smartico.ai/welcome/products/crm-automation/activities-of-flows/activity-js-code.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
