Record events with a custom user ID

By default, Analytics SDK uses the UGS installation ID as the user ID for each event. This ID is different for each installation on each device which can make it difficult to track the same user across multiple devices, or to join data between the Analytics platform and your own internal sources.

To help with this situation, you can use the External User ID feature to set a specific user ID that you control.

  • If you want Unity to generate user IDs for Analytics, no action is required.
  • If you want to use a specific ID to match with external data (for example, other analytics sources) then you need to set an external user ID.

You can set the external user ID to override the Analytics user ID at any point during the application lifecycle using this code:

using Unity.Services.Core;

void SetExternalUserId()
{
	UnityServices.ExternalUserId = "some-user-id";
}

Warning: Changing the user ID contributes a new user to metrics such as Monthly Active User counts, which might affect billing. For more information, see our billing page. To avoid this, ensure you set an external user ID before enabling data collection.

Even if you set an external user ID, the Analytics SDK still records the installation ID in the unityInstallationID parameter. Likewise, if the player has signed in using Unity Authentication, the authenticated player ID is included in the unityPlayerID parameter. These are not affected by the use of an external user ID.

Any events recorded after you set a custom user ID use the given value. Events recorded previously are not updated and retain the original value.

Note that the SDK doesn't save the external user ID value. If you want to keep a consistent custom ID for a given user over time, you need to save it manually (for example using PlayerPrefs) and set it each time your app starts.

You can revert to using the installation ID for the user ID by setting the ExternalUserId to a null or empty string:

using Unity.Services.Core;

void ClearExternalUserId()
{
	UnityServices.ExternalUserId = String.Empty;
}

If you are not sure what ID is current in use, you can call the AnalyticsService.Instance.GetAnalyticsUserID() method to retrieve the ID that the Analytics SDK is currently recording events against. It returns either the installation ID or the external user ID that you have set.