Plausible analytics from the API; version v1.12.0-accessibility

The API adds Plausible's script to every page it serves, counting
under the hostname of site.json's url, so each deployment reports as
itself (nginx proxies /pa/ to the self-hosted Plausible). One script,
no npm tracker: it follows the app's page changes on its own.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Zaldimmar 2026-10-03 03:03:07 -05:00
parent e324422f32
commit 420a8058b4
3 changed files with 19 additions and 1 deletions

View file

@ -445,6 +445,22 @@ const REPLACED = [
can end the element early. */
const scriptJson = (value) => JSON.stringify(value).replace(/</g, "\\u003c");
/* Plausible, self-hosted and reached through nginx at /pa/ (the
Privacy page describes it). Each deployment counts under its own
address, site.json's url, so dev and live stay apart. Its script
follows the app's page changes itself. */
const PLAUSIBLE = { script: "/pa/js/script.js", api: "/pa/api/event" };
function analyticsTag(site) {
let domain;
try {
domain = new URL(site.url).hostname;
} catch {
return null; // no url, nothing to count under
}
return `<script defer data-domain="${escapeHtml(domain)}" data-api="${PLAUSIBLE.api}" src="${PLAUSIBLE.script}"></script>`;
}
export function renderPage(html, meta) {
const site = meta.site;
let out = html;
@ -477,6 +493,7 @@ export function renderPage(html, meta) {
// script in the head runs during parsing; modules wait).
`<script>window.__SITE__ = ${scriptJson(forBrowser(site))}</script>`,
site.themeCss && `<style id="site-colors">${site.themeCss}</style>`,
analyticsTag(site),
].filter(Boolean);
// A blocked site (Site settings indexing off, or site.json's