Skip to content

Adding Live Visitor Stats to This Blog with a Cloudflare Worker

This site already used Cloudflare Web Analytics to track visitors, but that data only lived in the Cloudflare dashboard. I wanted a small, live stats badge visible on the site itself — partly for the fun of it, partly because it’s a compact example of a few things that come up constantly in platform and DevOps work: querying an API securely, caching at the edge, and wiring a static site to a small piece of serverless compute.

Every page on this blog now shows a live count of page views and visits over the last 30 days, pulled directly from Cloudflare’s own analytics data.

The site itself is static — built with Hugo and deployed through Cloudflare Pages. Static sites can’t hold secrets or make authenticated API calls on their own, so the stats can’t be baked in at build time and still be current. The fix was a small Cloudflare Worker sitting in between:

  • The Worker holds a scoped, read-only Cloudflare API token as a secret (never in source control)
  • On a request, it queries Cloudflare’s GraphQL Analytics API for the site’s visit and page view totals
  • The response is cached at the edge for ten minutes, so the analytics API isn’t hit on every single page load
  • The Worker returns a small JSON payload, with CORS locked down to this domain only

On the Hugo side, a small script in the footer partial fetches from the Worker and renders the result. No page rebuild is needed for the numbers to update — they refresh independently of the static site’s own deploy cycle.

Cloudflare’s Web Analytics dashboard shows a “beacon token” when you install the tracking snippet. It’s tempting to assume that’s also the site identifier used by the GraphQL Analytics API — it isn’t. The two are different values that happen to look similar (both 32-character hex strings). Querying the API without that filter first, then reading back the real site identifier from the response, made the mismatch obvious in a couple of test queries rather than a long guessing session.

Why this is a useful pattern beyond a stats badge

Section titled “Why this is a useful pattern beyond a stats badge”

The shape of this build — a static frontend, a small serverless function holding the one thing that needs to be secret, and a scheduled or cached call to a third-party API — comes up in plenty of real infrastructure work: surfacing metrics on an internal dashboard, proxying a rate-limited API, or adding a lightweight status indicator to a docs site. Cloudflare Workers are a genuinely low-friction way to do this without standing up a full backend service.

  • Hugo (PaperMod theme) — static site generation
  • Cloudflare Pages — hosting and CI/CD
  • Cloudflare Workers — serverless middle layer
  • Cloudflare GraphQL Analytics API — the actual data source
  • Wrangler CLI — Worker development and deployment

The full step-by-step setup — including the exact API queries used to track down the site-identifier mismatch — is documented in the project repo.