# Users and identity

> Device IDs, anonymous and identified users, user tokens and the three identity verification modes, sign-out and user switching.

Every install of your app has a device ID, and at any moment the device is either anonymous or identified as one of your users. Identity decides what Herald stores for a user and which messages the device can receive.

## Device ID

The SDK generates a random UUID the first time it runs and stores it in the app's private storage. It is not the IDFA, the IDFV, the Android ID or the advertising ID.

- It stays the same across sign-ins, sign-outs and app updates.
- `wipeData()` replaces it with a new one.
- On iOS it lives in Application Support, excluded from backups, and is removed with the app. On Android it lives in the app's no-backup files directory, which Android Auto Backup never copies. SDK 0.4.0 and earlier keep it in the app's files directory, which Auto Backup includes unless your backup rules exclude it.

## Anonymous and identified

| | Anonymous device | Identified and verified user |
|---|---|---|
| Events | Stored with the device ID | Stored with the device ID and the External ID |
| Traits and attributes | Not written to any profile | Merged into the user's profile |
| Push token | Not stored, so no push | Stored as the user's push contact |
| Content cards and in-app messages | None (the sync returns empty lists) | The user's cards and messages |
| Message outcomes | Not credited | Credited to the Herald decision |

A device is identified from the `identify` call until `reset()`. Events recorded before `identify` are linked to the user when they sign in on that device: see [Guests and signed-in users](#guests-and-signed-in-users). Whether Herald trusts that identity depends on the verification mode below.

## Guests and signed-in users

**Before sign-in.** A guest has no profile. Their events are stored with the device ID only: no profile, no push, no messages and no goal credit yet.

**At sign-in.** When a device is first bound to a verified user, Herald links that device's guest events from the last 30 days to the user (at most the 5,000 most recent). That includes events from the same batch as the `identify`. The linked events then count as the user's:

- The user's first seen time moves back to the earliest linked event, and the events count toward their activity and send-time stats.
- A goal event from before sign-in credits the user's earlier Herald message, within that goal's attribution window, as if it had been sent signed in. A first-time-only goal counts the linked events too.
- The person's activity timeline marks them **before sign-in**.

Only a verified identity links events. An unverified claim (no valid user token under **Optional**) links nothing, and its events stay anonymous.

**Shared devices.** Guest events are linked only if they come after everyone else's use of the device: after the last sign-out (`reset()`), and after the latest event of any other person on that device, signed in or claimed without a valid user token. When person A signs out, a guest browses and person B signs in, the guest's events go to B, never to A, and nothing from before A's time on the device does. Events stored for a signed-in user never move.

**Several devices.** Each device's guest history is linked to the person when they first sign in on that device. Signing in again after a sign-out links only the guest events since that sign-out.

**Reinstall.** Reinstalling the app or calling `wipeData()` gives the device a new device ID, so nothing recorded under the old ID is linked.

## Identify

Call `identify` after sign-in, with the same stable ID your other systems use for that person (for example the ID your warehouse syncs to Herald). Herald calls it the **External ID**. Do not use an email address: it changes, and it is personal data.

:::tabs
::tab ios
```swift
Herald.shared.identify(userId: "u_123", traits: ["plan": "free"])
Herald.shared.setUserToken(tokenFromYourBackend)
```
::tab android
```kotlin
Herald.identify("u_123", mapOf("plan" to "free"))
Herald.setUserToken(tokenFromYourBackend)
```
:::

- The External ID is 1 to 200 characters. Traits follow the attribute rules on [Events and attributes](/docs/concepts/events-and-attributes).
- Calling `identify` again with the same ID sends the new traits and changes nothing else.
- After identifying, the SDK fetches the user's cards and messages, and sends the device's push token again so it is stored for this user.

## User tokens and verification modes

A user token proves your backend vouches for the External ID. It is the hex HMAC-SHA256 of the External ID, keyed with the SHA-256 hex digest of the connection's ingest token. Mint it on your backend after sign-in and pass it to `setUserToken`. Never put the ingest token in the app.

```javascript
// Node.js, on your backend. HERALD_INGEST_TOKEN is the connection's ingest token.
import { createHash, createHmac } from "node:crypto";

const key = createHash("sha256").update(process.env.HERALD_INGEST_TOKEN).digest("hex");
const userToken = createHmac("sha256", key).update(userId).digest("hex");
```

The token does not expire. It changes only when someone rotates the connection's ingest token, which invalidates every user token at once.

Set the mode on the connection's **Mobile SDK** card. New connections start at **Required**.

| Mode | Valid token | No token | Wrong token | Content cards and in-app messages |
|---|---|---|---|---|
| Required (default) | User verified | Request rejected (401) | Request rejected (401) | Only with a valid token |
| Optional | User verified | Events stored as anonymous; nothing written for the user | Request rejected (401) | Empty without a token; 401 with a wrong one |
| Off | User trusted as sent | User trusted as sent | Not checked | Returned for any External ID |

A request that names no user (an anonymous device) never needs a token in any mode.

:::warning Off is for development only
With verification **Off**, anyone who has your app key can send events as any user and read any user's content cards and in-app messages. App keys ship inside your app, so treat them as public. Use **Off** only with test data, never in production.
:::

Events recorded between `identify` and `setUserToken` are not lost. If a flush runs first and the server answers 401, the SDK keeps the batch, logs `Herald needs a user token: call setUserToken` and sends the user's events once `setUserToken` is called. Anonymous events keep flowing meanwhile. Call `setUserToken` as soon as your app has the token.

`identify` starts a fetch of the user's cards and messages before your app can call `setUserToken`. Under **Required**, the first such fetch on a device gets a 401, logged at debug level; it is harmless. The SDK remembers that the connection needs a token, so later sign-ins wait for `setUserToken` instead of fetching without it. `setUserToken` then fetches with the token, so cards appear in the same session. A session-start in-app message for that user can first show in their next session, because the session-start trigger already ran.

## Sign-out

Call `reset()` when the user signs out. The device becomes anonymous and keeps its device ID.

:::tabs
::tab ios
```swift
Herald.shared.reset()
```
::tab android
```kotlin
Herald.reset()
```
:::

- The SDK clears the External ID, the user token, the User ID, the cards and the in-app messages on the device.
- Events the previous user queued before sign-out are still sent under that user, with that user's token.
- The next batch the device sends is anonymous. Herald then unbinds the device from the previous user and deletes that user's push tokens for the device, so a signed-out phone stops getting their notifications.
- In-app message history (which messages the user has seen) is kept per user and comes back if the same user signs in again.

## Support ID

Every identified person gets a **User ID** from Herald: eight letters and digits shown as `XXXX-XXXX`, such as `7K3M-9QXA`. It is short enough to read out loud and never changes. It is separate from the **External ID**: External ID is the id your app passes to identify(). Anonymous devices have no User ID.

- It shows at the top of the person's page in Herald.
- Support can search for it on **People**, with or without the dash and in any case. Every console role can search by it.
- The SDK receives it in the response to the first accepted batch for the user the batch is verified as: with a valid user token under Required or Optional, and for any claimed user under Off. Under Optional without a token it never arrives. It is nil until then, and again after `reset()` or a switch to another user. The SDK caches it, so it is there on the next launch.

:::tabs
::tab ios
```swift
let supportId = Herald.shared.heraldId // "7K3M-9QXA" or nil
```
::tab android
```kotlin
val supportId = Herald.heraldId // "7K3M-9QXA" or null
```
:::

Show it in your app's settings or help screen labeled "Support ID", so people can read it out when they contact you. It isn't secret, but it's only useful to someone with access to your Herald console.

## Random samples

Every person also has a **random number**: a whole number from 0 to 9,999. Herald assigns it when it first sees the person and never changes it. People who existed before this shipped got one then. It shows on the person's page under **Attributes**, for every console role.

Use it in a segment to aim a campaign or agent at a random share of people. Each number covers 0.01% of people, so:

- **1% pilot:** Random number is less than 100.
- **10%:** Random number is less than 1000.
- **Two samples that never overlap:** Random number is less than 100 for one, and greater than or equal to 100 and less than 200 for the other.

A person's number stays the same, so they stay in the same sample every time the segment is rebuilt.

## Switching users

Calling `identify` with a different ID while a user is identified switches users. The SDK clears the previous user's token, cards and messages, then identifies the new user and sends the device's push token for them. Call `setUserToken` with the new user's token straight after.

A switch and `reset()` both end the current session and start a new one.

## Edge cases

- **Calls before `configure`.** The SDK keeps up to 1,000 of them, in order, and applies them once `configure` runs. Later calls are dropped with a warning.
- **Verification set to Optional, no token sent.** Events are stored, but no profile, push token or outcome is written for the user. This is the most common reason a profile stays empty.
- **Rotating the ingest token.** Every existing user token stops working. Under **Required**, identified traffic is rejected until apps get new tokens from your backend.

## Related

- [Quickstart: iOS](/docs/overview/quickstart-ios)
- [Quickstart: Android](/docs/overview/quickstart-android)
- [Security and identity verification](/docs/data-and-privacy/security-and-identity-verification)
