UGS Analytics custom events not shown

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:

public static void LevelStarted(int chapter, int level)
{
    var unityEventData = new Dictionary<string, object>
    {
        {"Chapter", chapter},
        {"Level", level},
        {"PlayerId", UserId}
    };
    SendUnityCustomEvent("LevelStarted", unityEventData);
}

private static void SendUnityCustomEvent(string name, Dictionary<string, object> data)
        {
            var result = UnityEngine.Analytics.Analytics.CustomEvent(name, data);
            if (result != AnalyticsResult.Ok)
            {
                Debug.LogError(">>>>>> Analytics event " + name + " failed with result: " + result);
            }
            else
            {
                Debug.Log(">>>>>> Analytics event " + name + " sent with result: " + result);
            }
        }

It seems you are using legacy events.

See this documentation for UGS Analytics Custom Events: Record Custom Events (unity.com)

// Send custom event
Dictionary<string, object> parameters = new Dictionary<string, object>()
{
    { "fabulousString", "hello there" },
    { "sparklingInt", 1337 },
    { "spectacularFloat", 0.451f },
    { "peculiarBool", true },
};
// The ‘myEvent’ event will get queued up and sent every minute
AnalyticsService.Instance.CustomData("myEvent", parameters);

// Optional - You can call Events.Flush() to send the event immediately
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.

public static void LevelStarted(int chapter, int level)
{
    Dictionary<string, object> eventData = new Dictionary<string, object>();
    eventData.Add("Chapter", chapter);
    eventData.Add("Level", level);
    eventData.Add("PlayerId", UserId);

    // The ‘LevelStarted’ event will get queued up and sent every minute
    AnalyticsService.Instance.CustomEvent("LevelStarted", eventData);
}
2 Likes

Thanks, finally see it works!

1 Like

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:

public UgsAnalytics(PopupManager popupManager)
{
    _popupManager = popupManager;
    if (PlayerPrefs.HasKey(ANALYTICS_OPT_OUT_KEY) && PlayerPrefs.GetInt(ANALYTICS_OPT_OUT_KEY) == 1)
    {
        AnalyticsService.Instance.OptOut();
    }

    InitializeUnityAnalyticsAsync();
}

private async void InitializeUnityAnalyticsAsync()
{
    try
    {
        await UnityServices.InitializeAsync();
        List<string> consentIdentifiers = await AnalyticsService.Instance.CheckForRequiredConsents();
        foreach (var identifier in consentIdentifiers)
        {
            if (identifier == PIPL_CONSENT_IDENTIFIER)
            {
                CheckUserConsent();
            }       
        }
    }
    catch (ConsentCheckException e)
    {
        Debug.LogError("Can not initialize UGS Analytics! Consent error, reason: " + e.Reason);
    }
    _ = AnalyticsService.Instance.SetAnalyticsEnabled(true);
}

public void CheckUserConsent()
{
    try
    {
        var consentPopup = _popupManager.GetPopup<UgsAnalyticsConsentController, UgsAnalyticsConsentController.Factory>();
        consentPopup.OnResult += OnConsentResult;
        consentPopup.Show();
    }
    catch (ConsentCheckException e)
    {
        Debug.LogError("Error during getting PIPL consent, reason: " + e.Reason);
    }

}

private void OnConsentResult(bool result)
{
    AnalyticsService.Instance.ProvideOptInConsent(PIPL_CONSENT_IDENTIFIER, result);
}

Analytics change their syntax according new version.I used unity analytics with latest version in my project using this video.

Unity Analytics Tutorial 2024 | Track Player Data & Custom Events | Step-by-Step Guide