DataSag guides · Updated 27 October 2026
Fix duplicate analytics pageviews on PHP templates
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 PHP templates.
Start with the PHP templates installation
Add the deferred external script once to the local PHP include that emits your public HTML head. Preserve the existing document structure and include that file through a known local path in the pages you intend to measure. The tracking snippet itself is HTML; it does not need a PHP analytics library.
An independent PHP endpoint may return JSON, redirect before rendering, or use another header include. Keep the tracker in rendered HTML and avoid inserting output before code that needs to send response headers. Do not derive an include path from visitor input.
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 PHP templates
- Inspect the final HTML after PHP includes and conditionals run. A shared header plus an older inline snippet in the page body can execute the tracker twice.
- Keep one intended include owner for each served document. Confirm a second include path or a client-side partial update does not inject the same global loader. Preserve response-header logic while removing redundant output.
- Deploy and test two independent PHP pages and any partial navigation the frontend uses. Separate null-name pageviews from named events. Count rendered scripts and inspect later interactions to detect listeners retained from repeated initialization.
The rendered PHP documents and tested partial updates no longer create redundant tracker execution.
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 PHP templates
The shared installation belongs in the head emitted by the shared PHP header include. Use this as the installation reference while checking the procedure above.
<script defer src="https://www.datasag.com/tracker.js" data-datasag-key="YOUR_PUBLIC_KEY"></script>Related PHP templates guides
References
Official PHP templates documentation covers the integration point. For DataSag data handling, read the privacy policy. Use support if a request fails after these checks.