Build a real-time audience in a web experiment

Abstract

How to build a real-time audience (Sitecore Personalize).

If you are familiar with JavaScript, you can follow these instructions to build a real-time audience. Alternatively, you can apply a real-time audience template without needing to write, edit, or view code.

You can use custom fields when building your real-time audience. For example, you might have a web experience that you only want to run for users who are logged in. To cater for this, you can create a custom field called loggedInState. You can then reference the custom field when building your real-time audience, by using the following syntax:

request.params.customField

To avoid errors when including dates and times in JavaScript for Real-Time Audiences, use the moment.js library. The moment.js library enables you to parse, validate, manipulate, and display dates and time formats in JavaScript.

load("classpath:moment.js");

To build a real-time audience:

  1. From the Build screen, in the Audience block, click Add. The Audience slider modal displays.

  2. Click the Real-time Audience block. The window refreshes with a list of audience templates.

  3. Click the Add Custom Code link. The Custom Real-Time Audience screen displays.

  4. Enter the JavaScript that you want Sitecore Personalize to use to determine whether to run the experiment. As you write your JavaScript, ensure that you consider the data limits in real-time audiences.

    The experiment only executes for guests who match the real-time behaviors and guest attributes you define in the JavaScript, and who are members of the selected segment, if applicable.

    For example, the following JavaScript evaluates whether a guest has a data extension with a specific string value. You can use this JavaScript to execute the experiment only for guests that have the data extension with the specified string value.

    (function() {
        for (var i = 0; i < guest.dataExtensions.length; i++) {
            if (guest.dataExtensions[i].name === "[[ Data Extension Name | string]]" &&
                guest.dataExtensions[i].values &&
                guest.dataExtensions[i].values.[
                    [Custom Field Name | string]
                ]) {
                return guest.dataExtensions[i].values.[
                    [Custom Field Name]
                ][
                    [Operator | enum( === , !== )]
                ]
                "[[ Value | string ]]";
            }
        }
        return false;
    })();

    You can use any real-time data made available by an internal service known as the Guest Context Service. As you type your JavaScript, predictive helpers display the attributes you can use from this service.

  5. Click Save. You return to the Audience slider modal.

  6. Click Save. You return to the Build screen.