Skip to content

Instantly share code, notes, and snippets.

@kytta
Created February 24, 2023 00:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kytta/dd8ddb4ea6f255e4fec9bd7adb4974e5 to your computer and use it in GitHub Desktop.
Save kytta/dd8ddb4ea6f255e4fec9bd7adb4974e5 to your computer and use it in GitHub Desktop.
A very lightweight GoatCounter count script. It only counts the visit without transferring too much data. Perfect for single-page websites.
// Only count the production website
// TODO: You should replace these with your webpage production URL
if (window.location.host === "domain.tld") {
// Check if the default GC URL resolves
// This allows us to not track people with ad blockers
fetch("//gc.zgo.at/", { method: "HEAD" })
.then((result) => {
if (!result.ok) {
return;
}
// You can remove this if you don't need screen data
const screen = encodeURIComponent(
[
window.screen.width,
window.screen.height,
window.devicePixelRatio || 1,
].join(",")
);
const random = encodeURIComponent(Math.random().toString(36).slice(2));
navigator.sendBeacon(
// TODO: change this to your count URL
`https://YOUR_GOATCOUNTER_ID.goatcounter.com/count?p=%2F&s=${screen}&b=0&rnd=${random}`
);
})
.catch((_) => {});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment