# usage-forward

Posts what the runtime gathers of a program's usage
([docs/usage.md](../../docs/usage.md)) to an address of yours, for a team
that keeps its numbers in another tool. Thirty lines: read `forward.mote`,
and change it to say what your tool takes.

```toml
# mote.toml
[packages]
usage-forward = "../mote/extensions/usage-forward"
```

```text
app "Shop" usage=on
use "pkg:usage-forward/forward.mote"
```

```sh
USAGE_FORWARD_URL=https://collect.example/in USAGE_FORWARD_KEY=… USAGE_FORWARD_APP=shop bun dist/server/main.js
```

It can be taken with the usage extension or without it.

## What is posted

Every five seconds while there is anything, one `POST` with
`content-type: application/json` and, if `USAGE_FORWARD_KEY` is set,
`authorization: Bearer <key>`:

```json
{
  "app": "shop",
  "sent": "2026-11-20T09:14Z",
  "events": [
    {
      "name": "checkout",
      "kind": "track",
      "at": "2026-11-20T09:14Z",
      "who": "7c1e…",
      "signedIn": true,
      "session": "3f9c…",
      "route": "/cart",
      "ms": null,
      "ok": null,
      "failKind": null,
      "props": {"plan": "pro", "total": 40}
    }
  ]
}
```

`kind` is `screen`, `leave`, `action`, `failure`, `track`, `call`, `signin`,
`signup` or `job`. `who` is an account's id when `signedIn`, else a
visitor's random id, else null (a call nobody was signed in for). Times are
UTC, to the minute as `time.stamp` writes them; `e.at` in the source is the
moment in milliseconds if the collector wants that.

Anything but a 2xx answer, or no answer in ten seconds, is a line in the
back end's log and the batch is dropped.

## Another tool's own shape

Hosted analytics tools take events over HTTP, each in its own JSON: usually
one object an event with an event name, an id for the person, a time and a
record of properties, and the project's key in the body or a header.
`usageForwardEvent` in `forward.mote` is the one place that says the shape:
change the field names there to what the tool's capture or import API
documents (for PostHog, Mixpanel, Amplitude and the like: read their page
for the request; nothing here has been checked against them), put the key
where it wants it, and point `USAGE_FORWARD_URL` at its address. `name`
maps to the event's name, `who` to the person's id, `props` (with `route`,
`ms`, `ok` added if wanted) to its properties.
