The Trans Directory
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

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

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

authored by

Anton Bulakh and committed by
GitHub
6d350500 9b47279f

+8 -6
+8 -6
quartz/plugins/emitters/componentResources.ts
··· 122 122 `) 123 123 } else if (cfg.analytics?.provider === "goatcounter") { 124 124 componentResources.afterDOMLoaded.push(` 125 - const goatcounterScript = document.createElement("script") 126 - goatcounterScript.src = "${cfg.analytics.scriptSrc ?? "https://gc.zgo.at/count.js"}" 127 - goatcounterScript.async = true 128 - goatcounterScript.setAttribute("data-goatcounter", 129 - "https://${cfg.analytics.websiteId}.${cfg.analytics.host ?? "goatcounter.com"}/count") 130 - document.head.appendChild(goatcounterScript) 125 + document.addEventListener("nav", () => { 126 + const goatcounterScript = document.createElement("script") 127 + goatcounterScript.src = "${cfg.analytics.scriptSrc ?? "https://gc.zgo.at/count.js"}" 128 + goatcounterScript.async = true 129 + goatcounterScript.setAttribute("data-goatcounter", 130 + "https://${cfg.analytics.websiteId}.${cfg.analytics.host ?? "goatcounter.com"}/count") 131 + document.head.appendChild(goatcounterScript) 132 + }) 131 133 `) 132 134 } else if (cfg.analytics?.provider === "posthog") { 133 135 componentResources.afterDOMLoaded.push(`