Categories: Android Google Play

How to get install referrer information in Android

Google has switched to new Google Play Install Referrer API. Earlier Android had INSTALL_REFERRER broadcast receiver for receiving install information.

the new api is simple and secure. Lets check both methods to find install referrer information.

Using Broadcast Receiver:

Create broadcast receiver and register it in AndroidManifest.xml file.

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");
                    Log.w("TAG", "Install</code><code class="language-java"> R</code><code class="language-java">eferrer</code><code class="language-java"> is: " + referrerString);
                }
            }
        }
    }
}

Register broadcast receiver in AndroidManifest.xml

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

To validate your referrer you can use this adb shell command to send install referrer.

adb shell am broadcast -a com.android.vending.INSTALL_REFERRER -n com.package.app/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"

Using Install Referrer API:

This is the preferred reliable version to get install referrer information. Lets check how to use install referrer api in your code.

In app build.gradle add install referrer client api dependency.

implementation 'com.android.installreferrer:installreferrer:1.0'

Now where ever install referrer information is sought this code can be added to fetch install data.

import android.os.Bundle;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;

import com.android.installreferrer.api.InstallReferrerClient;
import com.android.installreferrer.api.InstallReferrerStateListener;
import com.android.installreferrer.api.ReferrerDetails;

import java.net.URLDecoder;
import java.util.HashMap;

public class InstallMainActivity extends AppCompatActivity implements InstallReferrerStateListener {

    private InstallReferrerClient mReferrerClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        
        mReferrerClient =  InstallReferrerClient.newBuilder(this).build();
        mReferrerClient.startConnection(this);

    }

    @Override
    public void onInstallReferrerSetupFinished(int responseCode) {
        switch (responseCode) {
            case InstallReferrerClient.InstallReferrerResponse.OK:
                // Connection established

                try {
                    getReferralUser();
                } catch (RemoteException e) {
                    e.printStackTrace();
                }

                break;
            case InstallReferrerClient.InstallReferrerResponse.FEATURE_NOT_SUPPORTED:
                // API not available on the current Play Store app
                break;
            case InstallReferrerClient.InstallReferrerResponse.SERVICE_UNAVAILABLE:
                // Connection could not be established
                break;
            case InstallReferrerClient.InstallReferrerResponse.DEVELOPER_ERROR:
                break;
            case InstallReferrerClient.InstallReferrerResponse.SERVICE_DISCONNECTED:
                break;
        }
    }

    private void getReferralUser() throws RemoteException {
        ReferrerDetails response = mReferrerClient.getInstallReferrer();
        String referrerData = response.getInstallReferrer();
        Log.e("TAG", "Install referrer:" + response.getInstallReferrer());


        // for utm terms
        HashMap<String, String> values = new HashMap<>();
        if (values.containsKey("utm_medium") && values.containsKey("utm_term")) {
            try {
                if (referrerData != null) {
                    String referrers[] = referrerData.split("&");

                    for (String referrerValue : referrers) {
                        String keyValue[] = referrerValue.split("=");
                        values.put(URLDecoder.decode(keyValue[0], "UTF-8"), URLDecoder.decode(keyValue[1], "UTF-8"));
                    }

                    Log.e("TAG", "UTM medium:" + values.get("utm_medium"));
                    Log.e("TAG", "UTM term:" + values.get("utm_term"));

                }
            } catch (Exception e) {

            }
        }
    }

    @Override
    public void onInstallReferrerServiceDisconnected() {

    }
}

Share

Recent Posts

  • Firebase

Save and Access Environment Variables in Firebase

Environment variables in the cloud functions allows you to set/get key values at the time of deployment which can later…

1 week ago
  • Android

How to migrate to AndroidX support library

AndroidX is the latest Android support library package and is an improved version of older support libraries. Android Studio seamlessly…

1 month ago
  • Ads Integration

Unity: How to automatically add Admob App ID

Admob is one of the most used Ad network used by developers all over the world. In recent sdk update…

4 months ago
  • Android
  • Guides

How to use Accessibility Service in Android

Accessibility Service is used to assist users with disabilities. Accessibility services are background services which is invoked by the system…

4 months ago
  • Android
  • Guides

How to track real time location in Android with Google Maps

Realtime location tracking is the backbone of all delivery and tracking apps in the market. For example tracking your food…

5 months ago
  • Android
  • Google Play
  • Guides

How to get user location in Android

Android Play Services SDK offers Location API that is used to get gps location. Location updates is one of the…

5 months ago