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:
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)
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.
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-bannerelement. -
replaceHTMLreplaces the existing banner with the personalized banner defined in the variant.
Replacing content exactly (replaceHTMLExact)
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.
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. -
replaceHTMLExactreplaces the originalh1element with the personalized title.
Inserting content before an element (insertHTMLBefore)
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.
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-listelement. -
The promotional banner is inserted directly before the product list, so it appears above it on the page.
Inserting content after an element (insertHTMLAfter)
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.
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-summaryelement. -
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:
<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.
<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.
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.