This article is part of an Android Development Guide. The guide focuses on several subjects necessary to build a fully functional basic app, talking about several features that were introduced in Honeycomb and are still valid for Ice Cream Sandwich. We use a Google Buzz client called Honeybuzz as an example for each topic. Refer to the introduction for a complete list of all articles.
Analytics will help you understand which parts of your application are most popular and where users get stuck the most. Google Analytics is not only for websites, it can also be used for Android applications.
Installing Google Analytics in your Android project
To add Google Analytics support in your Android application you need to follow these steps:
- Download the Google Analytics SDK for Android.
- Add the library libGoogleAnalytics.jar to your project. For how to do that refer to the article Development environment and project creation (see the part about adding external libraries).
- Add the necessary permissions to your Android manifest.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
- Go to your Google Analytics account and create a (fake) website profile. Make sure to choose a descriptive URL.
- You will have to indicate to your users, either somewhere in the application or in your terms of service, that you are tracking them anonymously.
Tracking a referrer install
An important part of your application's analytics is to know the source of the application install (in case you're using an advertisement, for example, or if you have several installation links in different websites).
To be able to track that you need to use a link with the following format:
- http://market.android.com/search?q=pname:<package>&referrer=<referral>
- <package> is the name of your application's package.
- <referral> is a campaign specific information.
- You can generate URLs using the Android Market Campaign Tracking tool.
You also need to add this to your application's manifest:
<application>
<!-- Used for install referrer tracking (analytics) -->
<receiver android:name="com.google.android.apps.analytics.AnalyticsReceiver"
android:exported="true">
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>
</application>
Adding analytics tracking to your Activities
You can use the GoogleAnalyticsTracker to track page views and events in your application. A simple example:
GoogleAnalyticsTracker tracker = GoogleAnalyticsTracker.getInstance();
tracker.start("your analytics tracking ID", 20, this);
tracker.trackPageView("/welcome");
this.tracker.trackEvent("Category", "Action", "Label", "Value");
tracker.setCustomVar(1, "Name", "Value"); // first parameter is the slot (1 to 5)
To save battery life, tracked data is only dispatched at certain intervals (in the above example, every 20 seconds), but you can also dispatch it manually by using the tracker.dispatch() method.
To make our life easier, instead of manually adding code to all Activities, I created a class (from which all Activities will inherit) that not only tracks the current Activity, but also provides useful methods for tracking events and custom vars.
package com.quasibit.honeybuzz;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import com.google.android.apps.analytics.GoogleAnalyticsTracker;
public class TrackedActivity extends Activity {
private static final String TRACKER_ID = "your analytics tracking ID ";
private static final boolean DISPATCH_MANUAL = false;
private static final int DISPATCH_INTERVAL = 20;
private static final boolean DEBUG = false; // If true stores analytics requests in the log cat
private static final boolean DRY_RUN = false; // If true only stores data locally (doesn't send data to analytics)
@SuppressWarnings("unused")
private static final int SCOPE_VISITOR = 1; // Call the first time your application is run on a device. Useful for anything that won't change during the lifetime of the installation of the application (app version, lite vs full, type of phone).
@SuppressWarnings("unused")
private static final int SCOPE_SESSION = 2; // Call at the beginning of an Activity. Applies to all pageviews and events for the lifecycle of the activity.
@SuppressWarnings("unused")
private static final int SCOPE_PAGE = 3; // Call before trackEvent or trackPageView that the custom variable should apply to.
@SuppressWarnings("unused")
private static final int SLOT_1 = 1;
@SuppressWarnings("unused")
private static final int SLOT_2 = 2;
@SuppressWarnings("unused")
private static final int SLOT_3 = 3;
@SuppressWarnings("unused")
private static final int SLOT_4 = 4;
@SuppressWarnings("unused")
private static final int SLOT_5 = 5;
private GoogleAnalyticsTracker tracker;
private String pageId;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.pageId = getClass().getSimpleName();
this.tracker = GoogleAnalyticsTracker.getInstance();
if (DISPATCH_MANUAL) {
this.tracker.start(TRACKER_ID, this);
} else {
this.tracker.start(TRACKER_ID, DISPATCH_INTERVAL, getApplicationContext());
}
this.tracker.setDebug(DEBUG);
this.tracker.setDryRun(DRY_RUN);
}
@Override
protected void onResume() {
super.onResume();
this.tracker.trackPageView("/" + this.pageId);
}
@Override
protected void onDestroy() {
super.onDestroy();
this.tracker.dispatch();
this.tracker.stop();
}
public void dispatch() {
if (this.tracker != null) {
this.tracker.dispatch();
}
}
public static void dispatch(final Context baseContext) {
final GoogleAnalyticsTracker tracker = GoogleAnalyticsTracker.getInstance();
tracker.start(TRACKER_ID, baseContext);
tracker.dispatch();
tracker.stop();
}
protected void trackPage(final String page) {
this.tracker.trackPageView(page);
}
protected void trackEvent(final String category, final String action, final String label, final int value) {
this.tracker.trackEvent(category, action, label, value);
}
protected void trackEvent(final String category, final String action, final String label) {
trackEvent(category, action, label, 0);
}
protected void trackEvent(final String action, final String label, final int value) {
this.tracker.trackEvent(this.pageId, action, label, value);
}
protected void trackEvent(final String action, final String label) {
trackEvent(this.pageId, action, label, 0);
}
protected void setCustomVar(final int slot, final String name, final String value, final int scope) {
this.tracker.setCustomVar(slot, name, value, scope);
}
protected void setCustomVar(final int slot, final String name, final String value) {
this.tracker.setCustomVar(slot, name, value);
}
}
Because we will also be using Fragments, a TrackedFragment class was also built.
package com.quasibit.honeybuzz;
import com.google.android.apps.analytics.GoogleAnalyticsTracker;
import android.app.Fragment;
import android.os.Bundle;
public class TrackedFragment extends Fragment {
private GoogleAnalyticsTracker tracker;
private String activityId;
private String fragmentId;
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.tracker = GoogleAnalyticsTracker.getInstance();
this.fragmentId = getClass().getSimpleName();
this.activityId = getActivity().getClass().getSimpleName();
}
@Override
public void onResume() {
super.onResume();
this.tracker.trackPageView("/" + this.activityId + "/" + this.fragmentId);
}
}
For more information on how to use Google Analytics in Android be sure to watch the Google I/O session on Optimizing Android Apps with Google Analytics. Another good resource is the article Analytics for Android Apps that was published in the Android Developers blog.