Sending Events
The SDK records what contacts do in your app as events on their timeline, which you can use in segments and automations. How events are stored and which names and attributes work well: Events.
Requirements
- The SDK installed and initialized, see Installation.
- A successful
ping()in this installation. Events need the contact key that the first ping returns. See Contacts.
Event types
| Type | Method | When to use |
|---|---|---|
| Custom events | sendEvent(name, attributes, callback) | Anything specific to your app: a feature used, a form submitted, a purchase step |
| Screen events | trackScreen(name, callback) | A screen the contact opened |
| Product events | sendProductEvent(productId, eventType, attributes, callback) | Commerce actions on a product: view, add to cart, purchase, refund and more |
attributes and callback are optional in every method.
Setup
- Make sure
ping()runs at app launch, before the first event. - Pick stable event names and a fixed set of attribute names. See Naming rules.
- Send events with the method for the event type.
UserCom.default?.sendEvent("subscription_started", mapOf("plan" to "pro")) { error ->
if (error != null) Log.w("App", "Event failed: ${error.message}")
}
The event appears on the contact's timeline in the panel, and the callback gets null.
Handling
Results
Every event method accepts an optional UserComCallback. It gets null on success and a UserComError on failure:
| Result | Meaning |
|---|---|
null | The backend accepted the event. |
NotIdentified | No contact yet. The event was not queued. Call ping() first. |
InvalidName or InvalidData | The name or an attribute value cannot be sent. Nothing was sent. |
Rejected | The backend rejected the event: a 4xx at once, a 5xx or 429 after all retries. |
Network | The connection failed on every attempt. See Delivery and Retries. |
The callback is not called for requests that are still waiting in the queue when the app process ends. They are sent after the next launch.
Callback thread
Every callback runs on the main thread, and never before the call returns. This includes errors the SDK detects before any request. You can update the UI directly in the callback.
Invalid input never throws. Every problem ends with a UserComError in the callback and in UserCom.errorHandler, and the SDK never crashes your app because of the data you pass.
Errors
Every failure is a UserComError, a sealed class with these subclasses:
| Error | code | Meaning | Request sent |
|---|---|---|---|
NotIdentified | 1 | No contact yet, call ping() first. | No |
InvalidName | 2 | The event name, screen name or product ID is not accepted. | No |
InvalidData | 3 | An attribute value JSON cannot hold, or an empty attribute name. | No |
Rejected | 4 | The server refused the request. statusCode holds the HTTP status. | Yes |
Network | 5 | The connection failed on every attempt. | Yes or no |
NotCollectingData | 6 | A ping was answered without a contact key: the app does not collect data. Check the panel. | Yes |
InvalidConfiguration | 7 | The SDK is not configured: an empty API key, or a base URL that is not a URL. | No |
LoggedOut | 8 | The request was dropped because the user logged out before it was sent. | No |
UserCom.errorHandler receives every error on the main thread, after the callback of the call. It also receives errors from calls without a callback, from push and in-app message reports, and a click from an email link that could not be reported. Use it to log SDK errors in one place:
UserCom.errorHandler = UserComErrorHandler { error ->
FirebaseCrashlytics.getInstance().recordException(error)
}
In Java:
UserCom.setErrorHandler(error -> {
if (error instanceof UserComError.Rejected) {
int status = ((UserComError.Rejected) error).getStatusCode();
}
});
A request waiting offline in the queue is not an error: its result comes when it is sent or finally dropped.
Naming rules
- Use the same spelling and letter case for an event name every time.
Purchaseandpurchaseare treated as the same name, and the second spelling is rejected by the backend. - An event name must not be empty, longer than 255 characters or contain HTML. A screen name is at most 128 characters. The SDK checks this and reports
InvalidNamewithout a request. - Use a fixed set of attribute names with stable value types. Every new attribute name creates a new event attribute, and its first value sets its type for good.
- Do not send empty attribute names.
nullvalues are skipped.
Attribute value types
Attribute values must be JSON values:
| Type | Example |
|---|---|
String | "checkout" |
Int, Long | 3 |
Double, Float | 49.99 |
Boolean | true |
List or array | listOf("red", "blue") |
Map | mapOf("size" to "42") |
| Date as ISO 8601 string | "2026-09-28T12:51:33Z" |
Any other value, for example a Date, a Uri or a Double that is NaN, ends with InvalidData and nothing is sent. Send dates as ISO 8601 strings.
The SDK adds the contact key and the time of the event to every request. Events created offline keep the time they were created.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
Callback reports NotIdentified | The event was sent before the first successful ping. | Call ping() at launch and send events after it succeeds. |
Callback reports InvalidName or InvalidData | The name breaks the naming rules, or a value is not a JSON value. | Fix the name or the value. |
Callback reports InvalidConfiguration | The base URL passed to initialize has no https://, or the API key is empty. | Pass the full workspace address. |
| Events are not on the contact you expect | The device is tied to a different contact than the one you look at. | Compare UserCom.default?.userKey with the contact key in the panel. See Troubleshooting. |
| The event is recorded, but some attributes are missing | The attribute value has a different type than the first value ever sent for it, or the name is invalid. | Keep attribute types stable. |
| An event name never appears in the panel | The same name was used before with different letter case. | Use one exact spelling. |
| No screen events at all | Screens are not tracked automatically. | Call trackScreen(name). |