Tracking install sources for Android apps

Have you ever wanted to have a better idea of what’s the best source of your app installs? There’s information around the web on how to do it, but spread out over various sites, so I decided to write this post to consolidate the information.

Starting with Android 1.6, the Android Market’s emits a broadcast Intent named com.android.vending.INSTALL_REFERRER whenever certain parameters are added to the market url. Note that web version of the Market also passes these parameters to your devices upon install. Since the parameters need to follow the Google Analytics referral link format, Google provides a simple tool for generating these urls, or you could use a copy of my shared Google spreadsheet that I use to keep track of all my campaigns. Here’s a description for the important parameters:

ParameterDescription
utm_campaignUsed for keyword analysis. Use utm_campaign to identify a specific product promotion or strategic campaign. 
Example: utm_campaign=spring_sale
utm_mediumUse utm_medium to identify a medium such as email or cost-per- click. 
Example: utm_medium=cpc
utm_sourceUse utm_source to identify a search engine, newsletter name, or other source. 
Example: utm_source=google

So now you have a url with information that is broadcast by the Android Market when your app is installed. The next step is to capture that information inside your app and submit them to your mobile analytics provider (Google Analytics, Flurry, Localytics, etc). The way to do that is to first register a BroadcastReceiver in your AndroidManifest.xml. You’ll need to handle the com.android.vending.INSTALL_REFERRER broadcast like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 ...>
    <application ...>
        <acivity ... />

        <!-- Android Market install receiver -->
        <receiver
            android:exported="true"
            android:name="com.your.namespace.InstallReferrerReceiver" >
            <intent-filter >
                <action android:name="com.android.vending.INSTALL_REFERRER" />
            </intent-filter>
        </receiver>
    </application>
</manifest>

Next we need the implement the class that handles the broadcast. All the class does is receives the INSTALL_REFERRER BroadcastIntent, takes the ‘referrer’ extra from the intent and breaks it down into key/value pairs for submitting it to the analytics package. (AnalyticsTrackerFactory is the Gist is just my generic wrapper for analytics; you can call your own directly instead).

public class InstallReferrerReceiver extends BroadcastReceiver {
	private static final String TAG = "InstallReferrerReceiver";

	@Override
	public void onReceive(Context context, Intent intent) {
		HashMap<String, String> values = new HashMap<String, String>();
		try {
			if (intent.hasExtra("referrer")) {
				String referrers[] = intent.getStringExtra("referrer").split("&");
				for (String referrerValue : referrers) {
					String keyValue[] = referrerValue.split("=");
					values.put(URLDecoder.decode(keyValue[0]), URLDecoder.decode(keyValue[1]));
				}
			}
		} catch (Exception e) {
		}

		Log.d(TAG, "referrer: " + values);
		AnalyticsTrackerFactory.getTracker(context).event("Installed", values);
	}
}

So that’s all there is to it! Just take some vary basic steps and small changes to your app to easily track who’s referring downloads of your app.

One Reply to “Tracking install sources for Android apps”

  1. I have checked the BroadcastReceiver but I am confused in that how to call Install referrer from the app and which jar file I need to use?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.