Activity API

Send what people do on your site, straight from the browser.

Introduction

The Activity API records what people do on your site or in your app: a page view, a sign-up, a plan change. Each event belongs to a person. An event can start an automation or put a person in an audience.

The base URL is https://api.acorndispatch.com. Requests and answers are JSON. There is one endpoint: Send an event. It is safe to call from a web page.

Authentication

Send a track key in the Authorization header on every request:

Authorization: Bearer $ACORNDISPATCH_TRACK_KEY
  • A workspace Admin creates track keys in AcornDispatch, on Activity, APIs & Keys. Every track key starts with adt_. An Admin can copy a key again at any time.
  • A track key works on the Activity API only. An Email API key gets 401 unauthorized here, and a track key gets 401 on every Email API route.
  • A missing, wrong or revoked key gets 401 unauthorized.

Browser use and allowed origins

A page can call the API with fetch. The browser first sends a preflight OPTIONS request. It gets 204 for any origin. Every answer to the POST allows any origin, so your page can read it, an error too. Send no cookies: the API reads none.

Each track key can have a list of up to 20 allowed origins. An empty list allows any origin.

  • An origin is https://host or https://host:port, with no path. For local work, http://localhost and http://127.0.0.1 work too, with or without a port.
  • With a list, the request must carry an Origin header that is in it. A request with no Origin, or Origin: null, gets 403 origin_not_allowed. So does another site.
  • A change to the list works from the next request.

What a track key can do

A track key sits in your page source, so it is public. Anyone who reads it can send events with it. Allowed origins stop other websites from using the key in a visitor's browser. They cannot stop a script outside a browser, which can send any Origin header.

With your track key, anyone can:

  • Add events to people who already exist in your workspace. An event for an identifier with no person is answered {} like any other, and is not stored. A track key never creates a person and never changes the attributes your app sets.
  • Change what AcornDispatch works out from events. For example, a subscribe success event marks the person as one who has paid.
  • Put an existing person in an audience whose rule reads events, and start an automation that starts on an event for that person. A person enters each automation once at most.
  • Send an event id for a person first, so that your later event with the same id for that person is not stored.

The rate limits cap how much one key can send. To stop a key, revoke it in AcornDispatch and create a new one.

Event ids

  • An event with an id is stored once per person. A repeat with the same id for the same person is answered {} and not stored, whatever key sent the first one.
  • The same id for two people stores two events.
  • An event with no id is always stored.

Rate limits

  • Each track key may send 1,200 events per minute.
  • Each client IP address may send 120 events per minute with one key. Visitors behind one address, such as an office or a mobile network, share it. Over it, their browsers get 429 and those events are lost.
  • Requests with a wrong key count per client IP address: over 60 per minute they get 429 too.
  • Over a limit, the API answers 429 rate_limited with a retry-after header in seconds, counted per clock minute.

Errors

Every error has this form:

{ "error": { "code": "origin_not_allowed", "message": "This track key does not allow requests from this origin" } }

Branch on code, not on message. Each code, its HTTP status and what it means:

  • invalid_request400The body or the identifier breaks a rule: not JSON, an unknown field, no name, a timestamp that is not in seconds, a NUL character. The message names the field.
  • payload_too_large413The body is over 32 KB.
  • unauthorized401The track key is missing, wrong or revoked, or the key is not a track key.
  • origin_not_allowed403The key has allowed origins and the request's Origin is not one of them, or is missing. See Browser use and allowed origins.
  • rate_limited429Over a limit. Wait the retry-after seconds, then retry. See Rate limits.
  • internal_error500Something failed on our side. Retry with the same event id.

Events

Send events about people from your site or app.

Send an event

POST/api/v1/customers/{identifier}/events

Needsa track key

Records something a person did, such as a page view or a plan change. The event is stored for a person who already exists. See What a track key can do.

  • The body is strict: any other field gets 400 invalid_request.
  • The body is at most 32 KB. Above it: 413 payload_too_large.
  • The body can be nested at most 32 levels deep. Deeper gets 400 invalid_request.
  • The answer is {} for every identifier. An identifier with no person gets the same answer, and the event is not stored.

Path

  • identifierstring, requiredThe person's email address, or your app's own id for them. 1 to 255 characters. Percent-encode it (encodeURIComponent). It cannot start with cio_.

Body

  • namestring, requiredThe event, for example page_viewed. 1 to 255 characters, trimmed.
  • dataobject, optionalFacts about the event. Stored with it. Not sent: {}.
  • timestampinteger, optionalWhen it happened, in Unix seconds. A value that is not a time in seconds, or that is more than 5 minutes in the future, gets 400 invalid_request. Not sent: the time AcornDispatch got it.
  • idstring or integer, optionalYour id for the event, up to 100 characters. A repeated id for the same person is stored once. See Event ids.

Errors

  • invalid_request400The body or the identifier breaks a rule: not JSON, an unknown field, no name, a timestamp that is not in seconds, a NUL character. The message names the field.
  • payload_too_large413The body is over 32 KB.
  • unauthorized401The track key is missing, wrong or revoked, or the key is not a track key.
  • origin_not_allowed403The key has allowed origins and the request's Origin is not one of them, or is missing. See Browser use and allowed origins.
  • rate_limited429Over a limit. Wait the retry-after seconds, then retry. See Rate limits.

Example

In a browser.
const identifier = 'person@example.com'

fetch(`https://api.acorndispatch.com/api/v1/customers/${encodeURIComponent(identifier)}/events`, {
  method: 'POST',
  headers: {
    Authorization: `Bearer ${ACORNDISPATCH_TRACK_KEY}`,
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    name: 'plan_upgraded',
    data: {
      plan: 'pro',
    },
    timestamp: 1790000000,
    id: 'evt_8f2c1',
  }),
})
Response, HTTP 200
{}