Category Archives: Analytics

How to setup Google Analytics for Android Apps?

Using Google Android Analytics API v4

The official documentation is here: https://developers.google.com/analytics/devguides/collection/android/v4 – but it is confusing.

Download Google Play Services

Using Android SDK Manager download Extras > Google Play Services.

Add Google Play Services to your Eclipse as a Project:

  • File > Import > Android > Existing Android Code into Workspace > Choose Google Play Services Lib project (that you just downloaded)
  • Make sure that yous select: Copy Project into Workspace checkbox.

Add Google Play Services Project as a Library to your Project

Eclipse > Your Android Project > Properties > Android > Library > Add (do not check Is Library checkbox)

Update Android Manifest

Add following in your manifest xml under Application node:

<meta-data
android:name=”com.google.android.gms.analytics.globalConfigResource”
android:resource=”@xml/global_tracker” />
<meta-data
android:name=”com.google.android.gms.version”
android:value=”@integer/google_play_services_version” />

Create Global Tracker XML

Create this file: res/xml/global_tracker.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<resources>

<!– the Local LogLevel for Analytics –>
<string name=”ga_logLevel”>verbose</string>

<!– how often the dispatcher should fire –>
<integer name=”ga_dispatchPeriod”>200</integer>

<!– Treat events as test events and don’t send to google –>
<bool name=”ga_dryRun”>false</bool>

<!– The screen names that will appear in reports –>
<!– <screenName name=”com.whatever.My1Activity”>My 1 Activity</screenName> –>
</resources>

Create App Tracker

Create this file: res/xml/app_tracker.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<resources>

<!– The apps Analytics Tracking Id. Important: Update this –>
<string name=”ga_trackingId”>XX-WHATEVER-Y</string>

<!– Percentage of events to include in reports –>
<string name=”ga_sampleFrequency”>100.0</string>

<!– Enable automatic Activity measurement –>
<bool name=”ga_autoActivityTracking”>true</bool>

<!– catch and report uncaught exceptions from the app –>
<bool name=”ga_reportUncaughtExceptions”>true</bool>

<!– How long a session exists before giving up –>
<integer name=”ga_sessionTimeout”>300</integer>

<!– If ga_autoActivityTracking is enabled, an alternate screen name can be specified to
substitute for the full length canonical Activity name in screen view hit. In order to
specify an alternate screen name use an <screenName> element, with the name attribute
specifying the canonical name, and the value the alias to use instead. –>
<!– <screenName name=”com.activity.My2Activity”>My 2 Activity</screenName> –>

</resources>

 

Create App Class

package com.whatever;

import android.app.Application;

import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.Tracker;

import java.util.HashMap;

public class YourApp extends Application {

// The following line should be changed to include the correct property id.
private static final String PROPERTY_ID = “XX-WHATEVER-Y”; //Important: Update this

// Logging TAG
private static final String TAG = “MyApp”;//Only used for logging

public static int GENERAL_TRACKER = 0;

public enum TrackerName {
APP_TRACKER, // Tracker used only in this app.
GLOBAL_TRACKER, // Tracker used by all the apps from a company. eg:
// roll-up tracking.
ECOMMERCE_TRACKER, // Tracker used by all ecommerce transactions from a
// company.
}

HashMap<TrackerName, Tracker> mTrackers = new HashMap<TrackerName, Tracker>();

public YourApp() {
super();
}

public synchronized Tracker getTracker(TrackerName trackerId) {
if (!mTrackers.containsKey(trackerId)) {

GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);

Tracker t = (trackerId == TrackerName.APP_TRACKER) ? analytics.newTracker(R.xml.app_tracker)

: (trackerId == TrackerName.GLOBAL_TRACKER) ? analytics.newTracker(PROPERTY_ID)

: analytics.newTracker(R.xml.app_tracker);
mTrackers.put(trackerId, t);

}
return mTrackers.get(trackerId);
}
}

 

Update your My1 Activity Class (i.e any Activity Class you want to track)

In onCreate method, just after: setContentView, add following:

((YourApp) getApplication()).getTracker(YourApp.TrackerName.APP_TRACKER);

Also add following two methods:

@Override
protected void onStart() {
super.onStart();
GoogleAnalytics.getInstance(this).reportActivityStart(this);
}

@Override
protected void onStop() {
super.onStop();
GoogleAnalytics.getInstance(this).reportActivityStop(this);
}

Add this code to all Activity Classes you want to track.

That’s it – Run your application.

Go to https://www.google.com/analytics – to see Analytics

Open Questions

  • There does not seem to be any way to track Active Users outside the context of Activities i.e If you do not care about tracking activities, but only want to track active user’s in your app (they maybe on any activity) – there is no way. You have to enable activity level tracking.  Anyone knows better?
  • What if Google Play App (assuming google play services uses Google Play internally) dies on the Android Mobile, will my app hang or get stuck?
  • How can we ensure that our App is unaffected (speed/performance) even if Google Play Services is not working for some reason.
  • Any other considerations – please leave your comments below – will add them to this blog post.
Advertisements