Search code examples
androidgoogle-analyticseasytracker

Should we create instance of EasyTracker multime times when trying to send logs to Google Analytics dashboard


I am using Google Analytics in android project. For this wherever I'm sending analytics I'm creating object of EasyTracker class.

Suppose user is on First Activity then I'm creating the object of EasyTracker in onCreate so when user navigate to second activity then again I'm creating object of EasyTracker in onCreate of second activity.

Should we create object of EasyTracker once only and use it same in entire application or this does not matter. I guess that by creating two object of EasyTracker Google Analytics is assuming that there are two real time users but exact is only one.

EasyTracker easyTracker = EasyTracker.getInstance(ActivityMain.this);

Thanks in advance.


Solution

  • Look at sample:

      @Override
      public void onStart() {
        super.onStart();
        ... // The rest of your onStart() code.
        EasyTracker.getInstance(this).activityStart(this);  // Add this method.
      }
    
      @Override
      public void onStop() {
        super.onStop();
        ... // The rest of your onStop() code.
        EasyTracker.getInstance(this).activityStop(this);  // Add this method.
      }
    

    https://developers.google.com/analytics/devguides/collection/android/v3/

    It seems that EasyTracker is singletone and you should call getInstance every time.