Search Unity

Help with Custom Events

Discussion in 'Unity Analytics' started by Duffking, Jun 13, 2017.

  1. Duffking

    Duffking

    Joined:
    Oct 3, 2014
    Posts:
    24
    Hello,

    I'm using a game built in Unity as part of a study I'm running. I've built a game which is played through the WebGL player, and at set points my game asks players to answer questions. These questions are stored as a result.

    I'm looking to collect all users' answers at the end of the game. This is set up fine, and collected into a single string post-game (since all the questions are just scales 1-7, the string is just a long number, basically). Initially I planned to have the game automatically email me with the result, but unfortunately this doesn't seem easy with the WebGL player. I've looked into the analytics instead, and set up a custom event, like so:

    Analytics.CustomEvent("TestEvent2", new Dictionary<string, object>
    {
    { "answers", result }
    });

    This is fired off at the end of the game. Result is the string of answers I've built. I've run the game inside the editor and the event runs fine in the validator:

    upload_2017-6-13_16-4-57.png

    So, I've uploaded the game to the page and played through a few times in different browsers giving different answers each time.

    When I check after Unity Analytics has updated, the Event does show up, but I'm not seeing the data I sent with it, at all. The only one that works is if I use my shortcut to skip questions and it detects that a question wasn't answered and returns the string "Something has gone horribly wrong" instead.

    Any ideas what I'm doing wrong? Have I set the event up incorrectly? Is my string too long? It's (I think) 136 characters each time (68 answers plus a comma separation).

    Thanks!
     
  2. Duffking

    Duffking

    Joined:
    Oct 3, 2014
    Posts:
    24
    Sending a string without the commas seems to work. So either it doesn't like commas, or the string's too long?
     
  3. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    I suspect what we're doing is treating the string "1,2,3,4,5,6" like a number which becomes too large. One thing to try is to precede your string with alpha characters so it becomes "My Event - 1, 2, 3" and is treated like a string. However, and probably more importantly, your data may not be entirely useful within the dashboard as a single string. You would need to download and parse the results locally (if you have a Pro license and access to Raw Data Export) to really provide valid information. A better approach might be to send individual Custom Events as the the user progresses through the game. The Dashboard would then be able to properly aggregate the results. Also, make sure you are not sending Custom Events during OnApplicationQuit which has shown to be unreliable, as the Analytics engine may be in the process of shutting down at that point.
     
  4. Duffking

    Duffking

    Joined:
    Oct 3, 2014
    Posts:
    24
    I did consider having multiple events but it's easier for me to just have a single string sent out at the end so I can paste one thing into my spreadsheet rather than have to find each individual user's output for each event and string it together manually.

    Almost got everything working now, is it normal for my daily user count to update before any metrics data comes through? I ran 4 tests this morning, 1 for each variation of the game (each sends an event SceneXResult). I see 4 users today in my overview but no event data yet other than the ones from a few days ago when I ran some tests in the validator.
     
  5. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
  6. Duffking

    Duffking

    Joined:
    Oct 3, 2014
    Posts:
    24
    I'll have a check. I haven't changed the events, just split into 4 different scenes which each fire the same event with a different name depending on the scene number. Not seeing any data on those events this morning so I'll have a look (there is data on them, but only from when I tested it on Monday) using Charles.

    Code (CSharp):
    1. switch (sceneID)
    2.             {
    3.                 case 0:
    4.                     Analytics.CustomEvent("TestSceneResult", new Dictionary<string, object>
    5.                     {
    6.                         { "answers", result }
    7.                     });
    8.                     break;
    9.                 case 1:
    10.                     Analytics.CustomEvent("Scene1Result", new Dictionary<string, object>
    11.                     {
    12.                         { "answers", result }
    13.                     });
    14.                     break;
    15.                 case 2:
    16.                     Analytics.CustomEvent("Scene2Result", new Dictionary<string, object>
    17.                     {
    18.                         { "answers", result }
    19.                     });
    20.                     break;
    21.                 case 3:
    22.                     Analytics.CustomEvent("Scene3Result", new Dictionary<string, object>
    23.                     {
    24.                         { "answers", result }
    25.                     });
    26.                     break;
    27.                 case 4:
    28.                     Analytics.CustomEvent("Scene4Result", new Dictionary<string, object>
    29.                     {
    30.                         { "answers", result }
    31.                     });
    32.                     break;
    33.  
    34.             }
     
  7. Duffking

    Duffking

    Joined:
    Oct 3, 2014
    Posts:
    24
  8. Duffking

    Duffking

    Joined:
    Oct 3, 2014
    Posts:
    24
    Ok, I've played around with Charles and:

    The Event sends correctly when playing in the editor and shows up in Charles
    The Event does not send when playing in a browser using WebGL, the only events are appRunning events.

    I'm wondering then if this is actually the problem I've had all along and the initial one was simply nothing to do with the formatting of the data I was sending. It seems like my custom events just aren't being sent when playing in WebGL.
     
  9. Duffking

    Duffking

    Joined:
    Oct 3, 2014
    Posts:
    24
    On further investigation, the Event does not send from a desktop build either. It's definitely running through the code that sends the event; I've checked the log generated after playing.

    The only place that seems to send my custom events is the editor.
     
  10. Duffking

    Duffking

    Joined:
    Oct 3, 2014
    Posts:
    24
    I think I've solved it. I noticed in Charles that when I run the desktop build immediately after running it once, the first thing it does is send the result from the previous playthrough.

    I threw in an Analytics.FlushEvents() and it now seems to send the event, at least in my desktop build.

    The flow currently is that when the game is finished, the player clicks a button to submit their results (which calls the method that handles sending the events) and then displays a thankyou screen. Normally my event when it does send gets sent with AppRunning, so I'm guessing Unity tries to bundle my custom Events in with other events to try and save on packets, but those events never happened because the game just sits there until the player closes the window? When I force it to send with FlushEvents, there's no AppRunning event sent with it.
     
  11. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446