Capture custom events
Using the Cloud SDK, you can track site visitors' behavior by capturing events on your site. Your JSS Next.js app automatically captures certain events, and you can implement other event capturing using the events package of the Cloud SDK. In this walkthrough, you'll capture custom events when site visitors click the Promo component on your webpage. You'll then log in to Sitecore CDP to find the event and all the collected data about your site visitor.
This walkthrough describes how to:
-
Import context into your component.
-
Capture events in your component.
-
Find the events in Sitecore CDP.
-
Make sure your SitecoreAI site has site identifiers.
-
Make sure your SitecoreAI 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 your JSS Next.js app 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
sxastarterfolder, opensrc/components/Promo.tsx.Promo.tsxis the source code for the Promo component that marketers can add to a webpage in SitecoreAI Pages. -
In
Promo.tsx, importcontext: -
Open
src/lib/context/sdk/events.ts.events.tsinitializes thebrowsermodule of theeventspackage of the Cloud SDK 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 theinitfunction, temporarily comment out theifstatement that checks whether you're in development mode: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 listening for clicks and capturing custom events.
To capture events in your component:
-
In
Promo.tsx, in theDefaultcomponent, within thereturnstatement, add a ReactonClickevent handler to the topmost JSX element:NoteIn production, we recommend that you listen for clicks on a clickable element such as a
<button>instead of a<div>. -
In the
Defaultcomponent, on the first line of the function body, paste the following code, then save your changes:This code runs the
eventfunction every time the Promo component is clicked, and sends a custom event namedmyretailsite:CLICKED_PROMOto Sitecore. -
Start your rendering app by entering the following command in your terminal:
-
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: https://edge-platform.sitecorecloud.io/events.
This lists the network requests related to capturing events.
-
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
POSTrequest with a status of201 Created.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_idkey. 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 the 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 events, you log in to Sitecore CDP and find the 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 guest displays.

This guest, or site visitor, 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 displays.
-
On the guest profile page, click Event viewer.
NoteThe Event viewer only displays if debug mode is enabled in Sitecore CDP.
A list of events associated with this guest displays. The list contains VIEW events. These 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 more
myretailsite:CLICKED_PROMOevents. 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 display.
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 in real time.
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 data collection 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 personalization by running an interactive full stack experience.