ผสานรวม WebView API สำหรับโฆษณา
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
API ของ WebView สำหรับโฆษณาจะทำให้สัญญาณของแอปพร้อมใช้งานกับแท็กใน
WebView
ซึ่งจะช่วยปรับปรุงการสร้างรายได้สำหรับ
ผู้เผยแพร่โฆษณาที่ให้เนื้อหาและปกป้องผู้ลงโฆษณาจากสแปม

วิธีการทำงาน
การสื่อสารกับ SDK โฆษณาในอุปกรณ์เคลื่อนที่ของ Google จะเกิดขึ้นเมื่อมีการตอบสนองต่อเหตุการณ์โฆษณา
ที่ทริกเกอร์โดยรายการใดรายการหนึ่งต่อไปนี้เท่านั้น
SDK จะเพิ่มตัวแฮนเดิลข้อความลงใน WebView
ที่ลงทะเบียนไว้เพื่อฟัง
เหตุการณ์โฆษณาเหล่านี้ ดูซอร์สโค้ดของหน้าทดสอบเพื่อทำความเข้าใจวิธีการทำงานของฟีเจอร์นี้ให้ดียิ่งขึ้น
ข้อกำหนดเบื้องต้น
- SDK โฆษณาในอุปกรณ์เคลื่อนที่ของ Google
เวอร์ชัน 20.6.0 ขึ้นไป
API ระดับ 21 ขึ้นไปของ Android
เพิ่มแท็ก <meta-data>
ต่อไปนี้ในไฟล์ AndroidManifest.xml
เพื่อ
ข้ามการตรวจสอบ APPLICATION_ID
หากคุณพลาดขั้นตอนนี้และไม่ได้ระบุแท็ก <meta-data>
SDK โฆษณาในอุปกรณ์เคลื่อนที่ของ Google จะแสดงข้อผิดพลาด IllegalStateException
เมื่อเริ่มต้นแอป
<!-- Bypass APPLICATION_ID check for web view APIs for ads -->
<meta-data
android:name="com.google.android.gms.ads.INTEGRATION_MANAGER"
android:value="webview"/>
ลงทะเบียนมุมมองเว็บ
เรียกใช้
registerWebView()
ในเทรดหลักเพื่อสร้างการเชื่อมต่อกับตัวแฮนเดิล JavaScript ในโค้ด AdSense หรือ Google Publisher Tag ภายในอินสแตนซ์ WebView
แต่ละรายการ คุณควรดำเนินการนี้โดยเร็วที่สุด เช่น ในเมธอด onCreate()
ของ MainActivity
Kotlin
import android.webkit.CookieManager
import android.webkit.WebView
import com.google.android.gms.ads.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.gms.ads.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 ของ WebView สำหรับโฆษณาจะไม่
ส่งต่อความยินยอมที่รวบรวมในบริบทของแอปบนอุปกรณ์เคลื่อนที่โดยใช้กรอบการปฏิบัติตามข้อกำหนดของ TCF เวอร์ชัน 2.0 ของ IAB หรือ CCPA ของ IAB
ไปยังแท็กใน WebView หากคุณสนใจที่จะ
ใช้ขั้นตอนความยินยอมเดียวในฐานะเจ้าของทั้ง WebView
และเนื้อหาเว็บที่เกี่ยวข้องซึ่งมีการสร้างรายได้ ให้ทำงานร่วมกับแพลตฟอร์มการจัดการความยินยอมเพื่อรวบรวมความยินยอมในบริบทของ WebView
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-31 UTC
[null,null,["อัปเดตล่าสุด 2025-08-31 UTC"],[[["\u003cp\u003eThe Web View APIs for Ads improves ad monetization by making app signals available to ad tags within your Android WebView.\u003c/p\u003e\n"],["\u003cp\u003eThe Google Mobile Ads SDK communicates with your WebView in response to ad events triggered by AdSense, Google Publisher Tag, or IMA for HTML5.\u003c/p\u003e\n"],["\u003cp\u003eTo use the Web View APIs for Ads, you need Google Mobile Ads SDK 20.6.0 or higher, Android API level 21 or higher, and to register your WebView using \u003ccode\u003eMobileAds.registerWebView()\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eYou need to gather consent within the WebView context as it's not automatically propagated from the mobile app's consent flow.\u003c/p\u003e\n"]]],[],null,["# Integrate the WebView API for Ads\n\nThe web view APIs for ads makes app signals available to the tags in your\n\n[`WebView`](//developer.android.com/reference/android/webkit/WebView), helping to improve monetization for the\npublishers that provided the content and protect advertisers from spam.\n\n\n### How it works\n\nCommunication with the\n\nGoogle Mobile Ads SDK\n\nonly happens in response to ad\nevents triggered by any of the following:\n\n- [AdSense code](//support.google.com/adsense/answer/9274634)\n- [Google Publisher Tag](//support.google.com/admanager/answer/181073)\n- [IMA for HTML5](//support.google.com/adsense/answer/6391192)\n\nThe SDK adds message handlers to the registered `WebView` to listen for\nthese ad events. For a better sense of how this works, view the\n[source code](//github.com/google/webview-ads/blob/main/test/index.html) of the\ntest page.\n\n### Prerequisites\n\n- [Google Mobile Ads SDK](/admob/android/quick-start#import_the_mobile_ads_sdk) version 20.6.0 or higher.\n- Android API level 21 or higher.\n\n- Add the following `\u003cmeta-data\u003e` tag in your `AndroidManifest.xml` file to\n bypass the check for the `APPLICATION_ID`. If you miss this step and don't\n provide the `\u003cmeta-data\u003e` tag, the\n\n Google Mobile Ads SDK\n\n throws an\n [`IllegalStateException`](//developer.android.com/reference/java/lang/IllegalStateException)\n on app start.\n\n \u003c!-- Bypass APPLICATION_ID check for web view APIs for ads --\u003e\n \u003cmeta-data\n android:name=\"com.google.android.gms.ads.INTEGRATION_MANAGER\"\n android:value=\"webview\"/\u003e\n\n\u003cbr /\u003e\n\n### Register the web view\n\nCall\n\n[`registerWebView()`](/admob/android/reference/com/google/android/gms/ads/MobileAds#registerWebView(android.webkit.WebView))\n\non the main thread to establish a connection with the JavaScript handlers in the\nAdSense code or Google Publisher Tag within each `WebView` instance. This\nshould be done as early as possible, such as in the\n\n`onCreate()` method of your `MainActivity`.\n\n\n### Kotlin\n\n import android.webkit.CookieManager\n import android.webkit.WebView\n import com.google.android.gms.ads.MobileAds\n\n class MainActivity : AppCompatActivity() {\n lateinit var webView: WebView\n\n override fun onCreate(savedInstanceState: Bundle?) {\n super.onCreate(savedInstanceState)\n setContentView(R.layout.activity_main)\n webView = findViewById(R.id.webview)\n\n // Let the web view accept third-party cookies.\n CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true)\n // Let the web view use JavaScript.\n webView.settings.javaScriptEnabled = true\n // Let the web view access local storage.\n webView.settings.domStorageEnabled = true\n // Let HTML videos play automatically.\n webView.settings.mediaPlaybackRequiresUserGesture = false\n\n // Register the web view.\n MobileAds.registerWebView(webView)\n }\n }\n\n### Java\n\n import android.webkit.CookieManager;\n import android.webkit.WebView;\n import com.google.android.gms.ads.MobileAds;\n\n public class MainActivity extends AppCompatActivity {\n private WebView webView;\n\n @Override\n protected void onCreate(Bundle savedInstanceState) {\n super.onCreate(savedInstanceState);\n setContentView(R.layout.activity_main);\n webView = findViewById(R.id.webview);\n\n // Let the web view accept third-party cookies.\n CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);\n // Let the web view use JavaScript.\n webView.getSettings().setJavaScriptEnabled(true);\n // Let the web view access local storage.\n webView.getSettings().setDomStorageEnabled(true);\n // Let HTML videos play automatically.\n webView.getSettings().setMediaPlaybackRequiresUserGesture(false);\n\n // Register the web view.\n MobileAds.registerWebView(webView);\n }\n }\n\n### Test your integration\n\nBefore using your own URL, we recommend that you load the following URL to test\nthe integration: \n\n https://google.github.io/webview-ads/test#api-for-ads-tests\n\nThe test URL shows green status bars for a successful integration if the\nfollowing conditions apply:\n\n- `WebView` connected to the Google Mobile Ads SDK\n\n### Next steps\n\n- Gather consent in `WebView`. The Web view APIs for Ads doesn't propagate consent collected in the mobile app context using [IAB TCF v2.0](//iabeurope.eu/tcf-2-0/) or [IAB CCPA](//iabtechlab.com/wp-content/uploads/2019/11/Technical-Specifications-FAQ-US-Privacy-IAB-Tech-Lab.pdf) compliance frameworks to the tags in your web views. If you're interested in implementing a single consent flow as the owner of both the `WebView` and its corresponding web content being monetized, work with your consent management platform to gather consent in the `WebView` context."]]