Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question UnityServices.ExternalUserId

Discussion in 'Unity Analytics' started by PieterAlbers, Jan 23, 2023.

  1. PieterAlbers

    PieterAlbers

    Joined:
    Dec 2, 2014
    Posts:
    250
    Hi!

    I am not sure if I am using the UnityServices.ExternalUserId correctly.
    Our app/game is using their own set of user ids and I want to use those for analytic events.

    I tried updating the UnityServices.ExternalUserId with our own user id - but it doesnt show up.

    When do you need to set it? Before or after initializing the services?
    Both are not working for me.

    Is there a limit no the length of the user id? Anything?

    Pieter
     
  2. PieterAlbers

    PieterAlbers

    Joined:
    Dec 2, 2014
    Posts:
    250
    Anyone?

    I just can not get it to work. I am expecting to have my externalUserID either show up as the actual user id OR somewhere in the event content - but it is shown in neither location.
     
  3. RandolfKlemola

    RandolfKlemola

    Unity Technologies

    Joined:
    May 1, 2020
    Posts:
    130
    Hey Pieter Albers,

    Thanks for reaching out! The key [UnityServices.ExternalUserId] being used to also set the Analytics userID is not yet integrated into the latest Analytics SDK, but should be added in Analytics v4.4.0. In the meantime, to set your userID, you could this small bit from the Custom user ID Support page in the Analytics documentation:

    using Unity.Services.Core.Analytics;
    var options = new InitializationOptions();
    options.SetAnalyticsUserId("some-user-id");

    await UnityServices.InitializeAsync(options);

    Let me know if that helps!

    Best,
    Randy
     
  4. PieterAlbers

    PieterAlbers

    Joined:
    Dec 2, 2014
    Posts:
    250
    Hi Randy,

    Thanks for your help and clarification - much appreciated.
    Will use the SetAnalyticsUserId() method for now - thanks!

    What would you suggest on how to change the userId after the services has been initialized?

    Best,
    Pieter
     
    mertbsn likes this.
  5. mertbsn

    mertbsn

    Joined:
    Jan 22, 2017
    Posts:
    6
    I also need to update userId on the fly (on user login and logout) and reinitialize UnityServices, but once the userId is set once, it doesn't seem to get reset.

    Code (CSharp):
    1. var options = new InitializationOptions();
    2. options.SetAnalyticsUserId("some-user-id");
    3.      
    4. await UnityServices.InitializeAsync(options);
    5. Debug.Log(AnalyticsService.Instance.GetAnalyticsUserID()); // Prints "some-user-id"
    6.  
    7. options.SetAnalyticsUserId("another-user-id");
    8. await UnityServices.InitializeAsync(options);
    9. Debug.Log(AnalyticsService.Instance.GetAnalyticsUserID()); // Still prints "some-user-id"
    Any help would be appreciated.
     
    PieterAlbers likes this.
  6. RandolfKlemola

    RandolfKlemola

    Unity Technologies

    Joined:
    May 1, 2020
    Posts:
    130
    Hi again Pieter, and hi mcbsn,

    Unfortunately, until UnityServices.ExternalUserId is officially synchronized with the AnalyticsUserID, there is no way to change the userID on the fly. An option might be to delay initialization until you have the userID you need. Future versions of the SDK should include this option.

    Best,
    Randy
     
    PieterAlbers likes this.
  7. pichikala

    pichikala

    Joined:
    Sep 18, 2021
    Posts:
    5
    Can you please update us when the new SDK version will be out?
    Thank you
     
  8. RandolfKlemola

    RandolfKlemola

    Unity Technologies

    Joined:
    May 1, 2020
    Posts:
    130
    Hi pichikala,

    Yes I can update here!

    Randy
     
    mertbsn likes this.
  9. RandolfKlemola

    RandolfKlemola

    Unity Technologies

    Joined:
    May 1, 2020
    Posts:
    130
    Hi @pichikala , @PieterAlbers , @mertbsn

    Thanks for your patience! Unity Analytics SDK 4.4.1, which can be found in the Package Manager, allows you to change USER_ID using:

    UnityServices.ExternalUserId = "TextUserID";

    Best,
    Randy
     
    mertbsn and PieterAlbers like this.
  10. PieterAlbers

    PieterAlbers

    Joined:
    Dec 2, 2014
    Posts:
    250
    that is awesome news! @RandolfKlemola
    I appreciate it you keeping us updated!
     
    mertbsn and RandolfKlemola like this.
  11. Trunksome

    Trunksome

    Joined:
    Jan 28, 2022
    Posts:
    17
    Hi there! I've read up on UserId and ExternalUserId and I'm a bit confused.

    The docs still mention both: https://docs.unity.com/analytics/en/manual/CustomUserIDSupport --> "If you want to use a custom ID to match with external data (for example, other analytics sources, custom user IDs) then you’ll need to set the AnalyticsUserID when initializing UnityServices."

    But now after looking into the code, it seems that SetAnalyticsUserId in the InitializationOptions is deprecated and replaced by SetExternalUserId. Maybe it would be good to clarify this in the docs.

    There are still some things it would be great to get some clarification around:
    1. Let's say a new user downloads my game. If I call "SetExternalUserId" after the user has registered in with my backend (in my case thats when i get the profileUUID), does this mean that my NewUsers/WAU/MAU metrics are increased by 2? First some events with the generated userID, then with the externalUserId? Or is this case being properly taken care of?
    2. When using the REST API to submit events, can I use the externalUserId for "userID" to reference that user?
    Thank you!
     
  12. RandolfKlemola

    RandolfKlemola

    Unity Technologies

    Joined:
    May 1, 2020
    Posts:
    130
    Hi @Trunksome ,

    That's our error. We didn't update the documentation as fast as we updated the SDK. It has been updated since your post!

    As for your questions, responses below
    • Let's say a new user downloads my game. If I call "SetExternalUserId" after the user has registered in with my backend (in my case thats when i get the profileUUID), does this mean that my NewUsers/WAU/MAU metrics are increased by 2? First some events with the generated userID, then with the externalUserId? Or is this case being properly taken care of?
      • If you allow your users to submit multiple userIDs, the data will reflect that. They will have 2 userIDs associated with them and will count as 2 for that day.
    • When using the REST API to submit events, can I use the externalUserId for "userID" to reference that user?
      • Yes, that would makes sense to me!
    Randy