@sitecore-cloudsdk/events/server
The server
module provides the following functions to capture and send events from the server side:
Function |
Description |
---|---|
|
Initializes the module. |
|
Sends a |
|
Sends an |
|
Sends a custom event. |
init(req, res, 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 |
Required/optional |
---|---|---|---|
|
object |
The HTTP request. |
Required |
|
object |
The HTTP response. |
Required |
|
settings object |
Details about your XM Cloud instance and cookie settings. |
Required |
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 |
Whether to set cookies from the server 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
Next.js
Here's an example middleware.ts
script showing how to initialize the module:
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
import { init } from "@sitecore-cloudsdk/events/server";
export async function middleware(req: NextRequest) {
const res = NextResponse.next();
// Initialize the module ->
await init(req, res,
{
sitecoreEdgeContextId: "<YOUR_CONTEXT_ID>",
siteName: "<YOUR_SITE_NAME>",
enableServerCookie: true,
}
);
console.log("Initialized the events/server module.");
// <- Initialize the module
return res;
};
pageView(req[, 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.
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 |
---|---|---|---|
|
object |
The HTTP request. |
Required |
|
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. |
RequestResponse
|
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 server-side events, the default is an empty string. |
|
Optional |
|
string |
The name of the webpage where the interaction with your brand takes place. This is a custom value of your choice. For server-side events, the default is an empty string. |
|
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 server-side events, if you don't set a value, it will be set to |
|
Optional |
Code examples
Next.js
Here's an example of how to use the pageView
function:
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
import { init, pageView } from "@sitecore-cloudsdk/events/server";
export async function middleware(req: NextRequest) {
const res = NextResponse.next();
// Initialize the module using init()...
// Send event:
const pageViewRes = await pageView(req);
console.log("Sent VIEW event.", pageViewRes);
return res;
};
Here's an example eventData
object:
let eventData: any = {
language: "EN",
page: "home",
pageVariantId: "page_variant_3",
extensionData: {customKey: "customValue"},
};
By default, VIEW
events include UTM parameters. Consider the following URL:
https://myretailsite.com?utm_source=newsletter&utm_medium=email&utm_campaign=summer_sale&utm_term=running+shoes
When you send a VIEW
event from the previous URL, the event data will contain all the UTM parameters:
{
"language": "EN",
"page": "home",
"pageVariantId": "page_variant_3",
"extensionData": "{'customKey': 'customValue'}",
"utm_source": "newsletter",
"utm_medium": "email",
"utm_campaign": "summer_sale",
"utm_term": "running shoes"
}
identity(req, 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 |
---|---|---|---|
|
object |
The HTTP request. |
Required |
|
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 |
|
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. |
RequestResponse
|
Optional |
array of objects |
The identifiers that are used to identify the users of your app. |
RequestResponse
|
Required | |
|
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 server-side events, the default is an empty string. |
|
Optional |
|
string |
The name of the webpage where the interaction with your brand takes place. This is a custom value of your choice. For server-side events, the default is an empty string. |
|
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
Next.js
Here's an example of how to use the identity
function:
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
import { init, identity } from "@sitecore-cloudsdk/events/server";
export async function middleware(req: NextRequest) {
const res = NextResponse.next();
// Initialize the module using init()...
// Send event:
let eventData: any = {
identifiers: [
{
id: "123456",
provider: "BXLP"
}
]
};
const identityRes = await identity(req, eventData);
console.log("Sent IDENTITY event.", identityRes);
return res;
};
Here's an example eventData
object:
let eventData: any = {
channel: "WEB",
currency: "EUR",
email: "[email protected]",
title: "Miss",
firstName: "Jane",
lastName: "Doe",
gender: "female",
dob: "1984-04-15",
mobile: "+3531234567",
phone: "+3531234567",
street: ["Tara Street"],
city: "Dublin",
country: "IE",
identifiers: [
{
id: "123456",
provider: "BXLP",
},
],
extensionData: { customKey: "customValue" },
};
event(req, 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 |
---|---|---|---|
|
object |
The HTTP request. |
Required |
|
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. |
RequestResponse
|
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
Next.js
Here's an example of how to use the event
function to send a custom event named "myretailsite:CLICKED_PROMO"
:
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
import { init, event } from "@sitecore-cloudsdk/events/server";
export async function middleware(req: NextRequest) {
const res = NextResponse.next();
// Initialize the module using init()...
// Send event:
let eventData: any = {
type: "myretailsite:CLICKED_PROMO",
};
const eventRes = await event(req, eventData);
console.log("Sent custom event.", eventRes);
return res;
};
Here's an example eventData
object:
let eventData: any = {
channel: "WEB",
currency: "EUR",
type: "myretailsite:CLICKED_PROMO",
extensionData: { customKey: "customValue" },
};