Set up IDENTITY events
You can use the Cloud SDK events package to capture IDENTITY events. IDENTITY events are typically captured in registration, login, and signup forms and are used to resolve the identity of an anonymous site visitor of your app.
JSS Next.js
In this topic, you'll start capturing events on the browser side, in the out-of-the-box Promo component in a JSS Next.js app (JSS version 22.2). Note that in production, you will most likely capture IDENTITY events in forms, not in the Promo component. This topic shows how to set up the event so you can later capture them in the components of your choice.
-
Make sure your SitecoreAI site has analytics identifiers.
-
Make sure your SitecoreAI site's webpage has a component to capture
IDENTITYevents 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 IDENTITY events on the browser side:
-
In
Promo.tsx, at the top of the file, import theidentityfunction from thebrowsermodule of theeventspackage:import { identity } 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() { identity({ identifiers: [ { id: "<YOUR_ID>", // example: "[email protected]" provider: "<YOUR_PROVIDER>" // example: "email" } ] }); console.log("Sent IDENTITY event."); }This code sends an
IDENTITYevent every time the Promo component is clicked.TipIn production, we recommend you pass your correct identity system data and pass other event data to the
identityfunction to make sure that your web app captures rich event data. -
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
IDENTITYevent. -
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
IDENTITYevent 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 IDENTITY event capturing will run the identity function every time a site visitor clicks a button in your web app.
In production, you will most often capture IDENTITY events in forms.
-
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 IDENTITY 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 { identity } from "@sitecore-cloudsdk/events/browser"; export default function Products(){ return ( <div> <button onClick={() => identity({ identifiers: [ { id: "<YOUR_ID>", // example: "[email protected]" provider: "<YOUR_PROVIDER>" // example: "email" } ] }) }> Send IDENTITY event </button> </div> ); }This code imports the
identityfunction from thebrowsermodule of theeventspackage, and it displays a button on the webpage. Every time the button is clicked, theidentityfunction runs, which sends anIDENTITYevent.TipIn production, we recommend you pass your correct identity system data and pass other event data to the
identityfunction to make sure that your web app captures rich event data. -
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
IDENTITYevent 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 IDENTITY 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 theidentityfunction from theservermodule of theeventspackage:// ... import { identity } from "@sitecore-cloudsdk/events/server"; -
Inside the
middlewarefunction, below theCloudSDKfunction, insert theidentityfunction, then save your changes:// Send event: await identity(request, { identifiers: [ { id: "<YOUR_ID>", // example: "[email protected]" provider: "<YOUR_PROVIDER>" // example: "email" } ] });This code sends an
IDENTITYevent.TipIn production, we recommend you pass your correct identity system data and pass other event data to the
identityfunction to make sure that your web app captures rich event data. -
In your terminal, enter
npm run devto start your Next.js app, then open the app in your web browser. -
Click the button you created in the previous step to send the
IDENTITYevent 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're now tracking IDENTITY events in your web app as site visitors interact with your site. 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:
-
To finish setting up identity resolution in SitecoreAI, create an identity rule.
-
Learn more about the
IDENTITYevent 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.