1. Developer guides

Set up web personalization

Version:
Important

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 experiences and web experiments. When these personalizations run, they automatically display to the site visitor.

This walkthrough describes how to:

  1. Enable web personalizations - after completing this step, web experiences will run automatically in your app.

  2. Rerun web personalizations (optional)

  3. Capture events in personalized content (optional)

Before you begin
  • 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 events and personalize packages, 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:

  1. In your code editor, in the source code of your web app, locate the file where you initialized the Cloud SDK and the events and personalize packages.

  2. In the initialization code, check that you import the browser modules of the events and the personalize packages:

    // ...
    import "@sitecore-cloudsdk/events/browser";
    import "@sitecore-cloudsdk/personalize/browser";
    // ...
  3. Check that the object in the addPersonalize method sets webPersonalization property's language attribute to 'en':

    CloudSDK({
      /* Your initialization settings */
    })
      .addEvents()
      .addPersonalize({ enablePersonalizeCookie: true, webPersonalization: { language: 'en' } })
      .initialize();
    Important

    Make sure that your initialization code also runs the addEvents method, which initializes the events package. Web personalizations require that both the events and the personalize packages 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:

  1. In your source code, depending on when you want to rerun web personalizations, check if scCloudSDK is available on the window object.

  2. If scCloudSDK is available, run the window.scCloudSDK.personalize.triggerExperiences function.

    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.

Note

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:

  1. In Sitecore Personalize, click Experiences.

  2. Click the web experience you want to work with.

  3. 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.

  4. In the JavaScript, depending on when you want to capture an event, run the applicable event-capturing function. These functions are exposed on the window object and are available on window.scCloudSDK.events.

    For example, to capture a click on a button, you can run the event function:

    window.scCloudSDK.events.event({ type: "myretailsite:CLICKED_BUTTON" });

    This script runs the event function, which sends a custom event named myretailsite:CLICKED_BUTTON to 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:

If you have suggestions for improving this article, let us know!