Search Unity

Can't send in OnDestroy or OnApplicationQuit?

Discussion in 'Unity Analytics' started by tbg10101_, Feb 8, 2015.

Thread Status:
Not open for further replies.
  1. tbg10101_

    tbg10101_

    Joined:
    Mar 13, 2011
    Posts:
    192
    Hello again,

    I can't seem to send any events in OnDestry or OnApplicationQuit handlers. For example, this doesn't work:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Cloud.Analytics;
    3. using System.Collections.Generic;
    4.  
    5. public class UnityAnalyticsIntegration : MonoBehaviour {
    6.     public static UnityAnalyticsIntegration instance = null;
    7.     private const string projectId = "SECRET";
    8.    
    9.     void Start () {
    10.         if (instance == null) {
    11.             instance = this;
    12.  
    13.             UnityAnalytics.StartSDK (projectId);
    14.         }
    15.     }
    16.  
    17.     void OnApplicationQuit() {
    18.         float avgFPS = Time.frameCount / Time.realtimeSinceStartup;
    19.  
    20.         UnityAnalytics.CustomEvent("Session Performance", new Dictionary<string, object> {
    21.             {"Version", SettingsHelper.version},
    22.             {"Average FPS", avgFPS},
    23.             {"Session Duration", Time.realtimeSinceStartup/60f}
    24.         });
    25.     }
    26. }
    However, if I put the same code all in Start then it does send.

    Is there a list of handlers that we can and cannot use?
     
  2. shaderop

    shaderop

    Joined:
    Nov 24, 2010
    Posts:
    942
    I would like to know that as well. Is it possible to log analytics events on exit? It's feasible to work around it by storing the information in PlayerPrefs and then raising the event in the next session, but that's way less than ideal.
     
    Last edited: Feb 9, 2015
  3. tbg10101_

    tbg10101_

    Joined:
    Mar 13, 2011
    Posts:
    192
    Yeah, that's how I am doing it now but storing it in a separate file. Unfortunately, since my project is in such an early stage I expect people to only play once to see what changed then discard my game, making it somewhat difficult to gather that data. I'll try tying the event send to the exit button push next to see if I can resolve the issue that way but I would still like to see my question answered, about the handlers.
     
  4. mbirk

    mbirk

    Unity Technologies

    Joined:
    Apr 25, 2014
    Posts:
    18
    Hi @tbg10101_! We did some investigation, and as far as we can tell there is currently no workaround for this problem -- i.e. no way to send custom events on application quit or pause. It sounds like you could benefit from a new feature, some sort of notification (e.g. invoking a delegate or firing an event) when the analytics session is quitting or pausing. I can't make any promises, but we will add it to our list.
     
    tbg10101_ likes this.
  5. phasiclabs

    phasiclabs

    Joined:
    Nov 13, 2013
    Posts:
    126
    A solution to this would be great - I've just hit exactly the same problem.
    Obviously I don't know how the internals work, but having set the logging level to Info for the analytics I can see responding to OnApplicationQuit before my MonoBehaviours - could this not be changed to it responds last, after all custom behaviours have handled this event?
     
  6. Alex2539

    Alex2539

    Joined:
    Jan 28, 2015
    Posts:
    13
    I've found a temporary, imperfect workaround. If you are closing the game using "Application.Quit ()" you can send the event first and Unity Analytics will catch it. Unfortunately it's not going to work if the user closes the game any other way (eg: Alt+F4, clicking the X, ending the task), but it's better than nothing.
     
  7. jeff9384

    jeff9384

    Joined:
    Jan 5, 2013
    Posts:
    1
    @Alex2539, I thought of this method too, but in my case the event is not send. The AnalyticsResult is "Ok" but the app shuts down to quickly. The next time I open my app the event will be sent.
    When will the Analytics.CustomEvent EXACTLY be send? Due to the issues it is not send immediately.
     
  8. Maverick

    Maverick

    Joined:
    Dec 18, 2009
    Posts:
    240
    Any news about that? If not, how should we send session stats when player exits that game?
     
  9. mpinol

    mpinol

    Joined:
    Jul 29, 2015
    Posts:
    317
    Hi @Maverick,

    This is on our list but I cannot give you any real information on when this might possibly be added. What kind of game are you developing? What stats are you trying to track? Would it be possible to send an event with session stats after some condition has been met in your game?
     
  10. Maverick

    Maverick

    Joined:
    Dec 18, 2009
    Posts:
    240
    In my current case we need overall stats about fps, memory, session time and other few params that are collected during game session.

    For now I'm sending this info each minute, but this is not really convenient.
    Is there a way to "Update" a custom event data?
     
  11. mpinol

    mpinol

    Joined:
    Jul 29, 2015
    Posts:
    317
    Hi @Maverick,

    Currently there is no way to 'Update' a custom event but I think it is a great idea for a feature. If you want to show your support for this idea you can add/vote for it to be added as a feature here, http://feedback.unity3d.com/forums/unity/suggestions?utf8=✓&status=0&category=analytics&view=hottest

    The best suggestion I can think of would be to create several Custom Events at different key parts of your game that send this data. Like at level completed, power-up acquired, etc. I hope this helps!
     
  12. umar43

    umar43

    Joined:
    Sep 18, 2013
    Posts:
    6
    Is this issue fixed or any one found any proper workarround?
    Any help :(
     
  13. tbg10101_

    tbg10101_

    Joined:
    Mar 13, 2011
    Posts:
    192
    AFAIK this has not been fixed but I'm also not sure it is a bug.

    Just put the code to send he metrics in the even handler for whatever UI element (or key press) the user is using to exit your game.
     
  14. umar43

    umar43

    Joined:
    Sep 18, 2013
    Posts:
    6
    I have added my event sending code in OnApplicationQuit and OnApplicationPause. I printed the result.
    In case of OnApplicationPause result says "Not initialized".
    In case of exit event is sent some time and some time not. I think event is sent when "Application.Quit" is called explicitly(on the user input to exit game through normal flow) and its not sent when user Force stop the game or remove from running apps or its stopped due to a crash.
     
  15. mpinol

    mpinol

    Joined:
    Jul 29, 2015
    Posts:
    317
    Hi @umar43,

    Because of the nature of OnApplicationPause Analytics is 'shutdown' during this call which is why you are receiving 'Not Initialized', this is the expected behaviour. Placing events in OnApplicationQuit has a similar issue but the difference is that events in this function will have an attempt made to be sent but very frequently the app will close before the event can be sent. This is why you are only sometimes receiving your events.
     
  16. umar43

    umar43

    Joined:
    Sep 18, 2013
    Posts:
    6
    @mpinol

    What can I do to make sure that events in OnApplicationQuit will be logged every time.
     
  17. tbg10101_

    tbg10101_

    Joined:
    Mar 13, 2011
    Posts:
    192
  18. mpinol

    mpinol

    Joined:
    Jul 29, 2015
    Posts:
    317
    Hi @umar43,

    As of right now there is no way to make sure that events are sent reliably from OnApplicationQuit. This is something we are aware would be extremely useful to developers and we are researching ways to make this a reality. tbg10101_'s suggestion might be of some help, but I have not tested it myself so I cannot say for sure and I do not believe it will work on iPhone devices.
     
  19. xahei

    xahei

    Joined:
    Sep 22, 2014
    Posts:
    8
    Hi @mpinol

    Any news on this?
     
  20. mpinol

    mpinol

    Joined:
    Jul 29, 2015
    Posts:
    317
    Hey @xavier.heimgartner,

    This is something we are looking into but I am sorry I cannot really give you a time frame of when it will become available. Just to get an idea of your use case, are you looking to send a Custom Event with parameters or would a generic 'user quit app' type of event be useful as well?
     
  21. tbg10101_

    tbg10101_

    Joined:
    Mar 13, 2011
    Posts:
    192
    See my original post for the particulars of my original event.
     
  22. xahei

    xahei

    Joined:
    Sep 22, 2014
    Posts:
    8
    @mpinol
    Thanks for the answer!
    I would like to send a Custom Event when a users unexpectedly quits my game.
    This would would give me some insight into when and why they leave etc.
     
    mpinol likes this.
  23. pated

    pated

    Joined:
    Jun 27, 2012
    Posts:
    31
    What I do in Seashine is saving the data I want to keep an eye on in PlayerPrefs OnApplicationQuit(). I then load them and send them to Unity Analytics the next time the user starts or resume the app.

    Not perfect since you get the info only if and when the player launches the game again but at least it does the job. The "CancelQuit" alternative doesn't work on iOS anyway.

    I hope you guys at Unity still plan to implement a way to make it work. ;)
     
    Railon23 and mpinol like this.
  24. Deleted User

    Deleted User

    Guest

    @minpol Still no fix for this?
     
  25. ap-unity

    ap-unity

    Unity Technologies

    Joined:
    Aug 3, 2016
    Posts:
    1,519
    Hi @MrScaryOVR,

    Sorry, there is still no update about support for this feature. We understand it would be a useful feature for developers, but I can't give a specific time frame when it will be available.
     
  26. Logicator

    Logicator

    Joined:
    Feb 27, 2015
    Posts:
    1
    @ap-unity Any updates for supporting this feature?
     
  27. ap-unity

    ap-unity

    Unity Technologies

    Joined:
    Aug 3, 2016
    Posts:
    1,519
    @Logicator

    After discussing this request with some of the Analytics engineers, I think it is very unlikely that we will ever be able to consistently send data from OnApplicationQuit.

    However, OnApplicationPause may work as an alternative, and that should work consistently as of Unity 5.4.

    And for clarification for anyone finding this thread via Google, events can be sent in any of the following places without issue (in Unity 5.4+):

    Start
    Awake
    OnEnable
    OnDisable
    OnDestroy
    OnApplicationPause
     
    CrandellWS likes this.
Thread Status:
Not open for further replies.