Search Unity

Question Is Analytics.CustomEvent supposed to work?

Discussion in 'Unity Analytics' started by Petter_H, Nov 22, 2021.

  1. Petter_H

    Petter_H

    Joined:
    Jul 25, 2020
    Posts:
    12
    I've created a custom event called "funnel", with a parameter "funnelStep" of type string in the dashboard Event Manager and also have enabled it.

    Code for sending the event is like so:

    Code (CSharp):
    1. var pars = new Dictionary<string, object>();
    2.         pars["funnelStep"] = name;
    3.  
    4.         Analytics.CustomEvent("funnel", pars);
    However, in my testing I just see standard events, no custom event... googling this reveals numerous similar complaints. However it is not clear to me if I should be using AnalyticsEvent.Custom instead since documentation hints at either.
     
  2. SamOYUnity3D

    SamOYUnity3D

    Unity Technologies

    Joined:
    May 12, 2019
    Posts:
    626
    Hi, both Analytics.CustomEvent and AnalyticsEvent.Custom should work the same, but it's worth trying AnalyticsEvent.Custom.

    In addition, there are 2 other ways to confirm if you are sending Analytics Events:
    1. Surround your Analytics.CustomEvent with a Debug.Log, like this: Debug.Log(Analytics.CustomEvent());
    This should print "Ok" in the console if the events are sent. Otherwise, it will give you an error message.
    https://docs.unity3d.com/ScriptReference/Analytics.AnalyticsResult.html

    2. Use a network traffic analyzer, such as Charles Proxy:
    https://www.charlesproxy.com/
    https://support.unity3d.com/hc/en-us/articles/115002917683-Using-Charles-Proxy-with-Unity

    I hope this helps. If the problem persists, please email "analytics-support@unity3d.com" to create a Zendesk ticket, please provide the project ID so we can check it.
     
  3. Petter_H

    Petter_H

    Joined:
    Jul 25, 2020
    Posts:
    12
    Already debugged stepped through yesterday to verify the events were indeed being sent. Could have a look at actual network communication as the next step, but TBH I might switch to Firebase before then.
     
  4. SamOYUnity3D

    SamOYUnity3D

    Unity Technologies

    Joined:
    May 12, 2019
    Posts:
    626
    Thanks, please feel free to contact us if you encounter any problems.
     
  5. Petter_H

    Petter_H

    Joined:
    Jul 25, 2020
    Posts:
    12
    Ok, Analytics.CustomEvent does show up... in the legacy analytics view.

    What steps do I need to take for it to show up in the beta view? Checking the obvious: I would assume I am using the beta since other information (including standard events) shows up on the Beta header in the dashboard. If there is a way to prove/disprove (e.g. by stating a required com.unity.analytics package version), let me know.

    This kind of illustrates my ongoing headache. I specially looked for a beta API for custom events, but ended up routing to legacy analytics. I have no idea if there's any way to actually do custom events in the beta.
     
    Last edited: Nov 23, 2021
  6. SebT_Unity

    SebT_Unity

    Unity Technologies

    Joined:
    Jun 21, 2021
    Posts:
    282
    Hi Petter,

    Thanks for your feedback. It has been noted and passed on to the teams.
    The code that you shared seems to be for legacy analytics.
    you can go to the following link to gain access to the analytics beta documentation.
    Here is a summary of the steps needed to send a custom event to the sdk with a simple example (required Unity 2019.4 and up)
    1. Sign up for the beta if you haven't already
    2. Download and install the following packages. These can be added directly to your projects PACKAGES folder. open the manifest.json file. Append the following to the dependencies
      "com.unity.services.analytics": "2.0.7-pre.7",
      "com.unity.services.authentication": "1.0.0-pre.4"
    3. Your project will now have the packages required for analytics beta.
    4. Link your project to the dashboard by going to EDIT > PROJECT SETTINGS
    5. Click on Services and link your project
    6. Create a custom event by going to your dashboard then ANALYTICS > EVENT MANAGER
    7. Add a new event called "funnel" by clicking add new event
    8. Assign a parameter > Click on add new parameter
      upload_2021-11-23_16-6-34.png
    9. Once your new event and parameter are created activate the event
      upload_2021-11-23_16-14-43.png
    10. Your event is now created in analytics beta. Head back in the editor
    11. Create a new game object called UGS_Analytics (Any name can be given here)
    12. Create a new script attached to the game object.
    13. Add the following code:
      Code (CSharp):
      1. using System.Collections;
      2. using System.Collections.Generic;
      3. using UnityEngine;
      4. using Unity.Services.Analytics;
      5. using Unity.Services.Core;
      6. using Unity.Services.Authentication;
      7.  
      8. public class UGS_Analytics : MonoBehaviour
      9. {
      10.     async void Start()
      11.     {
      12.  
      13.         await UnityServices.InitializeAsync();
      14.      
      15.         if (!AuthenticationService.Instance.IsSignedIn)
      16.         {
      17.             await AuthenticationService.Instance.SignInAnonymouslyAsync();
      18.         }
      19.  
      20.         HandleCustomEvent();
      21.     }
      22.  
      23.     private void HandleCustomEvent()
      24.     {
      25.         //Handle Custom event
      26.         Dictionary<string, object> parameters = new Dictionary<string, object>()
      27.         {
      28.             { "funnelStep", "Step 1"}
      29.         };
      30.        
      31.         Events.CustomData("funnel", parameters);
      32.         // You can call Events.Flush() to send the event immediately
      33.         Events.Flush();
      34.     }
      35. }

    14. To enable debugging follow the following documentation

      This code will send an event called "funnel" with parameter "funnelStep" and value "Step 1"
      to the analytics beta.

      Let me know if this helps or have additional questions.
     

    Attached Files:

    D00MX, unity_Ctri and Petter_H like this.
  7. Petter_H

    Petter_H

    Joined:
    Jul 25, 2020
    Posts:
    12
    Thanks for the very detailed explanation SebT, it's more than I could have hoped for! At the very least my packages and calls are off it appears. Will fix, now it feels 100% certain what went wrong.
     
    SebT_Unity likes this.
  8. LucianTheFak

    LucianTheFak

    Joined:
    Jan 11, 2019
    Posts:
    15
    I'm trying to make these Custom Events as well as you described SebT, but I just can't see the Custom Events.
    I installed the Authentication package, but using Unity.Services.Authentication gives an error.
    upload_2021-12-21_23-34-55.png

    I keep getting this in my Console. I can see Standard Events fine, but my custom event is not showing up.
    upload_2021-12-21_23-33-49.png
     
  9. RandolfKlemola

    RandolfKlemola

    Unity Technologies

    Joined:
    May 1, 2020
    Posts:
    130
    Hi Lucian, thanks for reaching out! When you hover your mouse over the error in Authentication, what does it say? Did you download and install the package for Authentication for the project in question?

    upload_2021-12-21_19-54-22.png

    Also, for your error in your console, when you double click on it, it should bring you to where the error is being generated in your code. Can you share a screenshot of that as well, or if you already know, share the code that is causing the error?

    Thanks!
    Randy
     
  10. LucianTheFak

    LucianTheFak

    Joined:
    Jan 11, 2019
    Posts:
    15
    Hi Randy.
    The Authentication is not giving the error anymore today. Looks like I just had to restart Unity.
    But it is still giving me the same console errors for the events not being uploaded.
    They bring me to the UploadComplete function here in the Dispatcher.cs when I click on them. I'm not sure what JSON I'm supposed to go and check.

    Code (CSharp):
    1.  
    2. public void Flush()
    3.         {
    4.             // There is still a request pending.
    5.             if (m_Request != null)
    6.             {
    7.                 return;
    8.             }
    9.  
    10.             FlushBufferToService();
    11.         }
    12.  
    13.         void FlushBufferToService()
    14.         {
    15.             // Serialize it into a JSON Blob, then POST it to the Collect bulk URL.
    16.             // 'Bulk Events' -> https://docs.deltadna.com/advanced-integration/rest-api/
    17.  
    18.             (string collectData, int sentTokens) = m_DataBuffer.Serialize();
    19.  
    20.             if (string.IsNullOrEmpty(collectData))
    21.             {
    22.                 return;
    23.             }
    24.  
    25.             byte[] postBytes = Encoding.UTF8.GetBytes(collectData);
    26.          
    27.             UnityWebRequest request = new UnityWebRequest(CollectUrl, UnityWebRequest.kHttpVerbPOST);
    28.             UploadHandlerRaw upload = new UploadHandlerRaw(postBytes);
    29.             upload.contentType = "application/json";
    30.             request.uploadHandler = upload;
    31.  
    32.             m_RequestSentTokens = sentTokens;
    33.             m_Request = request.SendWebRequest();
    34.             m_Request.completed += UploadComplete;
    35.          
    36.             #if UNITY_ANALYTICS_EVENT_LOGS
    37.             Debug.Log("Uploading events...");
    38.             #endif
    39.         }
    40.  
    41. void UploadComplete(AsyncOperation _)
    42.         {
    43.             long code = m_Request.webRequest.responseCode;
    44.  
    45.             #if UNITY_ANALYTICS_EVENT_LOGS
    46.             Debug.Assert(code == 204, "Incorrect response, check your JSON for errors.");
    47.             #endif
    48.  
    49.             if (!m_Request.webRequest.isNetworkError && code == 204)
    50.             {
    51.                 #if UNITY_ANALYTICS_EVENT_LOGS
    52.                 Debug.LogFormat("Events uploaded successfully!", code);
    53.                 #endif
    54.  
    55.                 // Remove the sent tokens from the buffer because they were accepted
    56.                 m_DataBuffer.RemoveSentTokens(m_RequestSentTokens);
    57.  
    58.                 // We have got the internet so the disk cache is no longer needed.
    59.                 // If the internet fails again, we will flush the buffer from scratch at that time.
    60.                 m_DataBuffer.ClearDiskCache();
    61.             }
    62.             else
    63.             {
    64.                 #if UNITY_ANALYTICS_EVENT_LOGS
    65.                 if (m_Request.webRequest.isNetworkError)
    66.                 {
    67.                     Debug.Log("Events failed to upload (network error) -- will retry at next heartbeat.");
    68.                 }
    69.                 else
    70.                 {
    71.                     Debug.LogFormat("Events failed to upload (code {0}) -- will retry at next heartbeat.", code);
    72.                 }
    73.                 #endif
    74.                 m_DataBuffer.FlushToDisk();
    75.             }
    76.  
    77.             // Clear the request to allow another request to be sent.
    78.             m_Request.webRequest.Dispose();
    79.             m_Request = null;
    80.         }
     
  11. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    @LucianTheFak Do you have another JSON library in your project? Please share the code you are using to send the event.
     
  12. LucianTheFak

    LucianTheFak

    Joined:
    Jan 11, 2019
    Posts:
    15
    I'm not sure, is there a way I can check if I have another one?
    As for the code:
    Code (CSharp):
    1. async void Start() {
    2.         await UnityServices.InitializeAsync();
    3.         if (!AuthenticationService.Instance.IsSignedIn) {
    4.             await AuthenticationService.Instance.SignInAnonymouslyAsync();
    5.         }
    6.     }
    7.  
    8.     public void OnPlayerDeath() {
    9.         string difficultyMethod = "";
    10.         switch (DifficultyManager.instance.difficultyMethod) {
    11.             case 1:
    12.                 difficultyMethod = "Predetermined";
    13.                 break;
    14.             case 2:
    15.                 difficultyMethod = "Sandbox";
    16.                 break;
    17.             case 3:
    18.                 difficultyMethod = "Dynamic";
    19.                 break;
    20.         }
    21.         Dictionary<string, object> parameters = new Dictionary<string, object>()
    22.         {
    23.             { "difficultyMethod", difficultyMethod }
    24.         };
    25.         Events.CustomData("playerDeath", parameters);
    26.         Events.Flush();
    27.     }
    I'm trying to send this playerDeath event when my player dies.
     
  13. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Please show a screenshot of the assets and packages in the Project view, and a screenshot of your Package Manager, we can check for a duplicate library. Have you checked that difficultyMethod is not null? You don't have a default case in your switch statement. Add the following just before your custom event call, the output will show in the Console window.

    Debug.Log("difficultyMethod value = " + difficultyMethod.ToString());
     
  14. LucianTheFak

    LucianTheFak

    Joined:
    Jan 11, 2019
    Posts:
    15
    difficultyMethod isn't null, it gets set to one of the 3 cases at the start of the game. It printed out just fine.

    Project View:
    upload_2021-12-22_16-26-57.png

    upload_2021-12-22_16-22-16.png

    Package Manager:
    upload_2021-12-22_16-22-40.png

    All the other packages in the project are mostly just models.
     
  15. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Yes, you've imported NewtsonSoft JSON. I might suggest testing your code in a new/empty project without it and compare. Are you actually using all these packages in your game? Generally you only import what you need.
     
  16. LucianTheFak

    LucianTheFak

    Joined:
    Jan 11, 2019
    Posts:
    15
    To be honest, I'm not sure where that one came from. I can try it in a new project, but would it work if I just deleted NewtsonSoft from Packages?
     
  17. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    You would want to make a full project backup before deleting components. Another package that you've imported may depend on it, but you can certainly try.
     
  18. LucianTheFak

    LucianTheFak

    Joined:
    Jan 11, 2019
    Posts:
    15
    It seems the Version Control package was dependent on NewtsonSoft, but I'm not using Collaborate in this project. But I see that the Authentication package lists NewtsonSoft in its dependencies, so I don't know if I can remove it.

    upload_2021-12-22_17-0-5.png
     
  19. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    One way to find out! I have Authentication in my project too, but I don't see the NewtonSoft package separately like in your screenshot. Please follow my previous suggestion with a new project to confirm. Don't break your current project.
     
  20. LucianTheFak

    LucianTheFak

    Joined:
    Jan 11, 2019
    Posts:
    15
    I just created a new project and some packages were automatically installed. NewtonSoft wasn't there.
    I install the Analytics and Authentication packages, and then the NewtonSoft folder appears in the Packages.
    upload_2021-12-22_17-24-10.png
    It appears that the Authentication package puts it there, since it lists it under its dependencies.
     
  21. LucianTheFak

    LucianTheFak

    Joined:
    Jan 11, 2019
    Posts:
    15
    upload_2021-12-22_17-41-25.png
    I tried sending a custom event in the new project, without the NewtonSoft folder in the project and without Authentication, but it's the same deal.
     
  22. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Sorry for the confusion, yes I see Newtonsoft Json in my project view also, I was looking in Package Manager previously. What version of Unity are you using? And you've created and enabled this event and parameter on your dashboard? The previous version of Analytics allowed you to send any events and parameters, now you need to specify the format first on the dashboard, as Randy mentioned above.
     
  23. LucianTheFak

    LucianTheFak

    Joined:
    Jan 11, 2019
    Posts:
    15
    I'm using Unity 2020.3.17f1.
    upload_2021-12-22_18-2-45.png
    upload_2021-12-22_18-2-58.png

    The test event and the previous playerDeath event are both enabled and have one custom parameter assigned to them.
    This is all under the default production environment, I didn't change anything about that.
     
  24. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Can you try without the Flush command? That's the only line different than my code. Edit: I just tried with Flush, no errors. Otherwise, I'm wondering if you might have a firewall or similar blocking the server endpoint. And you're not seeing these events on your dashboard?
     
  25. LucianTheFak

    LucianTheFak

    Joined:
    Jan 11, 2019
    Posts:
    15
    I'm only seeing the standard events gameRunning, gameStarted and gameEnded in my EventBrowser.
    If it might be a firewall or antivirus(in this case MalwareBytes), what should I exclude? Just Unity itself, or what's the server endpoint?
     
  26. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Can you turn off any firewall or antivirus for a short test? The endpoints would be *.unity.*:* and *.unity3d.*:* But it's interesting that your standard events are making it through.
     
  27. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
  28. LucianTheFak

    LucianTheFak

    Joined:
    Jan 11, 2019
    Posts:
    15
    Do I also need to download and import the CharlesProxyUpdated.package into the project or?
    And after I install Charles, do I just press Start Recording in Charles and try sending the event again?
     
  29. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    The unitypackage is only for Android, you don't need it. Start recording in Charles, then press Play in the Editor. Ensure to save the session as a .chls file. Are you on Mac or Windows?
     
  30. LucianTheFak

    LucianTheFak

    Joined:
    Jan 11, 2019
    Posts:
    15
    I'm on Windows. I'm installing Charles now.
     
  31. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Got it. Ensure to enter the values for the SSL Proxying Settings dialog and move the Charles certificate to Trusted Root Certification Authorities as mentioned under "For PC - Windows 10" and REBOOT.
     
  32. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    @LucianTheFak I should add, since it might contain private information, best to send me the .chls file in a private message here.
     
  33. LucianTheFak

    LucianTheFak

    Joined:
    Jan 11, 2019
    Posts:
    15
    Alright, I rebooted, will try out this thing now.
     
  34. LucianTheFak

    LucianTheFak

    Joined:
    Jan 11, 2019
    Posts:
    15
    I started a Conversation and uploaded the .chls file there. I think that's what you meant by private message, yeah? I'm new to actual forum posting.
     
  35. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    @LucianTheFak Your Charles capture was flawless, thank you for providing! I found a JSON error in the event request, but it's not in your code, it looks like a possible bug at our end. I will let our engineering team know. sorry for the inconvenience. I'm not familiar with a possible workaround yet.
     
    unity_Ctri likes this.
  36. LucianTheFak

    LucianTheFak

    Joined:
    Jan 11, 2019
    Posts:
    15
    Alright, I'll leave it be for now and focus on other things until you might have a solution. Thank you for helping me nonetheless!