Capture custom events

Version: 0.3

Using the Cloud SDK, you can track site visitors' behavior by capturing events on your site in real time. The captured events contain data that Sitecore uses for, for example, website analytics. JSS Next.js apps automatically capture certain events, and you can capture other events by using the browser module of the Cloud SDK events package.

Note

In JSS Next.js apps, the events package is already installed.

To get started with the events package, you start capturing custom events from the browser (client-side tracking) when site visitors click the Promo component on your webpage. You then log in to Sitecore CDP to find the event and all the collected data about your site visitor.

This walkthrough describes how to:

Before you begin

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 JSS Next.js apps 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:

  1. In your code editor, in the sxastarter folder, open src/components/Promo.tsx.

    Promo.tsx is the source code for the Promo component that marketers can add to a webpage in XM Cloud Pages.

  2. In Promo.tsx, import context:

    RequestResponse
    import { context } from "lib/context";
  3. Open src/lib/context/sdk/events.ts.

    events.ts initializes the browser module of the Cloud SDK events package 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.

  4. In events.ts, inside the init function, temporarily comment out the if statement that checks whether you're in development mode:

    RequestResponse
    // Comment out the following lines:
    if (process.env.NODE_ENV === 'development')
      throw 'Browser Events SDK is not initialized in development environment';

    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 having it capture custom events based on site visitor clicks.

To capture events in your component:

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

    RequestResponse
    // ...
    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>.

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

    RequestResponse
    function handleClick() {
      context
        .getSDK("Events")
        .then((Events) =>
          Events.event({ type: "myretailsite:CLICKED_PROMO" })
        )
        .catch((e) => console.debug(e));
      console.log("Sent custom event.");
    };
    

    This code runs the event function every time the Promo component is clicked, and sends a custom event named myretailsite:CLICKED_PROMO to Sitecore.

  3. Start your rendering app by entering the following command in your terminal:

    RequestResponse
    npm run start:connected
  4. In your web browser, navigate to your JSS Next.js app, typically at http://localhost:3000.

  5. 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.

  6. Reload your webpage to start recording network activity.

  7. On your webpage, click the Promo component.

    The click triggers the custom event.

  8. 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.

  9. In the request details pane, click the Payload tab for the request.

  10. In the request payload, copy the value of the browser_id key. 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 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.

Find the events in Sitecore CDP

After capturing some events, you log in to Sitecore CDP and find those events.

  1. 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, or site visitor, appears.

    Search field in the Guests screen.

    This guest is you interacting with your app just a minute ago. Next, you find the events associated with this guest.

  2. Click the guest. The guest profile appears.

  3. On the guest profile page, click Event viewer.

    Note

    The Event viewer only appears if debug mode is enabled in Sitecore CDP.

    A list of events associated with this guest appears. The list contains VIEW events, which 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_PROMO events. These are the custom events sent from your app when you clicked the Promo component on your webpage.

  4. Click Show event details next to the event that you want to see details for. The event details appear.

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 and their related data in real time. You're now tracking this specific visitor behavior on your site.

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:

Do you have some feedback for us?

If you have suggestions for improving this article,