Track Scroll Depth Without Cookies: A Practical Guide
In this article
You can track how far visitors scroll down your pages without using cookies by storing the scroll depth in the browser's sessionStorage (or localStorage) and sending it to your analytics via a beacon request. This approach respects privacy regulations like GDPR and avoids cookie consent banners for this metric. It works with any analytics tool that accepts custom events or hits (e.g., Google Analytics 4, Matomo, Plausible, or a simple server endpoint).
What You'll Need
- A website where you can add JavaScript (via a tag manager like Google Tag Manager or directly in the site's code).
- Access to your analytics tool's event tracking setup (e.g., GA4 event, Matomo custom event, or a server endpoint that logs hits).
- Basic familiarity with JavaScript and browser developer tools for testing.
Step-by-Step
- Decide on depth thresholds. Common thresholds are 25%, 50%, 75%, and 100% of the page height. You can also use pixel values (e.g., every 500px) if you prefer. Start with percentage thresholds for simplicity.
- Create a scroll tracking script. Write a JavaScript function that calculates the current scroll depth as a percentage of the total page height. The formula is:
scrollPercent = (window.scrollY + window.innerHeight) / document.documentElement.scrollHeight * 100. Round down to the nearest threshold (e.g., if at 47%, the last passed threshold is 25%). - Use sessionStorage to avoid duplicate events. For each threshold, store a flag in
sessionStoragewhen the user crosses it. For example,sessionStorage.setItem('scroll_25', 'true'). Before sending an event, check if the flag already exists; if yes, skip sending. This ensures each threshold is counted only once per session. - Attach a scroll event listener. Add an event listener to
windowfor thescrollevent. Inside the handler, call your tracking function. To avoid performance issues, userequestAnimationFrameto throttle the scroll events—this limits the function to run at most once per frame (about 60 times per second). - Send the event to your analytics. When a threshold is crossed and not yet logged, send an event. For GA4, use
gtag('event', 'scroll_depth', { depth: '25%' }). For Matomo, use_paq.push(['trackEvent', 'Scroll', 'Depth', '25%']). If you use a custom endpoint, send a beacon withnavigator.sendBeacon('/track', data)to ensure the request completes even when the user leaves the page. - Test in your browser. Open the page in a browser, open the developer console, and manually scroll. Check that the event fires at the right thresholds and that sessionStorage flags are set. Also verify the analytics dashboard receives the events.
- Deploy the script. Add the script to your site, either in the
<head>or via a tag manager. Make sure it loads after the DOM is ready (useDOMContentLoadedor place it at the end of the body).
Common Mistakes
- Not throttling scroll events: Scroll events fire rapidly; without throttling, you can overload the browser and cause lag. Always use
requestAnimationFrame. - Using cookies unnecessarily: Some analytics libraries default to cookies. Ensure your tracking code does not set cookies, or use a cookieless configuration if available.
- Forgetting to reset flags for new sessions: sessionStorage clears when the tab closes, which is good. But if you use localStorage, you must manually clear flags when the session ends or on a new page load—otherwise, you'll only track the first visit ever.
- Calculating depth incorrectly on short pages: If the page is shorter than the viewport, the percentage may exceed 100%. Clamp the value to 100% to avoid sending invalid data.
- Ignoring mobile browsers: Mobile browsers may handle scroll events differently (e.g., no scroll event during momentum scrolling). Use a fallback with
setIntervalor listen totouchmoveas well.
Tips
- Use a debounce or throttle library: If you don't want to write the throttling logic yourself, use a small utility like Lodash's
_.throttle. - Track scroll depth on key pages only: Not every page needs scroll tracking. Focus on long-form content, landing pages, and blog posts where depth is meaningful.
- Combine with other events: You can send scroll depth alongside other data, like page URL, to segment reports.
- Consider using IntersectionObserver: For more advanced tracking, you can use IntersectionObserver to detect when elements (e.g., a footer) become visible. This is more efficient than scroll events for specific elements.
- Test with private browsing: Ensure your script works in incognito mode, where sessionStorage is still available but cookies might be blocked.
Troubleshooting
- Events not firing: Check the browser console for JavaScript errors. Verify that the script is loaded and that the scroll event listener is attached. Also check that the thresholds are correctly defined and that the sessionStorage flags are being set.
- Events firing multiple times: This usually means the sessionStorage check is failing. Make sure you're using the same key consistently and that you're checking before sending the event. Also, ensure that the sessionStorage is not being cleared mid-session (e.g., by other scripts).
- Analytics not receiving events: Confirm that your analytics snippet is loaded and that you're using the correct event name and parameters. For GA4, check the DebugView in the Analytics interface. For custom endpoints, check the server logs.
- Scroll depth always 100%: This can happen if the page height is calculated incorrectly (e.g., using the wrong element). Use
document.documentElement.scrollHeightfor the full page height, notdocument.body.scrollHeight. - Performance issues: If the page lags while scrolling, your scroll handler is doing too much work. Throttle it more aggressively (e.g., every 200ms) or use IntersectionObserver instead.
Frequently Asked Questions
Does this work without any cookies at all?
Yes, sessionStorage is not a cookie. It's a web storage API that stores data in the browser and does not send it with HTTP requests. Your server never sees it unless you explicitly send it via JavaScript.
Will this work if the user has JavaScript disabled?
No, this method requires JavaScript. If you need to track scroll depth without JavaScript, you'd need a different approach like server-side tracking of mouse movement, which is not practical.
Can I track scroll depth in Google Tag Manager without custom code?
Yes, GTM has a built-in Scroll Depth trigger that can track percentage thresholds. It uses the same principle but doesn't require writing your own script. However, it may use cookies if your GA4 configuration does; you can set GA4 to be cookieless.
How do I know if my analytics tool is setting cookies?
Check your browser's developer tools under the Application tab, then Cookies. If you see any cookies from your analytics domain, that tool is setting them. Many tools offer a cookieless mode (e.g., GA4's storage: 'none' option).
Is sessionStorage reliable across tabs?
No, sessionStorage is per-tab. If a user opens the same page in two tabs, each tab has its own sessionStorage. This is usually fine for scroll tracking, but if you need cross-tab consistency, use localStorage with a session expiry.
Can I track scroll depth retroactively?
No, you can only track scroll depth from the moment the script is deployed. Historical data is not available unless you had a similar tracking mechanism before.