Custom Event AnalyticsResults always comes back as NotInitialized

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.

   async void Start()
    {
        await UnityServices.InitializeAsync();
        Debug.Log(UnityServices.State);
        List<string> consentIdentifiers = await AnalyticsService.Instance.CheckForRequiredConsents();
        if (consentIdentifiers.Count > 0)
        {
            consentIdentifier = consentIdentifiers[0];
            isOptInConsentRequired = consentIdentifier == "pipl";
        }
   
    }
    [ContextMenu("tr")]
    void Trigger()
    {
        AnalyticsResult result = Analytics.CustomEvent("score");

        Debug.Log("analyticsResult: " + result);
        Debug.Log(UnityServices.State);
    }

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.

1 Like

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 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,

2 Likes

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!

2 Likes

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

1 Like

I always remind myself that this is part of progress and the alternative is using Unity 1.0 :slight_smile: Good thing we have a forum and Unity folks replying so fast.

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,

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…

1 Like

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.

//This is no longer working, yet for some reason not marked as Obsolete
Analytics.CustomEvent(string customEventName)

//It has been replaced by this
AnalyticsService.Instance.CustomData(string customEventName)

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?

Were you able to fix that ? I’m not seeing any changes on my dashboard

How are we supposed to send custom event these days ?

The doc 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 ?

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.

    private void SendTestEvent()
    {
       
        CustomEvent testEvent = new CustomEvent("testEvent")
        {
            { "itemAmount", 42 }          
        };

      
        AnalyticsService.Instance.RecordEvent(testEvent);
    }

You can extend this base class with your own properties to introduce type safety and add your own logic.