Saturday 9 February 2013

Integrating Google Analytics in Android App

As an enthusiastic developer, I always wish to offer many features in my application. Many times, it happens that a particular feature gets popular. I may be interested in finding such features which I would like keep enhancing. I may also cut down the features showing similarity to less popular features. But the question is how do I get such list? The common answer to this question would be taking feedback from users, having surveys, etc. How about getting this statistical data without all this hassle?

Yes, we can get the application usage data anonymously using Google Analytics. It works the same way, as we track the hit count for a particular web page. Using the statistics collected by Analytics, it is easy to plan the features for application.

Before we use Google Analytics in our app, we need an Analytics account and the Google Analytics SDK.

Download the Analytics SDK from here. Unzip the SDK and add libGoogleAnalytics.jar to your project's build path.

Add following permissions in your project's AndroidManifest.xml.

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Now, sign into your analytics account and create a website profile for the App. The website URL can be fake but shall be descriptive enough. It is suggested that you use the reverse package name for this. For example, if the application package name is com.example.analytics.test; then the website URL for this app can be http://test.analytics.example.com. After successful creation of website profile, a web property ID is generated for that profile. Note it down as we will be using this in our app. This web property ID, also known as UA number of your tracking code, uniquely identifies the website profile. Note: You must mention in your App that you are collecting anonymous user data in your App to track your App.

After this setup, we are ready to track our application. Obtain the singleton instance of the tracker by calling GoogleAnalyticsTracker.getInstance() method. Then start tracking by calling its start()
method. Usually, we will want to track more than activities in the App. In such a scenario it is a good idea to have this tracker instance in onCreate() method of the Application class of the app.

public class TestApp extends Application {  
      
     //define your web property ID obtained after profile creation for the app   
      
      
     private String webId = "UA-XXXXXXXX-Y";  
      
      
     //Analytics tracker instance   
      
     GoogleAnalyticsTracker tracker;  
      
     @Override  
      
     public void onCreate() {  
      
      super.onCreate();   
      //get the singleton tracker instance  
      tracker = GoogleAnalyticsTracker.getInstance();  
      
      //start tracking app with your web property ID  
      tracker.start(webId,getApplicationContext());  
      
      //your app specific code goes here  
      
     }  
      
      
     /* This is getter for tracker instance.  
             This is called in activity to get reference to tracker instance. */  
      
     public GoogleAnalyticsTracker getTracker() {  
      
      return tracker;  
      
     }  
      
    }
You can track pageviews and events in the activity by calling trackPageView() and trackEvent() methods on tracker instance.
public class MainActivity extends Activity   
    {  
      
        @Override  
      
        protected void onCreate(Bundle savedInstanceState) {  
      
            super.onCreate(savedInstanceState);  
      
                  
      
            //track the page view for activity  
      
            GoogleAnalyticsTracker tracker = ((TestApp)getApplication()).getTracker();  
            tracker.trackPageView("/MainActivity");  
      
      
            /*You can track events like button clicks*/  
      
            findViewById(R.id.actionButton).setOnClickListener(  
                new View.OnClickListener() {           
      
            @Override  
            public void onClick(View v) {  
      
                  GoogleAnalyticsTracker tracker = ((TestApp)getApplication()).getTracker();  
                  tracker.trackEvent("Action Event","Action Button", "Button clicked",0);  
                  tracker.dispatch();  
      
            }  
      
             });  
      
            //your stuff goes here      
      
        }  
      
    }
Remember, your events and pageviews will not be sent to server until you call dispatch() method on tracker. In this way we can track all the activities and events inside them. By analyzing the various reports available on Google Analytics page, we can plan the features for the app.