Page View backend based on Cloudflare Workers and KV.
Live demo:
There are two pages containing demo/tracking.js for this live demo:
⚠️ Demo pages will collect your IP/Country/City, click at your own risk.
- Setup workers, refer to quickstarts
- Publish worker.js
- Add KV binding, refer to referencing KV from Workers
- Binding name is
KV
in this project.
- Binding name is
- Add following tracking code to HTML files you want to track, replace
apiEndpoint
with your worker address.const apiEndpoint = 'https://pageview-demo.jiacai2050.workers.dev/write'; const payload = { 'url': document.location.href, 'title': document.title, 'referrer': window.frames.top.document.referrer, }; fetch(`${apiEndpoint}?${new URLSearchParams(payload)}`, {mode: 'no-cors'}) .catch(console.log);
- Then when visit HTML, following information will be saved to KV via workers:
- URL
- Title
- Referrer
- User-Agent
- IP
- Country
- City
- demo/index.html is provided to view saved pageviews.
- Replace
apiEndpoint
with your worker address.
- Replace