ユーザーがリセット可能な広告 ID を取得するAndroid Jetpack の一部)。

ユーザーのプライバシーを保護するため、すべての Android アプリでユーザーがリセット可能な識別子を使用することをおすすめします。そのような識別子の 1 つに広告 ID があります。これは、広告のパーソナライズなど、広告のユースケースでは特定のユーザーを一意に識別します。

アプリを実行しているすべてのデバイスで標準化された広告トラッキング ソリューションをサポートするには、広告 ID ライブラリを使用します。このライブラリは、Android 4.0(API レベル 14)以降を実行しているデバイスで使用でき、システムレベルの広告プロバイダとやり取りするためのインターフェースを定義します。このインターフェースにより、アプリは一貫した広告 ID 値を受け取ることができます。

広告 ID ライブラリに含まれている広告プロバイダは、広告プロバイダが実装する設定画面を開くための標準的なインテントも定義します。この設定画面では、ユーザーが広告 ID をリセットしたり、広告のパーソナライズをオプトアウトしたりできます。

このガイドでは、広告 ID ライブラリのクライアント モジュールを使用して、デバイス ユーザーごとに一貫した広告 ID を取得する方法について説明します。さらに、広告 ID ライブラリのアーキテクチャについて概説します。

クライアント アプリを構成する

広告 ID ライブラリのクライアント モジュールと対話することで、アプリは、アプリを操作しているユーザーを表す一貫した広告 ID を取得できます。

広告 ID は、Universally Unique Identifier(UUID)形式のバージョン 3 または同等の 128 ビット形式を使用して表されます。

38400000-8cf0-11bd-b23e-10b96e40000d

広告 ID ライブラリは、この形式を使用して ID を提供するために、必要に応じて戻り値を正規化します。

アプリでユーザーがリセット可能な広告 ID を取得するには、次の手順を実行します。

  1. AdvertisingIdClient.isAdvertisingIdProviderAvailable() を呼び出して、広告プロバイダが利用可能かどうかを確認します。このメソッドが false を返した場合、アプリは別の手段を使用して、必要な広告トラッキングのユースケースを実行する必要があります。

  2. AdvertisingIdClient.getAdvertisingIdInfo() を呼び出して、広告 ID を含む広告識別子の詳細を取得します。広告 ID ライブラリは、ワーカー スレッドでこのメソッドを実行し、10 秒の接続タイムアウトを使用します。

次のコード スニペットは、広告プロバイダからの他の情報とともに、広告 ID を取得する方法を示しています。

app/build.gradle

Groovy

dependencies {
    implementation 'androidx.ads:ads-identifier:1.0.0-alpha01'

    // Used for the calls to addCallback() in the snippets on this page.
    implementation 'com.google.guava:guava:28.0-android'
}

Kotlin

dependencies {
    implementation("androidx.ads:ads-identifier:1.0.0-alpha01")

    // Used for the calls to addCallback() in the snippets on this page.
    implementation("com.google.guava:guava:28.0-android")
}

MyAdIdClient

Kotlin

// Used for the call to addCallback() within this snippet.
import com.google.common.util.concurrent.Futures.addCallback

private fun determineAdvertisingInfo() {
    if (AdvertisingIdClient.isAdvertisingIdProviderAvailable()) {
        val advertisingIdInfoListenableFuture =
                AdvertisingIdClient.getAdvertisingIdInfo(applicationContext)

        addCallback(advertisingIdInfoListenableFuture,
                object : FutureCallback<AdvertisingIdInfo> {
            override fun onSuccess(adInfo: AdvertisingIdInfo?) {
                val id: String = adInfo?.id
                val providerPackageName: String = adInfo?.providerPackageName
                val isLimitTrackingEnabled: Boolean =
                                adInfo?.isLimitTrackingEnabled
            }

            // Any exceptions thrown by getAdvertisingIdInfo()
            // cause this method to be called.
            override fun onFailure(t: Throwable) {
                Log.e("MY_APP_TAG",
                        "Failed to connect to Advertising ID provider.")
                // Try to connect to the Advertising ID provider again or fall
                // back to an ad solution that doesn't require using the
                // Advertising ID library.
            }
        }, Executors.newSingleThreadExecutor())
    } else {
        // The Advertising ID client library is unavailable. Use a different
        // library to perform any required ad use cases.
    }
}

Java

// Used for the call to addCallback() within this snippet.
import com.google.common.util.concurrent.Futures;

private void determineAdvertisingInfo() {
    if (AdvertisingIdClient.isAdvertisingIdProviderAvailable()) {
        ListenableFuture<AdvertisingIdInfo> advertisingIdInfoListenableFuture =
                AdvertisingIdClient.getAdvertisingIdInfo(getApplicationContext());
        Futures.addCallback(advertisingIdInfoListenableFuture,
                new FutureCallback<AdvertisingIdInfo>() {
                    @Override
                    public void onSuccess(AdvertisingIdInfo adInfo) {
                        String id = adInfo.getId();
                        String providerPackageName =
                                adInfo.getProviderPackageName();
                        boolean isLimitTrackingEnabled =
                                adInfo.isLimitTrackingEnabled();

                    // Any exceptions thrown by getAdvertisingIdInfo()
                    // cause this method to be called.
                    @Override
                    public void onFailure(Throwable throwable) {
                        Log.e("MY_APP_TAG",
                                "Failed to connect to Advertising ID provider.");
                        // Try to connect to the Advertising ID provider again
                        // or fall back to an ad solution that doesn't require
                        // using the Advertising ID library.
                    }
                });
    } else {
        // The Advertising ID client library is unavailable. Use a different
        // library to perform any required ad use cases.
    }
}

広告 ID ライブラリのアーキテクチャ

広告 ID ライブラリのアーキテクチャ。
図 1. 広告 ID ライブラリのアーキテクチャ

図 1 は広告 ID ライブラリの構造を示しています。ライブラリは、次のモジュールで構成されています。

  • クライアント モジュール。アプリに含まれるシンレイヤです。
  • プロバイダ モジュール。デバイスのメーカーが提供するモジュールです。このモジュールの実装では、設定 UI を定義して、ユーザーが広告 ID をリセットし、広告トラッキング設定を切り替えられるようにする必要があります。

クライアント モジュールはプロバイダ モジュールと通信して、広告トラッキングに関する広告 ID とユーザー設定を取得します。

ライブラリで複数のプロバイダを処理する方法

デバイスが複数のシステムレベルの広告プロバイダを同時にサポートすることは可能です。広告 ID ライブラリがこの状況を検出した場合、プロバイダが利用可能であると仮定して、アプリが常に同じプロバイダから情報を取得することを確認します。この処理により、広告 ID の一貫性が保たれます。

利用可能な広告プロバイダのセットが時間とともに変化し、アプリが別の広告識別子プロバイダとやり取りすると、他のすべてのクライアント アプリもその新しいプロバイダの使用を開始します。アプリは、ユーザーが広告 ID のリセットを要求した場合に発生するのと同じ動作を示します。

広告 ID プロバイダ ライブラリは、次の決定論的な順序を使用してプロバイダをランク付けします。

  1. androidx.ads.identifier.provider.HIGH_PRIORITY 権限をリクエストしたプロバイダ。
  2. 最長時間デバイスにインストールされていたプロバイダ。
  3. アルファベット順で最初に表示されるプロバイダ。