Google Analytics in Android app - dealing with multiple activities

后端 未结 7 2163
忘了有多久
忘了有多久 2020-11-28 18:42

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 th

相关标签:
7条回答
  • 2020-11-28 18:50

    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.

    0 讨论(0)
  • 2020-11-28 18:56

    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;
        }
    }
    
    0 讨论(0)
  • 2020-11-28 18:57

    You will need something like this: http://mufumbo.wordpress.com/2011/06/13/google-analytics-lags-on-android-how-to-make-it-responsive/

    That's on the previous version and used to work very well. Now I'm in the same struggle as you, as V2 doesn't seems to be very consistent.

    0 讨论(0)
  • 2020-11-28 19:02

    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

    0 讨论(0)
  • 2020-11-28 19:02

    I wonder if this is something that could be done using AOP.

    Android can only use compile-time AOP methods so maybe something like AspectJ?

    There's a little more info on using AspectJ in Android in this thread. The main issue being that you would still need to declare on classes you own.

    0 讨论(0)
  • 2020-11-28 19:08

    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
        }
    }
    
    0 讨论(0)
提交回复
热议问题