@sitecore-cloudsdk/events/browser
The browser module provides the following functions to capture and send events from the browser side:
|
Function |
Description |
|---|---|
|
|
Initializes the module. |
|
|
Sends a |
|
|
Sends an |
|
|
Sends a |
|
|
Sends a custom event. |
|
|
Adds events with a valid payload to the event queue. |
|
|
Sends all the events in the event queue, in the order they were added. |
|
|
Empties the event queue without sending any of the events. |
|
|
Retrieves the browser ID. |
|
|
Retrieves the guest ID. |
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 |
|---|---|---|
|
|
settings object |
Details about your SitecoreAI instance and cookie settings. |
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
The JSS Next.js app automatically initializes this module in src/lib/context/sdk/events.ts.
Next.js
Here's an example page.tsx script showing how to initialize the module:
pageView([eventData])
This asynchronous function sends a VIEW event.
The VIEW event captures the site visitor's action of viewing a webpage in your app. It triggers every time your webpage loads. You must send VIEW events from all webpages where you want to track site visitor behavior.
JSS apps automatically capture VIEW events, with no coding required.
We recommend that you capture VIEW events on the following webpage types:
-
Home
-
Search results
-
Product description
-
Payment
-
Confirmation
-
Promotional offers
The VIEW event also lets you capture data such as the name of the page where the VIEW event triggered, the referrer URL, UTM parameters, and whether the page view triggered on a personalized page variant created in XM Cloud Pages Personalize.
Parameters
|
Parameter |
Type |
Description |
Required/optional |
|---|---|---|---|
|
|
eventData object |
All the required and optional data about a |
Optional |
eventData
Use the following attributes when creating the eventData object for VIEW events:
|
Attribute |
Type |
Description |
Example |
Required/optional |
|---|---|---|---|---|
|
|
string (uppercase) |
The touchpoint where the user interacts with your brand. For example, for webpages, the channel is If you don't set a value, this attribute will not be part of the event data. |
Must be one of:
|
Optional |
|
|
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 If you don't set a value, this attribute will not be part of the event data. |
|
Optional |
|
|
object |
Any custom data you want to collect about an event in addition to the values you provided in the other attributes in This object can contain maximum 50 custom attributes of your choice. Use a flat object structure. Nested objects will be automatically flattened, and keys will be renamed as necessary. |
|
Optional |
|
|
boolean |
Specify whether to add every UTM parameter from the URL of the current webpage to the event object. The default is |
|
Optional |
|
|
string (uppercase ISO 639) |
The language the user is using your app in. For example, if the user selects the Japanese language on your website, the language is This is a custom value of your choice. For browser-side events, the default value is inferred from the HTML |
|
Optional |
|
|
string |
The name of the webpage where the interaction with your brand takes place. This is a custom value of your choice. For browser-side events, the default value for the root page of your website is |
|
Optional |
|
|
string |
The ID of a personalized page variant. |
|
Optional |
|
|
string or null |
The URI of the webpage that linked to the webpage where the event was captured. For browser-side events, if you don't set a value, it will be inferred from |
|
Optional |
Code examples
JSS Next.js
The JSS Next.js app automatically sends VIEW events from every webpage using the src/components/CdpPageView.tsx component. You can modify this component to send VIEW events from certain pages only.
Here's an example of how to send VIEW events from the /about webpage only. Update your existing code in src/components/CdpPageView.tsx by specifying the webpages you want tracked inside the return statement of the disabled function:
Next.js
Here's an example of how to use the pageView function:
Here's an example eventData object:
By default, VIEW events include UTM parameters. Consider the following URL:
When you send a VIEW event from the previous URL, the event data will contain all the UTM parameters:
identity(eventData)
This asynchronous function sends an IDENTITY event.
The IDENTITY event is used to resolve the identity of an anonymous site visitor of your app and turn them into a known customer. When Sitecore CDP receives an IDENTITY event, it runs a linking algorithm to try to match guest data from separate guest profiles, based on your organization's identity rules. We recommend that you learn about identity resolution in Sitecore CDP before you start sending IDENTITY events.
You can optionally include personally identifiable information (PII), such as email, first name, and last name.
Parameters
|
Parameter |
Type |
Description |
Required/optional |
|---|---|---|---|
|
|
eventData object |
All the required and optional data about an |
Required |
eventData
Use the following attributes when creating the eventData object for IDENTITY events:
|
Attribute |
Type |
Description |
Example |
Required/optional |
|---|---|---|---|---|
|
|
string (uppercase) |
The touchpoint where the user interacts with your brand. For example, for webpages, the channel is If you don't set a value, this attribute will not be part of the event data. |
Must be one of:
|
Optional |
|
|
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 If you don't set a value, this attribute will not be part of the event data. |
|
Optional |
|
array of objects |
The identifiers that are used to identify the users of your app. |
|
Required | |
|
|
object |
Any custom data you want to collect about an event in addition to the values you provided in the other attributes in This object can contain maximum 50 custom attributes of your choice. Use a flat object structure. Nested objects will be automatically flattened, and keys will be renamed as necessary. |
|
Optional |
|
|
string (uppercase ISO 639) |
The language the user is using your app in. For example, if the user selects the Japanese language on your website, the language is This is a custom value of your choice. For browser-side events, the default value is inferred from the HTML |
|
Optional |
|
|
string |
The name of the webpage where the interaction with your brand takes place. This is a custom value of your choice. For browser-side events, the default value for the root page of your website is |
|
Optional |
|
|
string (lowercase recommended) |
The email address of the guest. |
Optional | |
|
|
string (title case) |
The title of the guest. |
|
Optional |
|
|
string (title case recommended) |
The first name of the guest. |
|
Optional |
|
|
string (title case recommended) |
The last name of the guest. |
|
Optional |
|
|
string |
The gender of the guest. |
|
Optional |
|
|
string (ISO 8601) |
The date of birth of the guest. |
|
Optional |
|
|
string |
The mobile number of the guest. |
|
Optional |
|
|
string |
The phone number of the guest. |
|
Optional |
|
|
array of strings (title case recommended) |
The street address of the guest. |
|
Optional |
|
|
string (title case recommended) |
The city address of the guest. |
|
Optional |
|
|
string (title case recommended) |
The state address of the guest. |
|
Optional |
|
|
string (uppercase ISO 3166-1 alpha-2) |
The country address of the guest. |
|
Optional |
|
|
string |
The postal code of the guest. |
|
Optional |
The identifiers array of objects:
|
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 |
|
|
string (ISO 8601) |
The date the unique guest identifier expires. This is determined by your organization's identity system. |
|
Optional |
Code examples
JSS Next.js
Here's an example of how to use the identity function:
Next.js
Here's an example of how to use the identity function:
Here's an example eventData object:
form(formId, interactionType)
This asynchronous function sends a FORM event.
The FORM event is used to capture a user interaction with XM Cloud forms, for example, when the user views or submits the form. Data entered in the form is not collected.
FORM events can't be added to the event queue.
To capture and send behavioral data in a form not created in XM Cloud, you can send a custom event using the event function.
Parameters
|
Parameter |
Type |
Description |
Example |
Required/optional |
|---|---|---|---|---|
|
|
string (UUID) |
The UUID of the XM Cloud form. To find the form ID in XM Cloud Forms, click the form you want to work with, then click Settings. The form ID is in Form ID field. |
|
Required |
|
|
string (uppercase) |
The interaction the user has with the form. |
Must be one of:
|
Required |
Code examples
JSS Next.js
The JSS Next.js app automatically captures FORM events.
Next.js
Here's an example of how to use the form function:
event(eventData)
This asynchronous function sends a custom event with custom data of your choice.
Using a custom event, you can track any custom behavior of your choice, for example, a site visitor's action of clicking a component or adding a product item to their wish list.
Parameters
|
Parameter |
Type |
Description |
Required/optional |
|---|---|---|---|
|
|
eventData object |
All the required and optional data about a custom event. |
Required |
eventData
Use the following attributes when creating the eventData object for custom events:
|
Attribute |
Type |
Description |
Example |
Required/optional |
|---|---|---|---|---|
|
|
string (uppercase) |
The touchpoint where the user interacts with your brand. For example, for webpages, the channel is If you don't set a value, this attribute will not be part of the event data. |
Must be one of:
|
Optional |
|
|
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 If you don't set a value, this attribute will not be part of the event data. |
|
Optional |
|
|
object |
Any custom data you want to collect about an event in addition to the values you provided in the other attributes in This object can contain maximum 50 custom attributes of your choice. Use a flat object structure. Nested objects will be automatically flattened, and keys will be renamed as necessary. |
|
Optional |
|
|
string |
The type of the event. You can set this to any value of your choice that is not a reserved event name. Reserved event names: We recommend that you set this to a unique value by including, for example, your site name in the value. |
|
Required |
Code examples
JSS Next.js
Here's an example of how to use the event function to send a custom event named "myretailsite:CLICKED_PROMO":
Next.js
Here's an example of how to use the event function to send a custom event named "myretailsite:CLICKED_PROMO":
Here's an example eventData object:
addToEventQueue(eventData)
This asynchronous function saves an event with a valid payload to the event queue. After you call this function, you can run the event queue using processEventQueue or empty it using clearEventQueue.
FORM events can't be added to the event queue.
Parameters
|
Parameter |
Type |
Description |
Required/optional |
|---|---|---|---|
|
|
eventData object |
All the required and optional data about an event. |
Required |
eventData
In the eventData object, you must specify the type of the event:
|
Attribute |
Type |
Description |
Example |
Required/optional |
|---|---|---|---|---|
|
|
string |
The type of the event, for example, |
|
Required |
To determine what other attributes you have to or optionally can include in the eventData object, see the eventData object applicable to the type of event you want to save to the event queue:
Code examples
JSS Next.js
Here's an example of how to use the addToEventQueue function to save an event to the event queue.
Next.js
Here's an example of how to use the addToEventQueue function to save events to the event queue.
In this scenario, we add different events to the event queue depending on whether a site visitor clicked a dropdown menu and a dropdown menu item.
In the following order, either the DROPDOWN_CLICK then the DROPDOWN_ITEM_CLICK events are added to the event queue, or the DROPDOWN_CLICK then the DROPDOWN_ABANDON events are added.
processEventQueue()
This asynchronous function:
-
Runs the event queue, sending all the events that are in it.
-
Sends the events in the order they were added to the event queue.
-
Waits for a response from each event before it sends the next event.
Parameters
none
Code examples
JSS Next.js
Here's an example of how to use the processEventQueue function to run the event queue.
Next.js
Here's an example of how to use the processEventQueue function to run the event queue.
In this scenario, we add different events to the event queue depending on whether a site visitor clicked a dropdown menu and a dropdown menu item.
When you run the event queue, either the DROPDOWN_CLICK then the DROPDOWN_ITEM_CLICK events are sent, or the DROPDOWN_CLICK then the DROPDOWN_ABANDON events.
clearEventQueue()
This asynchronous function empties the event queue, removing all the events that are in it, without sending any of the events.
Parameters
none
Code examples
JSS Next.js
Here's an example of how to use the clearEventQueue function to empty the event queue.
Next.js
Here's an example of how to use the clearEventQueue function to empty the event queue.
In this scenario, we add different events to the event queue depending on whether a site visitor clicked a dropdown menu and a dropdown menu item.
If a certain condition is met, you decide to empty the event queue and not send any of the events you previously added to it.
getBrowserId()
This function returns the browser IDbrowser ID, a UUID assigned to every site visitor. This function is used for testing and debugging purposes.
Parameters
none
Code examples
JSS Next.js
Here's an example of how to use the getBrowserId function to log the browser ID to the console:
Next.js
Here's an example of how to use the getBrowserId function to log the browser ID to the console:
getGuestId()
This function:
Parameters
none
Code examples
JSS Next.js
Here's an example of how to use the getGuestId function to log the guest ID to the console:
Next.js
Here's an example of how to use the getGuestId function to log the guest ID to the console: