0

I am trying to log installs of my app using Firebase with this simple code below:

firebaseAnalytics.logEvent("foo", bundle);

However, I am not sure where to put this code. Does any one know of an "onInstall" method in the Application class?

Or is there another, easier way to log installs with Firebase?

Thank you!

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Melron
  • 459
  • 4
  • 14
  • 5
    "Does any one know of an "onInstall" method in the Application class?" -- there is none. Nothing of your code is run when the app is installed, only when the user first launches your app. – CommonsWare Jul 22 '19 at 15:26
  • Yeah that makes sense – Melron Jul 22 '19 at 15:26
  • 2
    The play store console already gives you nice statistics. However, you can log updates of your own app with the ACTION_PACKAGE_INSTALL broadcast action. – Reaper Jul 22 '19 at 15:27
  • 1
    ACTION_PACKAGE_INSTALL was deprecated in API level 15. ([reference](https://developer.android.com/reference/android/content/Intent.html#ACTION_PACKAGE_INSTALL)) – Chrisvin Jem Jul 22 '19 at 15:43

1 Answers1

3

You could determine if the user launches the application for the first time, and log that event.

public class MyActivity extends Activity {

    SharedPreferences prefs = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Perhaps set content view here

        prefs = getSharedPreferences("com.mycompany.myAppName", MODE_PRIVATE);
    }

    @Override
    protected void onResume() {
        super.onResume();

        if (prefs.getBoolean("firstrun", true)) {
            firebaseAnalytics.logEvent("foo", bundle);
            prefs.edit().putBoolean("firstrun", false).commit();
        }
    }
}

Code referenced from this SO answer.

There's another answer in that same SO question that explained how to differentiate between first run and subsequent upgrades, I'll just link that SO answer here for your reference.

The cleanest method would be to have a remote server that holds a unique ID for each user.

Also, you could theoretically write a file directly on the device. But then, you'd need to get the write permission and it's most definitely not a good idea to create and leave a file on the device.

P.S - To answer the actual question, No, Android doesn't have an onInstall method.

Chrisvin Jem
  • 3,940
  • 1
  • 8
  • 24
  • Thank you for the answer, but after the user cleans the cache, won't the shared preferences be deleted as well? – Melron Jul 22 '19 at 16:02
  • Indeed, it's not a foolproof method. The cleanest method would be to have a remote server that holds an unique ID for each user. – Chrisvin Jem Jul 22 '19 at 16:33
  • You could theoretically write a file directly on device. But then, you'd need to get the write permission and it's most definitely not a good idea to create and leave a file on the device. – Chrisvin Jem Jul 22 '19 at 16:42
  • Thanks a lot pal! – Melron Jul 22 '19 at 17:02
  • No probs mate, mark the question as correct and upvote if it answered your question. Cheers. – Chrisvin Jem Jul 22 '19 at 17:16