Browser session purchases
Record checkouts from any store, so Octocom can credit revenue to the conversations that led to it.
Browser session purchases link a completed checkout to the visitor who chatted with your AI agent in the same browser. That is how Octocom shows the revenue your conversations bring in, under Metrics -> Sales.
A purchase counts toward chat when it happens within 7 days of a conversation in the same browser session. The Sales tab is available to everyone with metrics access, whatever your ecommerce platform.
Shopify stores don't need to do anything. Connecting or reconnecting the Octocom app activates checkout tracking automatically. See Shopify revenue tracking for consent and browser-session requirements.
For any other store, your developer adds one call to your order confirmation step. It sends the purchase to a public Octocom endpoint that needs no API key. Instead, a per-session purchase token, which the chat widget stores in the visitor's browser, proves the purchase comes from that visitor's own session. The rest of this page is the reference for that call.
To compare shoppers who chatted with those who didn't using Google Analytics instead, see Revenue attribution with GA4.
What is a browser session?
When a visitor opens the Octocom chat widget, the widget creates a browser session. It is identified by a UUID stored in the visitor's localStorage, so it persists across page navigations and later visits in the same browser. A session expires after 180 days without the visitor opening the chat. No session is created for a visitor who never opens the chat, so those visits have no session ID to report a purchase against.
A browser session is how Octocom links activity across a visitor's journey: it connects chat conversations, page visits and (with this integration) purchases to a single anonymous visitor profile.
Prerequisites
Before you record purchases through the API, make sure the Octocom chat widget is installed on your site. See Launch the chat widget.
Getting the browser session ID
The browser session ID is stored in localStorage under the key:
octocom-browser-session-id:{configId}where {configId} is the ID of your web chat configuration (under Settings -> Channels -> Web Chat in the Octocom dashboard). The value is the session UUID itself: read it and send it as it is.
If you don't know your configId, scan localStorage for any key with the octocom-browser-session-id: prefix:
function getOctocomBrowserSessionId() {
for (let i = 0; i < localStorage.length; i++) {
const key = localStorage.key(i);
if (key?.startsWith("octocom-browser-session-id:")) {
return localStorage.getItem(key);
}
}
return null;
}
const browserSessionId = getOctocomBrowserSessionId();This returns null if the widget script has not run yet, or if the visitor has never opened the chat in this browser. In private or incognito windows the session exists for as long as the tab is open, and does not carry over once it is closed.
Without a valid browser session ID, the API returns 404 Browser session not found and the purchase is not stored.
Getting the purchase token
The chat widget also stores a purchase token for the session under:
octocom-purchase-token:{browserSessionId}Send it as purchaseToken with the purchase. It proves the purchase comes from the visitor's own session, so nobody who only knows a session ID can add purchases to your revenue metrics. It authorizes nothing else, so it is safe to use in checkout code. Never send the separate octocom-session-credential: value.
const purchaseToken = localStorage.getItem(
`octocom-purchase-token:${browserSessionId}`,
);Purchases without a valid token are recorded but marked unverified, and each browser session accepts at most 60 unverified purchases an hour. Always send the token so your purchases count as verified. If no token is stored for a session, send the purchase without one: it is recorded as unverified, and the widget stores a token the next time it loads.
Recording a purchase through the API
Endpoint
POST https://api.octocom.ai/rest/v1/browser-sessions/:browserSessionId/purchases- No API key is required. The purchase token and rate limits protect the endpoint.
- Rate limit: at most 100 requests per minute per IP address. Exceeding it returns 429 with an error message.
Example
const browserSessionId = getOctocomBrowserSessionId(); // see above
if (browserSessionId) {
try {
const response = await fetch(
`https://api.octocom.ai/rest/v1/browser-sessions/${browserSessionId}/purchases`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
lineItems: [
{
id: "product-123",
quantity: 2,
},
{
id: "product-456",
quantity: 1,
variant: { variantId: "variant-789" },
},
],
totalPrice: 99.99,
currencyCode: "USD",
provider: "shopify",
orderId: "1042",
purchaseToken: localStorage.getItem(
`octocom-purchase-token:${browserSessionId}`,
),
}),
},
);
if (!response.ok) {
console.error(
"Failed to record purchase",
response.status,
await response.text(),
);
}
} catch (err) {
console.error("Failed to record purchase", err);
}
}Full checkout integration example
A complete example to call at the end of a checkout flow:
async function recordOctocomPurchase({
lineItems,
totalPrice,
currencyCode,
provider,
orderId,
}) {
function getOctocomBrowserSessionId() {
for (let i = 0; i < localStorage.length; i++) {
const key = localStorage.key(i);
if (key?.startsWith("octocom-browser-session-id:")) {
return localStorage.getItem(key);
}
}
return null;
}
const browserSessionId = getOctocomBrowserSessionId();
if (!browserSessionId) {
// Widget hasn't initialised yet or Octocom isn't installed — skip silently.
return;
}
try {
const response = await fetch(
`https://api.octocom.ai/rest/v1/browser-sessions/${browserSessionId}/purchases`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
lineItems,
totalPrice,
currencyCode,
provider,
orderId,
purchaseToken: localStorage.getItem(
`octocom-purchase-token:${browserSessionId}`,
),
}),
},
);
if (!response.ok) {
console.error(
"Octocom: failed to record purchase",
response.status,
await response.text(),
);
}
} catch (err) {
console.error("Octocom: failed to record purchase", err);
}
}
// Call this after a successful checkout:
recordOctocomPurchase({
lineItems: [
{ id: "product-123", quantity: 2 },
{ id: "product-456", quantity: 1, variant: { variantId: "variant-789" } },
],
totalPrice: 99.99,
currencyCode: "USD",
provider: "shopify",
orderId: "1042",
});Request schema
The request body must be valid JSON with this shape:
{
lineItems: Array<{
id: string, // Required. The purchased item's (product) ID.
quantity: number, // Required. Quantity purchased.
variant?: { // Optional. Include only when the item is a variant.
variantId: string, // The variant's ID.
},
// You may include other fields; they are stored as JSON.
}>,
totalPrice: number, // Required. Total price of the purchase. Must be positive.
currencyCode: string, // Required. e.g. "USD", "EUR".
provider?: string, // Optional. e.g. "shopify", "api". Default: "api".
orderId?: string, // Optional. Your own order identifier, used for reconciliation. Any string you choose.
purchaseToken?: string, // Recommended. Read from localStorage `octocom-purchase-token:{browserSessionId}`.
}Limits: up to 1,000 line items, a request body of up to 1 MB, a currencyCode of up to 16 characters, a provider of up to 100 characters and an orderId of up to 256 characters.
Line items:
lineItemsmust be an array. Each element is one purchased item.- Each item must include:
id: the purchased product or item ID.quantity: how many of this item were purchased.
- If the item is a variant (for example a size or color), add a
variantobject with avariantIdfield.
Example for a simple product and a variant product:
lineItems: [
{ id: "prod_abc", quantity: 2 },
{ id: "prod_xyz", quantity: 1, variant: { variantId: "var_123" } },
];Order ID:
orderIdis optional and accepts any string you choose, for example your store's order number ("1042","#1042") or the platform's order ID ("order_abc123","gid://shopify/Order/123"). It is stored as it is, so you can later match chat-attributed sessions to orders in your own system.- If you omit it, the purchase is still recorded, but without a direct link to your order record.
- Sending the same
orderIdagain for the same session does not record the order twice. The API returns the existing record.
Response
| Status | Meaning |
|---|---|
| 201 | Purchase recorded. The response body includes success: true, message and id (the UUID of the purchase record). |
| 400 | Invalid request body (for example, validation failed). |
| 404 | Browser session not found. Check that browserSessionId is a valid, existing session UUID. |
| 429 | Rate limit exceeded. At most 100 requests per minute per IP, or 60 purchases without a valid token per browser session per hour. |
| 500 | Server error (for example, the purchase could not be stored). |
Security considerations
- No API keys: the endpoint is meant to be called from the browser after checkout. Because the URL and payload are visible in client-side code, it does not use API keys. Instead, the
purchaseTokenproves the purchase comes from the visitor's own session, and rate limits apply (100 requests a minute per IP, 60 purchases without a valid token an hour per session). - Abuse: if you see spam purchases in your metrics, contact Octocom support to investigate and limit them.
- CORS: the API allows browser-based calls from your storefront.
Summary
| What you need | Description |
|---|---|
| Browser session ID | Read from localStorage using the key octocom-browser-session-id:{configId}, or scan for any key with the octocom-browser-session-id: prefix. |
| Line items | Array of objects with id, quantity and an optional variant: { variantId } for variants. |
| totalPrice / currencyCode | Total amount and currency of the order. |
| provider | Optional identifier (for example "shopify" or "api") for where the purchase came from. |
| orderId | Optional. Any string identifying the order in your own system (for example "1042" or "order_abc123"). Used for reconciliation. |
| purchaseToken | Recommended. Read from the localStorage key octocom-purchase-token:{browserSessionId}. Purchases without it are marked unverified. |
Call POST /rest/v1/browser-sessions/:browserSessionId/purchases on https://api.octocom.ai with this body to record a purchase for that session.