Developer docs / Integrations
Events API
Send what shoppers do on a headless storefront, website, app or server to the store's own events: page views, the shop events with Google Analytics 4 names, and your own events with tags.
Why one events API
Why: every front end should count the same way, under the same consent rules, into the store's own data, with no outside analytics service. How: one endpoint per store, POST /api/events on the store's address. The store's theme and CMS sites use it without code; a front end elsewhere sends with a key; the store records carts, checkout, payments and searches itself.
Quick start
Make a publishable key in Admin, Developers, Event keys, then add the script the store serves at /events/v1.js. That alone sends page_view, view_item on /products/<slug>, lists, promotions, scroll, outbound_click, file_download and user_engagement.
<script src="https://shop.example.com/events/v1.js" data-key="epk_..." defer></script>
<script>
// Shop events and your own, one line each:
commernauts.events.send('add_to_cart', { currency: 'EUR', value: 24, items: [{ item_id: 'stoneware-mug', quantity: 1 }] });
</script>The same code is the package @commernauts/events on npm.commernauts.com, for front ends with a build step.
npm install @commernauts/events --registry https://npm.commernauts.com/
import { events, autoCollect } from '@commernauts/events';
events.init({ store: 'https://shop.example.com', key: 'epk_...' });
autoCollect(events); // optional: the same automatic events as the script
events.send('view_item', { items: [{ item_id: 'stoneware-mug' }] });| Call | What it does |
|---|---|
| events.init({ store, key, debug }) | Where to send. store is the store's address; leave store and key out on the store's own pages. |
| events.send(name, tags) | One event. Queued and sent together (at most 25 a call), and whatever is left goes out as the page is hidden. |
| events.set(tags) | Tags kept for every following event on this page or screen, such as an experiment. |
| events.consent({ analytics }) | Your banner's answer. Until it is true nothing is kept in the visitor's browser; false removes the id. |
| events.identify(customerId) | Links a signed-in customer across devices, only together with consent. |
| events.debug(true) | Marks this visitor's events as debug: answered with reason debug, never counted. |
| autoCollect(events, { productPath, cartPath }) | The automatic events; returns a function that stops them. |
HTTP
Why: any language, a server or an app can send without the package. How: POST JSON (text/plain works too, so a browser needs no preflight) to /api/events with the key in Authorization, or as key in the body when a beacon cannot set headers. Up to 25 events and 64 KB a call.
curl -X POST "https://shop.example.com/api/events" \
-H "Authorization: Bearer epk_..." -H "Content-Type: application/json" \
-d '{
"events": [{
"name": "view_item",
"time": "2026-11-23T10:42:07Z",
"client": { "id": "a1b2c3d4e5f6a7b8", "consent": { "analytics": true } },
"context": { "page": "https://headless.example/products/stoneware-mug", "referrer": "https://www.google.com/" },
"tags": { "items": [{ "item_id": "stoneware-mug" }] }
}]
}'{ "accepted": 1, "results": [{ "index": 0, "status": "accepted" }] }| Field | Meaning |
|---|---|
| name | a to z, 0 to 9 and _, starting with a letter, at most 40 characters. The standard names, or your own (up to 500 per store). |
| time | ISO 8601, when it happened; more than 72 hours ago or in the future, the arrival time is used |
| client.id | Your visitor id (8 to 64 letters, digits, - or _), used only where the visitor's market allows one |
| client.consent.analytics | Your banner's answer; the store's rule for the visitor's market applies on top |
| context.page, referrer, title | Where it happened; utm_* and the referrer of a visit's first event give its source |
| context.device, user_agent | For a server sending on a visitor's behalf: phone, tablet or desktop, and their browser |
| tags | Up to 25 tags (text up to 500 characters, numbers, true or false) plus items |
| debug | true: answered with reason debug, never counted |
| status | Meaning |
|---|---|
| accepted | Kept and counted, or kept but not counted with a reason (debug, team) |
| duplicate | The store recorded the same action itself; counted once |
| rejected | Not kept; reason says why (a bad name, bot, a purchase sent with a publishable key, or monthly_limit once the plan's events for the month are used up) |
Each result may list problems: what was wrong but did not stop the event, such as an unknown item id or a tag dropped.
Shop events and items
Why: Products and the funnels need to know which product an event is about. How: send item_id (a product id, its slug, a variant id or a SKU); the store fills in the name, category, variant and price from its catalog. A value you send yourself is kept, for example a price after a discount.
Item fields, as Google Analytics 4 names them: item_id (required), item_name, item_brand, item_category to item_category5, item_variant, price, quantity, coupon, discount, index, item_list_id, item_list_name, promotion_id, promotion_name.
| Event | Who sends it |
|---|---|
| page_view, view_item, view_item_list, select_item, view_promotion, select_promotion, view_cart, scroll, outbound_click, file_download, user_engagement | The script, the package's autoCollect, or you |
| add_to_cart, remove_from_cart, begin_checkout, add_shipping_info, add_payment_info, search | The store, when it goes through its own cart, checkout or search; a copy you send within two minutes is a duplicate |
| purchase, refund | The store, when the payment or refund happens. From outside only with a secret key (orders made elsewhere), matched by transaction_id |
| session_start | The store, on the first event of a visit |
A cart change or search through the API from another site is credited to Online Store. The store's own records carry no front end's key.
Visitors and consent
Why: the visitor id must follow the same rule as on the store's own pages. How: every event goes through the store's consent decision (Consent and the visitor id). Without a visitor id the store counts by a daily hash of the visitor's address and browser, which needs nothing in the browser. With one, client.id (or, on the store's own pages, the __Host-cn_vid cookie the store sets) tells returning visitors apart.
- On the store's own pages the banner's cookie decides; client.consent is ignored there.
- On another site client.consent decides together with the visitor's market: in an ask-first market an id needs analytics: true, in an opt-out market it is used unless analytics is false or the browser sends Global Privacy Control.
- GET /api/consent answers the decision for the calling browser, from any origin.
Channels and keys
| Key | Starts | May send |
|---|---|---|
| Publishable | epk_ | Every event but purchase and refund. Safe in a web page or an app: it cannot read anything. |
| Secret | esk_ | Everything, including purchase and refund for orders made elsewhere. Keep it on a server; shown once. |
Each key belongs to a channel; every event carries it, so each Observability page can be filtered by channel. The owner API manages them with settings.read and settings.write:
GET /api/owner/observability/channels channels and keys
POST /api/owner/observability/channels { name }
DELETE /api/owner/observability/channels/:id revokes its keys
POST /api/owner/observability/keys { label, kind: publishable | secret, channel }
DELETE /api/owner/observability/keys/:id stops within a minuteTesting your events
Why: a front end that sends the wrong names or unknown items gives wrong numbers. How: call events.debug(true) or send debug: true. The answer to POST /api/events lists each event with its status, reason debug, and its problems; debug events never count. web_vitals and error are answered rejected: page speed and script errors are not recorded per visitor.
Where events are kept
Why: the data belongs to the store. How: in the store's own D1 database. event_log holds each event (for the reports), event_sessions each visit with its source, event_daily the daily totals per event, channel, channel group, device and country, and event_visitors the visitors of each day. The daily hash's secret (event_salts) is deleted the day after.
History follows the store's plan (Starter 90 days, Growth 1 year, Scale 2 years, Custom 5 years): event_daily is kept for the whole history; event_log, event_sessions, event_visitors and event_orders for the plan's history, at most 90 days. In the store template every prune and the monthly events limit ask eventLimits() in api/events/events.limits.ts, which takes the plan's numbers from the platform (api/usage/usage.limits.ts, kept an hour). See Observability hooks and API, Usage and plan.
Hooks
Why: plug-ins and your own systems should follow observability as it happens. How: these events go to webhooks and plug-in hooks, signed and retried like store webhooks. Each is available; the data of each is in Observability hooks and API.
| Hook | When | Status |
|---|---|---|
| observability.event | Every counted event, in the batches they arrived in, for a plug-in that forwards data elsewhere; a plug-in only with the observability.events permission, which the owner grants at install | Available |
| observability.alert.opened, observability.alert.resolved | An alert rule fired or cleared | Available |
| observability.threshold | A number crossed a limit you set, such as fewer than 5 orders in a day | Available |
| observability.daily_summary | Yesterday's totals, once a day | Available |
In the store template
The layout renders <ZEvents /> once per page (components/events.tsx), which starts autoCollect from @commernauts/events; the editor's framed views leave it out. The store's records are api/events/events.records.ts, called after a cart change, a checkout step, a payment, a refund and a search. /events/v1.js is the package's script build, committed in public/events so every published template serves it.