Google Analytics in Android app - dealing with multiple activities Google Analytics in Android app - dealing with multiple activities android android

Google Analytics in Android app - dealing with multiple activities


The problem with calling start()/stop() in every activity (as suggested by Christian) is that it results in a new "visit" for every activity your user navigates to. If this is okay for your usage, then that's fine, however, it's not the way most people expect visits to work. For example, this would make comparing android numbers to web or iphone numbers very difficult, since a "visit" on the web and iphone maps to a session, not a page/activity.

The problem with calling start()/stop() in your Application is that it results in unexpectedly long visits, since Android makes no guarantees to terminate the application after your last activity closes. In addition, if your app does anything with notifications or services, these background tasks can start up your app and result in "phantom" visits. UPDATE: stefano properly points out that onTerminate() is never called on a real device, so there's no obvious place to put the call to stop().

The problem with calling start()/stop() in a single "main" activity (as suggested by Aurora) is that there's no guarantee that the activity will stick around for the duration that your user is using your app. If the "main" activity is destroyed (say to free up memory), your subsequent attempts to write events to GA in other activities will fail because the session has been stopped.

In addition, there's a bug in Google Analytics up through at least version 1.2 that causes it to keep a strong reference to the context you pass in to start(), preventing it from ever getting garbage collected after its destroyed. Depending on the size of your context, this can be a sizable memory leak.

The memory leak is easy enough to fix, it can be solved by calling start() using the Application instead of the activity instance itself. The docs should probably be updated to reflect this.

eg. from inside your Activity:

// Start the tracker in manual dispatch mode...tracker.start("UA-YOUR-ACCOUNT-HERE", getApplication() );

instead of

// Start the tracker in manual dispatch mode...tracker.start("UA-YOUR-ACCOUNT-HERE", this ); // BAD

Regarding when to call start()/stop(), you can implement a sort of manual reference counting, incrementing a count for each call to Activity.onCreate() and decrementing for each onDestroy(), then calling GoogleAnalyticsTracker.stop() when the count reaches zero.

The new EasyTracker library from Google will take care of this for you.

Alternately, if you can't subclass the EasyTracker activities, you can implement this manually yourself in your own activity base class:

public abstract class GoogleAnalyticsActivity extends Activity {    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        // Need to do this for every activity that uses google analytics        GoogleAnalyticsSessionManager.getInstance(getApplication()).incrementActivityCount();    }    @Override    protected void onResume() {        super.onResume();        // Example of how to track a pageview event        GoogleAnalyticsTracker.getInstance().trackPageView(getClass().getSimpleName());    }    @Override    protected void onDestroy() {        super.onDestroy();        // Purge analytics so they don't hold references to this activity        GoogleAnalyticsTracker.getInstance().dispatch();        // Need to do this for every activity that uses google analytics        GoogleAnalyticsSessionManager.getInstance().decrementActivityCount();    }}public class GoogleAnalyticsSessionManager {    protected static GoogleAnalyticsSessionManager INSTANCE;    protected int activityCount = 0;    protected Integer dispatchIntervalSecs;    protected String apiKey;    protected Context context;    /**     * NOTE: you should use your Application context, not your Activity context, in order to avoid memory leaks.     */    protected GoogleAnalyticsSessionManager( String apiKey, Application context ) {        this.apiKey = apiKey;        this.context = context;    }    /**     * NOTE: you should use your Application context, not your Activity context, in order to avoid memory leaks.     */    protected GoogleAnalyticsSessionManager( String apiKey, int dispatchIntervalSecs, Application context ) {        this.apiKey = apiKey;        this.dispatchIntervalSecs = dispatchIntervalSecs;        this.context = context;    }    /**     * This should be called once in onCreate() for each of your activities that use GoogleAnalytics.     * These methods are not synchronized and don't generally need to be, so if you want to do anything     * unusual you should synchronize them yourself.     */    public void incrementActivityCount() {        if( activityCount==0 )            if( dispatchIntervalSecs==null )                GoogleAnalyticsTracker.getInstance().start(apiKey,context);            else                GoogleAnalyticsTracker.getInstance().start(apiKey,dispatchIntervalSecs,context);        ++activityCount;    }    /**     * This should be called once in onDestrkg() for each of your activities that use GoogleAnalytics.     * These methods are not synchronized and don't generally need to be, so if you want to do anything     * unusual you should synchronize them yourself.     */    public void decrementActivityCount() {        activityCount = Math.max(activityCount-1, 0);        if( activityCount==0 )            GoogleAnalyticsTracker.getInstance().stop();    }    /**     * Get or create an instance of GoogleAnalyticsSessionManager     */    public static GoogleAnalyticsSessionManager getInstance( Application application ) {        if( INSTANCE == null )            INSTANCE = new GoogleAnalyticsSessionManager( ... ,application);        return INSTANCE;    }    /**     * Only call this if you're sure an instance has been previously created using #getInstance(Application)     */    public static GoogleAnalyticsSessionManager getInstance() {        return INSTANCE;    }}


The SDK now has a external library which takes care of all of this. Its called EasyTracker. You can just import it and extend the provided Activity or ListActivity, create a string resource with your code and you are done.


The tracker will only track the activity where it's executed. So, why don't you subclass an Activity which start it every time on onCreate:

public class GAnalyticsActivity extends Activity{    public void onCreate(Bundle icicle){        super.onCreate(icile);        tracker = GoogleAnalyticsTracker.getInstance();        tracker.start("UA-xxxxxxxxx", this);    }    // same for on destroy}

Then, you extends that class for every activity you use:

public class YourActivity extends GAnalyticsActivity{    public void onCreate(Bundle icicle){        super.onCreate(icile);        // whatever you do here you can be sure         // that the tracker has already been started    }}