DataSag guides · Updated 27 October 2026
Fix duplicate analytics pageviews on Next.js Pages Router
Multiple tracking requests do not necessarily mean duplicate pageviews. DataSag sends pageviews, scroll milestones, engagement updates and custom events. Inspect eventName before removing code. This guide applies to Next.js Pages Router.
Start with the Next.js Pages Router installation
Import Script from next/script in pages/_app.tsx. Keep Component and pageProps, and add the tracker once alongside the page component. The shared app stays mounted as users navigate between pages.
Do not install the same tracker in both _app and _document. Do not attach another pageview sender to router.events before testing the tracker's built-in History API handling.
Use the public project key from the DataSag project you are inspecting. A test on a preview domain does not confirm that the production domain has the same code or settings.
Fix duplicate analytics pageviews in Next.js Pages Router
- Search pages/_app.tsx and pages/_document.tsx together. Keep the global DataSag script in the intended shared location and remove a redundant copy only after confirming both render into the same production document.
- Inspect router.events listeners and page-level useEffect code for manual pageview calls or script insertion. The current tracker observes History API changes, so a second routing integration needs a demonstrated reason.
- Use production navigation between two page pathnames and Back. Classify payloads by eventName before counting pageviews. A scroll event after the route settles is not evidence that _app sent a duplicate pageview.
One global script and one pageview mechanism remain for the tested Pages Router navigation.
Complete the collection checks
- Open Network and filter /api/track. Reload once without clicking or scrolling. Inspect eventName in each payload: null identifies a pageview; scroll, click_heatmap and __datasag_engagement are other records.
- Count rendered script elements with data-datasag-key. There should be one intended installation for this project. Search your code, site header settings, embeds and tag manager for additional copies.
- Remove redundant script insertion mechanisms, keeping the site-wide installation. Two copies execute independently even when their browser download is cached.
- Check for custom router listeners that manually send pageviews. The current tracker handles pushState, replaceState and popstate and ignores a repeated pathname. Do not add a second pageview implementation without a demonstrated gap.
- Repeat a direct load, a second route and browser Back. Correlate pageview records with actual path changes. A query-only change on the same pathname is not a new pageview in the current tracker.
[...document.querySelectorAll('script[data-datasag-key]')]
.map(script => ({
src: script.src,
key: script.getAttribute('data-datasag-key')
}));What a successful check looks like
A single installation and one pageview dispatch for each actual pathname change in the tested sequence. Additional engagement records remain available.
Record the public path you tested, the browser state, the approximate time and the observed request. Check one direct page load and one later navigation so the result covers more than the homepage.
Common mistake
Comparing the total number of events with the number of pageviews produces a false duplicate diagnosis. Keep the event categories separate throughout the check.
Installation snippet for Next.js Pages Router
The shared installation belongs in pages/_app.tsx, alongside the page component. Use this as the installation reference while checking the procedure above.
import Script from "next/script";
// Alongside <Component {...pageProps} /> in pages/_app.tsx:
<Script
id="datasag"
src="https://www.datasag.com/tracker.js"
data-datasag-key="YOUR_PUBLIC_KEY"
strategy="afterInteractive"
/>Related Next.js Pages Router guides
- Verify DataSag analytics
- Fix DataSag analytics not working
- Track custom events
- Track UTM campaigns
- Measure AI referral traffic
- Configure CSP for DataSag analytics
- Exclude your own analytics visits
- Load analytics without blocking rendering
Full Next.js Pages Router setup guide · Search and AI visibility
References
Official Next.js Pages Router documentation covers the integration point. For DataSag data handling, read the privacy policy. Use support if a request fails after these checks.