使用廣告專用的網頁瀏覽 API,WebView
中的標記就能存取應用程式信號,有助內容發布商提升營利成效,同時保護廣告主不受垃圾內容干擾。這些應用程式信號 (例如應用程式 ID 和應用程式版本) 有助於啟用報表和指定應用程式內瀏覽器廣告空間,否則這些功能只能用於應用程式流量。
運作方式
只有在回應下列任一項目觸發的廣告事件時,才會與 Google Mobile Ads SDK (Beta 版) 通訊:
SDK 會將訊息處理常式加到已註冊的 WebView
,藉此監聽這些廣告事件。詳細運作方式請見測試網頁的原始碼。
必要條件
- Google Mobile Ads SDK (Beta 版) 0.6.0-alpha01 以上版本。
將應用程式 ID 傳遞至 SDK
如果您已有 Ad Manager 應用程式 ID,請使用現有的應用程式 ID 初始化 Google Mobile Ads SDK (Beta 版)。
如果沒有 Ad Manager 應用程式 ID,請在初始化 Google Mobile Ads SDK (Beta 版) 時,傳遞 InitializationConfig.WEBVIEW_APIS_FOR_ADS_APPLICATION_ID
做為應用程式 ID。
Kotlin
MobileAds.initialize(
this@MainActivity,
// Use this application ID to initialize the Google Mobile Ads SDK (beta) if
// you don't have an Ad Manager application ID.
InitializationConfig.Builder(InitializationConfig.WEBVIEW_APIS_FOR_ADS_APPLICATION_ID)
.build(),
) {
// Adapter initialization complete.
}
Java
MobileAds.initialize(
this,
// Use this application ID to initialize the Google Mobile Ads SDK (beta) if
// you don't have an Ad Manager application ID.
new InitializationConfig.Builder(InitializationConfig.WEBVIEW_APIS_FOR_ADS_APPLICATION_ID)
.build(),
initializationStatus -> {
// Adapter initialization is complete.
});
註冊網頁檢視畫面
在主要執行緒上呼叫 registerWebView()
,與每個 WebView
執行個體中的 AdSense 程式碼或 Google 發布商廣告代碼,建立 JavaScript 處理常式連線。這項操作應盡早完成,例如在 MainActivity
的 onCreate()
方法中執行。
Kotlin
import android.webkit.CookieManager
import android.webkit.WebView
import com.google.android.libraries.ads.mobile.sdk.MobileAds
class MainActivity : AppCompatActivity() {
lateinit var webView: WebView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
webView = findViewById(R.id.webview)
// Let the web view accept third-party cookies.
CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true)
// Let the web view use JavaScript.
webView.settings.javaScriptEnabled = true
// Let the web view access local storage.
webView.settings.domStorageEnabled = true
// Let HTML videos play automatically.
webView.settings.mediaPlaybackRequiresUserGesture = false
// Register the web view.
MobileAds.registerWebView(webView)
}
}
Java
import android.webkit.CookieManager;
import android.webkit.WebView;
import com.google.android.libraries.ads.mobile.sdk.MobileAds;
public class MainActivity extends AppCompatActivity {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webview);
// Let the web view accept third-party cookies.
CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);
// Let the web view use JavaScript.
webView.getSettings().setJavaScriptEnabled(true);
// Let the web view access local storage.
webView.getSettings().setDomStorageEnabled(true);
// Let HTML videos play automatically.
webView.getSettings().setMediaPlaybackRequiresUserGesture(false);
// Register the web view.
MobileAds.registerWebView(webView);
}
}
測試整合功能
套用自己的網址前,建議先載入下列網址測試整合結果:
https://google.github.io/webview-ads/test/#api-for-ads-tests
只要符合下列條件,測試網址就會顯示綠色狀態列,代表整合成功:
WebView
已連結至 Google Mobile Ads SDK (Beta 版)
後續步驟
- 在
WebView
中收集同意聲明。對於在行動應用程式執行情境中收集到的同意聲明,廣告專用的網頁瀏覽 API 不會使用 IAB 資訊公開和同意聲明架構第 2 版或 IAB CCPA 法規遵循架構,將這類同意聲明傳播至網頁瀏覽中的標記。如果您同時擁有WebView
及相應的營利網路內容,且想要導入單一同意聲明流程,可與同意聲明管理平台合作,在WebView
執行情境中收集同意聲明。