@tinyacre/telemetry
v0.4.0
Published
Tinyacre's first-party analytics and front-end error reporting
Readme
@tinyacre/telemetry
Front-end analytics (Umami) and error reporting (GlitchTip, through
@sentry/browser) for Tinyacre's apps. What it may send is fixed by
docs/data-boundary.md; ADR-0001
records why.
What the package guarantees:
- Pageviews and error events carry the route template your app maps the location to, never the concrete URL, query or hash.
- Every pageview carries
displayas Umami'stag:standalonefor an installed PWA, elsebrowser. Notitleorreferreris sent. - Custom events are names from your app's event list, with no properties. A name outside the list does not compile.
- Error events are rebuilt from a whitelist: exception type, stack trace,
route template (
transaction),release, and navigation breadcrumbs with templated paths. The exception message, request data, user, tags, extra and contexts are left behind; events without an exception type (such ascaptureMessage) are not sent. - The switch: off (cached choice) means neither SDK is imported. Turned off mid-session, the Sentry client is closed and Umami stops.
- It knows nothing about accounts. Your app stores the user's "help improve"
preference on the account and calls
setEnabledwith it. - Held: while the account's choice is unknown, nothing is sent and nothing is kept to send later.
Install
The package is public on npm.
pnpm add @tinyacre/telemetry # or: bun add / npm installReverse proxy
The browser only talks to your app's own origin. Forward two paths (the prefixes are your choice; they go into the config below):
/_s/to Umami, prefix stripped, so the browser posts to/_s/api/send./_e/to GlitchTip, prefix stripped and query string kept, so the browser posts envelopes to/_e/api/<project-id>/envelope/. The body istext/plain, and the DSN's public key arrives in theX-Sentry-Authheader, so the proxy must pass that header on. Keepsentry_keyout of the URL: ad blockers drop requests that carry it.
Both need the real client IP in X-Forwarded-For (Umami's visitor hash) and
GlitchTip should scrub IP addresses. Umami needs SALT_ROTATION=day.
Use
import { startTelemetry } from "@tinyacre/telemetry";
export const events = ["card_added", "card_removed"] as const;
const telemetry = await startTelemetry({
release: import.meta.env.VITE_RELEASE, // X.Y.Z, or the short sha of an edge build
events,
routeTemplate: ({ pathname }) => matchRoute(pathname)?.template ?? "/unknown",
umami: { endpoint: "/_s/api/send", websiteId: "<umami website id>", hostname: "<canonical host>" },
sentry: {
dsn: "https://<public-key>@<glitchtip host>/<project-id>",
tunnel: "/_e/api/<project-id>/envelope/",
},
});
router.afterEach(() => telemetry.pageview()); // reads `location`; or pass { pathname, search, hash }
telemetry.track("card_added");
telemetry.captureException(error); // for caught errors; uncaught ones are reported automatically
// On sign-in, before the account's choice has been read:
telemetry.hold();
// Once it is read, and whenever the user flips the account's "help improve" switch:
await telemetry.setEnabled(account.telemetryEnabled);startTelemetry reads the switch cached on the device (localStorage key
tinyacre.telemetry.enabled, default on) and imports the SDKs only when it
is on. setEnabled writes that cache; turning it back on in the same session
starts a fresh Sentry client. Browser privacy signals (GPC, DNT) are not
read: the switch alone decides.
hold() puts telemetry on hold until the next setEnabled. Until then, error
events (uncaught ones included) and breadcrumbs are dropped, pageviews and
custom events are dropped rather than queued, and the device cache is left as
it was. setEnabled(true) resumes with what happens from then on;
setEnabled(false) closes the Sentry client, stops Umami and caches off. An
app that knows at start-up that a signed-in session exists can pass
held: true to startTelemetry so the cache is not trusted: no SDK is
imported until setEnabled(true).
routeTemplate must map every location to a template (/cards/:id); a
query or hash it returns is cut off. Errors use the template of the last
pageview, so call pageview on the initial load as well.
umami.hostname is the app's canonical host; every event reports it,
whichever host served the page.
fetch and storage are optional overrides for the platform's fetch and
localStorage, used by the tests.
Develop
bun install
bun run typecheck # also checks the type-level tests in src/*.test-d.ts
bun run test
bun run buildRelease
Bump version in package.json, commit, then push a matching tag
(git tag v0.1.0 && git push origin v0.1.0). The Publish workflow checks
that the tag matches the version, then runs typecheck, tests and build and
publishes to npm through trusted publishing (GitHub OIDC, no token).
