Tracking Pageviews
Using manual pageview tracking
The trackPageview
function can be used to track pageviews manually.
You might use manual page tracking to add additional metadata to the pageview, e.g. whether the user is signed in.
Disabling automatic pageview collection
The SimpleAnalytics
component automatically collects pageviews, if we want to collect pageviews manually we should disable auto collection to prevent recording duplicate pageviews.
We can disable automatic pageview collection by setting the autoCollect
property to false
:
Tracking pageviews server-side
Simple Analytics determines the country of the visitor based on their time zone. Unlike during client-side tracking, this information is not available on when receiving a request on the server.
However, certain hosting provider, e.g. Vercel, Cloudflare, and Amazon CloudFront, might include the visitor's time zone (by default or optionally) in a request headers.
If one of the headers is present, the time zone will automatically be include with the information send to Simple Analytics unless explicitly ignored.
Usage in Edge Middleware
This feature is experimental
Tracking pageviews in middleware is an experimental feature and does not support the same functionality as the client-side tracking implementation.
You can add pageview tracing application-wide using trackPageview
and Next.js Edge Middleware.
Usage in Server Components
You can track pageviews manually in React Server Components using trackPageview
.
Enabling more granular control per page while tracking visitors, including specifying page specific metadata.
Usage without request object
We use the request headers and must specify the path explicity because the request object is not accessible in Server Components.
The function headers()
cannot be called inside the after
callback when used in a Server Component.
Handling parameters in dynamic routes
For routes containing dynamic segments (e.g. app/blog/posts/[id]/page.tsx
), you can include the segments and reconstruct the path before passing it to trackPageview
.