取得可由使用者重設的廣告 ID Android Jetpack 的一部分。
為保護使用者隱私,最佳做法是讓所有 Android 應用程式處理可由使用者重設的 ID。其中一種 ID 是廣告 ID,可用於識別特定使用者做為廣告用途 (例如廣告個人化)。
如要在執行應用程式的各種裝置上支援標準化廣告追蹤解決方案,您可以使用廣告 ID 程式庫。這個程式庫可在搭載 Android 4.0 (API 級別 14) 以上版本的裝置上定義與系統層級廣告供應商互動的介面。這個介面可讓應用程式接收一致的廣告 ID 值。
廣告 ID 程式庫隨附的廣告供應商也會定義用來開啟廣告供應商導入設定畫面的標準意圖。透過這個設定畫面,使用者可以重設廣告 ID 並停用廣告個人化。
本指南說明如何使用廣告 ID 程式庫的用戶端模組,針對個別裝置取得一致的廣告 ID。本指南接著會概略介紹程式庫的架構。
設定用戶端應用程式
應用程式可以與廣告 ID 程式庫的用戶端模組互動,藉此擷取一致的廣告 ID,代表與應用程式互動的使用者。
廣告 ID 是以第 3 版通用唯一識別碼 (UUID) 格式表示,或對等的 128 位元格式:
38400000-8cf0-11bd-b23e-10b96e40000d
廣告 ID 程式庫會視需要將傳回值正規化,以提供使用此格式的 ID。
如要擷取應用程式可由使用者重設的廣告 ID,請完成下列步驟:
呼叫
AdvertisingIdClient.isAdvertisingIdProviderAvailable()
來確認廣告供應商是否可用。如果這個方法傳回false
,應用程式應使用其他方法執行任何必要的廣告追蹤用途。呼叫
AdvertisingIdClient.getAdvertisingIdInfo()
取得廣告 ID 詳細資料 (包括廣告 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") }
我的廣告 ID 用戶端
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 程式庫的結構。程式庫包含下列模組:
- 用戶端模組,這是應用程式中的細層。
- 供應商模組,由裝置製造商提供的模組。這個模組的實作必須定義設定使用者介面,讓使用者能夠重設廣告 ID 及切換廣告追蹤偏好設定。
用戶端模組會與供應商模組通訊,擷取有關廣告追蹤的廣告 ID 和使用者偏好設定。
程式庫如何處理多個提供者
裝置可以同時支援多個系統層級的廣告供應商。如果廣告 ID 程式庫偵測到這種情況,就會假設供應商仍然可用,確保應用程式一律從同一供應商擷取資訊。此程序可讓廣告 ID 保持一致。
如果可用的廣告供應商組合會隨時間變更,而您的應用程式與不同的廣告 ID 提供者互動,則所有其他用戶端應用程式也會開始使用該新的供應商。您的應用程式之所以呈現相同的行為,是因為使用者要求重設廣告 ID。
廣告 ID 供應商程式庫會根據以下決定性順序為提供者排名:
- 已要求
androidx.ads.identifier.provider.HIGH_PRIORITY
權限的提供者。 - 已在裝置上安裝到最多時間的供應商。
- 先依字母順序排列的供應商。