DataSag guides · Updated 27 October 2026
Fix duplicate analytics pageviews on Drupal Twig themes
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 Drupal Twig themes.
Start with the Drupal Twig themes installation
Use a maintained custom theme or subtheme and identify its html.html.twig document wrapper. Add the deferred tracker inside head while preserving Drupal's existing placeholders, attributes and attached assets. Rebuild the relevant caches and inspect the public rendered document. Do not edit Drupal core or replace the full wrapper with a minimal example.
The active public theme and the administration theme can differ. A node or block template is not the global document wrapper, and Ajax-rendered fragments do not represent independent document loads. Check public routes, caching and any separately configured theme before assuming complete coverage.
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 Drupal Twig themes
- Search the custom html wrapper, attached theme libraries, contributed integrations and content blocks for DataSag. A document-level tag and a library attached by a block can both initialize tracking in the same public page.
- Inspect custom Drupal behaviors for repeated loader insertion after Ajax attachments. Behaviors can be applied to newly rendered content; keep the global tracker outside per-fragment initialization and use appropriate once-only handling for your action listeners.
- Rebuild relevant caches and test a direct public load followed by an Ajax interaction. Compare null-name pageviews with named events and engagement payloads. Do not count an attached content fragment as a separate browser document by default.
One global tracker remains and repeated behavior attachment does not reinitialize it during Ajax updates.
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 Drupal Twig themes
The shared installation belongs in the head in your custom theme html.html.twig override. 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 Drupal Twig themes 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 Drupal Twig themes setup guide · Search and AI visibility
References
Official Drupal Twig themes documentation covers the integration point. For DataSag data handling, read the privacy policy. Use support if a request fails after these checks.