Open In App
Related Articles

How to Use Google Play Install Referrer API in Android?

Improve Article
Save Article
Like Article

Google Play Install Referrer is the API that is used in most of the applications but it is not been seen in the app. This functionality works under the hood and is used to check the sources from where the app is getting most of the downloads. Google Play Install Referrer API tells us that from where the app has to go installs and the sources. This will helps us to improve the presence of our apps on different platforms. 

What is the use of Google Play Referrer API? 

Google Play Referrer API provides us information from where our app has been installed whether it may be play store or any other platform. With the help of this API, we can track the actions which are taken by the user to download our App. Below are some of the important data which we can gather using this API. 

  • With the help of this API, we can track from where the user has installed our application. We can get the URL from which our app has been downloaded.
  • We can get the timestamp when the user clicks on the referrer URL.
  • We can get the timestamp of the user when the user downloads our app from a specific URL.
  • We can get the app version when our app was first installed.
  • We can track whether the user has used the app’s instant experience with the previous 7 days.

What we are going to build in this article? 

We will be building a simple application in which we will be adding our Google Play Referrer API and we will be displaying the data which is tracked by this API in a simple text view. Note that as we have not published our App to Google Play. So we will only get the source from which our app installed as organic. Below is the screenshot in which we will get to see what we are going to build in this article. Note that we are going to implement this project using the Java language. 

Step by Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.

Step 2: Add the dependency for Google Play Referrer API in the build.gradle file

Navigate to Gradle scripts and then to build.gradle(Module) level. Add below line in build.gradle file in the dependencies section.

// below is the dependency for referrer

implementation “”

Step 3: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 


<?xml version="1.0" encoding="utf-8"?>
    <!--text view for displaying referrer details-->
        android:textSize="18sp" />

Step 4: Working with the file

Go to the file and refer to the following code. Below is the code for the file. Comments are added inside the code to understand the code in more detail.


import android.os.Bundle;
import android.os.RemoteException;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
    // creating variables for text view.
    private TextView referrerTV;
    // variable for install referrer client.
    InstallReferrerClient referrerClient;
    // creating an empty string for our referrer.
    String referrer = "";
    protected void onCreate(Bundle savedInstanceState) {
        // initializing all our variables.
        referrerTV = findViewById(;
        // on below line we are building our install referrer client and building it.
        referrerClient = InstallReferrerClient.newBuilder(this).build();
        // on below line we are starting its connection.
        referrerClient.startConnection(new InstallReferrerStateListener() {
            public void onInstallReferrerSetupFinished(int responseCode) {
                // this method is called when install referrer setup is finished.
                switch (responseCode) {
                    // we are using switch case to check the response.
                    case InstallReferrerClient.InstallReferrerResponse.OK:
                        // this case is called when the status is OK and
                        ReferrerDetails response = null;
                        try {
                            // on below line we are getting referrer details
                            // by calling get install referrer.
                            response = referrerClient.getInstallReferrer();
                            // on below line we are getting referrer url.
                            String referrerUrl = response.getInstallReferrer();
                            // on below line we are getting referrer click time.
                            long referrerClickTime = response.getReferrerClickTimestampSeconds();
                            // on below line we are getting app install time
                            long appInstallTime = response.getInstallBeginTimestampSeconds();
                            // on below line we are getting our time when
                            // user has used our apps instant experience.
                            boolean instantExperienceLaunched = response.getGooglePlayInstantParam();
                            // on below line we are getting our
                            // apps install referrer.
                            referrer = response.getInstallReferrer();
                            // on below line we are setting all detail to our text view.
                            referrerTV.setText("Referrer is : \n" + referrerUrl + "\n" + "Referrer Click Time is : " + referrerClickTime + "\nApp Install Time : " + appInstallTime);
                        } catch (RemoteException e) {
                            // handling error case.
                    case InstallReferrerClient.InstallReferrerResponse.FEATURE_NOT_SUPPORTED:
                        // API not available on the current Play Store app.
                        Toast.makeText(MainActivity.this, "Feature not supported..", Toast.LENGTH_SHORT).show();
                    case InstallReferrerClient.InstallReferrerResponse.SERVICE_UNAVAILABLE:
                        // Connection couldn't be established.
                        Toast.makeText(MainActivity.this, "Fail to establish connection", Toast.LENGTH_SHORT).show();
            public void onInstallReferrerServiceDisconnected() {
                // Try to restart the connection on the next request to
                // Google Play by calling the startConnection() method.
                Toast.makeText(MainActivity.this, "Service disconnected..", Toast.LENGTH_SHORT).show();

Now run your app and see the output of the app.

Note: As our app is not published on Google Play so we will only get the referrer details as organic and other details will get as zero. 


Last Updated : 31 Jul, 2022
Like Article
Save Article
Similar Reads
Related Tutorials