mirror of
https://gitlab.com/MisterBiggs/brain-quartz.git
synced 2025-07-23 06:41:30 +00:00
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.
This commit is contained in:
@@ -135,15 +135,19 @@ function addGlobalPageResources(ctx: BuildCtx, componentResources: ComponentReso
|
|||||||
`)
|
`)
|
||||||
} else if (cfg.analytics?.provider === "goatcounter") {
|
} else if (cfg.analytics?.provider === "goatcounter") {
|
||||||
componentResources.afterDOMLoaded.push(`
|
componentResources.afterDOMLoaded.push(`
|
||||||
|
const goatcounterScriptPre = document.createElement('script');
|
||||||
|
goatcounterScriptPre.textContent = \`
|
||||||
|
window.goatcounter = { no_onload: true };
|
||||||
|
\`;
|
||||||
|
document.head.appendChild(goatcounterScriptPre);
|
||||||
|
|
||||||
|
const endpoint = "https://${cfg.analytics.websiteId}.${cfg.analytics.host ?? "goatcounter.com"}/count";
|
||||||
const goatcounterScript = document.createElement('script');
|
const goatcounterScript = document.createElement('script');
|
||||||
goatcounterScript.src = "${cfg.analytics.scriptSrc ?? "https://gc.zgo.at/count.js"}";
|
goatcounterScript.src = "${cfg.analytics.scriptSrc ?? "https://gc.zgo.at/count.js"}";
|
||||||
goatcounterScript.defer = true;
|
goatcounterScript.defer = true;
|
||||||
goatcounterScript.setAttribute(
|
goatcounterScript.setAttribute('data-goatcounter', endpoint);
|
||||||
'data-goatcounter',
|
|
||||||
"https://${cfg.analytics.websiteId}.${cfg.analytics.host ?? "goatcounter.com"}/count"
|
|
||||||
);
|
|
||||||
goatcounterScript.onload = () => {
|
goatcounterScript.onload = () => {
|
||||||
window.goatcounter = { no_onload: true };
|
window.goatcounter.endpoint = endpoint;
|
||||||
goatcounter.count({ path: location.pathname });
|
goatcounter.count({ path: location.pathname });
|
||||||
document.addEventListener('nav', () => {
|
document.addEventListener('nav', () => {
|
||||||
goatcounter.count({ path: location.pathname });
|
goatcounter.count({ path: location.pathname });
|
||||||
|
Reference in New Issue
Block a user