From cdc5728c5216b8ab1ecd3e7116ae6be05ecc0162 Mon Sep 17 00:00:00 2001 From: Anton Bulakh Date: Sat, 4 Jan 2025 19:51:03 +0200 Subject: [PATCH 1/2] fix(goatcounter): properly count SPA page hits 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 --- quartz/plugins/emitters/componentResources.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/quartz/plugins/emitters/componentResources.ts b/quartz/plugins/emitters/componentResources.ts index 08278305e..49e281540 100644 --- a/quartz/plugins/emitters/componentResources.ts +++ b/quartz/plugins/emitters/componentResources.ts @@ -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(` From cd863a88e3528cad553efa7ee36d36dd50dc3a32 Mon Sep 17 00:00:00 2001 From: Aaron Pham Date: Sun, 26 Jan 2025 08:39:43 -0500 Subject: [PATCH 2/2] fix(goatcounter): asign spaPreserve to analytics script Signed-off-by: Aaron Pham --- quartz/plugins/emitters/componentResources.ts | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/quartz/plugins/emitters/componentResources.ts b/quartz/plugins/emitters/componentResources.ts index 49e281540..580082302 100644 --- a/quartz/plugins/emitters/componentResources.ts +++ b/quartz/plugins/emitters/componentResources.ts @@ -122,14 +122,13 @@ function addGlobalPageResources(ctx: BuildCtx, componentResources: ComponentReso `) } else if (cfg.analytics?.provider === "goatcounter") { componentResources.afterDOMLoaded.push(` - 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) - }) + const goatcounterScript = document.createElement("script") + goatcounterScript.src = "${cfg.analytics.scriptSrc ?? "https://gc.zgo.at/count.js"}" + goatcounterScript.async = true + goatcounterScript.setAttribute("spa-preserve", "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(`