---
title: "Use JavaScript in a web experiment"
slug: "use-javascript-in-a-web-experiment"
abstract: "How to insert dynamic data when using JavaScript in a web experiment (Sitecore Personalize)."
---
# Use JavaScript in a web experiment

To make a web experiment to go-live on your organization's site, you must specify in the JavaScript editor where in the Document Object Model (DOM) you want the offer or content to display. This is how you determine where the [HTML will be inserted and rendered](https://doc.sitecore.com/personalize/en/users/sitecore-personalize/introduction-to-experiments-in-sitecore-personalize/introduction-to-web-experiments-in-sitecore-personalize/create-a-web-experiment/inserting-a-web-experiment-in-your-html.html "Inserting a web experiment in your HTML") on your organization's website.

The JavaScript editor lets you [run client-side JavaScript](https://doc.sitecore.com/personalize/en/users/sitecore-personalize/using-client-side-javascript-in-personalization.html "Using client-side JavaScript in personalization") in your variant.

Most variants also must [pass server-side guest data](https://doc.sitecore.com/personalize/en/users/sitecore-personalize/using-server-side-javascript-in-sitecore-personalize.html "Using server-side JavaScript in Sitecore Personalize") to your organization's website, for example, the guest's first name, location, loyalty number, and so on. To facilitate this, you must configure the API response to include the dynamic data so that when the variant executes, the data is requested from the server and then passed to the variant.

You can use JavaScript to access nested values by index from the API response. For example, you might want to display Offers in an experiment that are returned from a Decision Model. Because the offer is a value that is nested in an array, you must use the `array.index` or `array.{index}` syntax.

> [!note]
> The experiment can call server-side data such as guest attributes, and use it in the CSJS. This can include a full history of the guest as well as a continuous stream of real-time session data.

To use JavaScript in a web experiment:

1. In the **Experiment variants** tile, click the **Edit** icon next to the variant you want to configure.
2. In the code editor, on the **JavaScript** tab, enter any JavaScript that you want to use in the web experiment. To automatically format and indent the JavaScript code, click ![Format JS icon](https://doc.sitecore.com/api/markdown-image/personalize/en/users/sitecore-personalize/images/preetify.png).
3. To insert dynamic data into JavaScript, on the **Data** tab, in the **Data Selector** search box, enter a guest's email address or other identifier to search. This enables you to access data model attributes and paths.

    > [!tip]
    > If there is a guest whose data you often use when configuring or testing code, click ![Bookmark icon](https://doc.sitecore.com/api/markdown-image/personalize/en/users/sitecore-personalize/images/bookmark.png) to bookmark the guest so you no longer have to search for them.

    ![Editing the JavaScript to apply personalization in a variant.](https://doc.sitecore.com/api/markdown-image/personalize/en/users/sitecore-personalize/images/Use_HTML_custom_code.png)

4. To include dynamic guest data, under the **Guest data model** tree, click ![Copy icon](https://doc.sitecore.com/api/markdown-image/personalize/en/users/sitecore-personalize/images/copy_data.png) and click **Copy Path as JavaScript**.

    You can use any part of the guest data model in your JavaScript, including data extensions and segment memberships, as shown below:

    ![Using segment memberships from the guest data model in the JavaScript.](https://doc.sitecore.com/api/markdown-image/personalize/en/users/sitecore-personalize/images/guestsegmentmembership1.png)

    In this example, the code uses `guest.segmentMemberships.name` to determine if the guest belongs to either the `rewards_member` segment or the `vip_tier` segment.

    > [!note]
    > Guest attributes are not available as a global JavaScript object. Direct dot notation access such as `guest.firstName` is not supported and will result in a Reference Error because the guest object is not defined in the JavaScript execution scope. To access guest attributes in JavaScript, use Handlebars interpolation:
    >
    > const firstName = "{{guest.firstName}}";
    >
    > Handlebars expressions are evaluated before the script runs, making them the supported method for retrieving guest data in JavaScript‑based variants. Autocomplete and `Copy Path as JavaScript` may suggest paths like `guest.firstName`, but these refer to the data model path, not the JavaScript variable scope, and must be wrapped in Handlebars syntax when used inside JavaScript.

5. To include dynamic data from the decision model response, expand the **Decision Model Response** tree, click ![Copy icon](https://doc.sitecore.com/api/markdown-image/personalize/en/users/sitecore-personalize/images/copy_data.png) and click **Copy Path as JavaScript**.
6. To include dynamic data from the experiment response, expand the **Experience Response** tree, click ![Copy icon](https://doc.sitecore.com/api/markdown-image/personalize/en/users/sitecore-personalize/images/copy_data.png) and click **Copy Path as JavaScript**.
7. On the **JavaScript** tab, paste the data and click **Save**.
8. Click ![back.png](https://doc.sitecore.com/api/markdown-image/personalize/en/users/sitecore-personalize/images/back.png) to close the code editor.

    You must also include the dynamic data when you compose the API request. This ensures that when the experiment executes, the [server requests the dynamic data](https://doc.sitecore.com/personalize/en/users/sitecore-personalize/using-server-side-javascript-in-sitecore-personalize.html "Using server-side JavaScript in Sitecore Personalize") and passes it to the variant.