Properly tracking install referrals on Play Store

Amit Tiwari picture Amit Tiwari · Jun 28, 2016 · Viewed 15.9k times · Source

I have a simple task: I want to track the referral id of an app install and pass it to backend.

What I did: I created a link with an extra parameter referrer and appended it to the invite link. When it is opened, the javascript detects if the browser is an Android mobile browser and then prepares an intent and issues a redirect to that intent. While preparing the intent, referrer field is extracted from the url and appended to the intent like this:

intent://scan/#Intent;scheme=com.example.android;package=com.example.android&referrer=4;end

And here is my code for BroadCastReceiver :

public class InstallReferrerReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        TinyDB tinyDB = new TinyDB(context);
        String referrer = intent.getStringExtra("referrer");
        tinyDB.putString(AppConstants.REFERRAL_ID, referrer);
        tinyDB.putBoolean(AppConstants.REFERRAL_SENT, false);
    }
}

So, what I expect to get here as the value of referrer is 4 based on the above intent. But the value that I am getting is this String utm_source=google-play&utm_medium=organic

What am I doing wrong and how can I fix it to get the correct value for referrer field?

Edit

I don't have any issues in creating the url or extracting values from referrer field once the app is installed.

Once the invite link is clicked through any button click or opened directly in the mobile browser, I use the above to "either open the app if it is already installed or open the app's page on Play Store app for users to install it".

The issue is, how should I pass the value of referrer field from the invite link to the Play Store app through the above intent so that the Play Store receives this value and passes it to the app when it is installed.

Answer

Neo picture Neo · Jul 7, 2016

You need to test it properly, I am posting mine use case, hope it will solve your problem :)

Refferal URL -

https://play.google.com/store/apps/details?id=com.mypackage&referrer=utm_source%3Dmobisoc%26utm_content%3D{transaction_id}%26utm_campaign%3D1

Code to receive refferal -

public static final String KEY_UTM_SOURCE = "utm_source";
public static final String KEY_UTM_CONTENT = "utm_content";
public static final String KEY_UTM_CAMPAIGN = "utm_campaign";
public void onReceive(Context context, Intent intent) {
    Utils.log("Referral Received");
    try {
        String referrer = intent.getStringExtra("referrer");
        if (referrer != null && !referrer.equals("")) {
            Utils.log("Referral Received - " + referrer);
            String[] referrerParts = referrer.split("&");
            String utmSource = getData(KEY_UTM_SOURCE, referrerParts);
            String utmContent = getData(KEY_UTM_CONTENT, referrerParts);
            String utmCampaign = getData(KEY_UTM_CAMPAIGN, referrerParts);
            if (utmSource != null && utmSource.equals("mobisoc")) {
                sendLogToMobisocServer(context, utmContent);
            } else if (utmSource != null && utmSource.equals("app_share")) {
                RawStorageProvider.getInstance(context).dumpDataToStorage(RaghuKakaConstants.REFFERAL_FOR, utmContent);
            }
            updateRKServerForReferral(context, utmSource, utmCampaign, utmContent);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private String getData(String key, String[] allData) {
    for (String selected : allData)
        if (selected.contains(key)) {
            return selected.split("=")[1];
        }
    return "";
}

Now the most important part testing. You can test the referral locally. Just you need to attach your phone, open the shell prompt by using adb shell. And broadcast the referral data. Here are the command sequence example -

C:\Users\Neo\Desktop>adb shell
$ am broadcast -a com.android.vending.INSTALL_REFERRER -n com.mypackage/<className of your ReferralReceiver with package> --es "referrer" "utm_source%3Dmobisoc%26utm_content%3D{transaction_id}%26utm_campaign%3D1"

Additional -

https://play.google.com/store/apps/details?id=com.mypackage&referrer=utm_source%3Dmobisoc%26utm_content%3D{transaction_id}%26utm_campaign%3D1

Just see my link. If user will go to the playstore via that link, and install the app. Then first time when the app will launch, your onReceive method will be fired automatically, and you will get all the data after referrer=.

Broadcast -

$ am broadcast -a com.android.vending.INSTALL_REFERRER -n com.mypackage/<className of your ReferralReceiver with package> --es "referrer" "utm_source%3Dmobisoc%26utm_content%3D{transaction_id}%26utm_campaign%3D1"

For testing it you no need to publish your app on playstore, Just put a debug point on first point of onReceive, launch in debug mode, and fire the command sequences I have posted, you will get all the data after "referrer" tag. So by this you can decide what data you need to add while creating the referrer link.

Let me know in case of more clarification you need :)