How to get Google Play referer in an Android application

You are looking for Campaign Measurement. In the documentation, it discusses how using INSTALL_REFERRER will help you determine which source is sending users to your App in the Google Play Store.

It's as simple as placing a receiver in your AndroidManifest and modifying your App's Google Play URLs.

From the docs:

Google Play Campaign Attribution

Google Play Campaign Measurement allows you to see which campaigns and traffic sources are sending users to download your app from the Google Play Store. It is recommended that all developers implement Google Play Store Campaign Measurement.

Implementing Google Play Campaign Attribution

When your app is downloaded from Google Play Store, the Play Store app broadcasts an INSTALL_REFERRER intent to your app during installation. This intent contains the value of the referrer parameter of the link used to reach your app's Google Play Store page, if one was present.

To attribute an app download to a campaign, you must add a referrer parameter to any links that point to Google Play Store, and add a BroadcastReceiver to your app to receive and set the campaign information contained in the intent on your Google Analytics tracker.

It is recommended that most developers use the BroadcastReceiver provided with the SDK. To implement Google Play Store Campaign Measurement using the included receiver:

1. Add the Google Analytics receiver to your AndroidManifest.xml file. To add the Google Analytics receiver to the manifest, copy and paste the following markup:

    <application>
    <!-- Used for Google Play Store Campaign Measurement-->
    <receiver android:name="com.google.android.gms.analytics.CampaignTrackingReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="com.android.vending.INSTALL_REFERRER" />
        </intent-filter>
    </receiver>
    <service android:name="com.google.android.gms.analytics.CampaignTrackingService"
        android:enabled="true"
        android:exported="false" />
</application>
  1. Add Google Analytics Campaign Parameters to Google Play URLs

    Next, add a referrer parameter to any URLs that will be linking directly to Google Play Store and set the value of that parameter to a string of Google Analytics campaign parameters that describe the source, as in this example:

    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

To learn how to build a campaign parameter strings, use the Google Play URL Builder, or consult the Campaign Parameters reference section.

Testing Google Play Campaign Attribution

To verify that your Google Play Campaign Measurement implementation is working as expected before publishing your app, use the Testing Google Play Campaign Attribution Solution Guide.

Also, see this similar post.


Try a Play Install Referrer Library.

It is a new, easy to use and reliable way to securely retrieve install referral content

https://android-developers.googleblog.com/2017/11/google-play-referrer-api-track-and.html

Also, switching to a new referrer API allows for deeper insights into the user conversion funnel, secures the Google Play Store referrer and finally (and most importantly), the additional data provided allows for the complete eradication of click injections.

(source: https://www.adjust.com/blog/eliminating-click-injections-with-google-play-referrer-api/)

Example:

final InstallReferrerClient referrerClient = InstallReferrerClient.newBuilder(YourActivity.this).build();
referrerClient.startConnection(new InstallReferrerStateListener() {

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

                    try {
                        ReferrerDetails response = referrerClient.getInstallReferrer();
                        String installReferrer = response.getInstallReferrer();

                        // handle referrer string

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

        @Override
        public void onInstallReferrerServiceDisconnected() {
            // Try to restart the connection on the next request to
            // Google Play by calling the startConnection() method.
        }
});

Add the following line to the dependencies section of the build.gradle file for your app:

dependencies {
    ...
    compile 'com.android.installreferrer:installreferrer:1.0'
}