Skip to main content

Contacts

The SDK ties the device to a contact in Positive User, starting with an anonymous contact and identifying it when the user logs in. How contacts are matched and when they switch: Contacts overview.


Requirements​

  • The SDK installed and initialized, see Installation.
  • Custom contact attributes created in the panel before you send them. Unknown attribute names are ignored.

Setup​

  1. Start a session. Call ping() after initializing the SDK on every launch, or setFcmToken(token), which also pings. The first ping creates an anonymous contact. The SDK stores its key and sends it with every following request.
  2. Identify the contact. At login or registration, call updateContact with a Contact that holds your own user ID and the data you have. Pass the FCM token in the same call.
  3. Log out. Call logout with the FCM token when the user logs out of your app.
// 1. App launch
UserCom.default?.ping()

// 2. Login
val contact = Contact(
userId = "12345",
email = "jane@example.com",
firstName = "Jane",
lastName = "Doe",
attributes = mapOf("plan" to "pro")
)
UserCom.default?.updateContact(contact, currentFcmToken)

// 3. Logout
UserCom.default?.logout(currentFcmToken)
tip

Send your own user ID (userId of the Contact) at login. It is the most reliable way to keep one contact for the same person across reinstalls and devices.

Check

After login, the contact in the panel shows the email and user ID you sent. For a new user ID, the activity from before login stays on the same contact. If a contact with this user ID already exists, the SDK switches to that contact.


Handling​

Results of contact methods​

Every contact method accepts an optional UserComCallback. It runs on the main thread when the request finishes, with null when the backend accepted it, or with a UserComError when the backend rejected it, all retries failed or the data could not be sent. Requests are queued and survive the app going offline and being closed, see Delivery and Retries.

Contact data is sent once​

What you pass to updateContact is added to the contact. The SDK keeps no copy, so later pings do not repeat it.

Switching contacts​

A userId different from the current contact's switches to the contact with that user ID, or creates one. That is why updateContact belongs in your login flow, with the push token, so the identity and the token reach the backend in one request.

Removing a value​

A field you leave null keeps its value on the contact. The SDK cannot clear a value. Clear it in the panel or through the REST API.

Attributes​

attributes takes your own contact attributes and standard ones such as gender or score. Your own attributes must exist in the panel, other names are dropped there without an error. The typed fields of Contact win over the same keys in attributes.

Values must be JSON values, see Attribute value types. A value JSON cannot hold, for example a Date, a Uri or a Double that is NaN, or an empty attribute name ends with InvalidData and nothing is sent.

warning

A value whose type does not match the attribute's type makes the backend reject the request, and the callback reports an error.

The contact key and backups​

The contact key is stored in the app's no backup storage. When Android restores an app backup on a new phone, the new installation starts with a new anonymous contact. Identify it with updateContact at login, as on any new device.


Methods​

ping​

Creates or updates the contact with the current device data. The first successful ping creates an anonymous contact and sets userKey.

Optional unless marked required

fcmTokenString?
An FCM token to store and send with this ping. Default null keeps the stored token.
callbackUserComCallback?
Called with the result on the main thread.
UserCom.default?.ping { error ->
if (error != null) Log.w("App", "Ping failed: ${error.message}")
}

A ping answered without a contact key ends with NotCollectingData: the app does not collect data, check the app settings in the panel.

updateContact​

Adds data to the current contact in one request.

Optional unless marked required

contactContactrequired
The data to add, see the table below.
fcmTokenString?
An FCM token sent in the same request and stored for later pings. Default null.
callbackUserComCallback?
Called with the result on the main thread.

Contact fields, all optional:

FieldTypeSent as
userIdString?user_id, your own user ID
emailString?email
firstNameString?first_name
lastNameString?last_name
phoneNumberString?phone_number
attributesMap<String, Any>Each key as a contact attribute. Default empty.
val contact = Contact(
userId = "42",
email = "jane@example.com",
firstName = "Jane",
attributes = mapOf("plan" to "pro")
)
UserCom.default?.updateContact(contact, fcmToken) { error ->
// contact updated when error is null
}

In Java, Contact has constructors with the fields in the order of the table, and a setter for each field.

logout​

Ends the session of the current contact and starts a new anonymous one, in this order:

  1. records a logout event on the contact who is leaving,
  2. deletes the contact key and a waiting in-app message on the device, and drops pings still waiting in the queue,
  3. sends a ping with the push token, which creates a new anonymous contact,
  4. after that ping, removes the given FCM token from the contact who is leaving.

Optional unless marked required

fcmTokenString?
The token to remove from the contact who is leaving. The anonymous contact gets this token. Default null skips the removal, and the anonymous contact gets the token the SDK has stored.
callbackUserComCallback?
Called on the main thread with null when the new ping and the token removal succeeded.
UserCom.default?.logout(currentFcmToken) { error ->
// a new anonymous contact is ready
}

Local data is deleted even when the network requests fail. Events collected offline before logout are still sent, for the contact who logged out. Contact updates of that contact still waiting in the queue end with LoggedOut. The anonymous contact keeps the push token, so the device keeps receiving push notifications. A push notification sent to the logged out contact does not reach the device. Turning push notifications off after logout is up to your app.

userKey​

The key of the current contact in Positive User, read only. It is null until the first successful ping and while logout is in progress, and changes after logout and when updateContact switches to another contact. It is not your own user ID.

val contactKey = UserCom.default?.userKey

Deprecated methods​

register(Customer, CustomerUpdateCallback) and the Customer class are deprecated since 2.0.0 and stay until 3.0. register calls updateContact and does not keep a copy of the data. Replace them with updateContact:

1.x CustomerContact
id(userId)userId
email(email)email
firstName(firstName)firstName
lastName(lastName)lastName
attr(name, value)attributes
RegisterResponse.getKey()userKey

Common errors​

SymptomCauseFix
Events go to a different contact than the one you look at in the panelYour app also identifies the user through the REST API or another integration.Identify the contact only through the SDK on the device. See Contacts overview.
A new contact appears at every loginA different userId is sent for the same person.Send the same stable user ID every time.
Custom attributes do not appearThe attribute does not exist in the panel.Create the attribute in the panel first.
A value set to null is still on the contactnull keeps the value, it does not clear it.Clear the attribute in the panel or through the REST API.
updateContact fails with InvalidDataAn attribute value JSON cannot hold, for example a Date, or an empty attribute name.Send dates as ISO 8601 strings.
A restored backup on a new phone starts a new contactExpected from 2.0.0: the contact key is not part of the backup.Call updateContact at login.
Contact data from the staging workspace appears after switching to productionThe contact key is kept when baseUrl changes.Clear the app data or reinstall the app before switching baseUrl.