Set up custom events
You can use the Cloud SDK events package to capture custom events. Custom events can capture any action you choose to track. For example, clicks on components and how many times a site visitor adds items to their wish list.
JSS Next.js
In this topic, you'll start capturing events on the browser side in a JSS Next.js app (JSS version 22.2).
Setting up custom event capturing will run the event function every time a site visitor clicks a component in your web app.
-
Make sure your SitecoreAI site has analytics 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're ready to start development with the Cloud SDK.
-
In your code editor, open your JSS Next.js app folder.
To capture custom events on the browser side:
-
In
Promo.tsx, at the top of the file, import theeventfunction from thebrowsermodule of theeventspackage:import { event } from "@sitecore-cloudsdk/events/browser"; -
In
Promo.tsx, in theDefaultcomponent, within thereturnstatement, add a ReactonClickevent handler to the topmost JSX element:// ... 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
Defaultcomponent, on the first line of the function body, paste the following code, then save your changes:function handleClick() { event({ type: "myretailsite:CLICKED_PROMO" }) .catch((e) => console.debug(e)); console.log("Sent custom event."); };This code runs the
eventfunction every time the Promo component is clicked, and sends a custom event namedmyretailsite:CLICKED_PROMOto Sitecore.TipIn production, we recommend you pass event data to the
eventfunction to make sure that your web app captures rich event data, and that you choose a unique event name that accurately describes the event you are tracking. -
In your terminal, enter
npm run start:productionto start your JSS Next.js app.TipIf your app fails to compile due to linting errors, consider disabling ESLint rules.
-
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
POSTrequest with a status of201 Createdand 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_idkey. You'll use it to find the captured events in your Sitecore DXP products.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.
Next.js
In this topic, you'll start capturing events in a Next.js app that uses the App Router.
Setting up custom event capturing will run the event function every time a site visitor clicks a button in your web app.
-
In your code editor, open the root folder of your web app.
-
Install and initialize the Cloud SDK and the
eventspackage.
Browser side
To capture custom events on the browser side:
-
In your code editor, in the source code of your web app, create a page, for example,
src/app/products/page.tsx. -
Paste the following code into
page.tsx, then save your changes:"use client"; import { event } from "@sitecore-cloudsdk/events/browser"; export default function Products(){ return ( <div> <button onClick={() => event({ type: "myretailsite:CLICKED_BUTTON" })}>Send custom event</button> </div> ); }This code imports the
eventfunction from thebrowsermodule of theeventspackage, and it displays a button on the webpage. Every time the button is clicked, theeventfunction runs, which sends a custom event namedmyretailsite:CLICKED_BUTTON.TipIn production, we recommend you pass event data to the
eventfunction to make sure that your web app captures rich event data, and that you choose a unique event name that accurately describes the event you are tracking. -
In your terminal, enter
npm run devto start your Next.js app, then open theproductspage of your app in your web browser. -
Click the button you just created to send the custom event to Sitecore.
TipUse your web browser's developer tools to check the payload that is sent to Sitecore.
-
To find the event in your Sitecore DXP products:
-
In your web browser's developer tools, view the cookies for your app.
-
Find a cookie named
sc_cid. -
Copy the cookie value. This value is the browser IDbrowser ID. You'll use it to find the event in your Sitecore DXP products.
Example:
a38b230c-11eb-4cf9-8d5d-274e9f344925
-
Server side
To capture custom events on the server side:
-
In your code editor, in the source code of your web app, locate the file where you initialized the Cloud SDK and the
eventspackage. The initialization code looks similar to:import type { NextRequest } from "next/server"; import { NextResponse } from "next/server"; // Import SDK modules -> import { CloudSDK } from "@sitecore-cloudsdk/core/server"; import "@sitecore-cloudsdk/events/server"; import "@sitecore-cloudsdk/personalize/server"; import "@sitecore-cloudsdk/search/server"; // <- Import SDK modules export default async function middleware(request: NextRequest) { const response = NextResponse.next(); await CloudSDK(request, response, { sitecoreEdgeContextId: "<YOUR_SITECORE_EDGE_CONTEXT_ID>", siteName: "<YOUR_SITE_NAME>", enableServerCookie: true }) .addEvents() // Initialize the events package .addPersonalize({ enablePersonalizeCookie: true, webPersonalization: { language: 'en' } }) // Initialize the personalize package .addSearch() // Initialize the search package .initialize(); // Run the initialization logic and set cookies return response; }; -
At the top of the file, directly below the
CloudSDKimport statement, import theeventfunction from theservermodule of theeventspackage:// ... import { event } from "@sitecore-cloudsdk/events/server"; -
Inside the
middlewarefunction, below theCloudSDKfunction, run theeventfunction, then save your changes:// Send event: await event(request, { type: "myretailsite:CLICKED_BUTTON" });This script sends a custom event named
myretailsite:CLICKED_BUTTON.TipIn production, we recommend you pass event data to the
eventfunction to make sure that your web app captures rich event data, and that you choose a unique event name that accurately describes the event you are tracking. -
In your terminal, enter
npm run devto start your Next.js app, then open your app in your web browser. -
Click the button you just created to send the custom event to Sitecore.
TipUse your web browser's developer tools to check the payload that is sent to Sitecore.
-
To find the event in your Sitecore DXP products:
-
In your web browser's developer tools, view the cookies for your app.
-
Find a cookie named
sc_cid. -
Copy the cookie value. This value is the browser IDbrowser ID. You'll use it to find the event in your Sitecore DXP products.
Example:
a38b230c-11eb-4cf9-8d5d-274e9f344925
-
You can now find the events in your Sitecore DXP products. The collected event data will drive, for example, web analytics, segmentation, personalization, and more.
Next, you can:
-
Learn more about the custom event and what data you can include in it.
-
Learn more about tracking, what other events you can capture, and what the collected data can be used for.
-
Explore the reference documentation for the
eventspackage. -
Set up other Sitecore DXP capabilities, such as using personalization to run web experiences or an interactive experience.
A universally unique identifier (UUID) that Sitecore CDP assigns to every site visitor of your site. It is unique per browser, and it associates sessions and events with the respective site visitor. The Cloud SDK stores the browser ID as the value for the first-party sc_cid cookie in the web browser.