---
title: "Set up custom events"
slug: "set-up-custom-events"
abstract: "Use the Cloud SDK to start capturing custom events as your site visitors interact with your web app."
---
# Set up custom events

You can use the Cloud SDK [`events`](https://doc.sitecore.com/sdk/en/developers/005/cloud-sdk/cloud-sdk-reference/cloud-sdk-events.html "@sitecore-cloudsdk/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.

::::tabs
:::tab{title="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](https://github.com/sitecorelabs/xmcloud-foundation-head/blob/main/headapps/nextjs-starter/package.json)).

Setting up custom event capturing will run the `event` function every time a site visitor clicks a component in your web app.

> [!note]
> **Before you begin**
> -   Make sure your SitecoreAI site has [analytics identifiers](https://doc.sitecore.com/xmc/en/users/xm-cloud/en/manage-personalization-and-analytics-for-sites.html).
>
> -   Make sure [your SitecoreAI site's webpage has a component](https://doc.sitecore.com/xmc/en/users/xm-cloud/en/add-or-remove-an-element-in-a-page.html) to capture custom events from. [This webpage must be published](https://doc.sitecore.com/xmc/en/users/xm-cloud/en/publish-a-page.html) so the component appears on the website. This walkthrough uses the *Promo* component as an example.
>
> -   Make sure you're [ready to start development](https://doc.sitecore.com/sdk/en/developers/005/cloud-sdk/developer-guides/tracking/prerequisites.html "Prerequisites") with the Cloud SDK.
>
> -   In your code editor, open your JSS Next.js app folder.

To capture custom events on the browser side:

1. In `Promo.tsx`, at the top of the file, import the `event` function from the `browser` module of the `events` package:

    ```typescript
    import { event } from "@sitecore-cloudsdk/events/browser";
    ```

2. In `Promo.tsx`, in the `Default` component, within the `return` statement, add a React `onClick` event handler to the topmost JSX element:

    ```typescript
    // ...
    return (
      <div
        onClick={handleClick}
        className={"..."}
        id={"..."}
      >
    {/*...*/}
    )
    ```

    > [!note]
    > In production, place the event listener on a clickable element such as a `<button>`, instead of a `<div>`.

3. In the `Default` component, on the first line of the function body, paste the following code, then save your changes:

    ```typescript
    function handleClick() {
      event({ type: "myretailsite:CLICKED_PROMO" })
        .catch((e) => console.debug(e));
      console.log("Sent custom event.");
    };
    ```

    This code runs the [`event`](https://doc.sitecore.com/sdk/en/developers/005/cloud-sdk/cloud-sdk-reference/cloud-sdk-events/cloud-sdk-events-browser/functions/cloud-sdk-events-browser-event.html "event") function every time the *Promo* component is clicked, and sends a custom event named `myretailsite:CLICKED_PROMO` to Sitecore.

    > [!tip]
    > In production, we recommend you [pass event data](https://doc.sitecore.com/sdk/en/developers/005/cloud-sdk/developer-guides/tracking/custom-events.html "Custom events") to the `event` function 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.

4. In your terminal, enter `npm run start:production` to start your JSS Next.js app.

    > [!tip]
    > If your app fails to compile due to linting errors, consider [disabling ESLint rules](https://nextjs.org/docs/pages/building-your-application/configuring/eslint#disabling-rules).

5. In your web browser, navigate to your JSS Next.js app, typically at `http://localhost:3000`.
6. 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.

7. Reload your webpage to start recording network activity.
8. On your webpage, click the *Promo* component.

    The click triggers the custom event.

9. In the developer tools, on the **Network** tab, in the list of requests, click the last `POST` request with a status of `201 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.

10. In the request details pane, click the **Payload** tab for the request.
11. In the request payload, copy the value of the `browser_id` key. 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.

:::
:::tab{title="Next.js"}
In this topic, you'll start capturing events in a Next.js app that uses the [App Router](https://nextjs.org/docs/getting-started/installation#the-app-directory).

Setting up custom event capturing will run the `event` function every time a site visitor clicks a button in your web app.

> [!note]
> **Before you begin**
> -   In your code editor, open the root folder of your web app.
>
> -   [Install and initialize](https://doc.sitecore.com/sdk/en/developers/005/cloud-sdk/developer-guides/tracking/install-and-initialize-the-cloud-sdk.html "Install and initialize the Cloud SDK") the Cloud SDK and the `events` package.

:::tab{title="Browser side"}
To capture custom events on the browser side:

1. In your code editor, in the source code of your web app, create a page, for example, `src/app/products/page.tsx`.
2. Paste the following code into `page.tsx`, then save your changes:

    ```typescript
    "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 `event` function from the `browser` module of the `events` package, and it displays a button on the webpage. Every time the button is clicked, the `event` function runs, which sends a custom event named `myretailsite:CLICKED_BUTTON`.

    > [!tip]
    > In production, we recommend you [pass event data](https://doc.sitecore.com/sdk/en/developers/005/cloud-sdk/developer-guides/tracking/custom-events.html "Custom events") to the `event` function 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.

3. In your terminal, enter `npm run dev` to start your Next.js app, then open the `products` page of your app in your web browser.
4. Click the button you just created to send the custom event to Sitecore.

    > [!tip]
    > Use your web browser's developer tools to [check the payload](https://doc.sitecore.com/sdk/en/developers/005/cloud-sdk/developer-guides/tracking/events.html#UUID-b78ff86e-4442-71e7-edc6-81adf8fabe27_section-idm234451877631545 "Checking the payload") that is sent to Sitecore.

5. To find the event in your Sitecore DXP products:
    1. In your web browser's developer tools, view the cookies for your app.
    2. Find a cookie named `sc_{SitecoreEdgeContextId}` , where `{SitecoreEdgeContextId}` is your [Context ID](https://doc.sitecore.com/sdk/en/developers/005/cloud-sdk/getting-started/prerequisites.html "Prerequisites").
    3. Copy the cookie value. This value is the browser ID[^UUID-101b62f6-b640-ed11-1057-29646ebb9fb7_N6659aee98cafa]. You'll use it to find the event in your Sitecore DXP products.

        Example: `a38b230c-11eb-4cf9-8d5d-274e9f344925​`

:::
:::tab{title="Server side"}
To capture custom events on the server side:

1. In your code editor, in the source code of your web app, locate the file where you [initialized](https://doc.sitecore.com/sdk/en/developers/005/cloud-sdk/getting-started/install-and-initialize-the-cloud-sdk.html "Install and initialize the Cloud SDK") the Cloud SDK and the `events` package. The initialization code looks similar to:

    ```typescript
    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;
    };
    ```

2. At the top of the file, directly below the `CloudSDK` import statement, import the `event` function from the `server` module of the `events` package:

    ```typescript
    // ...
    import { event } from "@sitecore-cloudsdk/events/server";
    ```

3. Inside the `middleware` function, below the `CloudSDK` function, run the `event` function, then save your changes:

    ```typescript
    // Send event:
    await event(request, { type: "myretailsite:CLICKED_BUTTON" });
    ```

    This script sends a custom event named `myretailsite:CLICKED_BUTTON`.

    > [!tip]
    > In production, we recommend you [pass event data](https://doc.sitecore.com/sdk/en/developers/005/cloud-sdk/developer-guides/tracking/custom-events.html "Custom events") to the `event` function 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.

4. In your terminal, enter `npm run dev` to start your Next.js app, then open your app in your web browser.
5. Click the button you just created to send the custom event to Sitecore.

    > [!tip]
    > Use your web browser's developer tools to [check the payload](https://doc.sitecore.com/sdk/en/developers/005/cloud-sdk/developer-guides/tracking/events.html#UUID-b78ff86e-4442-71e7-edc6-81adf8fabe27_section-idm234451877631545 "Checking the payload") that is sent to Sitecore.

6. To find the event in your Sitecore DXP products:
    1. In your web browser's developer tools, view the cookies for your app.
    2. Find a cookie named `sc_{SitecoreEdgeContextId}` , where `{SitecoreEdgeContextId}` is your [Context ID](https://doc.sitecore.com/sdk/en/developers/005/cloud-sdk/getting-started/prerequisites.html "Prerequisites").
    3. Copy the cookie value. This value is the browser ID[^UUID-101b62f6-b640-ed11-1057-29646ebb9fb7_N6659aee98cafa]. 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](https://doc.sitecore.com/sdk/en/developers/005/cloud-sdk/cloud-sdk-concepts/concepts-tracking/working-with-the-collected-data.html "Working with the collected data"). The collected event data will drive, for example, web analytics, segmentation, personalization, and more.

Next, you can:

- Learn more about the [custom](https://doc.sitecore.com/sdk/en/developers/005/cloud-sdk/cloud-sdk-concepts/concepts-tracking/events/custom-events.html "Custom events") event and what data you can include in it.
- Learn more about [tracking](https://doc.sitecore.com/sdk/en/developers/005/cloud-sdk/cloud-sdk-concepts/concepts-tracking.html "Tracking"), what other [events](https://doc.sitecore.com/sdk/en/developers/005/cloud-sdk/cloud-sdk-concepts/concepts-tracking/events.html "Events") you can capture, and [what the collected data can be used for](https://doc.sitecore.com/sdk/en/developers/005/cloud-sdk/cloud-sdk-concepts/concepts-tracking/working-with-the-collected-data.html "Working with the collected data").
- Explore the reference documentation for the [`events`](https://doc.sitecore.com/sdk/en/developers/005/cloud-sdk/cloud-sdk-reference/cloud-sdk-events.html "@sitecore-cloudsdk/events") package.
- Set up other Sitecore DXP capabilities, such as using [personalization](https://doc.sitecore.com/sdk/en/developers/005/cloud-sdk/cloud-sdk-concepts/concepts-personalization.html "Personalization") to run [web experiences](https://doc.sitecore.com/sdk/en/developers/005/cloud-sdk/developer-guides/personalization/set-up-web-personalization.html "Set up web personalization") or an [interactive experience](https://doc.sitecore.com/sdk/en/developers/005/cloud-sdk/developer-guides/personalization/set-up-interactive-personalization.html "Set up interactive personalization").

[^UUID-101b62f6-b640-ed11-1057-29646ebb9fb7_N6659aee98cafa]: 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_{SitecoreEdgeContextId}` [cookie](https://doc.sitecore.com/sdk/en/developers/004/cloud-sdk/en/cloud-sdk-cookies.html) in the web browser.
