views:

66

answers:

2

I was pretty excited to see how easy it is to set up Google Analytics with my app, but the lack of documentation has me sitting with a few questions. The only information that I can find is right from the documentation here, which only looks at reporting PageViews and Events from one Activity. I want to report PageViews and Events across multiple Activities in my app.

Right now in the onCreate() of all of my activities, I am calling:

    tracker = GoogleAnalyticsTracker.getInstance();
    tracker.start("UA-xxxxxxxxx", this);

And in the onDestroy() of all of my activities:

    tracker.stop();

I then track PageViews and Events as needed, and Dispatch them along with another HTTP request I am performing. But I'm not so sure this is the best way. Should I be calling start() and stop() in each activity, or should I only call start() and stop() in my main launcher activity?

+2  A: 

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
    }
}
Cristian
This is a snazzy idea, however one of my activities is extending ListActivity, while the others are just extending Activity.
Aurora
If you did want to go with this sort of approach, it should be fairly easy to replace the ListActivity with a standard activity. Its basically just an Activity with a ListView. Otherwise, just calling start and stop manually on the activities you are interested in should be fine.
Mayra
I guess a better extension to my question is, what does start() and stop() do? One of my guesses is that they deal with opening and closing the database that the tracker saves the events and pageviews before sending them, but if they are doing more intensive things than that, do I really want to be calling start() and stop() all the time?I'm curious as to what other people who are using Analytics in their android app are doing.
Aurora
Answering my secondary question:via the ReadMe.txt in the download for the GoogleAnalytics Jar file:"An Analytics tracking 'visit' is defined as the events and page views generated between calls to start() and stop() on the GoogleAnalyticsTracker. Every time start() is called a new visit is started. You should call stop() on GoogleAnalyticsTracker when your application is closing."So it looks like if you call start() and stop() only in your main launcher activity, that means each launch is a 'visit'. Multiple calls to start() stop() mean multiple 'visits' per app launch.
Aurora
Now that time has passed, I can see the results in Google Analytics. I am only calling start() and stop() in my main activity. Each launch of the application represents a 'visit'.If you want a unique 'visit' on each Activity, then the solution Cristian C. provides would probably work.
Aurora
Thanks for sharing your experience :D
Cristian
A: 

Hi!

This is not an answer, but I seem to be unable to comment at the moment. I started out as Aurora, with start/stop in each activity in my application.

However, both the API and the Visitor number gave me a smell, which is why I searched to find answers. The start()/stop() in application entry activity initially seems as a good idea, but what would one do when one has an application with several entry points? I mean, my application can be closed, but woken up by an alarm which leads the user directly to a specific resource, NOT via the normal entry point of the application - this, of course, I want to track as well, but what's the strategy for this? As I see it, I have to hack my way around this - either by examining if the specific activity is part of a larger task (wherein the main entry point activity will hold the responsibility for calling stop()), or, if a new task, will call stop() itself... but... errr... how?? ;)

Christer