Web personalization
You can use the Cloud SDK personalize
package to run web personalizations (web experiences and web experiments). These personalizations are typically created by marketers in Sitecore Personalize using web templates. This type of personalization is used to show personalized content, offers, and operational messages to site visitors.
Enabling web personalizations
When initializing the Cloud SDK, developers can enable web personalizations by initializing the browser
module of the events
and the personalize
packages, and then setting webPersonalization
to true
in the addPersonalize
method:
CloudSDK({
/* Your initialization settings */
})
.addEvents()
.addPersonalize({ enablePersonalizeCookie: true, webPersonalization: true })
.initialize();
Web personalizations require that both the events
and the personalize
packages are initialized.
Web personalization is only available on the browser side, not on the server side.
Behind the scenes, when web personalizations run, a <script>
tag is inserted into the HTML document. This JavaScript contains the personalized content that will be displayed to site visitors. By default, the script loads in your app with async
but without defer
. You can change how the script loads by updating the code that enables web personalizations, such as:
webPersonalization: { async: true, defer: true }
After enabling web personalizations, they automatically run once when your app first loads.
Rerunning web personalizations
When enabled, web personalizations run once when your app first loads, but they don't automatically run again on the same page. This means that you'll need to rerun web personalizations in, for example, the following scenarios:
-
You want to run web personalizations multiple times on the same page, such as when the site visitor fills out a form field or clicks a button.
-
You have a single-page application (SPA) and you need to run personalization after a client-side route change.
You can rerun web personalizations by running window.scCloudSDK.personalize.triggerExperiences
, either in your source code or directly inside the JavaScript of the web experience in Sitecore Personalize. Before running triggerExperiences
, decide when you want web experiences to run again and make sure that scCloudSDK
is available on the window
object.
Capturing events in personalized content
You can run functions available in the Cloud SDK events
package to capture events that occur inside a web personalization. For example, when a site visitor clicks a button in a personalized pop-up. To do this, you need to run the event-capturing functions in the JavaScript inside your web experience or web experiment.
After enabling web personalizations in your app, web experiences and web experiments based on the out-of-the-box Sitecore Library templates automatically capture events, no coding required.