Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Getting Started with Google Analytics on Android : Page 2

The Google Analytics SDK for Android allows you to track user activity and generate useful statistics about your Android apps. Learn how to set up and use this SDK.


advertisement

Integrate the Google Analytics SDK into Your Android Project

Next, you need to integrate the SDK into your existing Android project. We're going to assume you're using the popular Eclipse development environment, although using other IDEs should involve very similar steps. To integrate the SDK into your project, you must:

  1. Add the libGoogleAnalytics.jar to your project.
  2. Add two permissions to your Android manifest file.

Adding the JAR file to your project within Eclipse is easy. Simply:

  1. Click on the Project properties for your Android project.
  2. Under the Java Build Path settings, select the Libraries tab.
  3. Click the Add JARs… button and choose the jar within the /libs directory.



Since you are sending information over the network, you will need to add two permissions to your application, in order to access the network state and use the Internet. To do this:

  1. Click on the Permissions tab of the Android manifest file for your project.
  2. Add a new Uses Permission element for android.permission.INTERNET.
  3. Add another Uses Permission element for android.permission.ACCESS_NETWORK_STATE.
  4. Save your Android manifest file.

Start Using the Google Analytics SDK Features

You're now ready to start using the SDK!

To start tracking within an Activity class, retrieve an instance of the GoogleAnalyticsTracker. You'll typically want to start tracking in the onCreate() method of your Activity, track various events throughout your Activity lifecycle, and stop tracking in your onDestroy() method.

To start tracking, supplying your unique UA account number, and an interval (seconds) at which to dispatch events to the server, like this:

GoogleAnalyticsTracker tracker = GoogleAnalyticsTracker.getInstance();
tracker.start("UA-1234567-89", 30, this); // Fake UA number here… supply your own!

Track page views by supplying the name of the page or screen:

tracker.trackPageView("/ClickTracker-Main-Screen");

Track events by specifying a category, action, label and value (all developer-defined fields):

tracker.trackEvent("Clicks", "Button", "High Road", 0);

When you're done tracking, close the tracker:

tracker.close();

Interpreting App Statistics Using the Google Analytics Dashboard

Once you've run your application and collected some statistics, you can head over to the Google Analytics dashboard and start interpreting your results. Unfortunately, the statistics are not shown in real time; you have to wait about 24 hours after sending your first event to see it on the site. So your first day's dashboard will look something like this:


Google Analytics dashboard, no stats
Click here for larger image

Figure 1. Google Analytics Dashboard, No Stats

And then, about a day later, your dashboard should look more like this!


Google Analytics Dashboard with few stats
Click here for larger image

Figure 2. Google Analytics Dashboard with Few Stats

Conclusion

This concludes your brief introduction to Google Analytics for Android. The Google Analytics SDK for Android is an effective way to generate application statistics. It's flexible, powerful, and easy to integrate into your applications. There are different types of statistics you can generate for your applications: page views and event tracking, ecommerce events, and other useful information. Remember that whenever you collect data from users, you should make sure they are aware of your activities, so that privacy concerns are addressed. This tutorial just begins to scratch the surface of what you can achieve by using the Google Analytics SDK for Android. Much of the "magic" involves dropping statistics-gathering hooks at clever places within your specific applications.



Shane Conder Shane Conder and Lauren Darcey--Contributing Editors, Mobile Development--have coauthored two books on Android development: an in-depth programming book entitled Android Wireless Application Development (ISBN-13: 978-0-321-62709-4) and Sams Teach Yourself Android Application Development in 24 Hours (ISBN-13: 978-0-321-67335-0). When not writing, they spend their time developing mobile software at their company and providing consulting services.

   Email   |   Blog   |   Twitter   

Lauren Darcey
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap