# Site Metrics ingestion API

Privacy-first analytics for your own sites and apps: pageviews, events, web vitals, sessions, funnels,
revenue and business gauges. Your browser or server sends data to TaskNegar. TaskNegar never needs access to your
database or servers.

Base URL is `https://<tasknegar-host>/api`. All bodies are JSON (`Content-Type: application/json`), except where noted.

## Keys

Get both keys from the site's setup wizard in project settings:

| Key | Where it lives | Can do | Origin check |
|---|---|---|---|
| **browser key** (`key`) | Public, in your page's JavaScript | `POST /metrics/ingest` with events (no `ts`) | Yes: the browser's `Origin` must be in the site's allowed origins |
| **server key** (`server_key`) | Secret, only on your servers | Everything the browser key does, plus `ts` (backdating) and `POST /metrics/gauge` | No |

Send a key as `Authorization: Bearer <key>`, or in the JSON body as `"key": "<key>"` (browsers cannot always set
headers). Keys issued before server keys existed are browser keys.

Wrong or missing key: `401`. A browser key on a server-only route, or with `ts`: `403` with `"code": "SERVER_KEY_REQUIRED"`.

## Error format

```json
{ "error": "monthly event limit reached (1000000 events per month)", "code": "QUOTA_EVENTS" }
```

`code` is present when the client should branch on it. Known codes:

| Status | code | Meaning |
|---|---|---|
| 400 | - | Validation: the message says which field |
| 401 | - | Missing or unknown key |
| 403 | `SERVER_KEY_REQUIRED` | Browser key on a server-only call |
| 403 | `FEATURE_DISABLED` | The `site_metrics` tool is off for the workspace or project (`feature` names it) |
| 403 | - | Browser origin not allowed |
| 413 | - | Body over 32 KB |
| 415 | - | Not `application/json` (checked before the key) |
| 429 | `RATE_LIMIT` | More than 600 events per minute for the site |
| 429 | `QUOTA_EVENTS` | Monthly event quota of the workspace is used up |
| 429 | `QUOTA_GAUGES` | More gauge names than `gauges_per_site` for the site |

A `204` answer to ingest means the visitor sent `DNT: 1`. Nothing is stored, whatever the key.

## Ingest: events

`POST /api/metrics/ingest`

```json
{
  "key": "<browser key, optional when the header is set>",
  "user": "customer-user-id-123",
  "sid": "random-per-tab-id",
  "events": [
    { "name": "pageview", "path": "/pricing", "screen": 390,
      "referrer": "https://news.example/story?x=1", "utm_source": "newsletter" },
    { "name": "purchase", "path": "/checkout", "value": 49.5, "currency": "usd" },
    { "name": "vital", "props": { "metric": "LCP", "value": 1830 } },
    { "name": "error", "props": { "message": "TypeError: x is undefined" } }
  ]
}
```

Response `202`: `{ "accepted": 4 }`. Invalid single events are skipped and not counted. A bad `ts` or a `ts` from a
browser key refuses the whole request.

Request fields:

| Field | Limit / rule |
|---|---|
| `events` | List of up to 50 events |
| `user` / `sid` | Request level, used by events that do not carry their own. Hashed; the raw value is never stored |
| `key` | Browser key in the body (only when no `Authorization` header is sent) |

Event fields:

| Field | Rule |
|---|---|
| `name` | Required, 1 to 64 characters |
| `path` | Path only; query and fragment are dropped. Blank becomes `/`. At most 300 characters |
| `value` | Finite number, default `1`. For `vital` the metric value is copied from `props.value` |
| `props` | At most 10 keys (the rest are dropped), key at most 40 characters. Values: text (200 characters) or number. `referrer` is kept as the host only. Text values are PII-masked, see below |
| `user` | Per event, wins over the request-level `user`. At most 100 characters; longer ids are dropped |
| `sid` | Per event, wins over the request-level `sid`. At most 40 characters |
| `screen` | Screen width in pixels. Buckets to `mobile` (< 768), `tablet` (< 1024), `desktop` |
| `referrer` | URL, stored as the host only |
| `utm_source`, `utm_medium`, `utm_campaign`, `utm_term`, `utm_content` | Text, 100 characters each |
| `currency` | ISO 4217 code, three letters (`usd` is stored as `USD`). Anything else is dropped |
| `revenue` | `true` marks the event as revenue even if its name is not on the revenue list |
| `ts` | **Server key only.** Backdated time, see [Backdating](#backdating-server-key) |

Revenue: an event counts as revenue when `revenue` is `true` or its name is one of `purchase`, `order`, `payment`,
`subscription`.

Errors: `400` (`events must be a list of at most 50`, bad `ts`), `401`, `403`, `413`, `415`, `429`.

Content type: `application/json` and `text/plain` (a JSON body) are accepted. Other types get `415`.

CORS (ingest only): a browser on another site can call it. `OPTIONS /api/metrics/ingest` answers `204` with
`Access-Control-Allow-Origin` set to the request's `Origin`, `Access-Control-Allow-Methods: POST, OPTIONS`,
`Access-Control-Allow-Headers: Content-Type, Authorization` and `Access-Control-Max-Age: 86400`. The preflight has no
key, so it is not origin-checked. The `POST` is: a wrong origin still gets `403`. Every `POST` answer (202, 204, 400,
401, 403, 429) carries `Access-Control-Allow-Origin` (the request's `Origin`) and `Vary: Origin` when an `Origin`
header is sent, so the browser can read errors. No credentials are allowed. Gauges and other endpoints send no CORS
headers.

## Ingest: gauges

A gauge is a number your server reports, such as `active_subscribers` or `mrr`. One value per name and UTC
day. Sending the same name and day again replaces the value (last write wins).

`POST /api/metrics/gauge` (**server key only**)

Single gauge:

```json
{ "name": "active_subscribers", "value": 1284 }
```

Batch (1 to 50 items; a later item for the same name and day wins):

```json
{ "gauges": [
    { "name": "active_subscribers", "value": 1284 },
    { "name": "mrr", "value": 10250.5, "ts": "2026-11-28T00:00:00Z" }
] }
```

Response `202`: `{ "accepted": 2 }` (number of distinct name and day pairs stored).

Rules: name 1 to 64 characters of `A-Z a-z 0-9 _ . : -`; value a finite number (not a string, not a boolean);
`ts` optional, see [Backdating](#backdating-server-key). A browser key gets `403 SERVER_KEY_REQUIRED`.
Over `gauges_per_site` distinct names: `429 QUOTA_GAUGES`.

## Backdating (server key)

`ts` on an event or a gauge sets its time. Accepted forms: epoch seconds (`1795176000`) or ISO 8601
(`2026-11-20T09:30:00Z`; without a zone it is UTC).

- Not older than **90 days**, not more than **5 minutes** in the future. Otherwise `400`.
- The event lands on its own UTC day. Its visitor hash uses the salt of that day, so the same visitor on that day
  counts once.
- Backdated days are rebuilt by the nightly `rollup_and_prune` task. Until then the summary and the other growth
  reads do not show the backdated rows (the current day is always live).
- Backdated events count toward the monthly quota when they arrive, not when they happened.
- Raw events older than the workspace's `retention_days` are deleted, so a backdated event older than that is
  deleted by the next nightly run.
