Search Unity

Bug No Events are send OnApplicationQuit on Windows Build

Discussion in 'Unity Analytics' started by manuelgoellnitz, Dec 14, 2022.

  1. manuelgoellnitz

    manuelgoellnitz

    Joined:
    Feb 15, 2017
    Posts:
    397
    I have a couple of custom events that are fired OnApplicationQuit.
    It works fine in the Editor, my custom events and "gameEnded" are fired when I end playmode.
    In Windows Builds I get the Analytics debugs for my custom evetns and "gameEnded":
    Recorded event gameEnded at 2022-12-14 10:50:28.188 +01:00 (UTC)
    Serializing event gameEnded for dispatch...

    But the debug
    Uploading events...
    Events uploaded successfully!
    is missing.

    In WebGL I managed to get my custom events fired, when the browser tab is closed, but the "gameEnded" event is also never fired.

    Is there a special trick to get those events? Or is is just a bug?
     
  2. RandolfKlemola

    RandolfKlemola

    Unity Technologies

    Joined:
    May 1, 2020
    Posts:
    130
    Hi @manuelgoellnitz ,

    Thanks for reaching out! We attempt to send the gameEnded event if the game is shut down, whether it's closed by Alt+F4, swiped off, or closed with a properly-implemented Quit button. However, this is contingent on the OS letting it shut down gracefully rather than just killing it, as sending events does take some amount of time. If they don't make it out, the gameEnded event may be cached on disk, so it should then get uploaded the next time the player starts the app... Though, yes, it is quite unreliable because of the nature of trying to do work while being closed down.

    I see in the logs you've shared that the gameEnded event is recorded. Is it sent in the 1st upload on the subsequent session? It should be cached, held, and uploaded on the next session.

    Best,
    Randy
     
  3. manuelgoellnitz

    manuelgoellnitz

    Joined:
    Feb 15, 2017
    Posts:
    397
    As I saw it, The events are send out via a webrequest, which is never callled, when the application quits.

    So I made a workaround like this:
    Code (CSharp):
    1. void Start()
    2. {        
    3.      Application.wantsToQuit += WantsToQuit;
    4.    
    5. }
    6.  
    7. private bool WantsToQuit()
    8. {
    9.     if (sendingQuitAppsComplete)
    10.         return true;
    11.  
    12.    StartCoroutine(DelayQuitCoroutine());
    13.    return false;
    14. }
    15.  
    16. private IEnumerator DelayQuitCoroutine()
    17. {
    18.    //send out custom events here
    19.    //show some kind of "application is quitting"
    20.    yield return new WaitForSeconds(1f);
    21.    sendingQuitAppsComplete = true;  
    22.    Application.Quit();
    23. }
    I basically delay the quitting of the application by one second.
    This gives the analytics system enough time to send the gameEnded. Or at least my custom events are send then.
     
    SebT_Unity and RandolfKlemola like this.
  4. RandolfKlemola

    RandolfKlemola

    Unity Technologies

    Joined:
    May 1, 2020
    Posts:
    130
    Hi @manuelgoellnitz ,

    That's a really awesome workaround. I'm going to hold on to that for future reference. I see you're getting your custom events, but are you also getting your gameEnded events consistently?

    Best,
    Randy
     
  5. manuelgoellnitz

    manuelgoellnitz

    Joined:
    Feb 15, 2017
    Posts:
    397
    I looked into it again.
    The Unity event "gameEnded" is still not send (not even after restarting the app). But my custom events are, which was good enough for my purpose.
     
    RandolfKlemola likes this.
  6. grrava

    grrava

    Joined:
    Nov 11, 2012
    Posts:
    46
    I have the same issue, and after updating tot 2023.2 it got worse: the application crashes during the attempt. See the log in attachment.
     

    Attached Files:

  7. manuelgoellnitz

    manuelgoellnitz

    Joined:
    Feb 15, 2017
    Posts:
    397
    I can confirm this!
     
  8. manuelgoellnitz

    manuelgoellnitz

    Joined:
    Feb 15, 2017
    Posts:
    397
    On Unity 2023.2.0.b14 the crash does not happen
     
    SebT_Unity likes this.