Search Unity

Question Standard events in wrong environment

Discussion in 'Unity Analytics' started by arproducties, Nov 3, 2022.

  1. arproducties

    arproducties

    Joined:
    Sep 7, 2017
    Posts:
    14
    I am implementing Unity Analytics inside our project. I have a script that initializes the unity services with a given environment. I have two environments, production and development. If I am in the editor or using a development build, the environment set is development. The problem is that the editor still triggers
    gameStarted and gameEnded events for production. This is the code I am using. Am I missing something? My custom events that are triggerd in the editor only show in the dev environment, like its supposed to.

    Code (CSharp):
    1.        
    2.         private async void Awake()
    3.         {
    4.             DontDestroyOnLoad(this);
    5.             InitializationOptions options = new InitializationOptions();
    6.  
    7. #if UNITY_EDITOR || DEVELOPMENT_BUILD
    8.             options.SetEnvironmentName(DEV_ENVIRONMENT);
    9. #else
    10.             options.SetEnvironmentName(PROD_ENVIRONMENT);
    11. #endif
    12.             try
    13.             {
    14.                 await UnityServices.InitializeAsync(options);
    15.                 List<string> consentIdentifiers = await AnalyticsService.Instance.CheckForRequiredConsents();
    16.             }
    17.             catch (ConsentCheckException e)
    18.             {
    19.                 Debug.LogError(e.Reason);
    20.             }
    21.         }
     
  2. PhilG-Unity

    PhilG-Unity

    Unity Technologies

    Joined:
    Mar 17, 2022
    Posts:
    26
    Hi,

    Thanks for your question.

    In the snippet above where are DEV_ENVIRONMENT & PROD_ENVIRONMENT getting their values? Scripting Define Symbols setup in Project Settings > Payer > Other Settings > Script Compilation?

    Are you putting the human readable name of the envs in there or the env ID?

    I tested just now on a project of mine and was able to send events to my non-default environment using the human readable name with SetEnvironmentName(). Using the env ID resulted in an error from the SDK when trying to send an event which I believe is as designed. I didn't get any events in my default "production" env.

    Have you enabled the more verbose debug logging for the Analytics SDK? That can help track down issues.
     
    Last edited: Nov 3, 2022
    SebT_Unity likes this.
  3. arproducties

    arproducties

    Joined:
    Sep 7, 2017
    Posts:
    14
    I defined the values as strings at the top of my script, like this.

    private const string DEV_ENVIRONMENT = "development";
    private const string PROD_ENVIRONMENT = "production";

    I know this works because the custom events I trigger appear in the correct environment. I will look into the verbose logging.
     
  4. arproducties

    arproducties

    Joined:
    Sep 7, 2017
    Posts:
    14
    I just tested with verbose logging on and I can see that the initialization of the environment happens before the first event is called. So it shouldn't be possible that events in the editor are logged to production environment. It logs the gameStarted, gameRunning and gameEnded on both environments. But sdkStart and custom events only log for the environment selected like it's supposed to
     
  5. PhilG-Unity

    PhilG-Unity

    Unity Technologies

    Joined:
    Mar 17, 2022
    Posts:
    26
    Could you tell me the Unity Dashboard Organization & Project name please?

    You can send them to me by direct message if you'd prefer not to have them in a public reply.
     
  6. arproducties

    arproducties

    Joined:
    Sep 7, 2017
    Posts:
    14
    Yes it is:

    arproducties
    BioScope
     
  7. PhilG-Unity

    PhilG-Unity

    Unity Technologies

    Joined:
    Mar 17, 2022
    Posts:
    26
    Thanks.

    TLDR - It looks like you are sending both Legacy Analytics and UGS Analytics Events. Legacy Analytics events are unaffected by SetEnvironmentName() so end up in your default env, which is "production".


    Looking at the event JSON in Event Browser for the events in the "production" environment one thing stands out: sdkMethod: UA1_SDKLESS

    You can view the raw JSON of the events by going to Unity Dashboard > Analytics > Event Browser and then clicking the <> icon at the end of the row for any event.

    sdkMethod:UA1_SDKLESS indicates these events are being sent from your Unity project by Unity's Legacy Analytics system, not UGS Analytic's SDK. Legacy Analytics is our older analytics service which is in the process of being deprecated and will eventually be fully shutdown. As part of that deprecation process the standard event types originating from Legacy Analytics are mapped over into your UGS Analytics Dashboard. These events that are mapped over from Legacy Analytics are all identified with sdkMethod: UA1_SDKLESS. Your code snippet above changing environment only applies to UGS Analytics and will have no effect on Legacy Analytics.

    If you check the same thing for events in your "development" environment in Event Browser you'll see that for those it shows something like this (depending on the event type): sdkMethod": "com.unity.services.analytics.Events.Startup". This sdkMethod value is what you should see for UGS Analytics events send from the UGS Analytics SDK.

    So what I think is happening is that you have both Legacy Analytics and UGS Analytics enabled. When you play your project Legacy Analytics is automatically sending it's standard events (with sdkMethod:UA1_SDKLESS) and since Legacy is not environment aware at all and SetEnvironmentName() doesn't affect it ... those events are all mapped through to your default environment in UGS Analytics, which is "production". While this is happening you're also initializing UGS Analytics, correctly setting the environment and then the UGS SDK starts sending it's own events (with sdkMethod": "com.unity.services.analytics.Events.XXXX"). Since SetEnvironmentName() works for these UGS SDK events these ones are correctly ending up in the right environment as per your variables.

    What I'd like you to do is check if your Unity project still has Legacy Analytics enabled on it. This is in Project Settings > Services > Legacy Analytics. If that is toggled to ON then Legacy will be sending it's standard event types automatically and that fits with my theory above. If you're not actually using Legacy Analytics you can toggle that to OFF. Note that due to the deprecation of Legacy Analytics ... once you toggle it to OFF in your Editor the Legacy Analytics section will disappear from your Unity Dashboard.

    Let me know once you've had a look.
     
    tommo217 likes this.
  8. PhilG-Unity

    PhilG-Unity

    Unity Technologies

    Joined:
    Mar 17, 2022
    Posts:
    26
    Also, if you're on Unity 2019.X Legacy Analytics is not under Project Settings, it's under Windows > General > Services > Analytics I think.
     
  9. PhilG-Unity

    PhilG-Unity

    Unity Technologies

    Joined:
    Mar 17, 2022
    Posts:
    26
  10. arproducties

    arproducties

    Joined:
    Sep 7, 2017
    Posts:
    14
    Thanks for the detailed explanation. I checked for legacy analytics but couldn't find it. I did notice we use an older version of the iap package. Is it possible that this still sends legacy events?
     
  11. PhilG-Unity

    PhilG-Unity

    Unity Technologies

    Joined:
    Mar 17, 2022
    Posts:
    26
    Hi again,

    Apologies for the delay in replying.

    Regarding the IAP package, yes, absolutely. It does have a dependency on Analytics and will result in events being sent. In the newer versions of the package it uses the UGS Analytics system which AFAIK should respect the SetEnvironmentName() call ... but in older versions of the IAP plugin it does use Legacy Analytics and Legacy has no concept of environments so they will end up in your "production" environment by default.

    If you don't see Legacy Analytics enabled in your Project settings I think the IAP stuff is probably the likely source of these events.
     
    Last edited: Nov 11, 2022
    arproducties likes this.