The problem with calling start () / stop () in every action (as suggested by Christian) is that it leads to a new “visit” for every action that your user leads to. If this is suitable for your use, then that’s fine, however, it’s not like most people expect visits to work. For example, it makes it very difficult to compare Android numbers with numbers on the Internet or iphone, since a “visit” on the Internet and iphone is associated with a session, not a page / activity.
The problem with calling start () / stop () in your application is that it leads to unexpectedly long visits, since Android makes no guarantees to terminate the application after closing the last action. Also, if your application does nothing with notifications or services, these background tasks can launch your application and lead to phantom visits. UPDATE: stefano correctly indicates that onTerminate () is never called on a real device, so there is no obvious place to stop calling ().
The problem with calling start () / stop () in one “main” action (as suggested by Aurora) is that there is no guarantee that the activity will remain active for the entire time that your user uses for your application. If the “core” activity is destroyed (say, to free up memory), your subsequent attempts to write events to GA in other actions will fail because the session is stopped.
In addition, there is a bug in Google Analytics, at least with version 1.2, which forces it to maintain a strong link to the context that you pass in order to start (), preventing it from receiving garbage collected after its destruction. Depending on the size of your context, this could be a significant memory leak.
A memory leak is simple enough to fix, it can be solved by calling start () using the application, and not the activity instance itself. docs should probably be updated to reflect this.
eg. from within your business:
// Start the tracker in manual dispatch mode... tracker.start("UA-YOUR-ACCOUNT-HERE", getApplication() );
instead
// Start the tracker in manual dispatch mode... tracker.start("UA-YOUR-ACCOUNT-HERE", this ); // BAD
Regarding when you need to call start () / stop (), you can implement some kind of reference counting manually by increasing the counter for each call to Activity.onCreate () and decreasing onDestroy () for each of them, and then calling GoogleAnalyticsTracker.stop ( ) when the counter reaches zero.
Google’s new EasyTracker library will take care of this for you.
Alternatively, if you cannot subclass EasyTracker actions, you can manually implement this yourself in your base activity class:
public abstract class GoogleAnalyticsActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);