# Edge analytics

Bring the playback the Cloudflare Worker counts into ClipMux's Analytics with a second, read-only token.

Source: https://clipmux.com/docs/self-host/delivery/edge-analytics

With Cloudflare delivery, the Worker answers viewers instead of your server,
so your server no longer sees those plays. The Worker counts them itself,
the same way built-in delivery does, and writes the counts to Analytics
Engine in your Cloudflare account. Connecting edge analytics lets ClipMux
pull those counts back, so views, watch time, bandwidth and unique viewers
in **Analytics** include playback at the edge.

## Before you start [#before-you-start]

- [Cloudflare edge delivery](/docs/self-host/delivery/cloudflare), connected.
- Access to the Cloudflare account the Worker runs in, to create a token.

## What the Worker counts [#what-the-worker-counts]

The Worker counts what built-in delivery counts: a view per generated master
playlist, 6 seconds of watch time per audio segment, and the bytes actually
streamed for every GET response (including partial transfers cancelled early).
It writes one data point per counted response to the Analytics Engine dataset
`clipmux_delivery`.

A data point holds the project, app, video, playback ID, file kind, a viewer
key (an HMAC of `vid` under the project's analytics key, omitted on poster
requests), and a per-install `INSTALL_ID` (`blob7`) so multiple installs
sharing a Cloudflare account never mix their counts. Dashboard previews
(even on public IDs), library posters and the setup test aren't counted;
[share links](/docs/build/play/share-links) are.

## Connect edge analytics [#connect-edge-analytics]

<Steps>
<Step>
### Create a read-only token [#create-a-read-only-token]

On **Delivery**, in the **Edge analytics** card, choose
**Create token on Cloudflare**. The form opens with only
**Account Analytics: Read** chosen. Create the token on the account the
Worker runs in. It can't change anything.
</Step>

<Step>
### Paste it [#paste-it]

Paste it into **Read-only API token** and choose
**Connect edge analytics**. ClipMux checks it with one query and keeps it
sealed with the secrets key. It's never returned.
</Step>
</Steps>

Over the API, the same step is `POST /v1/cloudflare/analytics` with
`{"token": "…"}`. It answers `409 not_connected` without Cloudflare
delivery, and `400 token_rejected` for a token Cloudflare refuses.

## How counts are pulled [#how-counts-are-pulled]

- The server pulls every 5 minutes.
- Each pull recomputes from an hour before the last one and replaces those
  hours, so points that arrive late still count and nothing counts twice.
- A first connect starts at the later of 13 days back or when Cloudflare
  delivery was connected, and catches up immediately through `now`.
- Pasting a new token keeps the place the pulls have reached.
- Disconnecting forgets the token. Counts already pulled stay in Analytics.

`GET /v1/cloudflare/analytics` returns how far the pulls have reached
(`pulled_through`), the last pull's time and its error, if any.

## What's sampled [#whats-sampled]

Analytics Engine samples high-volume requests. The Worker stratifies its data
point `indexes` by request class (`master`, watch-time segments, non-watch-time
video rendition segments, and metadata/posters) so high-volume video segments
don't drown out master-playlist view counts or bias byte weights. ClipMux
weights views, watch time and bytes back up by `_sample_interval`, so edge
counts stay accurate estimates.

Other limits:

- ClipMux pulls only the last 13 days. If pulls stop for longer than that,
  the gap is lost.
- Data points and queries count against Analytics Engine's allowance on
  Workers Paid. A viewer-hour writes about 1,200 data points, one per
  request. See
  [Analytics Engine pricing](https://developers.cloudflare.com/analytics/analytics-engine/pricing/).
- When an older Worker needs an upgrade (for example, to add `INSTALL_ID` and
  stratified `indexes`), **Analytics** and **Delivery** prompt you to redeploy
  Cloudflare delivery.

## Check it works [#check-it-works]

Play a video through the media hostname, wait 5 minutes, and look at the
**Edge analytics** card on **Delivery**.

- It shows **Connected** and no error: pulls are running. The plays show in
  **Analytics** once the pull covers their hour.
- "The last pull failed", with the reason: if the token was revoked, choose
  **Replace token** and paste
  a new one.
- **Analytics** still says playback through Cloudflare isn't counted: no
  token is connected. `collecting.edge` in `GET /v1/analytics` says
  `not_connected`.

## Next [#next]

- [Views, watch time and unique viewers](/docs/build/analytics)
- [Cloudflare edge delivery](/docs/self-host/delivery/cloudflare)
- [Retention](/docs/self-host/operate/retention)
