# Events and attributes

> Custom events, screens, purchases, user attributes and subscriptions, with the value types, reserved keys and limits each call follows.

Events record what a user did, and attributes record what is true about them. Herald uses both to build the profile each decision is based on, and in-app messages can trigger on events.

## The calls

| Call | Records | Example |
|---|---|---|
| `track` | A custom event with properties | `workout_completed` with `minutes` |
| `screen` | A screen view with properties | `Plan` |
| `trackPurchase` | A purchase: product, price, currency, quantity, properties | `plus_annual` at 59.99 USD |
| `setAttribute`, `setAttributes` | User attributes, merged into the profile | `favorite_workout` |
| `setEmail`, `setPhone` | The user's email address and phone number | `maya@example.com` |
| `setSubscription` | Opt-in state for push, email or SMS | email subscribed |
| `identify` traits | Attributes sent with the identify call | `plan` |

:::tabs
::tab ios
```swift
Herald.shared.track("workout_completed", properties: ["minutes": 32, "type": "strength"])
Herald.shared.screen("Plan")
Herald.shared.trackPurchase(productId: "plus_annual", price: 59.99, currency: "USD")
Herald.shared.setAttributes(["plan": "plus", "favorite_workout": "strength", "goals": ["sleep", "strength"]])
Herald.shared.setEmail("maya@example.com")
Herald.shared.setPhone("+15551234567")
Herald.shared.setSubscription(.email, .subscribed)
```
::tab android
```kotlin
Herald.track("workout_completed", mapOf("minutes" to 32, "type" to "strength"))
Herald.screen("Plan")
Herald.trackPurchase(productId = "plus_annual", price = 59.99, currency = "USD")
Herald.setAttributes(mapOf("plan" to "plus", "favorite_workout" to "strength", "goals" to listOf("sleep", "strength")))
Herald.setEmail("maya@example.com")
Herald.setPhone("+15551234567")
Herald.setSubscription(SubscriptionChannel.EMAIL, SubscriptionState.SUBSCRIBED)
```
:::

`trackPurchase` has a default `quantity` of 1. Properties default to empty on every call.

## Events and agent goals

A `track` or `trackPurchase` call from an identified, verified user counts toward agent goals. Herald credits it to the most recent message it sent that user, among agents with a goal for that event name.

| Rule | Behavior |
|---|---|
| Event name a goal matches | `track`: the name you pass. `trackPurchase`: `purchase`. |
| Purchase properties | Your properties plus `productId`, `price`, `currency` and `quantity`, so a goal can weight its reward by `price` |
| Which message is credited | The most recent unconverted message sent to the user, from an agent with a goal for the event |
| Attribution window | 48 hours after the send; 30 days for `plan_completed`, `plan_read_day_3` and `plan_read_day_7` |
| Who counts | Identified, verified users only. Anonymous and unverified events are stored but never credited. |
| Retries | An event ID is credited once, however often the SDK resends its batch |
| `screen` calls | Never counted |

Each message is credited for one conversion. Send each goal event from one source: if the server API and the SDK both send it, a second open message can get the credit.

## Value types

| | Event and screen properties | Attributes and traits |
|---|---|---|
| Allowed values | Any JSON value: string, number, boolean, null, array, nested object | String, number, boolean, null, or an array of strings |
| Numbers | Must be finite (no NaN or infinity) | Must be finite |
| iOS type | `HeraldValue`, written as Swift literals | `HeraldValue`, written as Swift literals |
| Android type | `Map<String, Any?>` of strings, numbers, booleans, null, lists and maps | `Map<String, Any?>` of strings, numbers, booleans, null and lists of strings |

## How attributes merge

Each attribute call merges into the user's profile: a key you send replaces the stored value for that key, and keys you do not send stay as they are. Sending `null` removes the key from the profile, so it no longer counts as set in segments (an "exists" condition stops matching). Attributes are written only for an identified, verified user; see [Users and identity](/docs/concepts/users-and-identity).

## Reserved keys

| Key | Set by | Where it goes |
|---|---|---|
| `$email` | `setEmail` | The user's email contact, not the attribute map. Must be a valid address. |
| `$phone` | `setPhone` | The user's SMS contact, not the attribute map. Must be in E.164 format, such as `+15551234567`. |
| `$latitude`, `$longitude` | `setLocation(latitude:longitude:)` | The device record, not the user profile. Only sent when your app calls it. |
| `computed_…` | Herald only | [Computed attributes](/docs/concepts/computed-attributes). Your app can't set these: the event is rejected with `invalid_field`. |

Do not start your own attribute keys with `$`. Passing `nil` or `null` to `setEmail` or `setPhone` sends a null value.

## Limits

These are the server's limits. The SDK checks every call against them before queuing it.

| Field | Rule |
|---|---|
| Event and screen name | 1 to 255 characters |
| Property key | Up to 255 characters |
| Attribute and trait key | 1 to 255 characters |
| External ID (`userId`) | 1 to 200 characters |
| Product ID | 1 to 255 characters |
| Currency | A 3-letter ISO 4217 code, such as `USD` (the SDK converts it to upper case) |
| Quantity | A whole number, 1 or more |
| Price | A finite number |
| Email | A valid address |
| Phone | E.164: a plus sign, then 7 to 15 digits, not starting with 0 |

Lengths count UTF-16 code units, as the server does.

## Invalid calls are rejected on the device

The server rejects a whole batch of up to 100 events if one event breaks a rule. To keep one bad call from costing the rest, the SDK validates each call first. A call that fails is dropped on the device, never queued, and logged:

```text
track ignored: name must not be empty
```

The example is from iOS. The log is at error level on iOS and warning level on Android, so it shows at the default log level on both.

Email and phone values are handled per key. Herald skips an invalid `$email` or `$phone` and keeps the rest of the event. The SDK also checks both on the device: inside `setAttributes`, `setEmail` and `setPhone` it drops only the invalid key, with a warning; inside `identify` traits it rejects the whole call.

## Subscriptions

`setSubscription` records a user's opt-in state for one channel. Herald applies it for identified, verified users.

| State | Effect in Herald |
|---|---|
| `subscribed` / `SUBSCRIBED` | Lifts an earlier unsubscribe for the channel |
| `optedIn` / `OPTED_IN` | Lifts an earlier unsubscribe for the channel |
| `unsubscribed` / `UNSUBSCRIBED` | Suppresses the channel: for push, this device's tokens; for email and SMS, the user's addresses |

A subscribe never lifts a suppression caused by a bounce or a spam complaint. Push permission has its own guide: [Permission prompts](/docs/push-notifications/permission-prompts).

## Related

- [Sessions](/docs/concepts/sessions)
- [Device data collected](/docs/concepts/device-data)
- [Event payload reference](/docs/reference/event-payload)
- [Limits](/docs/reference/limits)
