Live stream
Stream who is on a site right now, and every pageview and event as it lands, over Server-Sent Events.
The live stream sends the visitors on a site right now, then each pageview and event within a second of it landing. It is the feed behind the Realtime page, for a script, an office screen or a chat bot.
{id} is the site ID, such as ult_fxpitz4w7h, or the site's domain. The key needs the stats:read permission, and the organization needs Insight or Foresight.
Connect
The answer is a Server-Sent Events stream that stays open. Pass -N so curl prints each message as it arrives.
curl -N https://api.ultanimus.com/v1/sites/YOUR_SITE_ID/live \
-H "Authorization: Bearer $API_KEY"event: snapshot
data: {"now":1792936825000,"minute":1792936800000,"views":[14,11,9,...],"named":{...},"visitors":[...],"events":[...]}
event: events
data: [{"at":1792936826250,"visitor":"7m2c0p4rt1","name":"signup","page":"/pricing",...}]
: pingKeep the key on your server. A browser's EventSource cannot send an Authorization header, so a web page that shows the feed reads it through your server.
Messages
snapshotcomes first, once. It holds the site as it was when you connected.eventsfollows whenever something lands. It holds the pageviews and events of the last 250 ms, up to 200 in one message.- A line that starts with
:is a comment the server sends every 20 seconds, so proxies keep a quiet connection open. Skip it.
snapshot
{
"now": 1792936825000,
"minute": 1792936800000,
"views": [14, 11, 9, 12, 16, 13, 10, 8, 11, 15, 17, 12, 9, 10, 13, 14, 12, 11, 9, 8, 12, 15, 13, 10, 11, 14, 16, 12, 10, 4],
"named": {
"signup": [0, 1, 0, 0, 0, 2, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1]
},
"visitors": [
{ "id": "3k9x2f1q8z", "seen": 1792936821000, "page": "/pricing", "source": "google", "country": "GB", "city": "London", "device": "desktop", "browser": "chrome", "lat": 51.5, "lon": -0.1 }
],
"events": [
{ "at": 1792936821000, "visitor": "3k9x2f1q8z", "name": "pageview", "page": "/pricing", "source": "google", "country": "GB", "city": "London", "device": "desktop", "browser": "chrome", "lat": 51.5, "lon": -0.1, "landing": true }
]
}- Times are Unix milliseconds.
nowis the server's clock when it sent the snapshot, so you can age visitors even when your own clock is off. viewsholds the pageviews of each of the last 30 minutes, oldest first. The last number is the current minute, which started atminute.namedholds the same 30 counts for each custom event sent in that time, keyed by event name: the 20 names with the most events. Add each event fromeventsto its name's last number, and shift every list along withviewswhen a minute ends.visitorsare the visitors active in the last 5 minutes, up to 2,000, the most recent first.seenis the time of their last event, and the other fields describe that event.eventsare the 40 latest pageviews and events of the last 30 minutes, newest first.
events
[
{ "at": 1792936826250, "visitor": "7m2c0p4rt1", "name": "signup", "page": "/pricing", "source": "chatgpt", "country": "DE", "city": "Berlin", "device": "mobile", "browser": "safari", "lat": 52.5, "lon": 13.4 }
]nameispageviewor the name of a custom event, such assignup.visitorlinks the events of one visitor through the day. It changes at midnight UTC and says nothing about who the person is.sourceis where the visit came from, such asgoogleorchatgpt.countryis a two-letter code.latandlonplace the visitor at their city, rounded to about 11 km, and are left out when the place is unknown.landingistrueon the pageview that started a visit, and left out on every other event. In a snapshot it is a best guess, made from the 40 events the snapshot holds.targetis the link the visitor followed, as its host and path, such asgithub.com/acme/app. Onlyoutbound_linkandfile_downloadevents carry it. The stream sends no other property of an event.
Keep a live view
To show the same numbers as the Realtime page:
- Load
snapshotinto your state. After a reconnect, replace your state with the new one. - Add each event in
eventsto the feed, and set its visitor'sseento itsat. Count a pageview in the minute it landed. - Drop a visitor once their
seenis 5 minutes old.
An event can arrive twice around a connect, once in snapshot and once in events. Skip an event when you already hold one with the same visitor, at and name.
When the stream ends
The server ends each stream after 10 minutes. It ends one sooner when a deploy restarts the server, or when your client stops reading and falls 256 events behind. Connect again, and the new snapshot brings the numbers back up to date.
Limits and errors
A key may hold 4 streams open at once and open 30 a minute. An error arrives as the usual JSON envelope, before the stream starts.
| Status | Code | When |
|---|---|---|
| 403 | ULT-1203 | The organization is on Glance. |
| 403 | ULT-1221 | The key lacks stats:read. |
| 429 | ULT-1902 | The key has 4 streams open. Close one first. |
| 429 | ULT-1902 | The key opened 30 streams in the last minute. Retry-After says how many seconds to wait. |
| 503 | ULT-9003 | Realtime is busy on the server. Try again in a few seconds. |