Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Thursday, June 8, for a Q&A with Unity's Content Pipeline group here on the forum, and on the Unity Discord, and discuss topics around Content Build, Import Workflows, Asset Database, and Addressables!
    Dismiss Notice

Question Best Way to track a Custom Event for Level Failed

Discussion in 'Unity Analytics' started by N1ghtrunner, Mar 6, 2023.

  1. N1ghtrunner

    N1ghtrunner

    Joined:
    Jan 5, 2015
    Posts:
    90
    I'm trying to setup a Custom Event that I can add to a method that gets triggered when a player loses a level. By doing this I want to be able to tell via analytics which levels are too difficult. Is there a way this can be done without having to create a data dictionary? Essentially what I have is something like this:

    Code (CSharp):
    1. AnalyticsService.Instance.CustomData("levelCompleted", levelProgression);
    levelProgression is an int value which is throwing a 'cannot be converted to' dictionary error. Hoping there is a relatively simple way to achieve this?
     
  2. N1ghtrunner

    N1ghtrunner

    Joined:
    Jan 5, 2015
    Posts:
    90
    So this is the code I am using and it does not appear to be working. Can anyone tell me why?

    Code (CSharp):
    1.     public void ReportLevelCompleted(int levelProgression)
    2.     {
    3.     AnalyticsService.Instance.CustomData("levelCompleted", new Dictionary<string, object>
    4.     {
    5.         { "level_id", levelProgression},
    6.  
    7.     });
    8.     }
    levelProgression is an integer and the value is updated at the end of every level. At the same time I want to take that value and report it in Analytics.

    I am using the 4.2 Analytics package and I have setup a levelCompleted custom event in the event manager on my dashboard. In addition I have created the following script which sits on the first scene of my game:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Unity.Services.Core;
    5. using Unity.Services.Analytics;
    6.  
    7. public class InitWithDefault : MonoBehaviour
    8. {
    9.     async void Start()
    10.     {
    11.         try
    12.         {
    13.             await UnityServices.InitializeAsync();
    14.             List<string> consentIdentifiers = await AnalyticsService.Instance.CheckForRequiredConsents();
    15.             Debug.Log("Initialised Unity Game Analytics");
    16.         }
    17.         catch (ConsentCheckException e)
    18.         {
    19.           Debug.Log("Consent Check Exception " + e);
    20.           // Something went wrong when checking the GeoIP, check the e.Reason and handle appropriately.
    21.         }
    22.     }
    23. }
    Please help :/
     
  3. N1ghtrunner

    N1ghtrunner

    Joined:
    Jan 5, 2015
    Posts:
    90
    ok dont mind me. The above works. Turns out I hadn't applied "level_id" parameter to the event in the dashboard, and I found some of these events under 'invalid events'. Setting this up and waiting an hour or so and things seem to be coming through properly. Amazing!
     
    SebT_Unity likes this.