ユーザーがリセット可能な広告 ID を取得する。Android Jetpack の一部
ユーザーのプライバシーを保護するため、すべての Android アプリで、ユーザーがリセット可能な識別子を使用することをおすすめします。そのような識別子の 1 つに広告 ID があります。この ID は、広告のパーソナライズなどの広告ユースケースで特定のユーザーを一意に識別します。
アプリを実行するデバイスで標準化された広告トラッキング ソリューションをサポートするには、広告 ID ライブラリを使用します。このライブラリは Android 4.0(API レベル 14)以降を搭載しているデバイスで利用可能で、システムレベルの広告プロバイダとやり取りするためのインターフェースを定義します。このインターフェースを使用すると、アプリで一貫した広告 ID 値を受け取ることができます。
広告 ID ライブラリに含まれる広告プロバイダは、広告プロバイダが実装する設定画面を開くための標準インテントも定義します。この設定画面では、ユーザーは広告 ID をリセットしたり、広告のカスタマイズをオプトアウトしたりできます。
このガイドでは、広告 ID ライブラリのクライアント モジュールを使用して、デバイス ユーザーごとに一貫した広告 ID を取得する方法について説明します。その後、このガイドでは、ライブラリのアーキテクチャの概要について説明します。
クライアント アプリを構成する
アプリは、広告 ID ライブラリのクライアント モジュールとやり取りすることで、アプリを操作しているユーザーを表す一貫性のある広告 ID を取得できます。
広告 ID は、Universally Unique Identifier(UUID)形式のバージョン 3 または同等の 128 ビット形式を使用して表されます。
38400000-8cf0-11bd-b23e-10b96e40000d
広告 ID ライブラリは、必要に応じて戻り値を正規化し、この形式で ID を提供します。
アプリのユーザーがリセット可能な広告 ID を取得する手順は次のとおりです。
広告プロバイダが利用可能かどうかを確認するには、
AdvertisingIdClient.isAdvertisingIdProviderAvailable()
を呼び出します。このメソッドがfalse
を返した場合、アプリは別の方法を使用して、必要な広告トラッキングのユースケースを行う必要があります。AdvertisingIdClient.getAdvertisingIdInfo()
を呼び出して、広告 ID などの広告 ID の詳細を取得します。広告 ID ライブラリは、このメソッドをワーカー スレッドで実行し、10 秒の接続タイムアウトを使用します。
次のコード スニペットは、広告 ID と広告プロバイダからその他の情報を取得する方法を示しています。
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") }
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 ライブラリのアーキテクチャ
図 1 は、広告 ID ライブラリの構造を示しています。ライブラリは、次のモジュールで構成されています。
- クライアント モジュール。アプリに含まれるシンレイヤです。
- プロバイダ モジュール: デバイスのメーカーが提供するモジュールです。 このモジュールを実装する場合は、設定 UI を定義して、ユーザーが広告 ID をリセットしたり広告トラッキング設定を切り替えられるようにしたりする必要があります。
クライアント モジュールはプロバイダ モジュールと通信して、広告 ID と広告トラッキングに関するユーザー設定を取得します。
ライブラリで複数のプロバイダを処理する方法
1 つのデバイスが同時に複数のシステムレベルの広告プロバイダをサポートできます。広告 ID ライブラリがこの状況を検出した場合、プロバイダが利用可能なままであることを前提として、アプリは常に同じプロバイダから情報を取得します。この処理により、広告 ID の一貫性が保たれます。
利用可能な広告プロバイダのセットが時間とともに変化し、アプリが別の広告 ID プロバイダとやり取りすると、他のすべてのクライアント アプリもその新しいプロバイダを使用するようになります。アプリには、ユーザーが広告 ID のリセットをリクエストした場合と同じ動作が表示されます。
広告 ID プロバイダ ライブラリは、次の決定論的な順序でプロバイダをランク付けします。
androidx.ads.identifier.provider.HIGH_PRIORITY
権限をリクエストしたプロバイダ。- 最長時間デバイスにインストールされていたプロバイダ。
- アルファベット順で最初に表示されるプロバイダ。