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 Custom Event AnalyticsResults always comes back as NotInitialized

Discussion in 'Unity Analytics' started by Shack_Man, Nov 24, 2022.

  1. Shack_Man

    Shack_Man

    Joined:
    Jun 7, 2017
    Posts:
    374
    I've set up Analytics but when trying to send a custom event, the result always comes back as NotInitialized even though analytics state says initialized.


    Code (CSharp):
    1.    async void Start()
    2.     {
    3.         await UnityServices.InitializeAsync();
    4.         Debug.Log(UnityServices.State);
    5.         List<string> consentIdentifiers = await AnalyticsService.Instance.CheckForRequiredConsents();
    6.         if (consentIdentifiers.Count > 0)
    7.         {
    8.             consentIdentifier = consentIdentifiers[0];
    9.             isOptInConsentRequired = consentIdentifier == "pipl";
    10.         }
    11.    
    12.     }
    13.     [ContextMenu("tr")]
    14.     void Trigger()
    15.     {
    16.         AnalyticsResult result = Analytics.CustomEvent("score");
    17.  
    18.         Debug.Log("analyticsResult: " + result);
    19.         Debug.Log(UnityServices.State);
    20.     }


    I tried to add the code in the UGS sample project in an empty scene and it works fine, even if I leave out the Start method and just send the event directly. Analytics events are enabled in the dashboard.

    On a side note, Analytics no longer shows up in the Services Window, I could swear I saw it there just a few days ago. Though I updated everything once the error started.


    And of course no data gets send to the dashboard. Normal events like gameStarted and Ended are being sent just fine, and I can log in the user anonymously and get the ID. So I guess I'm messing something up with the custom event? Just so weird that it works in the sample project without calling any methods before hand.

    upload_2022-11-24_21-49-11.png
     
    Last edited: Nov 24, 2022
    kmowers likes this.
  2. PhilG-Unity

    PhilG-Unity

    Unity Technologies

    Joined:
    Mar 17, 2022
    Posts:
    26
    Hey there,

    Thanks for reaching out with this question.

    Can you tell me what version of the Analytics SDK package you're using? It looks to me like you may be using an older API for sending the Custom Event that is not applicable anymore.

    And as a result your AnalyticsResult variable is not being initialized because the method you're using isn't returning anything.

    When using UGS Analytics with recent versions of the SDK you should be using
    Code (CSharp):
    1. Analytics.Instance.CustomData()
    to send Custom Events, with a Dictionary object containing key value pairs for your custom parameter names & values.

    You can find an example of it documented here:
    https://docs.unity.com/analytics/RecordingCustomEvents.html

    Could you try using the syntax in the example above? Also, just to be sure, you have created the Custom Event named 'score' in your Dashboard's Event Manager? Custom Events need to be defined / created in the Dashboard before they can be sent from code.

    Keep me posted on how it goes.

    Best,
     
    Last edited: Nov 24, 2022
    Ferroni and kmowers like this.
  3. Shack_Man

    Shack_Man

    Joined:
    Jun 7, 2017
    Posts:
    374
    Thanks for the fast reply, it's working now! Things seem to be changing quite fast... I'm using Analytics Version 4.2.0 btw and that is the same version used in the UGS sample package (I cloned today). It just through me off that the old method I posted aboved seemed to work in there (though of course all I had was the AnalyticsResult coming back as "OK".

    Thanks again!
     
    Ferroni and PhilG-Unity like this.
  4. Meltdown

    Meltdown

    Joined:
    Oct 13, 2010
    Posts:
    5,840
    Yeah it gets confusing asf, I ran into the same mistake you made when I recently integrated UGS, a lot of Unity's links in many places still point to the old analytics.

    The docs you want to reference are at
    https://docs.unity.com/analytics/RecordingCustomEvents.html
     
    Ferroni likes this.
  5. Shack_Man

    Shack_Man

    Joined:
    Jun 7, 2017
    Posts:
    374
    I always remind myself that this is part of progress and the alternative is using Unity 1.0 :) Good thing we have a forum and Unity folks replying so fast.
     
  6. PhilG-Unity

    PhilG-Unity

    Unity Technologies

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

    Sorry to hear you've found the docs confusing. We appreciate the feedback.

    If you can provide links to official Unity UGS Analytics documentation that is still showing the old API I'll do what I can to raise awareness here internally that this content needs to be updated. It's also important to make sure the docs you're looking at are not for Legacy Analytics, our previous (now deprecated) Analytics SDK.

    For third-party content unfortunately there's not much we can do.

    I know we have an offiicial UGS Bootcamp video for getting started with UGS Analytics which was made while the service was still in Beta and it does unfortunately demonstrate the outdated API. The high-level overview of how to work with UGS Analytics in the video is still useful but for code I'd always urge folks to consult the official UGS Analytics documentation.

    Cheers,
     
  7. Shack_Man

    Shack_Man

    Joined:
    Jun 7, 2017
    Posts:
    374

    Videos showing old stuff just happens, good thing there are comments on Youtube where those issues are often discussed and solved.

    I have no idea how easy/fast it is to change it, but I would have appreciated a warning in Visual Studio that the method is deprecated, like when using Transform.RotateAround or something (though unlike UGS events it still works...).

    And as I mentioned above, what confused me was that the old method returned "OK" in the UGS sample project even though the Analytics Package was up to date. And with no deprecation warning in my own project I figured something is wrong with the initialization method so I kept trying all sorts of things...
     
    Meltdown likes this.
  8. alexennerfelt

    alexennerfelt

    Joined:
    Jan 7, 2017
    Posts:
    26
    Since it doesn't work at all anymore, can we at least have it marked as Obsolete?
    It's end of 2023 and I spent 2 hours debugging why my custom events were not being recorded.


    Code (CSharp):
    1. //This is no longer working, yet for some reason not marked as Obsolete
    2. Analytics.CustomEvent(string customEventName)
    3.  
    4. //It has been replaced by this
    5. AnalyticsService.Instance.CustomData(string customEventName)
     
  9. ry511

    ry511

    Joined:
    Jul 24, 2016
    Posts:
    62
    This was super frustrating for me as well. Also there is no result object anymore. I just have to wait a while for the dashboard to pickup the event to see if its working?
     
  10. EMI_10

    EMI_10

    Joined:
    Mar 16, 2022
    Posts:
    3
    Were you able to fix that ? I'm not seeing any changes on my dashboard
     
  11. OctoSharko

    OctoSharko

    Joined:
    Dec 9, 2016
    Posts:
    49
    How are we supposed to send custom event these days ?

    The doc https://docs.unity.com/ugs/en-us/manual/analytics/manual/record-custom-events says :
    CustomEvent myEvent = new CustomEvent("MyEvent")
    {
    { "fabulousString", "woah!" },
    { "peculiarBool", true },
    { "sparklingInt", 1337 },
    { "spectacularFloat", 313.37f }
    };
    AnalyticsService.Instance.RecordEvent(myEvent);

    but CustomEvent does not have a string constructor for name in the latest version if the Analytics package... (5.1.1 April 18, 2024)

    AnalyticsService.Instance.RecordEvent("MyCustomEvent");
    works

    but how to send parameters ?

    Is the doc out of date ?
     
  12. Laurie-Unity

    Laurie-Unity

    Unity Technologies

    Joined:
    Mar 5, 2020
    Posts:
    236
    Some of the questions and responses earlier in this thread are quiite old and refer to the discontinued Legacy Analytics package or ealrly versions of the new Unity Analytics package.

    As of Mar 26 2024, the latest version of Unity Analytics is v5.1.1

    You can send a custom event using the generic Custom Event class to send custom events.
    Code (CSharp):
    1.     private void SendTestEvent()
    2.     {
    3.        
    4.         CustomEvent testEvent = new CustomEvent("testEvent")
    5.         {
    6.             { "itemAmount", 42 }          
    7.         };
    8.  
    9.      
    10.         AnalyticsService.Instance.RecordEvent(testEvent);
    11.     }
    You can extend this base class with your own properties to introduce type safety and add your own logic.