Analytics for Android, Revisited

In my previous post about Google Analytics I demonstrated how to use a parent class and inheritance.  Some applications will not be able to use inheritance so they need to use a helper class.  Here is my example of an example class.

public class AnalyticsHelper {
    static final String TAG = "AnalyticsHelper";
 
    GoogleAnalyticsTracker mTracker;
    private Context mApplicationContext;
 
    private static AnalyticsHelper sInstance;
 
    /**
     * Returns the global {@link AnalyticsUtils} singleton object, creating one
     * if necessary.
     */
    public static AnalyticsHelper getInstance(Context context) {
        if (sInstance == null && context != null) {
            sInstance = new AnalyticsHelper(context);
        }
 
        return sInstance;
    }
 
    private AnalyticsHelper(Context context) {
        if (context == null) {
            // This should only occur for the empty Analytics utils object.
            return;
        }
 
        mApplicationContext = context.getApplicationContext();
        mTracker = GoogleAnalyticsTracker.getInstance();
 
        mTracker.start("UA-CODE", mApplicationContext);
        //mTracker.setDebug(true);
        //mTracker.setDryRun(true);
 
        Log.d(TAG, "Initializing Analytics");
 
         
    }
 
    public void trackError(RuntimeException e, String action, String label, String errorMessage, Context context) {
        trackEvent("exception", action, label, 0);
        Toast.makeText(context, errorMessage, Toast.LENGTH_SHORT).show();
        throw e;
    }
 
    public void trackEvent(final String category, final String action, final String label, final int value) {
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... voids) {
                try {
                    mTracker.trackEvent(category, action, label, value);
                } catch (Exception e) {
                    Log.w(TAG, e.getMessage());
                }
                return null;
            }
        }.execute();
    }
 
    public void trackPageView(final String path) {
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... voids) {
                try {
                    mTracker.trackPageView(path);
                } catch (Exception e) {
                    Log.w(TAG, e.getMessage());
                }
                return null;
            }
        }.execute();
    }
 
    public void dispatch() {
        //didn't like running as a single thread
        mTracker.dispatch();
    }
     
    public void stop() {
        dispatch();
        mTracker.stop();
    }
}


In the code above I'm creating a singleton of my helper class and providing five methods to make it easy to track events and page views.  The main change here is that I'm calling trackPageView and trackEvent in ASyncTasks so that they don;t run on the UI thread.  I had issues running dispatch off the UI thread.

Here is an easy example of calling trackError through the AnalyticsHelper singleton.  

private void updateHandicap(int golferId) {
    try {
        AsyncTask.Status status = updateGolferTask.getStatus();
        if (AsyncTask.Status.PENDING.equals(status)) {
            updateGolferTask.execute(new String[]{golferId + ""});
        }
    } catch (Exception e) {
        AnalyticsHelper.getInstance(this).trackEvent("exception", "updateHandicap", this.getLocalClassName(), 0);
    }
}


This is how I'm implementing Google Analytics in my app and it's similar to how Google implemented it in their Google IO 2011 android app.  As I work more with this I'll try to keep this blog up to date on how it's working.  I do love having analytics in my android app and wish I could do it with AOP instead of writing this boilerplate code everywhere.

 

What did you think of this article?




Trackbacks
  • Trackbacks are closed for this post.
Comments
  • No comments exist for this post.
Leave a comment

 Name (required)

 Email (will not be published) (required)

Your comment is 0 characters limited to 3000 characters.