@sitecore-cloudsdk/personalize/browser
The browser
module provides the following functions to run Sitecore Personalize personalizations from the browser side:
Function |
Description |
---|---|
|
Initializes the module. |
|
Runs interactive experiences and interactive experiments that are live in Sitecore Personalize. |
To run Sitecore Personalize personalization in your JSS Next.js app, you need either XM Cloud Plus, or both XM Cloud and Sitecore Personalize.
init(settings)
This asynchronous function initializes the module. You must run this function before you can run any other functions in the module.
Parameters
Parameter |
Type |
Description |
Required/optional |
---|---|---|---|
|
settings object |
Details about your XM Cloud instance and cookie settings. |
Required |
settings
Attribute |
Type |
Description |
Example |
Required/optional |
---|---|---|---|---|
|
string (UUID) |
Accessible and stored as an environment variable in your app. |
|
Required |
|
string |
The name of the site. Accessible and stored as an environment variable in your app. |
|
Required |
|
boolean |
Flag to set cookies from the browser side. The default is If you're using both the |
|
Optional |
|
string |
Your cookie domain. The cookie domain is the top-level domain of your app. The cookie domain ensures that the Cloud SDK stores cookies in the web browser as first-party cookies. The default is your domain. You must set the same value for this attribute in every Cloud SDK module you initialize. |
|
Optional |
|
integer |
The number of days before the cookie expires. If you don't set a value, the cookie expires according to the You must set the same value for this attribute in every Cloud SDK module you initialize. |
For example, set the value to |
Optional |
|
string |
A URL path that must exist in the requested URL in order to send the cookie. The default is You must set the same value for this attribute in every Cloud SDK module you initialize. |
|
Optional |
Code examples
JSS Next.js
Here's an example [[...path]].tsx
script showing how to initialize the module:
import { useEffect } from "react";
import { init } from "@sitecore-cloudsdk/personalize/browser";
// ...
const SitecorePage = ({
notFound,
componentProps,
layoutData,
headLinks,
}: SitecorePageProps): JSX.Element => {
// ...
// Initialize the module ->
const initPersonalize = async () => {
await init({
sitecoreEdgeContextId: "<YOUR_CONTEXT_ID>",
siteName: "<YOUR_SITE_NAME>",
enableBrowserCookie: true,
});
console.log("Initialized the personalize/browser module.");
};
initPersonalize();
// <- Initialize the module
// ...
};
personalize(personalizationData)
This asynchronous function runs interactive experiences and experiments that are live in Sitecore Personalize.
This function automatically captures UTM parameters from the URL of the webpage where the function runs.
Parameters
Parameter |
Type |
Description |
Required/optional |
---|---|---|---|
|
personalizationData object |
Event and experiment data. You can include guest identifier attributes in this object to run personalization for a specific guest. |
Required |
personalizationData
Attribute |
Type |
Description |
Example |
Required/optional |
---|---|---|---|---|
|
string (uppercase) |
The touchpoint where the user interacts with your brand. For example, for webpages, the channel is |
Must be one of:
|
Required |
|
string (uppercase ISO 4217) |
The alphabetic currency code of the currency the user is using in your app. For example, if the user selects Australian dollars as the currency on your website, the currency is |
|
Required |
|
string |
The unique identifier of a live interactive experience or live interactive experiment that you want to run. To find the friendly ID in Sitecore Personalize, click the live experience or experiment you want to work with, then click Summary. The friendly ID is on the Details pane. |
|
Required |
|
string (uppercase ISO 639-1) |
The language the user is using your app in. For example, if the user selects the Japanese language on your website, the language is The default is |
|
Optional |
|
string (lowercase recommended) |
The email address of the guest. |
Optional | |
object |
The identifiers that are used to identify the users of your app. |
RequestResponse
|
Optional | |
object |
The site visitor's geolocation data. In JSS Next.js apps, leave unset. JSS Next.js apps use |
|
Optional | |
|
object |
An object of your choice. If the URL of the webpage where this function runs contains UTM parameters, those parameters are automatically captured in You can override the automatically captured UTM parameters by specifying values manually in |
|
Optional |
|
|
A list of IDs of personalized page variants. Ensures that the correct variants are rendered for personalization. If unset or an empty array, this property will not be part of the payload. |
|
Optional |
|
integer Minimum |
The number of milliseconds before the function times out and returns an error. For example, set the value to When you use |
|
Optional |
The identifier
object:
Attribute |
Type |
Description |
Example |
Required/optional |
---|---|---|---|---|
|
string |
The unique guest identifier provided by your organization's identity system, such as a Customer Relationship Management (CRM) system. |
|
Required |
|
string |
The name of your organization's identity system, external to XM Cloud, that provided the unique guest identifier. |
|
Required |
The params.utm
object:
Attribute |
Type |
Description |
Example |
Required/optional |
---|---|---|---|---|
|
string |
A product promotion or strategic campaign. |
|
Optional |
|
string |
The element the site visitor clicked that brought them to the website, such as a banner ad or a text link. |
|
Optional |
|
string |
The type of link used, for example, pay-per-click or email. |
|
Optional |
|
string |
The website that sent the traffic. |
|
Optional |
|
string |
Search terms the site visitor searched for. |
|
Optional |
Code examples
JSS Next.js
Here's an example of how to use the personalize
function:
import { useEffect } from "react";
import { init, personalize } from "@sitecore-cloudsdk/personalize/browser";
// ...
const SitecorePage = ({
notFound,
componentProps,
layoutData,
headLinks,
}: SitecorePageProps): JSX.Element => {
// ...
// Initialize the module using init()...
// Run personalization:
const personalizeRes = await personalize({
channel: "WEB",
currency: "EUR",
friendlyId: "running_shoes_popup_02",
});
console.log("Personalized content:", personalizeRes);
};
initPersonalize();
// ...
};
personalizeRes
contains the API response that the experience or experiment was configured to return.
Here's an example of a personalization data object that doesn't contain the email
attribute or the identifier
attribute to identify the guest. In this case, the browser ID, a UUID assigned to every site visitor, is the guest identifier. This personalization data object also contains an optional custom object.
const personalizationData = {
channel: "WEB",
currency: "EUR",
friendlyId: "running_shoes_popup_02",
// optional attributes:
params: { key: "value" }
};
Here's an example of a personalization data object that uses the email
attribute as the guest identifier.
const personalizationData = {
channel: "WEB",
currency: "EUR",
friendlyId: "running_shoes_popup_02",
// guest identifier:
email: "[email protected]"
}
Here's an example of a personalization data object that uses the identifier
attribute as the guest identifier.
const personalization = {
channel: "WEB",
currency: "EUR",
friendlyId: "running_shoes_popup_02",
// guest identifier:
identifier: {
id: "123456",
provider: "BXLP"
}
}
If the URL of the webpage where the personalize
function runs contains UTM parameters, the payload of the personalization data object will contain the UTM parameters. Consider the following URL:
https://myretailsite.com?utm_source=newsletter&utm_medium=email&utm_campaign=summer_sale&utm_term=running+shoes
For this URL, the payload will contain all the UTM parameters in params.utm
:
{
"channel": "WEB",
"currency": "EUR",
"friendlyId": "running_shoes_popup_02",
"params": {
"key": "value",
"utm": {
"source": "newsletter",
"medium": "email",
"campaign": "summer_sale",
"term": "running+shoes"
}
}
}