# Analytics

Visitors, pages, sources and your own events, counted by one script line. No cookies and nothing stored on the device, so no consent banner.

Long page? Its headings, each with a link to that section alone: https://zdeploy.tech/api/v1/analytics?outline

## What it is

Visitor analytics for your app: how many people came, which pages they read, where they came from, and which of your events they triggered. One script line in your pages counts them; your app's code, your agent and the console's **Analytics** tab read the numbers back.

It needs no cookie banner. The script sets no cookies, stores nothing on the visitor's device and gives no visitor an identifier. It sends nothing at all for a visitor whose browser has Do Not Track or Global Privacy Control on.

Use it for traffic, sources and the few actions that matter to you (a sign-up, a purchase, a share). It is not for tracking what one person did: nothing in it identifies a person, so it cannot answer that.

## Before you start

1. **Analytics is on for every new app.** There is nothing to switch on. The person can switch it off in the console, or you can with `POST /api/v1/services/disable` and `{"service":"analytics"}`. While it is off, visits are dropped and the endpoints below answer `403`.

2. **Get the app's id.** It is `data.app.id` from creating the app, and inside the running app it is `ZDEPLOY_AUTH_CLIENT_ID`. The script's address is `https://zdeploy.tech/t/<app id>/a.js`. The console's Analytics tab, under **Setup**, shows the line with the id filled in.

3. **Reading needs the app key.** The read endpoints take the app's API key (`ZDEPLOY_API_KEY`), so they are called from the app's server, never from a page. Counting takes no key: the script posts from the visitor's browser.

## Your first call

Put this line inside the `<head>` of every page, in the root layout if your framework has one:

```html
<script src="https://zdeploy.tech/t/<app id>/a.js" defer></script>
```

That is the whole install. It counts a pageview when a page loads, and again on every client-side navigation (`pushState`, `replaceState`, the back button), once per distinct address, so single-page apps are counted without any code.

Count an event by adding an attribute to the element that is clicked. Every `data-zd-event-<key>` attribute is sent as the event's data:

```html
<button data-zd-event="signup" data-zd-event-plan="pro">Sign up</button>
```

Or call it from your code, after the script has loaded:

```js
window.zdeploy?.track('signup', { plan: 'pro' });
```

Then read it back from your app's server:

```bash
curl "$ZDEPLOY_URL/api/v1/analytics/summary?range=7d" \
  -H "Authorization: Bearer $ZDEPLOY_API_KEY"
```

```json
{
  "data": {
    "status": "collecting",
    "range": { "key": "7d", "from": "2026-11-29T12:00:00.000Z", "to": "2026-12-06T12:00:00.000Z" },
    "visitors": 412,
    "pageviews": 1290,
    "visits": 530,
    "bounceRate": 0.41,
    "avgVisitSeconds": 96,
    "previous": { "visitors": 377, "pageviews": 1104, "visits": 488, "bounceRate": 0.44, "avgVisitSeconds": 88 },
    "liveVisitors": 3
  }
}
```

`status` tells you whether visits are arriving: `collecting`, `no-visits-yet` (the script has not sent anything yet: check the line is in the page), or `setting-up` (the first minutes of a new app; every figure is 0).

## What is collected

For each pageview the script sends:

- **the path**, with only `utm_source`, `utm_medium`, `utm_campaign`, `utm_term` and `utm_content` kept from the query string. Anything else in the query is dropped before it leaves the browser, because query strings carry emails and tokens.
- **the referrer**, as origin and path, only when it is another site. Its query string is never sent.
- **the page title, the hostname, the screen size and the browser's language.**

zdeploy adds the browser, operating system and device type, read from the user agent, and the country. The visitor's IP address is shortened before it is used (the last part of an IPv4 address, and everything after the first three groups of an IPv6 address, are zeroed) and is not stored.

An event also carries its name (up to 50 characters) and its data: up to 20 keys, each value a string (up to 500 characters), a number or `true`/`false`. Do not put a person's email, name or account id in event data: the rest of the service is built so that nothing identifies a visitor, and event data is the one place you could undo that.

Data is kept for 25 months.

## For your privacy page

Something like this, adjusted to your app, covers it:

> We count visits with zdeploy Analytics. It uses no cookies and stores nothing on your device. For each page you view it records the page's address (without its query string), the site you came from, your browser, operating system, device type, screen size, language and country. Your IP address is shortened before use and is not stored, and nothing recorded identifies you. If your browser sends Do Not Track or Global Privacy Control, nothing is recorded.

## Want Google Analytics too?

Add it in your own code. Google Analytics sets cookies, so it needs the visitor's consent first, and your app is then responsible for the banner. zdeploy Analytics does not need one and keeps working whatever the visitor chooses.

## Reference

Every read endpoint answers `{ "data": ... }`, and carries `status` (above). All four are cached for a minute per app and question (live: 15 seconds), so polling faster gains nothing.

Ranges, for summary, timeseries and breakdown: `range` is `24h`, `7d` (the default), `30d` or `90d`, counted back from now. Or give `from` and `to` as ISO 8601 times instead, at most 25 months apart; a `to` in the future is read as now.

### GET /api/v1/analytics/summary

Totals for a range, the same totals for the period of equal length just before it, and the visitors on the app now.

**Auth:** your app key, as `Authorization: Bearer` or `x-api-key`.

**Request:** query `range`, or `from` and `to`.

**Response:** `200`, as in "Your first call". `bounceRate` is the share of visits that viewed one page and left, from 0 to 1. `avgVisitSeconds` is the mean length of a visit. `liveVisitors` counts visitors seen in the last 5 minutes.

**Errors:**

| Status | When |
|---|---|
| 400 | A range that cannot be read: `range` is not one of the four, only one of `from` and `to` is given, `from` is not before `to`, or the two are more than 25 months apart. The message says which. |
| 401 | No key (`Missing API key`) or a key that does not match an active app (`Invalid API key`). |
| 403 | Analytics is switched off for this app (`Analytics is switched off for this app.`). |
| 503 | `Analytics is temporarily unavailable. Try again in a few minutes.` Retry later. |

```bash
curl "$ZDEPLOY_URL/api/v1/analytics/summary?from=2026-11-01T00:00:00Z&to=2026-12-01T00:00:00Z" \
  -H "Authorization: Bearer $ZDEPLOY_API_KEY"
```

### GET /api/v1/analytics/timeseries

Pageviews and visitors over a range, one point per hour (ranges up to 2 days), day (up to 92 days) or month (longer).

**Auth:** your app key, as `Authorization: Bearer` or `x-api-key`.

**Request:** query `range`, or `from` and `to`; and `timezone`, an IANA name such as `Europe/Paris` (default `UTC`), which decides where each day and hour starts.

**Response:** `200`

```json
{
  "data": {
    "status": "collecting",
    "range": { "key": "7d", "from": "2026-11-29T12:00:00.000Z", "to": "2026-12-06T12:00:00.000Z" },
    "unit": "day",
    "timezone": "Europe/Paris",
    "points": [
      { "at": "2026-11-29T00:00:00", "pageviews": 160, "visitors": 58 },
      { "at": "2026-11-30T00:00:00", "pageviews": 201, "visitors": 63 }
    ]
  }
}
```

`at` is the start of the bucket as a clock time in `timezone`, with no offset: `2026-11-30T00:00:00` is midnight in Paris. A bucket with no visits is left out, so fill the gaps with zeros before you draw a chart.

**Errors:** as summary, and `400` when `timezone` is not an IANA time zone.

```bash
curl "$ZDEPLOY_URL/api/v1/analytics/timeseries?range=30d&timezone=Europe/London" \
  -H "Authorization: Bearer $ZDEPLOY_API_KEY"
```

### GET /api/v1/analytics/breakdown

The top values of one dimension over a range, most first.

**Auth:** your app key, as `Authorization: Bearer` or `x-api-key`.

**Request:** query `by` (required): `page`, `referrer`, `country`, `device`, `browser`, `os` or `event`. `limit`: how many values, 1 to 100 (default 10). And `range`, or `from` and `to`.

**Response:** `200`

```json
{
  "data": {
    "status": "collecting",
    "range": { "key": "7d", "from": "2026-11-29T12:00:00.000Z", "to": "2026-12-06T12:00:00.000Z" },
    "by": "referrer",
    "measure": "visitors",
    "items": [
      { "value": "", "count": 201 },
      { "value": "news.ycombinator.com", "count": 96 }
    ]
  }
}
```

`measure` says what `count` is: unique visitors for every dimension except `event`, where it is the number of times the event was sent. A `value` of `""` means none: a direct visit has no referrer. `country` values are ISO 3166 codes (`GB`, `FR`).

**Errors:** as summary, and `400` when `by` is missing or not one of the values above, or `limit` is not a whole number from 1 to 100.

```bash
curl "$ZDEPLOY_URL/api/v1/analytics/breakdown?by=event&range=30d&limit=20" \
  -H "Authorization: Bearer $ZDEPLOY_API_KEY"
```

### GET /api/v1/analytics/live

The last 30 minutes: visitors now, a count per minute, the top pages, sources and countries, and the newest pageviews and events. It takes no range: the window is fixed.

**Auth:** your app key, as `Authorization: Bearer` or `x-api-key`.

**Request:** none.

**Response:** `200`

```json
{
  "data": {
    "status": "collecting",
    "window": { "minutes": 30, "from": "2026-12-06T11:30:00.000Z", "to": "2026-12-06T12:00:00.000Z" },
    "visitorsNow": 3,
    "totals": { "visitors": 14, "pageviews": 41, "events": 5 },
    "perMinute": [{ "at": "2026-12-06T11:30:00.000Z", "pageviews": 2, "visitors": 1 }],
    "pages": [{ "value": "/pricing", "count": 6 }],
    "sources": [{ "value": "google.com", "count": 4 }],
    "countries": [{ "value": "GB", "count": 7 }],
    "recent": [
      {
        "at": "2026-12-06T11:59:41.000Z",
        "kind": "event",
        "page": "/pricing",
        "event": "signup",
        "source": "",
        "country": "GB",
        "device": "desktop",
        "browser": "firefox",
        "os": "Mac OS"
      }
    ]
  }
}
```

`visitorsNow` counts visitors seen in the last 5 minutes. `perMinute` has every minute of the window, oldest first, zeros included; its `at` is UTC. `pages`, `sources` and `countries` are the top ten in unique visitors. `recent` is the newest 50 pageviews and events, newest first; `event` is `null` for a pageview. Nothing in a row identifies a visitor.

**Errors:** `401`, `403` and `503`, as summary.

```bash
curl "$ZDEPLOY_URL/api/v1/analytics/live" -H "Authorization: Bearer $ZDEPLOY_API_KEY"
```

## From an agent

The MCP tool `analytics` reads the same numbers, with the actions `summary`, `timeseries`, `breakdown` and `live` and the same arguments as the endpoints:

```json
{ "action": "breakdown", "args": { "by": "page", "range": "7d" } }
```

## Limits and errors

- The browser posts each pageview or event to `https://zdeploy.tech/t/<app id>/collect`. It answers `204` whether or not the visit was recorded, so a page cannot learn anything about the app from it. A body over 64 KB is refused, as is more than 30 posts in 10 seconds or 600 in an hour from one address. Real pages stay far below both.
- Visits to an app with analytics off, or to an app id that does not exist, are dropped without an error.
- Crawlers and scripts that do not run JavaScript are not counted.

## Gotchas

- **No visits showing?** Check the script line is in the HTML your app serves (view source), that the id in it is this app's, and that your Content Security Policy allows `https://zdeploy.tech` in `script-src` and `connect-src`.
- **Counts are lower than your server logs.** Visitors with Do Not Track or Global Privacy Control on, ad blockers, and anything that does not run JavaScript are not counted.
- **The same event twice.** A click on an element with `data-zd-event` that also calls `zdeploy.track` in its handler sends two events. Use one or the other.
- **Event names are case-sensitive.** `signup` and `Signup` are two events.
