3

I'm trying to integrate Google Analytics into my Android project using the information on the website of the respective SDK. However, there is very little documentation available. My project has 6 different Activities and I noticed that using the method on the website results in a unique visit in Google Analytics for each Activity that is being opened, even if it's still in the same session. Apparently, Google Analytics for Android never reuses a previously used session.

Their method is to start tracking activity in onCreate and then stop tracking in onDestroy. The problem I have with this is that a session will stay active if a user presses the home button instead of the back button, since the Activity will not be destroyed. Therefore I chose to do it in onResume and onPause instead but that means that new sessions gets opened when a new Activity is opened.

Does anyone know any way to really track a single session over multiple Activities?

Franklin
  • 612
  • 1
  • 9
  • 18
  • Related: http://stackoverflow.com/questions/6232177/gotchas-when-using-google-analytics-for-android-sdk – emmby Jun 03 '11 at 20:50

2 Answers2

3

After studying the lifecycle of an Activity, I came to the following conclusion.

When switching from an Activity A to another Activity B, the onStop method of A is called AFTER the onStart method of B. What I then did was increasing a reference counter every time the (static) tracker is accessed in an onStart method. In the onStop method, I would first check whether the reference counter was 0, and stop the tracker if it was. At the end of the onStop method, I would decrease the reference counter.

This seems to be working quite well at this moment, and should also work when the application has multiple Activities that can act as an entry point.

Franklin
  • 612
  • 1
  • 9
  • 18
  • I noticed one downside: onStop doesn't get called when the screen of the device is turned off so the session will stay active ... – Franklin Oct 06 '10 at 10:00
  • This doesn't always work, since `onStop` doesn't always get called straight after an `onStart` as it may be delayed until a third activity is started, leading to the counter reaching 2 and being decremented to 1 when the current activity is no longer visible. – James Goodwin Jan 26 '12 at 12:00
  • It might be a problem in some cases, but I only need to know whether there are any references or not, so I only check for zero or non-zero values of the reference counter. – Franklin Feb 14 '12 at 12:38
2

Repeating an answer that I posted here: Google Analytics in Android app - dealing with multiple activities

The approach I am using is to use a Bound Service (I happen to be using one already so was spared the creation of extra boiler plate code.)

A Bound Service will only last as long as there are Activities bound to it. All the activities in my app bind to this service, so it lasts only as long as the user is actively using my application - therefore very much a real 'session'.

I start the tracker with a singleton instance of Application which I have extended and added a static getInstance() method to retrieve the instance:

// Non-relevant code removed

public IBinder onBind(Intent intent) {
    tracker = GoogleAnalyticsTracker.getInstance();
    tracker.startNewSession(PROPERTY_ID, MyApp.getInstance());
}


public boolean onUnbind(Intent intent) {
    tracker.stopSession();
}

See: http://developer.android.com/guide/topics/fundamentals/bound-services.html

Community
  • 1
  • 1
evaneus
  • 759
  • 6
  • 9
  • But if you're using this approach, a session will stay active if a user presses the home button, right? The behavior that I would expect is that pressing a home button ends a session as well. – Franklin Sep 15 '11 at 07:22
  • No, not in this case. But it depends on where you bind and unbind your service in your activities. In my case I bind in onStart() and unbind in onStop(). According to: http://developer.android.com/guide/topics/fundamentals/activities.html#Lifecycle "The visible lifetime of an activity happens between the call to onStart() and the call to onStop()" So if one of my activities is not visible in the foreground, then the service is not bound and the session is not active. – evaneus Sep 15 '11 at 17:21
  • I should add, that if you launch external Intents from your activities, you'll probably need to use startActivityForResult() or some other mechanism to track when your app is open but your Task is off-screen. – evaneus Sep 15 '11 at 17:26