- Tracking
Set up VIEW events
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.
-
In your code editor, open the root folder of your web app.
-
Install and initialize the Cloud SDK and the
eventspackage.
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:
-
In your code editor, in the source code of your web app, create a page, for example,
src/app/products/page.tsx. -
Paste the following code into
page.tsx, then save your changes:This code imports the
pageViewfunction from thebrowsermodule of theeventspackage. Every time the webpage loads, the Effect Hook runs thepageViewfunction, which sends aVIEWevent.In production, we recommend you pass event data to the
pageViewfunction to make sure that your web app captures rich event data.TipBy default, the
VIEWevent triggers when a webpage loads, but not when a route changes. To captureVIEWevents in response to a route change, send the events using the routing tools of your framework:-
Next.js App Router - use an effect when the value returned by
usePathnamechanges. -
Next.js Pages Router - send the events in
routeChangeComplete.
Contributed by:
Boris Brodsky -
-
In your terminal, enter
npm run devto start your Next.js app, then open theproductspage of your app in your web browser. When the webpage loads, theVIEWevent triggers and is sent to Sitecore.TipUse your web browser's developer tools to check the payload that is sent to Sitecore.
-
To find the event in your Sitecore DXP products:
To capture VIEW events on the server side:
-
In your code editor, in the source code of your web app, locate the file where you initialized the Cloud SDK and the
eventspackage. The initialization code looks similar to: -
At the top of the file, directly below the
CloudSDKimport statement, import thepageViewfunction from theservermodule of theeventspackage: -
Inside the
middlewarefunction, below theCloudSDKfunction, run thepageViewfunction, then save your changes:This script runs the
pageViewfunction, which sends aVIEWevent.In production, we recommend you pass event data to the
pageViewfunction to make sure that your web app captures rich event data.TipBy default, the
VIEWevent triggers when a webpage loads, but not when a route changes. To captureVIEWevents in response to a route change, send the events using the routing tools of your framework:-
Next.js App Router - use an effect when the value returned by
usePathnamechanges. -
Next.js Pages Router - send the events in
routeChangeComplete.
Contributed by:
Boris Brodsky -
-
In your terminal, enter
npm run devto start your Next.js app, then open your app in your web browser. When the webpage loads, theVIEWevent triggers and is sent to Sitecore.TipUse your web browser's developer tools to check the payload that is sent to Sitecore.
-
Optionally, to find the event in your Sitecore DXP products:
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:
- Learn more about the
VIEWevent and what data you can include in it. - Learn more about tracking, what other events you can capture, and what the collected data can be used for.
- Explore the reference documentation for the
eventspackage. - Set up other Sitecore DXP capabilities, such as using personalization to run web experiences or an interactive experience.