1. Capture custom events

Capture custom events

Version:

Using the Cloud SDK, you can track site visitors' behavior by capturing events on your site. Your JSS Next.js app automatically captures certain events, and you can implement other event capturing using the events package of the Cloud SDK. In this walkthrough, you'll capture custom events when site visitors click the Promo component on your webpage. You'll then log in to Sitecore CDP to find the event and all the collected data about your site visitor.

This walkthrough describes how to:

  • Import context into your component.

  • Capture events in your component.

  • Find the events in Sitecore CDP.

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 your JSS Next.js app 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 SitecoreAI Pages.

  2. In Promo.tsx, import context:

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

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

    // 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 listening for clicks and capturing custom events.

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:

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

    In production, we recommend that you listen for clicks 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:

    function handleClick() {
      context
        .getSDK("Events")
        .then((Events) =>
          Events.event(
            "myretailsite:CLICKED_PROMO",
            {
              channel: "WEB",
              currency: "USD",
            }
          )
        )
        .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:

    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: https://edge-platform.sitecorecloud.io/events.

    This lists the network requests related to capturing events.

  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.

    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 the 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 events, you log in to Sitecore CDP and find the 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 displays.

    Search field in the Guests screen.

    This guest, or site visitor, 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 displays.

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

    Note

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

    A list of events associated with this guest displays. The list contains VIEW events. These 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 next to the event that you want to see details for. The event details display.

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 in real time.

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:

If you have suggestions for improving this article, let us know!