This works well if you run a server, but for a static site, you need client-side JS. I recently found 2 free AND open source services (though I did donate): GoatCounter[0] and Counter.dev[1]. I've added them to my static site[2]. Naturally they disagree on my (low in any case) traffic, but they're still fun to look at :)
[0] https://www-bbkane-com.goatcounter.com/
[1] https://counter.dev/dashboard.html?user=bbkane&token=MEzACRA...
[2] https://www.bbkane.com/