Skip to content

Commit

Permalink
fix(goatcounter): properly count SPA page hits (#1718)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
necauqua authored Feb 27, 2025
1 parent 9b47279 commit 6d35050
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions quartz/plugins/emitters/componentResources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,14 @@ function addGlobalPageResources(ctx: BuildCtx, componentResources: ComponentReso
`)
} else if (cfg.analytics?.provider === "goatcounter") {
componentResources.afterDOMLoaded.push(`
const goatcounterScript = document.createElement("script")
goatcounterScript.src = "${cfg.analytics.scriptSrc ?? "https://gc.zgo.at/count.js"}"
goatcounterScript.async = true
goatcounterScript.setAttribute("data-goatcounter",
"https://${cfg.analytics.websiteId}.${cfg.analytics.host ?? "goatcounter.com"}/count")
document.head.appendChild(goatcounterScript)
document.addEventListener("nav", () => {
const goatcounterScript = document.createElement("script")
goatcounterScript.src = "${cfg.analytics.scriptSrc ?? "https://gc.zgo.at/count.js"}"
goatcounterScript.async = true
goatcounterScript.setAttribute("data-goatcounter",
"https://${cfg.analytics.websiteId}.${cfg.analytics.host ?? "goatcounter.com"}/count")
document.head.appendChild(goatcounterScript)
})
`)
} else if (cfg.analytics?.provider === "posthog") {
componentResources.afterDOMLoaded.push(`
Expand Down

0 comments on commit 6d35050

Please sign in to comment.