Web Analytics in an SPA, pushState not working as expected

Hi there, I’m having a bit of a problem with Web Analytics in my SPA.

Visits when a user loads the SPA directly in a certain route are registered properly, but routes visited programmatically using pushState are not.

But for some reason, some of the routes visited using pushState are shown in my analytics dashboard, just with no visits. [see image below]

I checked out these three links:

https://developers.cloudflare.com/analytics/web-analytics/getting-started/web-analytics-spa

And made sure to:

  • Include the script like this (I also tried not including the “spa” property at all):
<script 
  defer src='https://static.cloudflareinsights.com/beacon.min.js' 
  data-cf-beacon=' {"token": "[blurred on purpose]", "spa": true} '
></script>
  • Deploy my site after the change
  • Have only one index.html file in my deploy folder
  • Use a non-hash based router that doesn’t override the pushState method itself but does use it
    • I’m using pwa-helpers router
    • I’m also calling pushState myself but I’m not overriding the prototype

Any ideas on why this might be happening?

Thanks in advance

Edit: Did some extra tests in incognito mode just to check my extensions weren’t getting in the way and that had the same results, no visit registered

Hi, thanks for your feedback. You should be looking at the Page Views instead of Visits for those route changes made within your application.

https://developers.cloudflare.com/analytics/web-analytics/understanding-web-analytics
Visit counts are counted when the user came from other sites or direct visits. So SPA route changes are not counted as visits, so you are seeing the path with zero counts (showing zero counts is our bug, btw), so please check from your Page Views whether expected route change events are being captured. Let us know if you have any other questions. Thank you!

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.