Uncategorized

Tracking referrers for your Android app

Our Android app mCent has seen a tremendous growth in its user-base since its launch in May, 2014. It is currently the the #1 lifestyle app in India and has consistently held a position among the top 10 apps in that country. Some of the marketing success can be attributed to referrals from our existing users.

To encourage our users to refer their friends to mCent, we have developed many referral related features. Thanks to the referrer broadcast from the Play store on installation, we can detect who referred you to the app, identify the features that effectively drive referrals, and improve them.

This android feature is extremely useful when you need to track what campaigns are driving your app installations. In this post, I will present a short tutorial on how to configure your android app to utilize this referrer broadcast.

When is the broadcast made?

The Play store makes the broadcast after the installation if the play store link has the referrer argument in it. For example, https://play.google.com/store/apps/details?referrer=tc%3DWAPP%26mcode%3D0F9HZE&id=com.mcent.app has a referrer argument in the link and the value stored in the argument contains information about who had referred the app and what medium they used to make the referral.

Configuring your app

The Android Manifest

First you need to register the broadcast consumer in AndroidManifest.xml.

 <service android:name="com.google.android.gms.analytics.CampaignTrackingService" />
 <receiver
 android:name=".receivers.InstallReceiver"
 android:exported="true" >
 <intent-filter>
 <action android:name="com.android.vending.INSTALL_REFERRER" />
 </intent-filter>
 </receiver>

Here we are configuring our app to listen to the intent INSTALL_REFERRER and receive the broadcast event by InstallReceiver.

InstallReceiver


package com.example.app.receivers;
...
 
import java.net.URLDecoder;

...


public class InstallReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String rawReferrer = intent.getStringExtra("referrer");
        if (rawReferrer != null) {
            trackReferrerAttributes(rawReferrer, context);
        }
    }

    private void trackReferrerAttributes(String rawReferrer, Context context) {
        String referrer = "";

        try {
            referrer = URLDecoder.decode(rawReferrer, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            return;
        }

        if (Strings.isNullOrEmpty(referrer)) {
            return;
        }

        Uri uri = Uri.parse('?' + referrer); // appends ? for Uri to pickup query string
    
        String memberCode;
        try {
            referringMember = uri.getQueryParameter("mcode");
        } catch (UnsupportedOperationException e) {
            return;
        }

        SharedPreferences.Editor editor = context.getSharedPreferences(
            BuildConfig.PACKAGE_NAME, Context.MODE_PRIVATE).edit();

        if (!Strings.isNullOrEmpty(memberCode)) {
            editor.putString(Constants.REFERRER_CODE, memberCode);
        }

        String referralMedium = uri.getQueryParameter("tc");
        if (!Strings.isNullOrEmpty(referralMedium)) {
            editor.putString("referral_medium", referralMedium);
        }

        editor.apply();
    }
}

InstallReceiver extends BroadcastReceiver and implements the onReceive method to get the referrer string stored in the received intent. In this implementation, trackReferrerAttributes gets called to store the referring member id and app used for sending the link in SharedPreference. The data can then be used later by the application for different purposes.

Debugging the code

You can broadcast the intent using adb shell. adb can be found in <sdk>/platform-tools/. You can start the shell by simply typing ./adb shell. The broadcast can be simulated by executing the command am broadcast -a com.android.vending.INSTALL_REFERRER -n "com.mcent.app.local/com.mcent.app.receivers.InstallReceiver" --es "referrer" "<your string here>" in the shell.

That is all you need to do to track who is referring installations of your app. Please feel free to comment if you have any questions or suggestions. Thanks to  for implementing this functionality in the mCent app.

Discussion

24 responses to ‘Tracking referrers for your Android app

  1. Hello,
    I am using the above code which is describe, But I am not getting how I open the other app by code not cmd command.

    I am waiting for your response.

    Thanks & Regards,
    Vivek Kumar

      • Hello Binay,

        Let’s example I have an Android App A that is installed in the user’s device and I have an AppWidget/Or List Options with in App A, where we let other Android developers publish their App promotion ads on a Cost Per Install basis, Open App & Install app in device for a number of days. So I need to track if App B is being installed on the user’s device through App A’s referral.

        For This What I need to do? I am waiting for your response.

        Thanks & Regards,
        Vivek Kumar

    • Hi Vivek,

      You need to have implemented a feature in your android app B that somehow informs your app A that the installation was referred by your app A. If you only have control over A but not over B and B does not directly/indirectly lets you know, I do not think it will be possible for your app to ascertain who referred the user to the app B.

      Best way to solve your problem is to find out if app B has any mechanisms to report its installation source and develop a relationship with its developers so that app B communicates that information to your service.

      Hope that helps.

      • Hello Binay,

        Can you please tell me how you are managing in MCent application? Is other apps(Ads app) provide you some information internally?

        I am waiting for your response.

        Thanks & Regards,
        Vivek Kumar

    • Hi Binay..

      Can you please let us know which tracking & reward distribution platform is being used in Mcent to distribute commissions to respective users of Mcent? How your app distribute payment in real time ??

  2. Hi Binay,
    Does this work even if the user clicks the install button on the Google Play Store on a desktop computer and the app gets installed on the phone / tablet?

    Thanks and regards
    Aditya Rao

    • Thanks a lot for your reply sir…
      Now i am getting the referral in my application..

      Problem is that :- If i have already installed the application in my device than i am NOT able to get refferal from google play store…Please help me out to short from these problem .. Is there is another way to get referral from google play store when the application is already installed on my device…Thank you very much sir 🙂

    • Yes Yogi.. It broadcast only when you install the app from the google play…Not boradcast when the app is already installed on yours device

  3. What happens when the user installs another app with a referrer?
    Will the broadcast receiver in my app listen to this broadcast? If yes, how to prevent the listener from getting the referrer when other apps are installed with a referrer?
    Thanks

  4. Will the broadcast receiver also listen to broadcasts when other apps are installed with a referrer? If yes, then how to limit the broadcast receiver only to my app

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s