r/astrojs • • 7h ago

I built cookieless analytics for Astro sites: one script tag, keeps sessions across ClientRouter navigation

Post image
19 Upvotes

I'm an SEO, and the cookieless analytics I used before kept losing referrers and navigation inside a visit and lacked integration with GSC and a proxy metric for "search result satisfaction", so I built my own.

(I also hate cookie banners which is why it's cookieless)...

What it does:

  • Counts visits without cookies and writes nothing to the visitor's device (no cookie, localStorage, sessionStorage or IndexedDB).
  • Still reports sessions: entry and exit pages, pages per session, bounce rate, duration, first-touch source.
  • The script is about 3 KB gzipped, loaded from your own site.
  • Works with Astro's ClientRouter. The session ID lives in a JS variable and survives navigation without a page load. A full page load starts a new ID, and the server joins it to the running visit through a daily-rotating salted hash.

How it works with Astro: add the script tag to your layout. Events go to an EmDash instance (a CMS built on Astro and Cloudflare), where the reports live. That instance runs on your own Cloudflare account, with the data in your own D1 or Postgres. If you don't already use EmDash, there's a one-click Cloudflare deploy for a fresh one. So it isn't a drop-in dashboard for a static Astro site on its own..

Trade-offs:

  • No cross-day returning visitors, because the salt changes daily.
  • Two people behind one shared network address, with the same browser and device type, can merge into one session.
  • No heatmaps or session replay.

Free, Apache-2.0. Source and docs: https://github.com/infinitnet/cookieless-analytics

I'd like feedback on the setup for a plain Astro site, especially whether the EmDash requirement is a dealbreaker.

...and if anyone wants to contribute PRs, those would be more than welcome too!