Open In App

How to Use Google Play Install Referrer API in Android?

Last Updated : 31 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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 “com.android.installreferrer:installreferrer:2.2”

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




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
 
    <!--text view for displaying referrer details-->
    <TextView
        android:id="@+id/idTVReferrer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_margin="3dp"
        android:gravity="center_horizontal"
        android:padding="4dp"
        android:text="Referrer"
        android:textAlignment="center"
        android:textColor="@color/purple_200"
        android:textSize="18sp" />
 
</RelativeLayout>


Step 4: Working with the MainActivity.java file

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

Java




import android.os.Bundle;
import android.os.RemoteException;
import android.widget.TextView;
import android.widget.Toast;
 
import androidx.appcompat.app.AppCompatActivity;
 
import com.android.installreferrer.api.InstallReferrerClient;
import com.android.installreferrer.api.InstallReferrerStateListener;
import com.android.installreferrer.api.ReferrerDetails;
 
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 = "";
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         
        // initializing all our variables.
        referrerTV = findViewById(R.id.idTVReferrer);
         
        // 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() {
            @Override
            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.
                            e.printStackTrace();
                        }
                        break;
                    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();
                        break;
                    case InstallReferrerClient.InstallReferrerResponse.SERVICE_UNAVAILABLE:
                        // Connection couldn't be established.
                        Toast.makeText(MainActivity.this, "Fail to establish connection", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
 
            @Override
            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. 

Output:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads