1. Tracking

Set up VIEW events

Version:
Note

If you're using a Content SDK or JSS app, you don't need to complete this walkthrough because those apps automatically capture VIEW events, with no coding required.

You can use the Cloud SDK events package to capture VIEW events. The VIEW event tracks when the site visitor views a webpage in your web app. Because it is an essential event to track, it's important that you send VIEW events from all webpages where you want to track site visitor behavior. In this topic, you'll start capturing events in a Next.js app that uses the App Router.

Before you begin
  • In your code editor, open the root folder of your web app.

  • Install and initialize the Cloud SDK and the events package.

Setting up VIEW event capturing will run the pageView function every time a webpage loads in your web app.

To capture VIEW 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:

    "use client";
    import { useEffect } from "react";
    import { pageView } from "@sitecore-cloudsdk/events/browser";
    
    export default function Products(){
      useEffect(() => {
        pageView();
      }, []);
    
      return (<></>)
    }

    This code imports the pageView function from the browser module of the events package. Every time the webpage loads, the Effect Hook runs the pageView function, which sends a VIEW event.

    In production, we recommend you pass event data to the pageView function to make sure that your web app captures rich event data.

    Tip

    By default, the VIEW event triggers when a webpage loads, but not when a route changes. To capture VIEW events in response to a route change, send the events using the routing tools of your framework:

    Contributed by: mvp-boris-brodsky.jpg Boris Brodsky

  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. When the webpage loads, the VIEW event triggers and is sent to Sitecore.

    Tip

    Use your web browser's developer tools to check the payload that is sent to Sitecore.

  4. 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_cid.

    3. Copy the cookie value. This value is the browser ID. 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. 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!