Docsllms.txtSign inGo to your sites

Updated

View as Markdown

Identify

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.

JavaScript
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_id or email is required. Without both, the script logs [twelvebit] identify needs user_id or email and sends nothing.
  • name replaces the friendly random name (amber-fox-4821) in the dashboard.
  • image (or avatar) 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.
  • null and undefined values are skipped.
  • On the server, user_id keeps 128 characters, email 200 and name 120.

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

TypeScript
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

Shell
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"}}'
FieldTypeNotes
visitor_idstringRequired. The value of the _tb_vid cookie. 1 to 64 characters.
user_idstringYour user id. 1 to 128 characters.
emailstringA valid email address, at most 200 characters. Lets twelvebit match payments by email.
namestringDisplay name. 1 to 120 characters.
imagestringA URL, at most 500 characters. The avatar in journeys.
traitsobjectFlat 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:

TypeScript
// 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:

JSON
{ "data": { "identified": true, "visitor_id": "VISITOR_ID", "user_id": "u_8f2a" }, "meta": { "site": { "…": "…" } } }

Errors

StatusCodeCause
400invalid_bodyA field has the wrong type or is unknown, or only visitor_id was sent. details names each problem.
400invalid_jsonThe body is not a JSON object.
401unauthorized or invalid_keyNo key, or the key is not valid.
403insufficient_scopeThe key does not have write:events.
403forbiddenThe key's owner can read the site but not change it.
404visitor_not_foundNo visitor with this id. The tracker must send a page view first, so read the cookie in a request after the first page load.
413too_largeThe body is larger than 32 KB.
429rate_limitedMore 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 identify event. 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 status shows the identify calls of the last 24 hours.
  • API: GET /api/v1/people?filter=identified&q=u_8f2a lists the visitor. GET /api/v1/people/u_8f2a returns the journey, because the path takes a visitor id or a user id.
  • MCP: list_people with filter: "identified", or get_journey with your user id.

Edge cases

  • Several devices. Each browser and each app install is its own visitor. The same user_id on two visitors does not merge them. A lookup by user id returns one of them.
  • Cookieless mode. There is no _tb_vid cookie, 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_found until 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.