Skip to content

Commit 7253557

Browse files
authored
fix(goatcounter): properly count SPA page hits (#2035)
1. Should not create new instance after count.js, as it already setup an instance. 2. Script block would be removed when navigating with SPA, and it cause count.js can not find endpoint by query. The solution is to set endpoint manually.
1 parent 82c53a1 commit 7253557

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

quartz/plugins/emitters/componentResources.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,19 @@ function addGlobalPageResources(ctx: BuildCtx, componentResources: ComponentReso
135135
`)
136136
} else if (cfg.analytics?.provider === "goatcounter") {
137137
componentResources.afterDOMLoaded.push(`
138+
const goatcounterScriptPre = document.createElement('script');
139+
goatcounterScriptPre.textContent = \`
140+
window.goatcounter = { no_onload: true };
141+
\`;
142+
document.head.appendChild(goatcounterScriptPre);
143+
144+
const endpoint = "https://${cfg.analytics.websiteId}.${cfg.analytics.host ?? "goatcounter.com"}/count";
138145
const goatcounterScript = document.createElement('script');
139146
goatcounterScript.src = "${cfg.analytics.scriptSrc ?? "https://gc.zgo.at/count.js"}";
140147
goatcounterScript.defer = true;
141-
goatcounterScript.setAttribute(
142-
'data-goatcounter',
143-
"https://${cfg.analytics.websiteId}.${cfg.analytics.host ?? "goatcounter.com"}/count"
144-
);
148+
goatcounterScript.setAttribute('data-goatcounter', endpoint);
145149
goatcounterScript.onload = () => {
146-
window.goatcounter = { no_onload: true };
150+
window.goatcounter.endpoint = endpoint;
147151
goatcounter.count({ path: location.pathname });
148152
document.addEventListener('nav', () => {
149153
goatcounter.count({ path: location.pathname });

0 commit comments

Comments
 (0)