If your app has its own login system that you want to track users with, you can call setUserId at any time:
[[Amplitude] instance] setUserId:@"USER_ID"];
You can also add the User ID as an argument to the init call:
[[Amplitude] instance] initializeApiKey:@"API_KEY" userId:@"USER_ID"];
Amplitude.getInstance().initialize(this, "API_KEY", "USER_ID");
You should not assign users a User ID that could change as each unique User ID is interpreted as a unique user in Amplitude. Please see our article on how we identify and count unique users for further information.
A user's data will be merged on the backend so that any events up to that point from the same browser will be tracked under the same user. If a user logs out or you want to log the events under an anonymous user, you will need to:
- Set the userId to null.
- Regenerate a new deviceId.
After doing that, events coming from the current user/device will appear as a brand new user in Amplitude. Note: If you choose to do this, you will not be able to see that the two users were using the same device. Here is an example:
amplitude.getInstance().setUserId(null); // not string 'null' amplitude.getInstance().regenerateDeviceId();
[[Amplitude instance] setUserId:nil]; // not string nil [[Amplitude instance] regenerateDeviceId];
Updated 5 months ago