Refer this: Google Analytics For Android
In details, apart from including the analytic jar from the above link, you need to use the following code:
import android.content.Context;
import com.google.android.apps.analytics.GoogleAnalyticsTracker;
/**
* @description Google analytics This class is used to create Google Analytics
* tracker instance. This will track actions performed in the
* application.
*/
public final class GoogleAnalytics {
private static GoogleAnalyticsTracker tracker;
private static GoogleAnalytics googleAnalytics = null;
private final static int VALUE = -1;
private final static String CATEGORY = "Application Name";
/**
*
* @param context
* @return
*/
public static GoogleAnalytics getGoogleAnalyticsInstance(Context context) {
if (googleAnalytics == null) {
googleAnalytics = new GoogleAnalytics(context);
tracker = GoogleAnalyticsTracker.getInstance();
tracker.startNewSession(--enter your reg number here--, 10, context);
}
return googleAnalytics;
}
private GoogleAnalytics(Context context) {
}
/**
* Stop current session
*/
public void stopSession() {
tracker.stopSession();
googleAnalytics = null;
}
/**
* Tracks Event of actions performed
*
* @param action
* @param label
*/
public void trackEvent(String action, String label) {
tracker.trackEvent(CATEGORY, // Category
action, // Action
label, // Label
VALUE);
}
}
Now anywhere you need to track an event just call:
GoogleAnalytics.getGoogleAnalyticsInstance(this).trackEvent("Event Name", "Event Desc");
To get your application registraion number see this:
Register for Google Analytics
Also remeber to call following when you want a single session to end:
GoogleAnalytics.getGoogleAnalyticsInstance(this).stopSession();