Skip to main content
Users
CloudPortalLogin
  • 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
AI in Personalize
Glossary
  • 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

Inserting a web experiment in your HTML

When creating a web experiment in Sitecore Personalize, you must specify where the personalized content should appear on the page.

This is done in the JavaScript editor of the variant, where you select an element on the page and use a Sitecore Personalize method to insert or replace content in the Document Object Model (DOM).

In a web experiment variant:

  • The HTML tab defines the content of the experiment (for example, a banner or message)

  • The JavaScript tab determines where that content is displayed on the page.

The following sections show how to select an element on the page and use Sitecore Personalize methods to render personalized content.

Selecting an element on the page

To place your experiment on the page, you must first select the HTML element where the content should appear.

Sitecore Personalize uses document.querySelector() to select elements in the DOM.

If multiple elements match the selector, querySelector always returns the first matching element.

You can select elements using:

  • An ID (for example, #header)

  • A class (for example, .promo-banner)

  • An HTML tag (for example, h1)

  • Combined selectors (for example, .product-card h2)

Example:

RequestResponse
document.querySelector("#header")

This selects the element with the ID header.

Rendering methods

Sitecore Personalize provides several methods that control how the experiment is rendered on the page.

You can use these methods to:

  • replace existing content

  • insert content before an element

  • insert content after an element

The following sections demonstrate how these methods can be used in a web experiment variant.

Replacing existing content (replaceHTML)

Use replaceHTML when you want to replace an existing element on the page with personalized content. This method adds a wrapper <div>.

Example scenario: Replace the homepage banner with a personalized promotion.

RequestResponse
const banner = document.querySelector(".homepage-banner");

replaceHTML(banner, `
  <div class="personalized-banner">
    <h2>Special offer just for you</h2>
    <p>Get 20% off your next purchase.</p>
  </div>
`);

In this example:

  • The script selects the .homepage-banner element.

  • replaceHTML replaces the existing banner with the personalized banner defined in the variant.

Replacing content exactly (replaceHTMLExact)

Use replaceHTMLExact when you want to replace the selected element itself without adding a wrapper <div>.

Example scenario: Replace the main page title with a personalized welcome message.

RequestResponse
const title = document.querySelector("h1");

replaceHTMLExact(title, `
  <h1>Welcome back! Check out your personalized offers.</h1>
`);

In this example:

  • The script selects the page heading h1.

  • replaceHTMLExact replaces the original h1 element with the personalized title.

Inserting content before an element (insertHTMLBefore)

Use insertHTMLBefore when you want to insert personalized content before an existing element on the page.

Example scenario: Insert a promotional banner before the product list.

RequestResponse
const productList = document.querySelector(".product-list");

insertHTMLBefore(productList, `
  <div class="promo-banner">
    Free shipping on orders over €50!
  </div>
`);

In this example:

  • The script selects the .product-list element.

  • The promotional banner is inserted directly before the product list, so it appears above it on the page.

Inserting content after an element (insertHTMLAfter)

Use insertHTMLAfter when you want to insert personalized content after an existing element on the page.

Example scenario: Add a recommendation message below the cart summary.

RequestResponse
const cartSummary = document.querySelector("#cart-summary");

insertHTMLAfter(cartSummary, `
  <div class="recommendation">
    Customers who bought this also liked our premium warranty.
  </div>
`);

In this example:

  • The script selects the #cart-summary element.

  • The recommendation message is inserted after the cart summary, so it appears below it on the page.

Example

This simple example shows how a web experiment variant can add personalized content to a page, even if the page does not already contain a space for that content.

Scenario

A retailer wants to display a promotional message below the site header. The page does not contain a slot for the promotion, so the variant inserts the content dynamically.

Existing element of the page

The website already contains a header element at the top of the page:

RequestResponse
<header class="site-header">...</header>

The variant will insert the promotional message directly below the header.

HTML tab of variant

In the HTML tab, define the content you want to display.

RequestResponse
<div class="promo-message">
  <strong>Welcome back!</strong> Enjoy 10% off your next order.
  <a href="/offers">View offer</a>
</div><header class="site-header">...</header>

This HTML defines the promotional message that will appear on the page.

JavaScript tab of variant

In the JavaScript tab of the variant, select the element where the personalized content should appear and insert the content using one of the Sitecore Personalize rendering methods.

RequestResponse
const header = document.querySelector(".site-header");

insertHTMLAfter(header, `
  <div class="promo-message">
    <strong>Welcome back!</strong> Enjoy 10% off your next order.
    <a href="/offers">View offer</a>
  </div>
`);

In this script:

  • document.querySelector(".site-header") selects the existing header element on the page.

  • insertHTMLAfter() inserts the personalized content directly after the header.

Result on the page

Before the experiment runs, the page contains the standard layout with the header at the top, followed by the page content and footer.

When the variant is applied, the promotional message is inserted directly below the header. As a result, visitors assigned to this variant see the header first, then the promotional message, followed by the rest of the page content and the footer.

Do you have some feedback for us?

If you have suggestions for improving this article,

Privacy policySitecore Trust CenterCopyright © 1999-2026 Sitecore