Docs
BlogHomeStart building

Site Analytics (totalum-sdk/analytics)


import … from 'totalum-sdk/analytics' — or the same namespace on totalum from 'totalum-sdk'. Install, configuration and errors: Totalum SDK.

Your site's own traffic: page views, visits, unique visitors, errors, bytes and response times, as a time series or a top list (top pages, referrers, countries, devices, browsers). Use it for a stats page in your app's admin. Free.

ts
const { top } = await totalum.analytics.site({ dimension: 'path', metric: 'page_views', limit: 10 });
Method or typeWhat it does
totalumAnalyticsCreates the analytics namespace (the same object as totalum.analytics, scope analytics:read): the site's own traffic.
analytics.siteThe site's traffic: page views, hits, unique visitors, errors, bytes or response time (TTFB), as a time series (no dimension) or a top list by path, referrer, country, device, browser, OS, status or hostname.
SiteAnalyticsOutputThe answer of analytics.site().
SiteAnalyticsQueryWhat analytics.site() reads: the window, the metrics and, for a top list, the dimension; all optional.
SiteDimensionWhat a top list of analytics.site() groups by.
SiteMetricWhat analytics.site() counts.

#totalumAnalytics

ts
totalumAnalytics(options?: TotalumClientOptions): TotalumAnalytics

Creates the analytics namespace (the same object as totalum.analytics, scope analytics:read): the site's own traffic. Free.

Parameters

NameTypeRequiredDescription
optionsTotalumClientOptionsnoThe project key, API URL and environment; each falls back to its environment variable.
options.keystringnoProject key tlm_pk_…; default TOTALUM_PROJECT_KEY. Server-side only.
options.apiUrlstringnoThe Totalum API's URL: default TOTALUM_SDK_API_URL, else https://sdk-api.totalum.app.
options.env'dev' | 'live'noSent as x-totalum-env on every call (the database it targets, and dev vs live usage); default TOTALUM_ENV, else live.

Returns TotalumAnalytics — The analytics namespace: the method below.

Example

ts
import { totalumAnalytics } from 'totalum-sdk/analytics';

const analytics = totalumAnalytics();
const { series } = await analytics.site({ metric: 'page_views', granularity: 'day' });

The namespace's type is exported as TotalumAnalytics (import type { TotalumAnalytics } from 'totalum-sdk/analytics').

#analytics.site

ts
analytics.site(q?: SiteAnalyticsQuery): Promise<SiteAnalyticsOutput>

The site's traffic: page views, hits, unique visitors, errors, bytes or response time (TTFB), as a time series (no dimension) or a top list by path, referrer, country, device, browser, OS, status or hostname. Counts come from sampled hits, so very small numbers are approximate. Free.

Parameters

NameTypeRequiredDescription
qSiteAnalyticsQuerynoThe window, metrics and dimension; all optional.
q.fromstringnoThe window's start (ISO date-time); default 7 days ago. Raw hits are kept 90 days. Allowed: ISO 8601 date-time.
q.tostringnoThe window's end (ISO date-time); default now. Allowed: ISO 8601 date-time.
q.granularity'day' | 'minute' | 'hour'noThe series' step: minute (only within the last 30 minutes), hour or day (default).
q.metric'uniques' | 'bytes' | 'errors' | 'page_views' | 'hits' | 'ttfb_p50' | 'ttfb_p95' | SiteMetric[]noWhat to count: one metric or up to 4. Default page_views.
q.dimension'browser' | 'country' | 'status' | 'device' | 'path' | 'hostname' | 'os' | 'referrer'noOmit for a time series (series); set for a top list by this dimension (top).
q.limitnumbernoWith dimension: how many entries, at most 100. Default 10. Allowed: ≥ 1, ≤ 100.
q.bots'exclude' | 'include' | 'only'noBot traffic: exclude (default), include or only.
q.env'dev' | 'live'nodev = the preview host, live = the published and custom hosts; omitted = both.

Returns Promise<SiteAnalyticsOutput> — series (a time series) or top (a top list), the window used and when the numbers were computed.

FieldTypeAlways presentDescription
window{ from, to, granularity }yesThe window used.
window.fromstringyesIts start.
window.tostringyesIts end.
window.granularity'day' | 'minute' | 'hour'yesIts step.
source'wae'yesAlways wae.
approximate{ uniques }yesWhich numbers are estimates.
approximate.uniquesbooleanyestrue when uniques is estimated.
cachedAtstringyesWhen the numbers were computed (cached for a short time).
series({ })[]noWithout dimension: one entry per step, { t, <metric>: value, … }.
top({ })[]noWith dimension: the top entries, { key, share, <metric>: value, … }.

Errors — besides the errors every call can throw:

CodeHTTPWhen
ANALYTICS_INVALID_QUERY400from after to, minute over more than 30 minutes, or a bad metric.
ANALYTICS_WINDOW_OUT_OF_RETENTION400The window starts more than 90 days ago.
ANALYTICS_UPSTREAM_ERROR502Analytics are unavailable; retry shortly.

Examples

ts
const { series } = await totalum.analytics.site({ metric: ['page_views', 'uniques'], granularity: 'day' });

Result:

json
{
  "series": [
    { "t": "2026-10-09T00:00:00.000Z", "page_views": 412, "uniques": 128 },
    { "t": "2026-10-10T00:00:00.000Z", "page_views": 538, "uniques": 171 }
  ],
  "window": { "from": "2026-10-04T17:39:00.000Z", "to": "2026-10-11T17:39:00.000Z", "granularity": "day" },
  "source": "wae",
  "approximate": { "uniques": true },
  "cachedAt": "2026-10-11T17:38:40.720Z"
}
ts
const { top } = await totalum.analytics.site({ dimension: 'path', metric: 'page_views', limit: 3 });

Result:

json
{
  "top": [
    { "key": "/", "share": 0.41, "page_views": 1210 },
    { "key": "/pricing", "share": 0.18, "page_views": 532 },
    { "key": "/blog/launch", "share": 0.07, "page_views": 207 }
  ],
  "window": { "from": "2026-10-04T17:39:00.000Z", "to": "2026-10-11T17:39:00.000Z", "granularity": "day" },
  "source": "wae",
  "approximate": { "uniques": false },
  "cachedAt": "2026-10-11T17:38:41.388Z"
}

#SiteAnalyticsOutput

The answer of analytics.site().

FieldTypeAlways presentDescription
window{ from, to, granularity }yesThe window used.
window.fromstringyesIts start.
window.tostringyesIts end.
window.granularity'day' | 'minute' | 'hour'yesIts step.
source'wae'yesAlways wae.
approximate{ uniques }yesWhich numbers are estimates.
approximate.uniquesbooleanyestrue when uniques is estimated.
cachedAtstringyesWhen the numbers were computed (cached for a short time).
series({ })[]noWithout dimension: one entry per step, { t, <metric>: value, … }.
top({ })[]noWith dimension: the top entries, { key, share, <metric>: value, … }.

#SiteAnalyticsQuery

What analytics.site() reads: the window, the metrics and, for a top list, the dimension; all optional.

NameTypeRequiredDescription
fromstringnoThe window's start (ISO date-time); default 7 days ago. Raw hits are kept 90 days. Allowed: ISO 8601 date-time.
tostringnoThe window's end (ISO date-time); default now. Allowed: ISO 8601 date-time.
granularity'day' | 'minute' | 'hour'noThe series' step: minute (only within the last 30 minutes), hour or day (default).
metric'uniques' | 'bytes' | 'errors' | 'page_views' | 'hits' | 'ttfb_p50' | 'ttfb_p95' | SiteMetric[]noWhat to count: one metric or up to 4. Default page_views.
dimension'browser' | 'country' | 'status' | 'device' | 'path' | 'hostname' | 'os' | 'referrer'noOmit for a time series (series); set for a top list by this dimension (top).
limitnumbernoWith dimension: how many entries, at most 100. Default 10. Allowed: ≥ 1, ≤ 100.
bots'exclude' | 'include' | 'only'noBot traffic: exclude (default), include or only.
env'dev' | 'live'nodev = the preview host, live = the published and custom hosts; omitted = both.

#SiteDimension

What a top list of analytics.site() groups by.

ValueMeaning
'browser'The browser.
'country'The visitor's country (two-letter code).
'status'The HTTP status.
'device'desktop, mobile or tablet.
'path'The page path.
'hostname'The host (the published domain, a custom domain, the preview).
'os'The operating system.
'referrer'Where visitors came from.

#SiteMetric

What analytics.site() counts.

ValueMeaning
'uniques'Unique visitors (approximate).
'bytes'Bytes sent.
'errors'Requests answered with a 4xx or 5xx status.
'page_views'Page views (HTML pages only).
'hits'Every request (pages, assets, API).
'ttfb_p50'Median time to first byte, in ms.
'ttfb_p95'95th-percentile time to first byte, in ms.