androidandroid-broadcastreceiverreferrals

Referral Code not retrieve after installing from play store


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...


Solution

  • 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;
            }
    }