Friday, January 22, 2021

Google play Install Referrer

You can use the Google Play Store's Install Referrer API to securely retrieve referral content from Google Play. 

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

dependencies {

    ...

    implementation 'com.android.installreferrer:installreferrer:2.1'

}

Connecting to Google Play

Before you can use the Play Install Referrer API Library, you must establish a connection to the Play Store app using the following steps:

Call the newBuilder() method to create an instance of InstallReferrerClient class.

Call the startConnection() to establish a connection to Google Play.

The startConnection() method is asynchronous, so you must override InstallReferrerStateListener to receive a callback after startConnection() completes.

Override the onInstallReferrerSetupFinished() method to handle lost connections to Google Play. For example, the Play Install Referrer Library client may lose the connection if the Play Store service is updating in the background. The library client must call the startConnection() method to restart the connection before making further requests.

InstallReferrerClient referrerClient;

referrerClient = InstallReferrerClient.newBuilder(this).build();

referrerClient.startConnection(new InstallReferrerStateListener() {

    @Override

    public void onInstallReferrerSetupFinished(int responseCode) {

        switch (responseCode) {

            case InstallReferrerResponse.OK:

                // Connection established.

                break;

            case InstallReferrerResponse.FEATURE_NOT_SUPPORTED:

                // API not available on the current Play Store app.

                break;

            case InstallReferrerResponse.SERVICE_UNAVAILABLE:

                // Connection couldn't be established.

                break;

        }

    }


    @Override

    public void onInstallReferrerServiceDisconnected() {

        // Try to restart the connection on the next request to

        // Google Play by calling the startConnection() method.

    }

});


The following code demonstrates how you can access the install referrer information:


ReferrerDetails response = referrerClient.getInstallReferrer();

String referrerUrl = response.getInstallReferrer();

long referrerClickTime = response.getReferrerClickTimestampSeconds();

long appInstallTime = response.getInstallBeginTimestampSeconds();

boolean instantExperienceLaunched = response.getGooglePlayInstantParam();

Closing service connection

After getting referrer information, call the endConnection() method on your InstallReferrerClient instance to close the connection. Closing the connection will help you avoid leaks and performance problems.

references:

https://developer.android.com/google/play/installreferrer/library#java


No comments:

Post a Comment