Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(goatcounter): properly count SPA page hits #1718

Open
wants to merge 3 commits into
base: v4
Choose a base branch
from

Conversation

necauqua
Copy link
Contributor

On the surface it seems that only google and plausible scripts handle
the SPA correctly - but I don't know if maybe others handle
window.history API themselves somehow or something like that.

However, I am trying out goatcounter and in it's docs I see that it
does no special SPA handling, so this has to be added.

Note:
The whole significantLoad thing is to correctly count things -
goatcounter will not be loaded and initialized yet when the initial
"nav" is dispatched (because of the weird dynamic script tag insertion
thing you guys have going on here), but it will count that page load
when it itself is loaded.

If there was a way to ensure goatcounter is loaded before the first
nav, you could just configure it to not count anything on it's own load
(like in their spa example) and
have the "nav" call it unconditionally, but that's not the case 🤷

@necauqua
Copy link
Contributor Author

So I finally actually deployed a quartz site and I tested the goatcounter thing - apparently my previous fix didn't quite work.

The new way (force-pushed) is the only one I've found to actually work relatively reliably at least.

On the surface it seems that only google and plausible scripts handle
the SPA correctly - but I don't know if maybe others handle
window.history API themselves somehow or something like that.

However, I am trying out goatcounter and in it's docs I see that it
does no special SPA handling, so this has to be fixed.

Just doing the dynamic script thing on every nav seems to do the trick.
The script is not "spa-preserve" so they wouldn't accumulate - and when
I tried the "spa-preserve" + call goatcounter api route it didn't quite
work, they actually did accumulate
Copy link
Collaborator

@aarnphm aarnphm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

plausible works given that the plausible script will attach a window.plausible :)

quartz/plugins/emitters/componentResources.ts Show resolved Hide resolved
@aarnphm
Copy link
Collaborator

aarnphm commented Jan 26, 2025

Can you check again if this works?

@necauqua
Copy link
Contributor Author

Owie owie owies, what did you do to the branch lol

Anyway, re-tested your exact commit to be sure, but just "spa-preserve" like that does nothing - it only counts the first pageload.
Adding "spa-preserve" and then calling goatcounter api on nav events for some reason doubles the amount of hits on every hit - I mentioned that in the updated comment description.

The only thing that seemingly worked great is this ugly re-adding the script on nav events - at least the script itself is cached, and also tiny so not like we re-execute alot, pfew

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants