I am trying to use this feature (Google Analytics) in my application and cannot figure out what is the best way to do this.
First of all, I started to implement this in every application action.
public class MyActivity extends Activity {
public static GoogleAnalyticsTracker tracker;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tracker = GoogleAnalyticsTracker.getInstance();
tracker.start("UA-YOUR-ACCOUNT-HERE", this);
tracker.trackPageView("/myActivityView");
}
@Override
public void onDestroy() {
tracker.stop();
super.onDestroy();
}
}
But now I think that it would be better to create only one tracker in the Application class and refer to it from each activity, rather than repeating this code in each of the operations I create.
Can someone explain which way is better and why?
Thank.
Regys source
share