広告用のウェブビュー API を使用すると、WebView
のタグでアプリシグナルが利用できるようになり、コンテンツを提供したパブリッシャーの収益を増やしたり、広告主をスパムから保護したりできます。アプリ ID やアプリのバージョンなどのアプリ シグナルは、アプリ トラフィックでのみ利用可能なレポートとアプリ内ブラウザのインベントリをターゲットに設定するユースケースを有効にするのに役立ちます。
仕組み
Google Mobile Ads SDK(ベータ版)との通信は、次のいずれかによってトリガーされた広告イベントへの応答でのみ行われます。
SDK は登録された WebView
にメッセージ ハンドラを追加して、これらの広告イベントをリッスンします。この仕組みをより詳しく理解するには、テストページのソースコードをご覧ください。
前提条件
- Google Mobile Ads SDK(ベータ版)バージョン 0.6.0-alpha01 以降。
アプリケーション ID を SDK に渡す
アド マネージャー アプリケーション ID がすでにある場合は、既存のアプリケーション ID を使用して Google Mobile Ads SDK(ベータ版)を初期化します。
アド マネージャー アプリケーション ID がない場合は、Google Mobile Ads SDK(ベータ版)を初期化する際に、アプリケーション ID として InitializationConfig.WEBVIEW_APIS_FOR_ADS_APPLICATION_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);
}
}
統合をテストする
独自の URL を使用する前に、次の URL を読み込んで統合をテストすることをおすすめします。
https://google.github.io/webview-ads/test/#api-for-ads-tests
次の条件が満たされている場合、テスト URL には統合が問題なく完了したことを示す緑色のステータスバーが表示されます。
WebView
が Google Mobile Ads SDK(ベータ版)に接続されている
次のステップ
WebView
で同意を取得します。広告用のウェブビュー API は、IAB TCF v2.0 または IAB CCPA コンプライアンス フレームワークを使用してモバイルアプリのコンテキストで収集された同意を、ウェブビューのタグに反映しません。WebView
と、それに対応する収益化対象のウェブ コンテンツの両方のオーナーであり、単一の同意フローを実装したい場合は、同意管理プラットフォームと連携して、WebView
のコンテキストで同意を取得します。