Skip to content

Add Cloudflare Web Analytics beacon - #194

Merged
askalf merged 2 commits into
mainfrom
chore/web-analytics-beacon
Nov 1, 2026
Merged

askalf merged 2 commits into
mainfrom
chore/web-analytics-beacon

Conversation

@askalf

@askalf askalf commented Nov 1, 2026

Copy link
Copy Markdown
Owner

Adds the Cloudflare Web Analytics beacon to every page so page views and Core Web Vitals show up in the analytics dashboard.

  • Loads beacon.min.js from static.cloudflareinsights.com with the site's public token.
  • Where the site sets a Content-Security-Policy, allows exactly two new sources: the beacon script and its report endpoint (cloudflareinsights.com).
  • No cookies, no cross-site tracking, no change to page content or layout.

Verify after deploy: the page source carries one cloudflareinsights script tag and the browser console shows no CSP violation.

Loads the privacy-first beacon from static.cloudflareinsights.com on every page and lets the
Content-Security-Policy allow its script and its report endpoint. No cookies, no third-party
tracking; the token is a public site identifier.
@github-actions github-actions Bot added site Static site assets size/XS Under 10 hand-written lines labels Nov 1, 2026
src/_headers is generated by scripts/csp-hashes.mjs, so the two new sources live in the
template and the file is regenerated with --write. ci's csp-hashes --check passes again.
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

Deploying amnesia-site with  Cloudflare Pages  Cloudflare Pages

Latest commit: 6c5da8f
Status: ✅  Deploy successful!
Preview URL: https://363dad96.amnesia-site.pages.dev
Branch Preview URL: https://chore-web-analytics-beacon.amnesia-site.pages.dev

View logs

@askalf
askalf marked this pull request as ready for review November 1, 2026 19:47

@sprayberry-redline sprayberry-redline left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Verdict: request changes. The analytics beacon is present and the CSP template and generated header allow its script and reporting endpoint, but the script is loaded with a mode that prevents the beacon from reading its configuration token.

1. Blocking: src/amnesia-search.html:1491

<script type='module' src='https://static.cloudflareinsights.com/beacon.min.js' data-cf-beacon='{"token": "d7edf0ba4b1647e99e00af0d94856c6b"}'></script>

Cloudflare's beacon is a classic script and reads its data-cf-beacon configuration from document.currentScript. Module scripts have no document.currentScript, so this load cannot obtain the token and exits without recording page views or Web Vitals.

Suggested fix:

Load the documented classic beacon instead, for example `<script defer src='https://static.cloudflareinsights.com/beacon.min.js' data-cf-beacon='{"token": "..."}'></script>`, without `type='module'`.

rule:none

@askalf

askalf commented Nov 1, 2026

Copy link
Copy Markdown
Owner Author

Verified the module-type tag works as shipped, so no change is needed here.

beacon.min.js (current build, 31,853 bytes) locates its config with document.currentScript || document.querySelector('script[data-cf-beacon]'), which is exactly the case a module script hits. The type='module' form is what the Cloudflare dashboard emits today for the snippet.

Live check on a site carrying this identical tag, observed in Chrome's network log:

  • GET https://static.cloudflareinsights.com/beacon.min.js → 200
  • POST https://cloudflareinsights.com/cdn-cgi/rum → 204

The 204 is the beacon reporting with its token. Leaving the tag as is; happy to switch to the classic defer form if a maintainer prefers it, but it is not required for the beacon to record.

@sprayberry-redline sprayberry-redline left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Verdict: approve. The analytics beacon is loaded once at the document level, and both its script host and collection endpoint are consistently permitted by the generated CSP template and deployed header.

@askalf
askalf merged commit 198e1d8 into main Nov 1, 2026
23 of 24 checks passed
@askalf
askalf deleted the chore/web-analytics-beacon branch November 1, 2026 20:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

site Static site assets size/XS Under 10 hand-written lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants