Install referrer information in Android.

You have seen many application have a option to referrer your buddy via text, WhatsApp etc. so will see how we can do the same in our application.

firstly every user should have some unique key eg- userId so that you can verify who referred whom. suppose your userId is abc123 now created a refer url eg- www.yoururl.com/refer/abc123 now whoever will click on this can be install/open app.

How to receive refer code from playstore

below we are registering receiver to receive data after app install from playstore

<receiver android:name="com.package.app.InstallReceiver" android:exported="true">
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>

This will broadcast the a action of INSTALL_REFERRER which we need to receive and extract the data

public class InstallReferrer extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction() != null) {
if (intent.getAction().equals("com.android.vending.INSTALL_REFERRER")) {

Bundle extras = intent.getExtras();
if (extras != null) {
String referrerString = extras.getString("referrer");
}
}
}
}
}

Now we are done with the app data receive part after installing the app from playstore but a big question is how to test ?

So, we can generate a broadcast from adb and text

adb shell am broadcast -a com.android.vending.INSTALL_REFERRER -n your.package.name/com.package.app.InstallReferrer --es "referrer"--es "referrer" "utm_source=test_source&utm_medium=test_medium&utm_term=utm_test&utm_content=test_content&utm_campaign=test_name"

And, If app is getting launch from link directly then you can fetch the url using intent

getIntent().getData().toString()

But you need to add intentFilter

<intent-filter>
<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />

<data
android:host="yourappurl"
android:pathPrefix="/"
android:scheme="http"
/>
<data
android:host="yourappurl"
android:pathPrefix="/"
android:scheme="https"
/>
</intent-filter>

Please do comment if you have any or clap if you like.

Lead Android Engineer