Capture custom events
Using the Cloud SDK, you can track site visitors' behavior by capturing events on your site in real time. The captured events contain data that Sitecore uses for, for example, website analytics. JSS Next.js apps automatically capture certain events, and you can capture other events by using the browser
module of the Cloud SDK events
packagepackage.
In JSS Next.js apps, the events
package is already installed.
To get started with the events
package, you start capturing custom events from the browser (client-side tracking) when site visitors click the Promo component on your webpage. You then log in to Sitecore CDP to find the event and all the collected data about your site visitor.
This walkthrough describes how to:
-
Make sure your XM Cloud site has site identifiers.
-
Make sure your XM Cloud site's webpage has a component to capture custom events from. This webpage must be published so the component appears on the website. This walkthrough uses the Promo component as an example.
-
Make sure you have access to Sitecore CDP, and that debug mode is enabled.
-
Make sure you're prepared for development with the Cloud SDK.
-
In your code editor, open the rendering app root folder
sxastarter
.
Import context into your component
The first step to capturing custom events is to import the context
instance into the component where you want to track site visitors' behavior. The context
instance is used by JSS Next.js apps to automatically capture certain events, and you can use it to capture events of your choice.
To import the context
instance into the Promo component:
-
In your code editor, in the
sxastarter
folder, opensrc/components/Promo.tsx
.Promo.tsx
is the source code for the Promo component that marketers can add to a webpage in XM Cloud Pages. -
In
Promo.tsx
, importcontext
:RequestResponseimport { context } from "lib/context";
-
Open
src/lib/context/sdk/events.ts
.events.ts
initializes thebrowser
modulemodule of the Cloud SDKevents
package in production mode, so your JSS Next.js app can automatically capture certain events, but not in development mode. Because you're currently in development mode, you have to update this file in order to test event capturing. -
In
events.ts
, inside theinit
function, temporarily comment out theif
statement that checks whether you're in development mode:RequestResponse// Comment out the following lines: if (process.env.NODE_ENV === 'development') throw 'Browser Events SDK is not initialized in development environment';
Your event capturing code will now run in development mode.
Capture events in your component
After importing context
into Promo.tsx
, continue editing the component by having it capture custom events based on site visitor clicks.
To capture events in your component:
-
In
Promo.tsx
, in theDefault
component, within thereturn
statement, add a ReactonClick
event handler to the topmost JSX element:RequestResponse// ... return ( <div onClick={handleClick} className={"..."} id={"..."} > {/*...*/} )
NoteIn production, place the event listener on a clickable element such as a
<button>
, instead of a<div>
. -
In the
Default
component, on the first line of the function body, paste the following code, then save your changes:RequestResponsefunction handleClick() { context .getSDK("Events") .then((Events) => Events.event({ type: "myretailsite:CLICKED_PROMO" }) ) .catch((e) => console.debug(e)); console.log("Sent custom event."); };
This code runs the
event
function every time the Promo component is clicked, and sends a custom event namedmyretailsite:CLICKED_PROMO
to Sitecore. -
Start your rendering app by entering the following command in your terminal:
RequestResponsenpm run start:connected
-
In your web browser, navigate to your JSS Next.js app, typically at
http://localhost:3000
. -
In your web browser's developer tools, on the Network tab, in the Filter field, paste the following URL:
edge-platform.sitecorecloud.io
.Doing this lists the network requests made to Sitecore.
-
Reload your webpage to start recording network activity.
-
On your webpage, click the Promo component.
The click triggers the custom event.
-
In the developer tools, on the Network tab, in the list of requests, click the last
POST
request with a status of201 Created
and a name that starts with events.This is the network request made when you clicked the Promo, and it contains the event data for the custom event that you captured.
-
In the request details pane, click the Payload tab for the request.
-
In the request payload, copy the value of the
browser_id
key. You'll use it in the next procedure to find the captured events in Sitecore CDP.The browser ID uniquely identifies a site visitor and associates captured events with that visitor. In this example, the browser ID identifies you and associates you with the actions you took: viewing the webpage and clicking the Promo component.
Find the events in Sitecore CDP
After capturing some events, you log in to Sitecore CDP and find those events.
-
In Sitecore CDP, click Guests. Enter the browser ID you copied in the previous procedure into the Search field, then select the Guest Type: All filter.
An anonymous guestguest, or site visitor, appears.
This guest is you interacting with your app just a minute ago. Next, you find the events associated with this guest.
-
Click the guest. The guest profile appears.
-
On the guest profile page, click Event viewer.
NoteThe Event viewer only appears if debug mode is enabled in Sitecore CDP.
A list of events associated with this guest appears. The list contains
VIEW
events, which are the events sent from your app when you reloaded it and viewed the webpage in a previous procedure. The list also contains one or moremyretailsite:CLICKED_PROMO
events. These are the custom events sent from your app when you clicked the Promo component on your webpage. -
Click next to the event that you want to see details for. The event details appear.
Next steps
You've now successfully started capturing custom events from your app as site visitors interact with your site. You sent custom events when a component is clicked, and you verified that Sitecore captures these events and their related data in real time. You're now tracking this specific visitor behavior on your site.
You can now undo the changes you made to src/lib/context/sdk/events.ts
to ensure the if
statement runs again.
Next, you can:
-
Learn more about tracking and analytics and what the collected data can be used for.
-
Send other events, for example,
IDENTITY
and more custom events. -
Implement other Cloud SDK functionality, such as using personalization to run an interactive experience.