Skip to content

POST /track/events/batch

Records multiple events in a single request. Used by the frontend tracker script to flush queued events every 5 seconds.

Request Body

json
{
  "session_id": "550e8400-e29b-41d4-a716-446655440000",
  "visitor_id": "a1b2c3d4e5f6...",
  "events": [
    { "event_type": "scroll", "scroll_percent": 25, "page": "https://example.com/home", "timestamp": 1740000000000 },
    { "event_type": "click", "x": 400, "y": 200, "page": "https://example.com/home", "timestamp": 1740000001000 },
    { "event_type": "page_view", "page": "https://example.com/about", "timestamp": 1740000002000 }
  ]
}
FieldRequiredDescription
session_idYesSession ID
eventsYesNon-empty array of events (max 100)
visitor_idNoVisitor ID

Each event in the array supports the same fields as /track/event.

Response (200)

json
{
  "accepted": 3,
  "timestamp": 1740000000000
}

Limits

  • Maximum 100 events per batch request
  • Events exceeding the limit receive a 400 error

Background Processing

  1. Batch-insert all events to D1 using DB.batch() (single round trip)
  2. Update session last_seen and exit_page with the last event
  3. Append event batch to R2 as a single archive file

Errors

StatusCondition
400Invalid JSON body
400Missing session_id or empty events[]
400Batch size exceeds 100