When Leanplum start is called for the first time on a device, a new user profile is created and Leanplum starts tracking activity and sessions for the user. If no custom User ID is sent with the
start call, an anonymous user id will be assigned based on the Device ID.
However, you can set your own user id by passing a user id. This way, you can store multiple devices under a singular user id.
Below are the situations when we would recommend setting the User ID
While you are able to pass in a user id on the
Leanplum.start() call, to track user activity early on it is best to set the user ID in Leanplum upon the user logging in.
Once you set the user ID for the first time on a device, the existing profile in Leanplum will updated with that user ID and all previously tracked data remains.
After the initial call, each time you set a different User Id on login, you will end the current User session, and create a new session on the new User. If the new User ID doesn't exist, a new User Profile will be created in Leanplum.
Here's how setting the user ID with
setUserId works with typical registration and login scenarios:
- Register: If a user ID has not been set on this device yet and the supplied user ID does not exist, Leanplum will update the current user profile (created on
start) with the supplied user ID (replacing the device ID).
- Login: If a user ID has not been set on this device yet and the supplied user ID does exist, the current and existing user profiles will be merged. This ensures that users with multiple devices are tracked as one user. If the same user logs back in on this device, no changes will be made to their profile since their user ID is already set.
- Switch user: If a user ID has been set on this device and the supplied user ID is different, the current session will be ended and a new session will be started for the supplied user ID. A user with the supplied user ID will be created if one does not already exist.
Leanplum will not end the session after a user logs out and does not include any methods to do so. All user activity is tracked and attributed to the last logged-in user (set by the
setUserId call). This allows you to track activity in your app even while the user is logged out.
If you want to keep track of which users are logged in and which are logged out, set a user attribute (e.g. logged_in).
Handling User ID on Logout
You do not need to set a different user ID when handling logouts. If you do this, you will create a new user profile in Leanplum and start a new session on that user, which in turn will skew your analytics.