Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Resolved UGS Analytics custom events not shown

Discussion in 'Unity Analytics' started by Bryarey, Apr 19, 2023.

  1. Bryarey

    Bryarey

    Joined:
    Aug 24, 2014
    Posts:
    25
    Hi there! Having a hard time figuring out UGS Analytics custom events.
    What I have:
    1. project is set up, I see default analytics events.
    2. Set up few custom events with custom parameters, created 2 funnels.
    3. In my code, I am defining a dictionary with arguments. I am not feed default parameters to events - only my custom ones, is it correct?
    4. In both editor and android build I see in the log that my custom event was sent with result AnalyticsResult.Ok
    5. In dashboard, I don't see any custom events in Events Browser nor DataExplorer nor Funnels... Also no Invalid events.

    6. Don't think it can really affect, but I also have GameAnalytics enabled - so maybe t conflicts with UGS analytics?

    What I am doing wrong?

    My code:

    Code (CSharp):
    1.  
    2. public static void LevelStarted(int chapter, int level)
    3. {
    4.     var unityEventData = new Dictionary<string, object>
    5.     {
    6.         {"Chapter", chapter},
    7.         {"Level", level},
    8.         {"PlayerId", UserId}
    9.     };
    10.     SendUnityCustomEvent("LevelStarted", unityEventData);
    11. }
    12.  
    13. private static void SendUnityCustomEvent(string name, Dictionary<string, object> data)
    14.         {
    15.             var result = UnityEngine.Analytics.Analytics.CustomEvent(name, data);
    16.             if (result != AnalyticsResult.Ok)
    17.             {
    18.                 Debug.LogError(">>>>>> Analytics event " + name + " failed with result: " + result);
    19.             }
    20.             else
    21.             {
    22.                 Debug.Log(">>>>>> Analytics event " + name + " sent with result: " + result);
    23.             }
    24.         }
    25.  
     
  2. Julian-Unity3D

    Julian-Unity3D

    Unity Technologies

    Joined:
    Apr 28, 2022
    Posts:
    192
    It seems you are using legacy events.

    See this documentation for UGS Analytics Custom Events: Record Custom Events (unity.com)
    Code (CSharp):
    1. // Send custom event
    2. Dictionary<string, object> parameters = new Dictionary<string, object>()
    3. {
    4.     { "fabulousString", "hello there" },
    5.     { "sparklingInt", 1337 },
    6.     { "spectacularFloat", 0.451f },
    7.     { "peculiarBool", true },
    8. };
    9. // The ‘myEvent’ event will get queued up and sent every minute
    10. AnalyticsService.Instance.CustomData("myEvent", parameters);
    11.  
    12. // Optional - You can call Events.Flush() to send the event immediately
    13. AnalyticsService.Instance.Flush();
    As for your use-case, you could potentially do something like this, however this is untested so may need a little work.
    Code (CSharp):
    1. public static void LevelStarted(int chapter, int level)
    2. {
    3.     Dictionary<string, object> eventData = new Dictionary<string, object>();
    4.     eventData.Add("Chapter", chapter);
    5.     eventData.Add("Level", level);
    6.     eventData.Add("PlayerId", UserId);
    7.  
    8.     // The ‘LevelStarted’ event will get queued up and sent every minute
    9.     AnalyticsService.Instance.CustomEvent("LevelStarted", eventData);
    10. }
    11.  
     
    Last edited: Apr 21, 2023
    VOXELIUM and Bryarey like this.
  3. Bryarey

    Bryarey

    Joined:
    Aug 24, 2014
    Posts:
    25
    Thanks, finally see it works!
     
    Julian-Unity3D likes this.
  4. Bryarey

    Bryarey

    Joined:
    Aug 24, 2014
    Posts:
    25
    Hi there! After having UGS Analytics perfectly work for a while, I have created a new build (without any major changes related to analytics itself) - and have everything stuck. My errors during initialization:
    1:
    Error Unity ServicesInitializationException: The Analytics service has not been initialized. Please initialize Unity Services.
    2:
    Error Unity [ServicesCore]: An error occured while trying to get the project configuration for services.
    3:
    Error Unity Can not initialize UGS Analytics! Consent error, reason: DeserializationIssue

    - My UGS Analytics version from package manager is 4.4.2 (and I can't get the docs for exactly this version)
    - My unity engine version is 2020.3.38f1

    How I initialize:

    await UnityServices.InitializeAsync();

    // checking consent identifiers and showing popup, if needed - no errors here

    //if got PIPL consent:
    AnalyticsService.Instance.ProvideOptInConsent(PIPL_CONSENT_IDENTIFIER, result);

    //after all the consent manipulations
    _ = AnalyticsService.Instance.SetAnalyticsEnabled(true);

    Full code:

    Code (CSharp):
    1. public UgsAnalytics(PopupManager popupManager)
    2. {
    3.     _popupManager = popupManager;
    4.     if (PlayerPrefs.HasKey(ANALYTICS_OPT_OUT_KEY) && PlayerPrefs.GetInt(ANALYTICS_OPT_OUT_KEY) == 1)
    5.     {
    6.         AnalyticsService.Instance.OptOut();
    7.     }
    8.  
    9.     InitializeUnityAnalyticsAsync();
    10. }
    11.  
    12. private async void InitializeUnityAnalyticsAsync()
    13. {
    14.     try
    15.     {
    16.         await UnityServices.InitializeAsync();
    17.         List<string> consentIdentifiers = await AnalyticsService.Instance.CheckForRequiredConsents();
    18.         foreach (var identifier in consentIdentifiers)
    19.         {
    20.             if (identifier == PIPL_CONSENT_IDENTIFIER)
    21.             {
    22.                 CheckUserConsent();
    23.             }      
    24.         }
    25.     }
    26.     catch (ConsentCheckException e)
    27.     {
    28.         Debug.LogError("Can not initialize UGS Analytics! Consent error, reason: " + e.Reason);
    29.     }
    30.     _ = AnalyticsService.Instance.SetAnalyticsEnabled(true);
    31. }
    32.  
    33. public void CheckUserConsent()
    34. {
    35.     try
    36.     {
    37.         var consentPopup = _popupManager.GetPopup<UgsAnalyticsConsentController, UgsAnalyticsConsentController.Factory>();
    38.         consentPopup.OnResult += OnConsentResult;
    39.         consentPopup.Show();
    40.     }
    41.     catch (ConsentCheckException e)
    42.     {
    43.         Debug.LogError("Error during getting PIPL consent, reason: " + e.Reason);
    44.     }
    45.  
    46. }
    47.  
    48. private void OnConsentResult(bool result)
    49. {
    50.     AnalyticsService.Instance.ProvideOptInConsent(PIPL_CONSENT_IDENTIFIER, result);
    51. }