Analytics
Emit event
PUT /access/api/v1/analytics
Sends a page view or interaction event to the Dukanext analytics system. Call this on every page navigation and on key user interactions.
Request body:
{
"userId": "0bc41c04-4eed-4aae-a318-4f9ab9b6f338-1759904642100",
"sessionId": "SESSION-b0b13d41-7651-4ec3-87e2-77a6dc0aa60e-1770181330638",
"sessionStartPage": "/",
"referrerUrl": "https://google.com",
"hasCustomerAccount": false,
"customersId": null,
"event": "page_view",
"metadata": {
"url": "/products/running-shoes"
}
}
Fields:
| Field | Required | Description |
|---|---|---|
userId | Yes | Persistent anonymous ID for the visitor. Generate once and store in localStorage. Format: uuid-timestamp |
sessionId | Yes | Session-scoped ID. Generate on session start. Format: SESSION-uuid-timestamp |
sessionStartPage | No | The first page of the session |
referrerUrl | No | document.referrer |
hasCustomerAccount | No | Whether the visitor is logged in |
customersId | No | The customer's _id if logged in |
event | Yes | Event name (e.g. page_view, add_to_cart, checkout_start) |
metadata | No | Additional event data (current URL, product ID, etc.) |
Generating IDs:
// userId — generate once, persist forever in localStorage
function getUserId() {
const key = 'dn_user_id';
let id = localStorage.getItem(key);
if (!id) {
id = `${crypto.randomUUID()}-${Date.now()}`;
localStorage.setItem(key, id);
}
return id;
}
// sessionId — 30-minute TTL in localStorage; refreshed on each page view
function getSessionId() {
const key = 'dn_session_id';
const ttl = 30 * 60 * 1000; // 30 minutes
const stored = localStorage.getItem(key);
if (stored) {
try {
const { id, expires } = JSON.parse(stored);
if (Date.now() < expires) {
// Still valid — extend TTL
localStorage.setItem(key, JSON.stringify({ id, expires: Date.now() + ttl }));
return id;
}
} catch (_) {}
}
// Expired or missing — start a new session
const id = `SESSION-${crypto.randomUUID()}-${Date.now()}`;
localStorage.setItem(key, JSON.stringify({ id, expires: Date.now() + ttl }));
return id;
}
Response:
{ "status": "success" }
Always returns 200 — duplicate and bot events are silently dropped on the server.
Possible errors:
| Code | HTTP | Meaning |
|---|---|---|
INVALID_USER_ID | 400 | userId is missing or not a string |
INVALID_SESSION_ID | 400 | sessionId is missing or not a string |
INVALID_EVENT | 400 | event is missing or not a string |