0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Play Install Referrer Libraryを導入する

Posted at

Googleさんがインストールリファラ・ブロードキャストをやめてしまったので、ライブラリを導入することになりました。
当時の導入時のメモです。


まずはライブラリ導入

dependencies {
    ...
    implementation 'com.android.installreferrer:installreferrer:1.1'
}

実装します。
Android Developerでは、
image.png
と、さらっとかいてあります。
これくらいケアしてくれよ!っと、若干イラっとしますがぐっとこらえて。
APIからのレスポンス受信後、SharedPreferenceで「処理済み」フラグを保存します。

    public void handleGooglePlayReferrer(Context context) {
        // Google recommends only calling this API the first time you need it:
        // https://developer.android.com/google/play/installreferrer/library#install-referrer

        // Store a boolean in SharedPreferences to ensure we only call it once.
        final SharedPreferences prefs = context.getSharedPreferences("acquisition", 0);
        if (prefs != null &&
                prefs.getBoolean("referrerHasBeenProcessed", false)) {
            return;
        }

        final InstallReferrerClient referrerClient =
                InstallReferrerClient.newBuilder(context.getApplicationContext()).build();
        referrerClient.startConnection(new InstallReferrerStateListener() {
            private boolean complete = false;

            @Override
            public void onInstallReferrerSetupFinished(int responseCode) {
                switch (responseCode) {
                    case InstallReferrerClient.InstallReferrerResponse.OK:
                        // connection is established
                        complete();
                        try {
                            final ReferrerDetails details = referrerClient.getInstallReferrer();

                            // pass the install referrer url to the SDK
                            Analytics.processGooglePlayInstallReferrerUrl(details.getInstallReferrer());

                        } catch (final RemoteException ex) {
                            //Log.w("Acquisition - RemoteException while retrieving referrer information (%s)", ex.getLocalizedMessage() == null ? "unknown" : ex.getLocalizedMessage());
                        } finally {
                            referrerClient.endConnection();
                        }
                        break;
                    case InstallReferrerClient.InstallReferrerResponse.FEATURE_NOT_SUPPORTED:
                    case InstallReferrerClient.InstallReferrerResponse.SERVICE_UNAVAILABLE:
                    default:
                        // API not available in the Play Store app - nothing to do here
                        complete();
                        referrerClient.endConnection();
                        break;
                }
            }

            @Override
            public void onInstallReferrerServiceDisconnected() {
                if (!complete) {
                    referrerClient.startConnection(this);
                }
            }

            void complete() {
                complete = true;
                SharedPreferences.Editor editor =
                        context.getSharedPreferences("acquisition", 0).edit();
                editor.putBoolean("referrerHasBeenProcessed", true);
                editor.apply();
            }
        });
    }

いじょ。


参考:https://developer.android.com/google/play/installreferrer/library

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?