@upmetrics/sdk
v0.11.0
Published
Upmetrics error-tracking SDK for browser/node/bun — Sentry-envelope compatible, PII-scrubbed.
Readme
@upmetrics/sdk
Error-tracking SDK for browser / node / bun. Sends Sentry-envelope-compatible events to an Upmetrics ingest endpoint, with PII scrubbing on by default.
import { init, captureException, captureMessage } from '@upmetrics/sdk';
init({
dsn: 'https://<publicKey>@upmetrics.org/<projectId>',
environment: 'production',
release: `${appName}@${appVersion}`,
});
// window.onerror, unhandledrejection and failed fetches are auto-instrumented.
// Manual capture:
captureException(new Error('boom'));
captureMessage('something happened', 'warning');PII scrubbing drops Authorization/Cookie/key headers and masks
email / Danish CPR / phone patterns before send. Disable with
init({ ..., disableScrub: true }) (not recommended).
Reading analytics back — fetchAnalytics (SERVER-SIDE ONLY)
Read a project's aggregated analytics to build your own dashboard. This carries
the project api key (uk_…) — a secret with full project access — so call it
only from your backend, never from browser code (the key would be visible in
the network tab). Your frontend talks to your backend; your backend calls this.
import { fetchAnalytics, type AnalyticsResponse } from '@upmetrics/sdk';
const data: AnalyticsResponse = await fetchAnalytics({
projectKey: process.env.UPMETRICS_UK!, // uk_… — server-side secret
days: 7, // 1..365, server clamps; default 30
// host defaults to https://upmetrics.org
});
console.log(data.totals.visitors, data.totals.pageviews);
// Breakdown lists are NamedCount[] — the label field is `value`, NOT `name`:
for (const page of data.topPages) {
console.log(page.value, page.count); // e.g. "/pricing", 42
}Notes:
- The label field is
valueon every breakdown entry (topPages,referrers,countries,devices,browsers,os,events) — importAnalyticsResponseand use it directly rather than re-declaring the type, so you don't accidentally read.name. seriescontains only days that had activity — there is no zero-fill. If you render a continuous line/bar chart, fill the empty days in yourself (iterate your date range and default missing days to{ visitors: 0, pageviews: 0 }).fetchAnalyticsdeliberately returns the data as-is and leaves this render decision to you.- Throws on a non-2xx response (e.g. a
401from a wrong/missing key), so a hidden auth failure surfaces instead of rendering an empty dashboard.
