benvinegar / counterscale

Scalable web analytics you run yourself on Cloudflare
https://counterscale.dev
MIT License
1.51k stars 66 forks source link

Tracking page views in a Single Page App #83

Open ninjaPixel opened 4 months ago

ninjaPixel commented 4 months ago

I'm using counterscale with Next.js and on page load it sends a trackPageview event to Cloudflare. However, on subsequent page views (where Next.js is effectively a SPA) nothing is sent to Cloudflare. I have tried:

  1. Calling window.counterscale("trackPageview") on navigation
  2. Setting the data-spa="auto" property on the counterscale script tag. (This property is available on the Fathom script so I thought I'd give it a go here!)

How can I make sure that my SPA page views are tracked? Thanks!

benvinegar commented 4 months ago

@ninjaPixel First, thanks for submitting this. 🙏

How can I make sure that my SPA page views are tracked?

I think we need to augment tracker.js to be aware of (and track) navigation changes using PopStateEvent (mdn).

But before that, we need to add tests to the tracker using Playwright or something similar. I've been reticent to add functionality to the tracker because it's not covered at the moment.