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:
Adding the JAR file to your project within Eclipse is easy. Simply:
- Click on the Project properties for your Android project.
- Under the Java Build Path settings, select the Libraries tab.
- 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:
- Click on the Permissions tab of the Android manifest file for your project.
- Add a new Uses Permission element for
android.permission.INTERNET
.
- Add another Uses Permission element for
android.permission.ACCESS_NETWORK_STATE
.
- 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:
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!
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.