Created
February 24, 2023 00:35
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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