Google Analytics, what is the best way to use (implement) it in terms of resources / programming?

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;   

    /** 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 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.

+3
source share
2 answers

This: Google Analytics in the Android app - working with multiple actions

Google Analytics Android.

, start()/stop() , start() , , . , , , .

, , , start()/stop(), , stop(), . .

+3

- singleton?

(, ) .

0

All Articles