Skip to main content

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:

FieldRequiredDescription
userIdYesPersistent anonymous ID for the visitor. Generate once and store in localStorage. Format: uuid-timestamp
sessionIdYesSession-scoped ID. Generate on session start. Format: SESSION-uuid-timestamp
sessionStartPageNoThe first page of the session
referrerUrlNodocument.referrer
hasCustomerAccountNoWhether the visitor is logged in
customersIdNoThe customer's _id if logged in
eventYesEvent name (e.g. page_view, add_to_cart, checkout_start)
metadataNoAdditional 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:

CodeHTTPMeaning
INVALID_USER_ID400userId is missing or not a string
INVALID_SESSION_ID400sessionId is missing or not a string
INVALID_EVENT400event is missing or not a string