Mega Infomatix
Mega Infomatix

Reputation: 105

How to implement Play Install Referrer API in Android?

I want to implement Play Install Referrer API and I found document and I read that but I have have some confusion. First I have implemented all code provided by google. But I want to know which type of url I need to create so that user can click on link and go to play store and install my app and then I get the referral detail..

I use this code:

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

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

           /* ReferrerDetails response = null;
            try {
                response = mReferrerClient.getInstallReferrer();
                response.getInstallReferrer();
                response.getReferrerClickTimestampSeconds();
                response.getInstallBeginTimestampSeconds();
            } 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 () {

    }

But which type of link I create so user go to play store and after install play store referral api give me data..

Upvotes: 2

Views: 6047

Answers (2)

Vikrant_Dev
Vikrant_Dev

Reputation: 390

Sample url - "https://play.google.com/store/apps/details?id=com.dummy.app&referrer=referralCode%3D311566%26source%3DFacebook+App"

When using the Google Play Referrer API -

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

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

           /* ReferrerDetails response = null;
            try {
                response = mReferrerClient.getInstallReferrer();
                response.getInstallReferrer();
                response.getReferrerClickTimestampSeconds();
                response.getInstallBeginTimestampSeconds();
            } catch (RemoteException e) {
                e.printStackTrace();
            }*/
            
                // End the connection once you get the data
                referrerClient.endConnection();

                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 () {

    }

getInstallReferrer()

will return String 'referralCode=311566&source=Facebook App'

Upvotes: 2

Najaf Ali
Najaf Ali

Reputation: 1473

play install referral library i wants to describe this in simple wording, being a developer you wants to know about these elements how much time you app bundle take to install on the user devices from play store, and referral url , referral click time and many others elements , google make it easy for you know you have to use play install referral library for this purpose.

add this dependency

implementation 'com.android.installreferrer:installreferrer:1.1'

you can follow the guidelines from here:

play installer referral guidelines

declare this variable in any java activity

InstallReferrerClient referrerClient;

in on create method use this code below :

 referrerClient = InstallReferrerClient.newBuilder(this).build();
    referrerClient.startConnection(new InstallReferrerStateListener() {
        @Override
        public void onInstallReferrerSetupFinished(int responseCode) {
            switch (responseCode) {
                case InstallReferrerClient.InstallReferrerResponse.OK:
                    // Connection established.
                    break;
                case InstallReferrerClient.InstallReferrerResponse.FEATURE_NOT_SUPPORTED:
                    // API not available on the current Play Store app.
                    break;
                case InstallReferrerClient.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.
        }


    });

inside onInstallReferrerSetupFinished stabled you can get these data easily,you code will be after that like this

  ReferrerDetails response = null;
                    try {
                        response = referrerClient.getInstallReferrer();
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                    String referrerUrl = response.getInstallReferrer();
                    long referrerClickTime = response.getReferrerClickTimestampSeconds();
                    long appInstallTime = response.getInstallBeginTimestampSeconds();
                    boolean instantExperienceLaunched = response.getGooglePlayInstantParam();

whole code will be like this

 referrerClient = InstallReferrerClient.newBuilder(this).build();
    referrerClient.startConnection(new InstallReferrerStateListener() {
        @Override
        public void onInstallReferrerSetupFinished(int responseCode) {
            switch (responseCode) {
                case InstallReferrerClient.InstallReferrerResponse.OK:
                    // Connection established.

                    ReferrerDetails response = null;
                    try {
                        response = referrerClient.getInstallReferrer();
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                    String referrerUrl = response.getInstallReferrer();
                    long referrerClickTime = response.getReferrerClickTimestampSeconds();
                    long appInstallTime = response.getInstallBeginTimestampSeconds();
                    boolean instantExperienceLaunched = response.getGooglePlayInstantParam();

                    break;
                case InstallReferrerClient.InstallReferrerResponse.FEATURE_NOT_SUPPORTED:
                    // API not available on the current Play Store app.
                    break;
                case InstallReferrerClient.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.
        }


    });

implementation

impelemtation

Upvotes: 0

Related Questions