@miisite/analytics
v0.2.2
Published
Cookieless page views and Core Web Vitals for miisite client sites.
Maintainers
Readme
@miisite/analytics
Cookieless page views and Core Web Vitals for miisite client sites.
Sends a path — or a metric name and a number — to an endpoint. Nothing else. No cookie, no localStorage, no sessionStorage, no fingerprint, no visitor identifier. Nothing is stored on the visitor's device, which is why the sites that use it carry no consent banner: the ePrivacy consent requirement is triggered by storing or accessing information on terminal equipment, and this does neither.
That is a constraint on what may be added here, not a description of what is here today. A change that stores anything on the device silently makes every site using this package non-compliant.
Use
// src/routes/+layout.svelte, in the <script>
import { afterNavigate } from '$app/navigation';
import { trackPageviews } from '@miisite/analytics/svelte';
trackPageviews({ afterNavigate });Must be called during component initialisation — afterNavigate registers a
lifecycle callback and throws outside init.
Why you pass afterNavigate in. $app/navigation is a virtual module that
exists only inside your SvelteKit build. A published package that imports it
emits that specifier into its dist, and Vite externalises node_modules for
SSR — so the prerender loads it in plain Node and fails with
Cannot find package '$app', on every route, at build time. The app owns the
import; the package owns the logic.
Without SvelteKit:
import { createTracker } from '@miisite/analytics';
const tracker = createTracker({ endpoint: '/_px' });
tracker.pageview(location.pathname);
tracker.vitals();What it reports, and what it deliberately does not
| | | | --- | --- | | Client-side navigations | Every one. This is the reason the package exists | | The first page view | No. See below | | LCP, INP, CLS | Once per document, when each settles |
The first page view is skipped on purpose. Behind the miisite router it is
recorded server-side from the HTML response, which also counts visitors with
JavaScript disabled or a blocker installed. Reporting it here as well would
double every session's first page. On other infrastructure, call
tracker.pageview(location.pathname) yourself at startup.
Why this exists
SvelteKit navigates by history.pushState without reloading, so after the first
page nothing server-side sees a page view. Cloudflare Web Analytics does not fill
that gap: its beacon is one-shot, so it reports the entry page and the exit page
and loses everything in between. That was measured, twice, on a live site —
including with the documented "spa": true set, which changes nothing.
Endpoint contract
GET {endpoint}?p={path} — one page view.
GET {endpoint}?p={path}&m={metric}&v={value}&r={rating} — one vital.
The receiver replies 204 with no body. sendBeacon ignores the response.
Everything else — host, country, device, bot classification — is filled in by the receiver from the request it already has. The client sends the minimum it alone knows.
Notes for whoever changes this
web-vitalsis imported dynamically and only in a browser. It touchesdocumentat module scope, so a static import breaks server rendering — on a prerendered SvelteKit site that is every route, at build time.- Vitals register once per document, never per navigation. Each metric reports a single time for the life of the document; re-registering on a route change either double-reports or is silently ignored, depending on the metric.
- Never import a virtual module here.
$app/*,$env/*and friends only resolve inside a consumer's bundler. See the note at the top ofsvelte.ts. - Failure is always silent. A blocked beacon, a missing API, a chunk that will not load — none of it is worth an exception on a page someone is reading.
