Metrickle

HTTP ingest API

Last updated

Use the ingest API when there's no Metrickle SDK for your platform, or to send events from your own servers, such as an order confirmed by your back end. The SDKs use this same endpoint, so events you send this way work in goals, funnels, journeys and protected flows like any other. Read Events, people and properties first for what to send.

Send a batch

POST https://in.metrickle.com/v1/batch
# Any other platform (server, game engine, desktop): POST batches of events
curl -X POST https://in.metrickle.com/v1/batch \
  -H "content-type: application/json" \
  -H "x-metrickle-key: mk_pub_YOUR_WRITE_KEY" \
  -d '{
    "sentAt": '$(date +%s000)',
    "context": { "library": { "name": "my-sdk", "version": "1.0" }, "platform": "ios",
                 "app": { "version": "2.3.0" }, "device": { "type": "mobile", "os": "iOS" } },
    "events": [{
      "id": "'$(uuidgen | tr A-Z a-z)'", "type": "screen", "name": "$screen", "ts": '$(date +%s000)',
      "anonymousId": "device-uuid", "sessionId": "session-uuid", "path": "Onboarding/Step1"
    }]
  }'

Send JSON with content-type: application/json. Put the app's write key (mk_pub_…, from the app's Settings page) in the x-metrickle-key header. If you can't set headers, for example with navigator.sendBeacon, put it in the body as writeKey instead.

A write key can only add events to its app, so it's safe to ship inside an app.

The batch

FieldTypeNotes
sentAtnumber, requiredWhen you sent the batch, in Unix milliseconds. Used to correct the sender's clock
contextobject, requiredWhere the events come from (below). Applies to every event in the batch
eventsarray, required1 to 100 events
writeKeystringThe write key, when you can't send the header

Context

FieldTypeNotes
libraryobject, required{ "name", "version" } of the code sending the batch, such as { "name": "my-sdk", "version": "1.0" }
platformstring, requiredweb, ios, android, react-native, flutter or server
appobject{ "version", "build" } of your app. A new version is marked as a release
deviceobjecttype (desktop, mobile, tablet, tv or other), model, os, osVersion
screenobject{ "width", "height" } in whole points or pixels
localestringA language tag such as en-GB
timezonestringAn IANA time zone such as Europe/London
a11yarray of stringsAccessibility flags seen on the device, such as screen_reader. Unknown flags are ignored. See accessibility context

When you leave out device, Metrickle reads the device type and operating system from the request's User-Agent.

Events

FieldTypeNotes
idUUID, requiredMade by you, new for each event. Sending the same id again doesn't store the event twice, so retries are safe
typestring, requiredpage, screen, track or identify
namestring, requiredUp to 128 characters, such as $pageview, $screen, $identify or your own event name
tsnumber, requiredWhen it happened, in Unix milliseconds
anonymousIdstringA random id for the device or install, up to 64 characters
userIdstringYour id for the signed-in person, up to 128 characters
sessionIdstringUp to 64 characters
pathstringThe page path or screen name
urlstringThe full page address. The query string is used for UTM tags, then dropped
titlestringThe page or screen title
referrerstringWhere the visitor came from
propertiesobjectUp to 64 flat key–value pairs. Values are strings up to 1,024 characters, finite numbers, booleans or null
traitsobjectOn identify events: facts about the person, with the same rules as properties

Send at most 256 KB per request.

Visitors and sessions

  • Send the same anonymousId for every event from one device or install, and userId once the person has signed in. Metrickle then counts one person as one visitor across devices. See people and sessions.
  • If you leave out anonymousId, Metrickle makes a visitor id from the request's IP address and user agent, which changes daily. From a server, every event would then look like the same visitor, so always send an id.
  • If you leave out sessionId, a session is each 30-minute period of a visitor's activity.

Timestamps

Metrickle uses sentAt to correct for a sender whose clock is wrong. A time in the future is moved to the moment the batch arrived. A time more than 7 days in the past is moved to 7 days ago, so you can't use this API to import older history.

Revenue

Put a numeric revenue property on one of your own events. Send a refund as negative revenue with the same userId. See revenue.

Responses

StatusBodyWhat to do
202{ "ok": true, "accepted": 3 }Done
400{ "error": "invalid_json" }Fix the body
400{ "error": "invalid_batch", "issues": [...] }Fix the fields listed in issues (up to 10)
401{ "error": "missing_write_key" } or "unknown_write_key"Check the key, and that the app hasn't been archived
403{ "error": "origin_not_allowed" }The request came from a browser page whose domain isn't in the app's allowed origins
413{ "error": "payload_too_large" }Split the batch
429{ "error": "rate_limited" }Wait and send again

Treat any 2xx, and any 4xx other than 429, as finished: sending the same batch again won't change the answer. After a 429, a 5xx or a network error, keep the events and try again with a growing delay, for example 1 second doubling up to a minute.

Requests are limited to about 600 a minute for each write key and sending address.

When a workspace is far over its plan's monthly events, Metrickle keeps a steady share of visitors and the response includes "sampled": true. See plans and billing.

Allowed origins and bots

  • An app with allowed origins set accepts browser requests only from those domains and their subdomains. Requests without an Origin header, such as from a server or a native app, are always accepted.
  • Batches with platform set to web are checked for crawlers and headless browsers by their User-Agent. They get a 202 but nothing is stored. Batches from other platforms aren't checked this way, so send server from your own back end.

What needs an SDK

The ingest API takes events you can describe yourself. Friction signals, accessibility settings, surveys, feedback, heatmaps and session replay come from the device, so they need a Metrickle SDK. See Web and React Native, iOS, Android or Flutter.