Skip to main content
Sitecore Documentation
Personalize logo
Users
Developers
Changelog
  • Powered byPowered by
Introduction to Sitecore Personalize
Managing accounts and system settings
Identifying guests
Introduction to experiments
Introduction to experiences
Decisioning
View dashboards
Developer Center
Connecting to an external system
Using client-side JavaScript
Using server-side JavaScript
  • Sitecore Personalize
  • Introduction to experiences
  • Introduction to web experiences in Sitecore Personalize
  • Using custom code in a web experience variant
  • Use JavaScript in a web experience
Edit in Paligo

Use JavaScript in a web experience

Abstract

How to insert dynamic data when using JavaScript in a web experience (Sitecore Personalize).

To make a web experience 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 on your organization's website.

The JavaScript editor lets you run client-side JavaScript in your variant.

Most variants also must pass server-side guest data 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.

Note

The experience 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 experience:

  1. On the Build screen, in the Experience section, click the Edit icon next to the variant you want to configure.

  2. In the template editor, on the JavaScript tab, enter any JavaScript that you want to use in the web experience.

  3. To insert dynamic data into JavaScript, on the Data tab, in the Data Selector search box, enter a guest's email address 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 the star icon to bookmark the guest so you no longer have to search for them.

    Editing the JavaScript to apply personalization in a variant.
  4. To include dynamic data from the decision model response, expand the Decision Model Response tree, click and click Copy Path as JavaScript.

  5. To include dynamic data from the experience response, expand the Experience Response tree, click and click Copy Path as JavaScript.

  6. On the JavaScript tab, paste the data and click Save.

  7. Click to return to the Build screen.

    You must also include the dynamic data when you compose the API response. This ensures that when the experience executes, the server requests the dynamic data and passes it to the variant.

Do you have some feedback for us?

If you have suggestions for improving this article,

Privacy policySitecore Trust CenterCopyright © 1999-2023 Sitecore