Skip to content

feat: visit analytics - #1067

Open
Brooooooklyn wants to merge 18 commits into
mainfrom
feat/visit-analytics
Open

Brooooooklyn wants to merge 18 commits into
mainfrom
feat/visit-analytics

Conversation

@Brooooooklyn

@Brooooooklyn Brooooooklyn commented Dec 6, 2026 •

Copy link
Copy Markdown
Member

Server-side visit analytics for Void. This replaces #1064, #1065 and #1066, and the rest of the work lands on this branch too.

HOSTED
visitor ─► dispatch ─► classifyPageView() ─► same void_requests row + view fields (still 1 row per request)
API  GET /projects/:id/analytics ─► dashboard /projects/:slug/analytics
                                 └► void project analytics [on|off]
NATIVE
void project analytics ─► cf login ─► custom domain → zone ─► Cloudflare GraphQL (page loads only)

Decisions: server-side only (no browser script), page views only (no unique-visitor counting), on by default with a per-project off switch, no paths or referrers in --runtime-logs minimal mode, and bot labels from the user agent.

Steps

  • 1. Shared parser: @void/edge/visit-analytics exports parseUserAgent() (device, browser, bot label, bot group; no regex, no new dependency) plus VISIT_RANGES and botGroupName().
  • 2. SDK prefetch header: static /_void/pages/*.json prefetch now sends Purpose: prefetch, like the dynamic prefetch already did, so the server can tell it from a visit.
  • 3. Dispatch: adds blobs 7-15 (kind, path, host, referrer host, country, device, browser, bot label, bot group) and double5 = 1 to the existing row. Rows that are not views keep today's exact shape, and double4 stays 0 because the proxy uses it. Off switch: the ROUTING key project-analytics-off:<id>, cached 60s.
  • 4. API: GET /projects/:id/analytics (read permission). Ranges are 24h|7d|30d|90d, with host, path and traffic filters, validated before any Analytics Engine call. It returns totals, a series, the bot split, and top pages, referrers, countries, devices and browsers. PUT /projects/:id/analytics/settings (settings permission) writes projects.analytics_enabled (migration 0068, default true) and the KV off marker. Minimal mode hides paths and referrers, including older rows. A 1.13.x compatibility entry lets those installs upgrade past 0068.
  • 5. Dashboard: /projects/:slug/analytics adds stat tiles, an area chart of views, a stacked bar chart of humans vs bot labels, and top lists. The charts are inline SVG with a "Show numbers" table, they work without JS, and they have dark-mode tokens. The range, host and traffic filters use a plain GET form. Owners and admins get an on/off switch. The page handles the off, minimal-mode, empty and unavailable states. It adds an Analytics tab next to Security.
  • 6. CLI and docs: void project analytics [on|off] [--range] [--host] [--path] [--traffic] [--json]. Every visitor string goes through cleanLine. Docs: a new guide docs/guide/analytics.md, the CLI reference, the help text and hint, setup.md (minimal mode), roles, and the skill.
  • 7. Native CLI: for void deploy --platform cloudflare apps, void project analytics reads the app's own zone httpRequestsAdaptiveGroups (HTML GET 2xx, requestSource: eyeball) for the app's custom domains and whole-host zone routes. It shows page loads only, with no referrers (Free zones refuse that field) and 31 days of history. OAuth logins need analytics.read; API tokens get a "Zone → Analytics → Read" hint. Read-only: no binding, deploy step or probe is added, so it stays Workers Free compatible. A regression test checks the native config is unchanged. Checked live, read-only, on the test zone.

What counts as a view (step 3)

Counted Not counted
HTML page load (GET, 2xx except 204/206, or 304) HEAD, POST, 404, 5xx
Pages client navigation answered with page data or a static HTML page prefetch (Purpose, Sec-Purpose, X-Moz, X-Purpose)
/_void/pages/*.json on void or static apps, mapped back to its page Pages redirects (200 + X-VoidPages-Redirect)
the page-data fetch after an HTML answer (X-VoidPages-Followup)
private previews, uptime monitors

Tests

  • packages/edge/test/unit/visit-analytics.test.ts: 99 cases built from real UAs
  • packages/void/test/unit/pages-client.test.ts: prefetch sends Purpose, a visit doesn't (fast path off and on)
  • platform/packages/dispatch: new page-view.test.ts, plus updates to metering.test.ts and index.test.ts; 913 passed
  • vp check: clean
  • vp run test: 202/202 (one full run, 88% not from cache)
  • Plus: API integration tests for the analytics routes, dashboard page and chart tests, CLI hosted and native tests, and the 1.13.x upgrade test for migration 0068

🤖 Generated with Claude Code

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 6, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-06T12:16:58.725001Z 0d66617 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Dec 6, 2026 •

Copy link
Copy Markdown

Cloudflare builds and deploys previews for this pull request. Learn more about Cloudflare previews.

Worker Status Deployment Commit
void-cloud Deployed Preview 4e0ba1a

Updated: Dec 6, 2026, 14:28 UTC | Dashboard

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0d666171ca

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Brooooooklyn and others added 18 commits December 6, 2026 22:21
Add `@void/edge/visit-analytics`, a small pure module the platform will use
for visit analytics:

- parseUserAgent(ua, secChUaMobile?, secChUa?) returns device, browser,
  bot label and bot group (ai/search/social/seo/monitor/tool/other). It
  lowercases once and checks ordered token lists with includes(); the
  first match wins. No regex, no new dependency.
- VISIT_RANGES (24h, 7d, 30d, 90d hosted-only) and isVisitRangeId().
- botGroupName() friendly group names for the dashboard and CLI.

The module is its own pack entry and subpath export, so the dispatch
worker can import it without the routing and static-asset runtime.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… archive.org_bot

- Node's built-in fetch() sends `User-Agent: node`; match that whole UA as a
  `tool` (not with includes(), so device names containing "node" stay human).
- Chrome-Lighthouse (PageSpeed Insights) is a tool, not a Chrome view.
- Cloudflare-Healthchecks is a monitor, so dispatch drops it as a probe.
- archive.org_bot gets a named token; the generic label cut it at the dot.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Dispatch now marks a request as a page view and adds blobs 7-15 (kind,
path, host, referrer host, country, device, browser, bot label, bot group)
and doubles 4-5 ([.., 0, 1]) to the one usage row it already writes. Rows
that are not views keep the 6-blob, 3-double shape, so billing is unchanged.

- page-view.ts: classifyPageView (GET only, no previews, no prefetch
  markers, 2xx except 204/206 or 304, HTML document loads and Void Pages
  client navigations incl. /_void/pages/*.json, monitors dropped).
- analytics-setting.ts: per-project off switch read from the
  project-analytics-off:<id> ROUTING key, cached 60s per isolate; a read
  error counts as off.
- metering.ts: optional view; path capped at 512 bytes; path and referrer
  blank in minimal runtime-logs mode.
- index.ts: still exactly one row per request; written from waitUntil only
  when the setting read is still pending.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…tic deploys

- A Void Pages client navigation that hits a redirect gets a 200 with
  X-VoidPages-Redirect and Location; the client then fetches the target.
  Do not count that 200 as a view of the source page.
- output: "static" apps with no backend deploy as routing type static.
  Count their /_void/pages/*.json page-data fetches as nav views too. The
  X-VoidPages rule stays void-only: a static deploy answers it with HTML
  and the client follows with the page-data file.
- Skip classifyPageView when the project is known to have analytics off.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… page data

An X-VoidPages request answered with HTML or the stale-build reload answer is
followed by a page-data file fetch or a full page load, which is the counted
view. Counting both counted one visit twice.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Dynamic X-VoidPages prefetch already sends Purpose: prefetch, but the
static /_void/pages/*.json fetch did not, both on the fast path and when
the HTML answer has deferred props. Pass the purpose through
loadStaticPageData and loadStaticPageDataFile so every prefetch request
says so, and a visit never does. The header does not change the static
file or its cache key.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
When the fast-path static JSON fetch misses and the dynamic GET returns
HTML, loadStaticPageData fetches the static file again. Run the HTML-first
Purpose test with the fast path on too, so that retry must carry
Purpose: prefetch for a prefetch and no Purpose for a visit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A Pages navigation answered with a static HTML page often makes no other
request: the client reads the page data embedded in the HTML. Count that
answer as the view, and mark the page-data file fetch that can follow it with
X-VoidPages-Followup so dispatch does not count the same navigation twice.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
GET /projects/:id/analytics reads the dispatch page-view fields in
void_requests (series by bot label, top pages, referrers, countries,
devices, browsers) for 24h/7d/30d/90d with host, path and traffic
filters. PUT /projects/:id/analytics/settings stores
projects.analytics_enabled (migration 0068, default true) and the
ROUTING marker dispatch reads; project deletion removes the marker.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…past 0068

- Minimal runtime-log mode now hides referrers as well as paths: no
  referrer query is sent and `referrers` is null, so rows stored before
  the switch no longer show referrer hosts.
- `traffic` filters only the five top lists. `totals.views`,
  `series[].views`, `documentViews` and `navViews` count humans and bots,
  so `views = humanViews + botViews` as the API contract says.
- Record 1.13.0 and 1.13.1 (schema 0000..0067) as rollback-safe for
  migration 0068 and cover it in the upgrade compatibility test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add /projects/:slug/analytics: range links and a plain GET filter form
(host, which visitors the top lists count), stat tiles with the sampled
mark, views over time with bots shaded, a stacked bar per bucket for
humans and each bot label, top lists, and the on/off switch for owners
and admins. The loader drops a range, traffic or host the API would
refuse before calling it; a hostname that is not one of the project's
hosts falls back to all hosts with a notice.

Charts are inline SVG with no new dependency: role="img" with a summary
label, a legend, a "Show numbers" table, and colors as --chart-1..6
tokens with dark-mode steps. findProject moves to lib/projects.ts so the
security and analytics pages share it, and both get project tabs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…le check

- Read `?done=` from the copy's own keys only, so `?done=constructor`
  shows no notice instead of failing the page.
- Name the chart's folded bot series "More bots", apart from the API's
  "Other bots" row in the list, and drop the "Other bots" group line from
  named bots in the `other` group.
- Use the shared `canChangeSettings` on the security page.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Reads GET /projects/:id/analytics and prints totals, a trend line, top
pages / referrers / countries (side by side when they fit), devices,
browsers and AI & bots. `on` and `off` call the settings route. Every
visitor-set string goes through cleanLine. Direct Cloudflare projects get
a clear message for now; analytics.ts holds that branch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…e older double count

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…or direct deploys

A direct Cloudflare deploy now reads page loads for its custom domains and
zone routes from the user's own zone (httpRequestsAdaptiveGroups over
GraphQL, one request per zone). It only reads: no binding, deploy step or
entitlement probe, so Workers Free apps keep working.

- OAuth logins need analytics.read; API tokens get a permission hint on
  authz; a Global API Key is refused by name.
- 24h reads hours, 7d folds hours into 6h buckets, 30d reads days; 90d is
  refused (Cloudflare keeps 31 days).
- Bots are split with the shared user-agent parser plus Cloudflare's
  verified bot category.
- email-cloudflare exports zoneCandidates and the /zones?name= read without
  changing email behaviour.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…deploys

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…e --traffic wording

Zone analytics filter by host, so a route such as example.com/blog/* would
count every page on that host. Such a route is now skipped with a note
unless a custom domain or a whole-host route covers the host. A custom
domain set as the singular `route` is now read. The help text and docs now
say that only the human count leaves out bots found by user agent, that
`--traffic bots` lists only bots Cloudflare verifies, and that on/off stop
with an error.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant