1. Tracking

Set up custom events

Version:

You can use the Cloud SDK 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.

In this topic, you'll start capturing events on the browser side in a JSS Next.js app (JSS version 22.2).

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

Before you begin

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:

    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:

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

    function handleClick() {
      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.

    Tip

    In production, we recommend you pass event data 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.

  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.

:::

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:

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