32

I have noticed some Google Play app links in the browser has the referrer= attribute to them, which obviously tells the referrer that sent you to that app's page in Google Play.

Is it possible to see that referrer (if any) in the code of my app? And if not, to see it anywhere at all?

Cœur
  • 32,421
  • 21
  • 173
  • 232
Borislav
  • 879
  • 3
  • 10
  • 22

3 Answers3

57

You can use com.android.vending.INSTALL_REFERRER.

The Google Play com.android.vending.INSTALL_REFERRER Intent is broadcast when an app is installed from the Google Play Store.

Add this receiver to AndroidManifest.xml

<receiver
    android:name="com.example.android.InstallReferrerReceiver"
    android:exported="true"
    android:permission="android.permission.INSTALL_PACKAGES">
    <intent-filter>
        <action android:name="com.android.vending.INSTALL_REFERRER" />
    </intent-filter>
</receiver>

Create a BroadcastReceiver:

public class InstallReferrerReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String referrer = intent.getStringExtra("referrer");

        //Use the referrer
    }
}

You can test the referral tracking following the steps of this answer.

Mattia Maestrini
  • 30,486
  • 15
  • 81
  • 89
  • 2
    I just updated the answer with a link to an answer that explains how to test referral tracking. – Mattia Maestrini May 15 '15 at 09:03
  • just did it, and I get "Broadcast completed: result=0" when installing it directly from Eclipse. Is that it? Do I need to set up anything else in my app or Google Play link for it, to get the referrer? – Sartheris Stormhammer May 15 '15 at 09:08
  • You should receive the intent in the `onReceive` of your `BroadcastReceiver`. If you didn't receive anything probably you have made some mistake in configuring the receiver in your AndroidManifest.xml – Mattia Maestrini May 15 '15 at 09:16
  • I did it just like you said. Also, your example is very different from the one here https://developer.android.com/reference/com/google/android/gms/tagmanager/InstallReferrerReceiver.html why? – Sartheris Stormhammer May 15 '15 at 09:27
  • ok, I debugged it with adb shell, with the command you linked me to, and now it entered in debug mode in those receivers, is it working now? – Sartheris Stormhammer May 15 '15 at 09:32
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/77883/discussion-between-sartheris-stormhammer-and-mattia). – Sartheris Stormhammer May 15 '15 at 09:40
  • sorry for bothering you again, but when I put this code in Google Play, and a user downloads and runs my app, when will the code inside the BroadcastReceiver run? – Sartheris Stormhammer May 15 '15 at 12:39
  • 2
    The intent is broadcast when an app is installed from the Google Play Store. So when the user installs your app the Google Play Store send the intent and your code inside onReceiver will be executed. – Mattia Maestrini May 15 '15 at 12:54
  • 1
    Can we have other parameters other than `referrer`, something like `source`? – tehmaestro Sep 15 '15 at 12:12
  • What if user not opening app immediately from Google Play store? Can we save Referral in SharePrefrence from the receiver itself? – piyush poriya Jun 28 '19 at 07:37
8

Use Google Play Referrer API (from 20 Nov 2017)

InstallReferrerClient mReferrerClient
...
mReferrerClient = newBuilder(this).build();
mReferrerClient.startConnection(this);

@Override
public void onInstallReferrerSetupFinished(int responseCode) {
   switch (responseCode) {
       case InstallReferrerResponse.OK:
           try {
               ReferrerDetails response = mReferrerClient.getInstallReferrer();
               String referrer = response.getInstallReferrer()
               mReferrerClient.endConnection();
           } catch (RemoteException e) {
               e.printStackTrace();
           }
           break;
       case InstallReferrerResponse.FEATURE_NOT_SUPPORTED:
           Log.w(TAG, "InstallReferrer not supported");
           break;
       case InstallReferrerResponse.SERVICE_UNAVAILABLE:
           Log.w(TAG, "Unable to connect to the service");
           break;
       default:
           Log.w(TAG, "responseCode not found.");
   }
}
Deven
  • 2,438
  • 1
  • 22
  • 30
4

Campaign Parameters are used to pass information about the campaign or traffic source that referred a user to your app's Google Play Store page into your app's Google Analytics implementation.

Once you've built your campaign parameter string, add it to your Google Play Store URLs as the value of the referrer parameter, as in this example:

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

The Google Play Store will pass the value of the referrer parameter to your app's Google Analytics implementation.

References: https://developers.google.com/analytics/devguides/collection/android/v2/campaigns https://developers.google.com/analytics/devguides/collection/android/v2/campaigns#google-play-url-builder

aygul
  • 3,079
  • 12
  • 34
  • 42
  • I don't understand, so lets say I have an app already in Google Play, and I have analytics SDK in it, what then, how do I get in the code who is the install referrer? – Borislav May 08 '15 at 06:48
  • @Borislav To get the code, follow the tutorial https://blog.branch.io/technical-guide-to-google-play-referrer/ – Aniket Feb 07 '20 at 12:19