API
Send events with the Loogika API
POST /track records an event from any code that can make an HTTP request. Use it for things the browser never sees, such as a payment confirmed by your payment provider or activity in a mobile app.
Last updated . This guide belongs to Custom events and goals.
What does a request look like?
curl -X POST "https://app.loogika.com/api/track" \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"site_id": "12",
"type": "custom_event",
"event_name": "order_completed",
"pathname": "/checkout",
"properties": "{\"total\": 129.5, \"currency\": \"EUR\"}",
"anonymous_id": "visitor-8f2c",
"ip_address": "203.0.113.7",
"user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.0 Safari/605.1.15"
}'
properties is a JSON object written as a string.
Do you need an API key?
The endpoint accepts requests without a key. Send one anyway when you track from a server.
Without a key, Loogika ignores ip_address and user_agent in the body and uses those of your server. Every event then looks like one visitor, and most HTTP clients are classified as bots.
The key must be a full-access key or have the ingest:write scope. It must be sent as a Bearer token. A key in the query string does not count here.
A key without that scope causes no error. Loogika ignores the two fields and records the event as usual.
Which fields can you send?
| Field | Type | Required | Content |
|---|---|---|---|
site_id | Text | Yes | The site the event belongs to |
type | Text | Yes | The event type. See the next table |
event_name | Text | For custom_event and error | The name, up to 256 characters |
properties | Text | For some types | A JSON object as text, up to 2,048 characters |
pathname | Text | No | The page path, up to 2,048 characters |
querystring | Text | No | The query string |
hostname | Text | No | The domain |
page_title | Text | No | Up to 512 characters |
referrer | Text | No | Where the visitor came from |
anonymous_id | Text | No | Your own ID for the visitor. Keep it the same for one visitor |
user_id | Text | No | Your user ID, for identified users |
ip_address | Text | No | The visitor's IP address. Used only with a qualifying key |
user_agent | Text | No | The visitor's user agent. Used only with a qualifying key |
language | Text | No | The visitor's language |
screenWidth, screenHeight | Number | No | The screen size in pixels |
tag | Text | No | A tag, up to 256 characters |
A field that is not in the list makes the request fail with status 400.
Which event types are there?
type | Needs | Content of properties |
|---|---|---|
pageview | Nothing more | |
custom_event | event_name | Your own properties, optional |
performance | Nothing more | Not used. Send lcp, cls, inp, fcp and ttfb as numbers at the top level |
outbound | properties | url, and optionally text and target |
error | event_name and properties | message, and optionally stack, fileName, lineNumber, columnNumber |
button_click | Nothing more | Optional object |
copy | properties | sourceElement, and optionally text and textLength |
form_submit | properties | formId, formName, formAction, method and fieldCount |
input_change | properties | element and inputName |
What does the API answer?
| Status | Body | Meaning |
|---|---|---|
| 200 | {"success":true} | The event was accepted |
| 200 | {"success":true,"message":"Event not tracked - ..."} | An exclusion rule matched. The event was not stored |
| 200 | Plain text | The event was not stored, for example when the site is over its monthly limit |
| 400 | {"success":false,"error":"Invalid payload","details":...} | A field is wrong. details names it |
| 404 | {"success":false,"error":"Site not found"} | The site_id does not exist |
An event that Loogika classifies as a bot also gets status 200. It is stored as bot traffic and stays out of your reports.
Check the body, not only the status, when you test.
How do you keep visitors apart?
Send the same anonymous_id for every event from one visitor, and send the visitor's real IP address and user agent with a key.
If you send neither, all your events share your server's address and count as one visitor.
How do you identify a user from a server?
POST /identify links a visitor to your user ID and stores traits.
| Field | Type | Required |
|---|---|---|
site_id | Text | Yes |
user_id | Text | Yes |
anonymous_id | Text | No |
traits | Object | No |
ip_address, user_agent | Text | No. Used only with a qualifying key |
Questions about sending events
Can you put the API key in browser code?
No. Anyone can read a key in a web page. Use the tracking script in the browser and the API from your server.
Do events sent through the API count toward your plan?
Yes. An event counts the same way however it arrives.