Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sending Firebase analytics from OnApplicationQuit #451

Closed
ColorTwist opened this issue Aug 20, 2019 · 3 comments
Closed

Sending Firebase analytics from OnApplicationQuit #451

ColorTwist opened this issue Aug 20, 2019 · 3 comments

Comments

@ColorTwist
Copy link

ColorTwist commented Aug 20, 2019

Hello,

Question, would it be a good practice to send some information to Firebase analytics from the App in the function OnApplicationQuit (Triggers when the application quits),
could it cause any issues?

I want to send values like how much time used in the App and some other statics before quit.
I am sending total time as a float of total seconds dividing by 60 to get a float value 'like' minutes and seconds.

@patm1987
Copy link

Hi @ColorTwist,

I've done a little bit of digging on this, and have a short answer and a fun answer.

The short answer is that you should be safe to send the data in OnApplicationQuit, but I'd probably recommend OnApplicationPause on iOS and Android.

The long answer is that on Android, events are sent in Google Play Services. Once you log an event, it should get logged even if your app isn't running.

On iOS, the behavior is a little different. Firebase Analytics attempts to send all events up in the call to applicationDidEnterBackground. If you look at the documentation for OnApplicationQuit, it indicates that this may not be called immediately unless you have "Exit on Suspend" selected in the player settings. This leads me to assume that it's associated with the applicationWillTerminate callback which will happen after applicationDidEnterBackground. In this case though, and since the user may be offline anyway when an event is logged, Firebase Analytics does try to cache all the events locally. Then, the next time your game is running and the player has internet access, it will try to upload your events.

I have seen subtle changes between Unity versions when relying on this sort of native-level behavior in the past. The good news is that you can easily test the exact behavior for your combination of Unity, Analytics, your player settings, and your particular stack of other native plugins quickly using realtime analytics.

I hope that this all helps!
--Patrick

@ColorTwist
Copy link
Author

ColorTwist commented Aug 23, 2019

Thanks, @patm1987 for the reply.

Then I'll have to move to OnApplicationPause and find a way to distinguish between if the app Quit or Paused. Looking to know where player truly exited APP and not paused it to go to "Whatsup" and write something and return back again.

Regarding iOS, I guess the 'fun' with it will be with I publish the APP on iOS :)

@patm1987
Copy link

Awesome! I'm closing this issue, but do not hesitate to re-open and comment some more if you have no related questions.

I do find that, when at all possible, it's best to try to treat OnApplicationPause(true) as a pseudo-quit (in that you should try to persist everything as if you will need to resume from there, but try not to logically quit the game if possible). Something that occurred to me is that onDestroy (what typically corresponds to OnApplicationQuit) may not always get called on Android: https://developer.android.com/reference/android/app/Activity.html#onDestroy()
via
https://answers.unity.com/questions/824790/help-with-onapplicationquit-android.html

and I think similar rules may apply to iOS, especially in low memory situations.

@firebase firebase locked and limited conversation to collaborators Oct 23, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants