Sunday, August 7, 2011

Track your android application with Google Analytics

Google Analytics has been the best available tool for tracking visitors activity on the websites, now Google has extended the support for it for mobile application developers and introduced the Google analytics for mobile.This lets you track the application usage and also the custom events. The mobile API is as simple as easy to use as the web API.

To get started with the development, you can involve the jar downloaded to your project and get the Google Analytics key from the analytics website. To start with data usage collection, create an object of the GoogleAnalyticsTracker class and instantiate with you unique API key.

        tracker = GoogleAnalyticsTracker.getInstance();

        tracker.start("UA-YOUR-ACCOUNT-HERE", <time to dispatch> ,this);

It is better to call the "start" function at the start of the main android activity and set the  "time to dispatch"  wisely.The system is designed in a way which does not consume a lot of battery by sending the usage data continuously,rather it stores all the data in a temporary SQLLite database which at the "dispatch" call sends all the data to the analytics server.Adding a parameter "time to dispatch" to the start function calls the dispatch function automatically after the specified time.

To catch an event call the  trackPageView function with the page name as the parameter.

        tracker.trackPageView("/testApplicationHomeScreen");

Custom variables can also be tracked by the setCustomVar function.

        tracker.setCustomVar(1, "Navigation Type", "Button click", 2);

Events such as button clicks can also be tracked with the trackEvent function

        tracker.trackEvent("Clicks","Button","clicked",123);   
The tracker can be manually dispatched by using

        tracker.dispatch();
 and the tracker object can also be destroyed so that it does not collect any further statistics by using

         tracker.stop();

So, In case you are having a website and an iPhone+android application for  same, you can track the complete usage using a single service and have the consolidated data about the users.Although the statistics data is delayed by few hours but still for a service with considerable usage, there is nothing better than Google Analytics!