Track Button Clicks Without Cookies
In this article
Tracking button clicks is essential for understanding user behavior, but cookie-based tracking is increasingly blocked by browsers and privacy regulations. This guide covers practical, cookieless alternatives that respect user privacy while still giving you the data you need. You'll learn about event tracking via analytics platforms, server-side logging, and privacy-friendly tools—all without setting a single cookie.
What You'll Need
- Access to your website's HTML and JavaScript files (or a tag manager like Google Tag Manager).
- An analytics platform that supports cookieless tracking (e.g., Plausible, Fathom, or a self-hosted Matomo with cookieless mode).
- If you choose server-side tracking: access to your server logs or a backend endpoint.
- Basic familiarity with HTML/JavaScript for adding event listeners.
Step-by-Step
- Choose your tracking method. Decide between client-side (JavaScript sends data to an analytics service) or server-side (your server logs requests). For most sites, client-side is easier; for high-security or high-accuracy needs, server-side is better.
- Set up a cookieless analytics tool. If you're using a privacy-focused service, sign up and get a tracking script. For example, with Plausible, you add a script tag to your site; it uses localStorage or a hash, not cookies. For self-hosted Matomo, enable the cookieless mode in settings.
- Add a click event listener. In your JavaScript, target the button by its ID, class, or data attribute. Use
addEventListener('click', function() { ... })to trigger tracking. - Send the event data. For analytics tools, call their API. For Plausible: . For Matomo: _paq.push(['trackEvent', 'Button', 'Click', 'Download']) . If you're using server-side, send a request to your own endpoint using fetch or navigator.sendBeacon .
- Test in the browser. Open your site, click the button, and check the network tab or your analytics dashboard to see if the event fires. Use incognito mode to ensure no cookies are set.
- Verify cookieless compliance. Use browser dev tools to check that no cookies are created. For server-side, check your server logs for the request.
- Analyze the data. In your analytics dashboard, create a custom report or view the events. For server-side, parse your logs with a tool like GoAccess or a simple script.
Common Mistakes
- Not using a cookieless tool. Some tools claim privacy but still set cookies. Always verify.
- Forgetting to prevent default. If your button submits a form or navigates, the page may unload before the tracking request completes. Use
event.preventDefault()and then track, or usesendBeaconwhich is designed for this. - Tracking only one button. If you have multiple buttons, use a common class and loop through them, or use event delegation.
- Ignoring ad blockers. Some ad blockers block all analytics scripts. Consider server-side tracking as a fallback.
- Not testing in different browsers. Safari and Firefox have stricter privacy features; test in all major browsers.
Tips
- Use
navigator.sendBeacon()for reliable delivery when the user navigates away immediately after clicking. - Add a
data-trackattribute to your buttons to keep tracking logic separate from styling. - For server-side tracking, log the button ID, timestamp, and referrer page. This gives you context without cookies.
- Combine with server-side logging for a complete picture: client-side for user behavior, server-side for technical accuracy.
- If you use Google Analytics, enable Consent Mode and use the cookieless features, but note that it still uses cookies for some features; for fully cookieless, choose a dedicated tool.
Troubleshooting
Events not showing up in dashboard. Check the browser console for errors. Ensure the tracking script is loaded before your event listener. For Plausible, verify the domain matches your site.
Click works but no data. If using sendBeacon, check the network tab for a POST request. If using fetch, ensure the server endpoint is correct and CORS is configured if cross-origin.
Ad blocker blocks tracking. Use a server-side solution: add an endpoint that logs clicks, and send a beacon to it. This bypasses ad blockers.
Cookie still being set. Some tools set a cookie for session purposes. Disable that feature in settings, or switch to a tool that uses localStorage or fingerprinting.
Multiple clicks tracked as one. If you use event delegation, ensure you're not attaching multiple listeners. Use a flag or remove the listener after the first click.
Frequently Asked Questions
Is cookieless tracking accurate?
Yes, it can be very accurate for event counting. It misses some cross-session data, but for button clicks, it's reliable.
Does cookieless tracking violate GDPR?
No, cookieless tracking is generally more compliant because it doesn't store personal data. However, you still need to inform users if you collect any data.
Can I track clicks without any analytics tool?
Yes, you can log clicks to your server using a simple endpoint. This gives you full control and no third-party scripts.
What is the difference between cookieless and cookie-less?
They are the same; cookieless means not using HTTP cookies. Some tools use localStorage or fingerprinting instead.
Will cookieless tracking work on all browsers?
Yes, it works on all modern browsers, including Safari and Firefox, which block third-party cookies.
How do I track clicks on a button that is inside an iframe?
You need to track from within the iframe's context. Add the tracking script to the iframe's document, or use postMessage to communicate with the parent page.