I am trying to use this feature (Google Analytics) in my app and can't figure out what is the best way to do it.
In first place I began to implement this in each Activity of the application.
public class MyActivity extends Activity {
public static GoogleAnalyticsTracker tracker;
/** Called when the activity is first created. */
@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() {
// Stop the tracker when it is no longer needed.
tracker.stop();
super.onDestroy();
}
}
But now I am thinking it would be better to create just one tracker at Application class and reference it from every Activity instead of repeat this code in each Activity I do create.
Can anyone explain which way is better and why?
Thanks.