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 (#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.

authored by

Wen Taichi and committed by
GitHub
7253557d 82c53a1c

+9 -5
+9 -5
quartz/plugins/emitters/componentResources.ts
··· 135 135 `) 136 136 } else if (cfg.analytics?.provider === "goatcounter") { 137 137 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"; 138 145 const goatcounterScript = document.createElement('script'); 139 146 goatcounterScript.src = "${cfg.analytics.scriptSrc ?? "https://gc.zgo.at/count.js"}"; 140 147 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); 145 149 goatcounterScript.onload = () => { 146 - window.goatcounter = { no_onload: true }; 150 + window.goatcounter.endpoint = endpoint; 147 151 goatcounter.count({ path: location.pathname }); 148 152 document.addEventListener('nav', () => { 149 153 goatcounter.count({ path: location.pathname });