Val Town is a collaborative website to build and scale JavaScript apps.
Deploy APIs, crons, & store data ā all from the browser, and deployed in milliseconds.
A tiny pageview tracker for your Val Town HTTP vals. One SQLite table, three endpoints, and a one-line SDK.
Wrap your handler ā every HTML page view is counted, fire-and-forget:
import { withPageviews } from "https://esm.town/v/dcm31/pageviews/sdk.ts";
async function handler(req: Request) {
return new Response("<h1>hi</h1>", {
headers: { "content-type": "text/html" },
});
}
export default withPageviews(handler, { site: "my-cool-val" });
Or count manually anywhere:
import { count, hit } from "https://esm.town/v/dcm31/pageviews/sdk.ts";
await hit({ site: "my-cool-val", path: "/about" }); // -> { total, page }
await count({ site: "my-cool-val" }); // read only, no increment
| Export | What it does |
|---|---|
withPageviews(handler, { site }) | Wraps an HTTP handler; counts each 2xx HTML GET. Options: contentType (RegExp or null for all), path (custom path fn). |
hit({ site, path? }) | Increments, returns { site, path, total, page }. |
count({ site, path? }) | Reads totals without incrementing. |
track({ site, path? }) | Fire-and-forget increment; never throws, never blocks. |
Anything that speaks HTTP can use these directly:
GET /hit?site=foo&path=/bar increment, returns JSON totals
GET /count?site=foo read totals (no increment)
GET /stats?site=foo total + per-path breakdown (top 50)
site is required; path defaults to /. Responses are JSON and sent with
no-store so counters never freeze on a cached value.
State lives in a val-scoped SQLite table keyed by (site, path):
CREATE TABLE pageviews (
site TEXT NOT NULL,
path TEXT NOT NULL,
count INTEGER NOT NULL DEFAULT 0,
updated_at TEXT,
PRIMARY KEY (site, path)
);
total is SUM(count) across every path for a site; page is the count for
one path.
dcm31/hit-counter renders this
data as a retro 7-segment LED odometer image you can drop in as an <img>.