Updated
View as MarkdownIdentify
Identify links an anonymous visitor to a user of your app. Then the visitor's journey, goals and payments show a real user, and payments can match by email.
Before identify, each visitor is anonymous. The dashboard gives them a friendly random name such as amber-fox-4821. After identify, the visitor keeps the same visits and goals, and gets your user id, the email, the name and the traits that you send.
From the browser
Call it after sign-in and after signup, when you know the user. You can also call it on each page load of a signed-in user: repeated calls are safe.
window.twelvebit?.("identify", {
user_id: "u_8f2a", // your opaque user id
email: "ada@example.com",
name: "Ada Lovelace",
image: "https://example.com/avatars/ada.png", // or avatar
plan: "pro", // any other key is stored as a trait
});user_idoremailis required. Without both, the script logs[twelvebit] identify needs user_id or emailand sends nothing.namereplaces the friendly random name (amber-fox-4821) in the dashboard.image(oravatar) shows in journeys and on the live view.- Other keys are stored as traits. Later calls merge into earlier traits.
The script cleans the object before it sends it:
- At most 30 keys. Keys are cut to 64 characters.
- Text values are cut to 255 characters. Objects and arrays become JSON text.
nullandundefinedvalues are skipped.- On the server,
user_idkeeps 128 characters,email200 andname120.
When the script does not track (for example on localhost, see Script), window.twelvebit does nothing, so the call never throws. To call identify before the script has loaded, add the queue stub.
With the web SDK
import { identify, reset } from "@twelvebit/web";
identify("u_8f2a", { email: "ada@example.com", name: "Ada Lovelace", plan: "pro" });
// At logout: a new visitor and a new session for the next person on this device.
reset();The first argument is the user id. The SDK puts it in user_id. The script tag has no reset. In cookieless mode reset() does nothing. See SDKs.
From your server
curl -sS -X POST https://twelvebit.app/api/v1/identify -H "Authorization: Bearer $TWELVEBIT_API_KEY" \
-H "Content-Type: application/json" \
-d '{"visitor_id": "VISITOR_ID", "user_id": "u_8f2a", "email": "ada@example.com", "name": "Ada Lovelace", "traits": {"plan": "pro"}}'| Field | Type | Notes |
|---|---|---|
visitor_id | string | Required. The value of the _tb_vid cookie. 1 to 64 characters. |
user_id | string | Your user id. 1 to 128 characters. |
email | string | A valid email address, at most 200 characters. Lets twelvebit match payments by email. |
name | string | Display name. 1 to 120 characters. |
image | string | A URL, at most 500 characters. The avatar in journeys. |
traits | object | Flat strings, numbers or booleans. Merged into earlier traits. At most 30 keys of 1 to 64 characters, text values at most 255 characters. |
Send at least one field next to visitor_id. A field that is not in the table is an error, so a typo does not get lost. The key needs the write:events scope (see API scopes). The body can be at most 32 KB.
In a Next.js route handler, read the cookie after the account is created:
// app/api/signup/route.ts, after the account is created
import { cookies } from "next/headers";
const visitorId = (await cookies()).get("_tb_vid")?.value;
if (visitorId) {
await fetch("https://twelvebit.app/api/v1/identify", {
method: "POST",
headers: { Authorization: "Bearer " + process.env.TWELVEBIT_API_KEY, "Content-Type": "application/json" },
body: JSON.stringify({ visitor_id: visitorId, user_id: user.id, traits: { plan: "free" } }),
}).catch(() => {}); // analytics must never break the signup
}A successful call answers 200:
{ "data": { "identified": true, "visitor_id": "VISITOR_ID", "user_id": "u_8f2a" }, "meta": { "site": { "…": "…" } } }Errors
| Status | Code | Cause |
|---|---|---|
| 400 | invalid_body | A field has the wrong type or is unknown, or only visitor_id was sent. details names each problem. |
| 400 | invalid_json | The body is not a JSON object. |
| 401 | unauthorized or invalid_key | No key, or the key is not valid. |
| 403 | insufficient_scope | The key does not have write:events. |
| 403 | forbidden | The key's owner can read the site but not change it. |
| 404 | visitor_not_found | No visitor with this id. The tracker must send a page view first, so read the cookie in a request after the first page load. |
| 413 | too_large | The body is larger than 32 KB. |
| 429 | rate_limited | More than 120 requests a minute with one key. Wait for Retry-After. |
Repeated calls
- A value that you send replaces the earlier value. A field that you leave out keeps its earlier value.
- Traits merge: a new key is added, a key that exists gets the new value. Identify cannot remove a value.
- Each call stores one
identifyevent. Identify calls do not count toward your plan (see Usage) and are never sampled.
Payments and email
When a payment arrives without a visitor in its checkout metadata, twelvebit looks for a visitor that identified with the customer's email. The match ignores upper and lower case. When several visitors have that email, the one seen last gets the payment. See Revenue for the full order.
After a payment is linked, twelvebit adds the customer's email to the visitor when the visitor has none. It also adds the provider's customer id as a trait, for example stripe_customer_id.
After identify, server goals can use the user id instead of the cookie: POST /api/v1/events with user_id finds the visitor that identified with it (the one seen last). See Goals.
Check that it worked
- Dashboard: open People and pick Signed in. Search by email, name, visitor id or user id. The person's page shows the email (or "user u_8f2a") and the traits.
- CLI:
npx -y twelvebit@latest statusshows the identify calls of the last 24 hours. - API:
GET /api/v1/people?filter=identified&q=u_8f2alists the visitor.GET /api/v1/people/u_8f2areturns the journey, because the path takes a visitor id or a user id. - MCP:
list_peoplewithfilter: "identified", orget_journeywith your user id.
Edge cases
- Several devices. Each browser and each app install is its own visitor. The same
user_idon two visitors does not merge them. A lookup by user id returns one of them. - Cookieless mode. There is no
_tb_vidcookie, and when the site setting is on the server ignores cookie ids. Use the browser call, not the server API. - Before the first page view. The server API answers
404 visitor_not_founduntil the tracker has sent a page view for this visitor.
Privacy
- Identify only where your privacy policy and consent allow it. Some laws need consent before you link analytics to a person.
- Use an opaque
user_id, not an email, as the id. - Never put personal data in event props. Identify is the one place for identity.
- In cookieless mode the visitor id changes every day, so an identify call links only that day's visitor.