# Events and users

Track the outcomes that matter with `track`, and tell datacub who is signed in with `identify`.

## Custom events

```ts web
datacub.track('signup_completed', { method: 'email' })
datacub.track('project_created', { template: 'blank', members: 3, trial: true })
```

Call it after the action succeeds: when the server answered or the payment was confirmed, not on the button click. A click on "Pay" happens whether or not the card was declined.

```ts web
async function pay(planId: string) {
  const res = await fetch('/api/checkout', { method: 'POST', body: JSON.stringify({ planId }) })
  if (res.ok) datacub.track('checkout_completed', { plan: planId })
}
```

Start with five to eight events. Good first ones: `signup_completed`, `onboarding_step_completed` (with `step`), the first moment of value such as `project_created`, `checkout_completed`, `subscription_started`, `invite_sent`.

## Names and properties

- `object_action`, snake case, past tense: `invoice_paid`, `report_exported`. English names, whatever your UI language.
- One event with a property beats many events: `onboarding_step_completed` with `{ step: 'profile' }`.
- Names never change: a renamed event is a new event.
- 1 to 100 characters, not starting with `$`, and never an ID: names with 6 or more digits in a row, long hex, part of a UUID or an `@` are refused.
- Properties are few, flat and repeatable: plan, step, count, true or false. Strings, numbers, booleans, `null`, dates, arrays and objects are allowed; up to 200 keys, objects 3 levels deep, arrays 100 items. A string over 256 characters or 12 words is treated as free text and becomes `[redacted]`.

datacub also drops keys named like secrets or personal data (password, OTP, card number, IBAN, Iqama, national ID, token and others) and redacts values containing an email, 9 or more digits, or a token, on the device and again on the server. That is a safety net, not a plan: send product facts, never who someone is or what they typed.

Events are sent in batches: 5 seconds after the first, at 20 events, or when the page is hidden. Retries never store an event twice.

## Identify signed-in users

```ts web
async function login(email: string, password: string) {
  const user = await api.login(email, password)
  datacub.identify(user.id, { email: user.email, name: user.name })
  return user
}

async function logout() {
  await api.logout()
  datacub.reset()
}
```

- Use your own stable internal user ID. IDs that look like an email, phone, national or Iqama ID, card number or JWT, and placeholders such as `null` or `guest`, are refused.
- `email`, `name` and `phone` show who a user is on the **People** page and make them searchable. Pass them at login and when they change, not on every page load. They are the only keys that may carry contact data.
- Pass `account` with the ID of their company, team or workspace to count active companies, not only people: `datacub.identify(user.id, { account: workspace.id })`.
- Other traits (`plan`, `role`) follow the same rules as event properties. Facts that change belong on events, so last month's funnel keeps last month's plan.
- Call `reset()` at logout, or the next person on a shared device is counted as the last user.

## Guests and Registered users

| | Guest | Registered |
|---|---|---|
| Who | anyone before `identify` | after `identify(userId)` |
| On **People** | `Guest #8d4c25`, with country, device and browser | the `name` or `email` you passed, and your user ID |
| ID | random, in a first-party cookie or app storage | your own user ID |
| Across devices | one per browser or phone | every device where they signed in |

![The People page: every visitor, the latest first, with where they came from, their latest activity and sessions](https://docs.datacub.to/images/en/people.webp "People")

Everyone starts as a **Guest**: a random ID in a first-party cookie (web) or app storage (mobile), never built from the device, IP address or person. After `identify`, they are **Registered**, and their Guest history on that browser or phone comes with them. Each device links when the user signs in there with the same ID. datacub never matches people by IP, device traits, email or phone.

On the web the Guest cookie lasts up to 400 days; Safari deletes it after 7 days without a visit. A session ends after 30 minutes without activity, or after 24 hours.

## See one person's journey

Open anyone on **People** to see their profile: first and last seen, sessions, active days, and the journey, day by day. Each row is a page with time on it, a click, or one of your events; expand it for the details. **About this person** lists the user ID, traits, where they first came from, and the last country, language and device.

![A Registered user's profile: first and last seen, sessions and active days, the journey of pages, clicks and events, and the About this person card](https://docs.datacub.to/images/en/profile.webp "People, one person's profile")

Use it to check your own tracking: sign in on your site, then find yourself by your user ID in the search on **People**.
