3

I needed to integrate referral code implementation in my app for that I created url with : https://play.google.com/store/apps/detailsid=MY_PACKAGE_NAME&referrer=USER_REFERRAL_CODE

and created broadcast receiver for that

InstallReferrerReceiver.java

public class InstallReferrerReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals("com.android.vending.INSTALL_REFERRER")) {
        String referrer = "";
        Bundle extras = intent.getExtras();
        if (extras != null) {
            referrer = extras.getString("referrer");
        }
        Log.e(TAG, "Referal Code Is: " + referrer);
        AppMethod.setStringPreference(context, AppConstant.PREF_REF_ID, referrer);
       }
    }
 }

Register Receiver in manifest.xml

<receiver
        android:name="com.gum.getumoney.Service.InstallReferrerReceiver"
        android:exported="true">
        <intent-filter>
            <action android:name="com.android.vending.INSTALL_REFERRER" />
        </intent-filter>
</receiver>

There is my receiver call after installing app from play store but I got null value in Referral Code

I needed to get user code who refer app to another user. But to doing this I am getting fail. Also I test my receiver in Terminal using shell script it works fine for me.

So if there is any issue with this code then address me for doing this or suggest me another way to doing this. Thanks...

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Tecksky Android
  • 133
  • 3
  • 14

1 Answers1

2

Verify that the play store url you are testing with is correct and has the expected value for your test. Follow the scheme defined as:

https://play.google.com/store/apps/details?id=com.example.application
&referrer=utm_source%3Dgoogle
%26utm_medium%3Dcpc
%26utm_term%3Drunning%252Bshoes
%26utm_content%3Dlogolink
%26utm_campaign%3Dspring_sale

For more info, check the documentation at https://developers.google.com/analytics/devguides/collection/android/v4/campaigns.

for example to make a referral:

 public void sendReferral(Context context) {
            Intent sendIntent = new Intent();
            sendIntent.setAction(Intent.ACTION_SEND);
            sendIntent.putExtra(Intent.EXTRA_TEXT, getInvitationMessage()), PreferencesManager.getInstance().getKeyReferrerUrl()));
            sendIntent.putExtra(Intent.EXTRA_SUBJECT, context.getString(R.string.invitation_subject));
            sendIntent.setType("text/plain");
            context.startActivity(Intent.createChooser(sendIntent, context.getResources().getText(R.string.invitation_extended_title)));
        }

private String getInvitationMessage(){
  String playStoreLink = "https://play.google.com/store/apps/details?id=app.package.com&referrer=utm_source=";
  return invitationId = playStoreLink + getReferralId();
}

Then in your receiver:

public class InstallReferrerReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        if (intent == null) {
            return;
        }

        String referrerId = intent.getStringExtra("referrer");

        if (referrerId == null){
            return;
        }
}
Brian
  • 573
  • 4
  • 8
  • If i use this for share play store url then where do i put my referral code and how to retrive it. I check google developer console also but not getting it properly. If you have any sample then please provide it to me. – Tecksky Android Aug 25 '17 at 12:50
  • @Brian how we can test it without sending a build on playstore? – Parth Anjaria Aug 03 '18 at 10:06
  • You can test it locally using adb: adb shell am broadcast -a com.android.vending.INSTALL_REFERRER -n --es referrer Note the the path to your BroadcastReceiver should include your package name. – Tako Nov 13 '18 at 22:27