Set up web personalization
If you installed and initialized the Cloud SDK using all the browser-side code examples, web personalizations already run automatically in your app.
Web personalizations are only available on the browser side.
You can use the Cloud SDK personalize package to run Sitecore Personalize web experiencesweb experiences and web experiments. When these personalizations run, they automatically display to the site visitor.
This walkthrough describes how to:
-
Enable web personalizations - after completing this step, web experiences will run automatically in your app.
-
Have a web experience in Sitecore Personalize to work with.
-
In your code editor, open the root folder of your web app.
-
Install and initialize the Cloud SDK and the
eventsandpersonalizepackages, on the browser side.
Enable web personalizations
The first step to running web personalizations is to enable them in your Cloud SDK initialization code. You wrote this code when you installed and initialized the events and personalize packages on the browser side.
To enable web personalizations:
-
In your code editor, in the source code of your web app, locate the file where you initialized the Cloud SDK and the
eventsandpersonalizepackages. -
In the initialization code, check that you import the
browsermodules of theeventsand thepersonalizepackages: -
Check that the object in the
addPersonalizemethod setswebPersonalizationproperty'slanguageattribute to'en':ImportantMake sure that your initialization code also runs the
addEventsmethod, which initializes theeventspackage. Web personalizations require that both theeventsand thepersonalizepackages are initialized.This code initializes all the required packages and enables web personalizations to run. Web personalizations will now automatically run once, when your web app first loads.
Rerun web personalizations (optional)
After you enabled web personalizations, they automatically run once when your app first loads. Depending on your application requirements, you might want to rerun web personalizations.
To rerun web personalizations:
-
In your source code, depending on when you want to rerun web personalizations, check if
scCloudSDKis available on thewindowobject. -
If
scCloudSDKis available, run thewindow.scCloudSDK.personalize.triggerExperiencesfunction.When this function runs, web experiences will run again.
Capture events in personalized content (optional)
Optionally, you can capture events that occur inside a web experience or web experiment. For example, when a site visitor clicks a button in a personalized pop-up.
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.
Only complete this procedure if you want to capture events in web experiences and web experiments based on your own, custom templates.
To capture events in personalized content:
-
In Sitecore Personalize, click Experiences.
-
Click the web experience you want to work with.
-
Access the JavaScript of the web experience depending on the experience status:
-
In a draft web experience, in the Experience section, click Create variant, then select a template, then click Advanced edit > JavaScript.
-
In a live web experience, click Pause, then click Build > Experience > Edit > Advanced edit > JavaScript.
-
-
In the JavaScript, depending on when you want to capture an event, run the applicable event-capturing function. These functions are exposed on the
windowobject and are available onwindow.scCloudSDK.events.For example, to capture a click on a button, you can run the
eventfunction:This script runs the
eventfunction, which sends a custom event namedmyretailsite:CLICKED_BUTTONto Sitecore when, for example, a site visitor clicks a button in the web experience.
Next steps
You've now successfully enabled web personalizations in your web app.
Next, you can:
-
Learn more about capturing events in personalized content.
-
Learn more about running personalizations.
-
Run Sitecore Personalize interactive experiences.
-
Explore the reference documentation for the
personalizepackage. -
Set up other Sitecore DXP capabilities, such as using tracking to capture
VIEW,IDENTITY, and custom events.