🧮 viewcounter
created 2022/09/17 modified 2025/11/18 category software views 352
an view counter i wrote for this site
why?
i wanted to have really basic analytics (if you can even call it that) on my website so i could know what pages people are reading with the criteria:
A. doesn't track you
B. can be easily disabled by just disabling javascript
C. lightweight, simple to deploy
how
in about 190 lines of vanilla node.js (60 of which is comments & blanks) viewcounter listens to requests on a single path
if it is a GET request, a summary of all the page view counts are returned
if it is a POST request with a page path as a body, that page's view count is incremented
preventing abuse
there are three mechanisms in place;
1. for every request a counter per its ip is incremented and a timer is set to decrement it, if the counter reaches this number further requests are blocked until it is decremented
2. the sent url is checked against a regular expression, if it passes then viewcounter sends a HEAD request to make sure it's an actual page on the website (and not idk a slur or something) of which the result is cached so further requests to the same url can be quickly processed
3. there is also a 24h ratelimit in place to prevent spam. each ip has a "profile" of when it has last visited a page, if that date is today[^*] then their view is not counted, otherwise the view count is incremented and the date is set to today[^*]
[^*]: today in time of the server