📊 ethical view counting api
Go to file
sophie 9d783dbf41 hotfix for caddy 2022-09-18 02:58:45 +01:00
.gitignore implement ratelimiting 2022-09-17 23:49:38 +01:00
LICENSE Initial commit 2022-09-17 19:52:28 +02:00
README.md add web widget example 2022-09-18 00:39:52 +01:00
conf.json implement ratelimiting 2022-09-17 23:49:38 +01:00
index.js hotfix for caddy 2022-09-18 02:58:45 +01:00
package.json create viewcounter 2022-09-17 22:34:29 +01:00
viewcounter.service add example systemd service 2022-09-18 00:59:00 +01:00

README.md

viewcounter

ethical view counting api

examples

embedded view counter

<div id="view-count"></div>
<script>
	var req = new XMLHttpRequest();
	req.addEventListener("load", function () {
		document.getElementById("view-count")
			.innerHTML = "<b>page views</b> " + JSON.parse(this.responseText).views;
	});
	req.open("POST", "/viewcounter");
	req.send("/index.html");
</script>

requesting all view counts

sending a GET request will return a json object with all the stored viewcounts

$ curl localhost:8001/viewcounter
{"code":200,"/index.html":1,...}

incrementing view counts/retrieving view count

to increment a view count, simply send a post request with the body containing the page path. the response will contain the view count

$ curl localhost:8001/viewcounter -d /index.html
{"code":200,"message":"counted page view","views":2}

if you have already incremented the view

$ curl localhost:8001/viewcounter -d /index.html
{"code":200,"message":"you have seen this page today","views":2}

configuration

{
	"port": 8001,
	"path": "/viewcounter",
	"site": "zvava.org",
	"countDB": ".count.json",
	"ratelimitDB": ".ratelimit.json",
	"saveTimeout": 5000
}

port

port the service is hosted on

path

path to listen on

eg. a path of /api/viewcount would respond to http://ip:port/api/viewcount

site

site to validate urls against

eg. with a site of example.com, posting /cheese.html will check if https://example.com/cheese.html returns a 200 status code or not

countDB + ratelimitDB

filename for both the view counts and last viewed dates of pages attributed to ips

if you see your ratelimitDB getting absurdly large i'd recommend deleting it every once in a while

saveTimeout

period to elapse before an update in view count is saved to disk, this saves the storage from constant strain from frequent changes