整合廣告適用的 WebView API

廣告專用的 WebView API 可讓應用程式信號提供給 WebView 中的代碼,協助提供內容的發布商改善營利成效,並保護廣告客戶免受垃圾訊息干擾。這些應用程式信號 (例如應用程式 ID 和應用程式版本) 有助於啟用報表指定應用程式內瀏覽器廣告空間用途,這些用途原本只適用於應用程式流量。

運作方式

與 Google Mobile Ads SDK 的通訊僅會在下列任一事件觸發時發生:

SDK 會將訊息處理常式新增至已註冊的 WebView,以便監聽這些廣告事件。如要進一步瞭解運作方式,請查看測試頁面的原始碼

必要條件

將應用程式 ID 傳遞至 SDK

如果您已有 Ad Manager 應用程式 ID,請使用現有的應用程式 ID 初始化 Google Mobile Ads SDK

如果您沒有 Ad Manager 應用程式 ID,請在初始化 Google Mobile Ads SDK 時,將 InitializationConfig.WEBVIEW_APIS_FOR_ADS_APPLICATION_ID 傳入為應用程式 ID。

Kotlin

MobileAds.initialize(
    this@MainActivity,
    // Use this application ID to initialize the Google Mobile Ads SDK 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 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 處理常式建立連線。這項操作應盡早完成,例如在 MainActivityonCreate() 方法中。

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

後續步驟

  • WebView 中收集同意聲明。廣告的 Web 檢視畫面 API 不會將使用 IAB 資訊公開和同意聲明架構第 2.0 版IAB 消費者隱私權和廣告法案 法規遵循架構,在行動應用程式情境中收集到的同意聲明,傳播至網頁檢視畫面中的代碼。如果您想在 WebView 和對應的營利網站內容中,以單一同意聲明流程收集同意聲明,請透過同意聲明管理平台在 WebView 情境中收集同意聲明。