# Site Analytics (totalum-sdk/analytics)

> 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.

`import … from 'totalum-sdk/analytics'`, or the same namespace on `totalum` from `'totalum-sdk'`. Install, configuration and errors: [Totalum SDK](https://www.totalum.app/docs/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 type | What it does |
|---|---|
| [`totalumAnalytics`](#totalumanalytics) | Creates the `analytics` namespace (the same object as `totalum.analytics`, scope `analytics:read`): the site's own traffic. |
| [`analytics.site`](#analyticssite) | 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. |
| [`SiteAnalyticsOutput`](#siteanalyticsoutput) | The answer of `analytics.site()`. |
| [`SiteAnalyticsQuery`](#siteanalyticsquery) | What `analytics.site()` reads: the window, the metrics and, for a top list, the dimension; all optional. |
| [`SiteDimension`](#sitedimension) | What a top list of `analytics.site()` groups by. |
| [`SiteMetric`](#sitemetric) | What `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**

| Name | Type | Required | Description |
|---|---|---|---|
| `options` | [`TotalumClientOptions`](https://www.totalum.app/docs/sdk/types#totalumclientoptions) | no | The project key, API URL and environment; each falls back to its environment variable. |
| `options.key` | `string` | no | Project key `tlm_pk_…`; default `TOTALUM_PROJECT_KEY`. Server-side only. |
| `options.apiUrl` | `string` | no | The Totalum API's URL: default `TOTALUM_SDK_API_URL`, else `https://sdk-api.totalum.app`. |
| `options.env` | `'dev' \| 'live'` | no | Sent as `x-totalum-env` on every call (the database it targets, and dev vs live usage); default `TOTALUM_ENV`, else `live`. |

**Returns** [`TotalumAnalytics`](#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**

| Name | Type | Required | Description |
|---|---|---|---|
| `q` | [`SiteAnalyticsQuery`](#siteanalyticsquery) | no | The window, metrics and dimension; all optional. |
| `q.from` | `string` | no | The window's start (ISO date-time); default 7 days ago. Raw hits are kept 90 days. Allowed: ISO 8601 date-time. |
| `q.to` | `string` | no | The window's end (ISO date-time); default now. Allowed: ISO 8601 date-time. |
| `q.granularity` | `'day' \| 'minute' \| 'hour'` | no | The 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[]` | no | What to count: one metric or up to 4. Default `page_views`. |
| `q.dimension` | `'browser' \| 'country' \| 'status' \| 'device' \| 'path' \| 'hostname' \| 'os' \| 'referrer'` | no | Omit for a time series (`series`); set for a top list by this dimension (`top`). |
| `q.limit` | `number` | no | With `dimension`: how many entries, at most 100. Default 10. Allowed: ≥ 1, ≤ 100. |
| `q.bots` | `'exclude' \| 'include' \| 'only'` | no | Bot traffic: `exclude` (default), `include` or `only`. |
| `q.env` | `'dev' \| 'live'` | no | `dev` = 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.

| Field | Type | Always present | Description |
|---|---|---|---|
| `window` | `{ from, to, granularity }` | yes | The window used. |
| `window.from` | `string` | yes | Its start. |
| `window.to` | `string` | yes | Its end. |
| `window.granularity` | `'day' \| 'minute' \| 'hour'` | yes | Its step. |
| `source` | `'wae'` | yes | Always `wae`. |
| `approximate` | `{ uniques }` | yes | Which numbers are estimates. |
| `approximate.uniques` | `boolean` | yes | `true` when `uniques` is estimated. |
| `cachedAt` | `string` | yes | When the numbers were computed (cached for a short time). |
| `series` | `({  })[]` | no | Without `dimension`: one entry per step, `{ t, <metric>: value, … }`. |
| `top` | `({  })[]` | no | With `dimension`: the top entries, `{ key, share, <metric>: value, … }`. |

**Errors** — besides the [errors every call can throw](https://www.totalum.app/docs/sdk#errors):

| Code | HTTP | When |
|---|---|---|
| `ANALYTICS_INVALID_QUERY` | 400 | `from` after `to`, `minute` over more than 30 minutes, or a bad metric. |
| `ANALYTICS_WINDOW_OUT_OF_RETENTION` | 400 | The window starts more than 90 days ago. |
| `ANALYTICS_UPSTREAM_ERROR` | 502 | Analytics 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()`.

| Field | Type | Always present | Description |
|---|---|---|---|
| `window` | `{ from, to, granularity }` | yes | The window used. |
| `window.from` | `string` | yes | Its start. |
| `window.to` | `string` | yes | Its end. |
| `window.granularity` | `'day' \| 'minute' \| 'hour'` | yes | Its step. |
| `source` | `'wae'` | yes | Always `wae`. |
| `approximate` | `{ uniques }` | yes | Which numbers are estimates. |
| `approximate.uniques` | `boolean` | yes | `true` when `uniques` is estimated. |
| `cachedAt` | `string` | yes | When the numbers were computed (cached for a short time). |
| `series` | `({  })[]` | no | Without `dimension`: one entry per step, `{ t, <metric>: value, … }`. |
| `top` | `({  })[]` | no | With `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.

| Name | Type | Required | Description |
|---|---|---|---|
| `from` | `string` | no | The window's start (ISO date-time); default 7 days ago. Raw hits are kept 90 days. Allowed: ISO 8601 date-time. |
| `to` | `string` | no | The window's end (ISO date-time); default now. Allowed: ISO 8601 date-time. |
| `granularity` | `'day' \| 'minute' \| 'hour'` | no | The 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[]` | no | What to count: one metric or up to 4. Default `page_views`. |
| `dimension` | `'browser' \| 'country' \| 'status' \| 'device' \| 'path' \| 'hostname' \| 'os' \| 'referrer'` | no | Omit for a time series (`series`); set for a top list by this dimension (`top`). |
| `limit` | `number` | no | With `dimension`: how many entries, at most 100. Default 10. Allowed: ≥ 1, ≤ 100. |
| `bots` | `'exclude' \| 'include' \| 'only'` | no | Bot traffic: `exclude` (default), `include` or `only`. |
| `env` | `'dev' \| 'live'` | no | `dev` = the preview host, `live` = the published and custom hosts; omitted = both. |

## `SiteDimension`

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

| Value | Meaning |
|---|---|
| `'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.

| Value | Meaning |
|---|---|
| `'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. |
