การเปลี่ยนแปลงโค้ดเพียงเล็กน้อยจะช่วยให้คุณรวมโฆษณาเนทีฟและโฆษณาแบนเนอร์ไว้ในคำขอโฆษณาได้
ข้อกำหนดเบื้องต้น
- Google Mobile Ads SDK เวอร์ชัน 11.0.0 ขึ้นไป
- ทำตามคู่มือเริ่มต้นใช้งานให้เสร็จสมบูรณ์
โหลดโฆษณา
โฆษณาเนทีฟที่แสดงผลที่กำหนดเองจะโหลดโดยใช้คลาส AdLoader ซึ่งมีคลาส AdLoader.Builder ของตัวเองเพื่อปรับแต่งในระหว่างการสร้าง แอปจะระบุประเภทรูปแบบโฆษณาที่พร้อมรับโดยการเพิ่ม Listener ลงใน AdLoader ขณะสร้าง จากนั้น AdLoader จะขอเฉพาะประเภทเหล่านั้น
นอกจากนี้ คุณยังกำหนดค่าออบเจ็กต์ AdLoader ให้ส่งคำขอโฆษณาที่อาจส่งผลให้เกิดโฆษณาแบนเนอร์หรือโฆษณาเนทีฟได้ด้วย การเพิ่ม OnAdManagerAdViewLoadedListener ลงใน AdLoader ขณะสร้างจะเป็นการระบุว่าโฆษณาแบนเนอร์ควรแข่งขันกับโฆษณาเนทีฟเพื่อตอบสนองคำขอ
โค้ดต่อไปนี้แสดงวิธีสร้าง AdLoader ที่สามารถโหลดโฆษณาเนทีฟหรือโฆษณาแบนเนอร์ในคำขอเดียว
Java
AdLoader adLoader = new AdLoader.Builder(context, "/21775744923/example/native-and-banner") .forNativeAd(new NativeAd.OnNativeAdLoadedListener() { @Override public void onNativeAdLoaded(NativeAd nativeAd) { // Show the ad. } }) .forAdManagerAdView(new OnAdManagerAdViewLoadedListener() { @Override public void onAdManagerAdViewLoaded(AdManagerAdView adView) { // Show the banner ad. } }, AdSize.BANNER, AdSize.MEDIUM_RECTANGLE) .withAdListener(new AdListener() { @Override public void onAdFailedToLoad(LoadAdError error) { // Handle the failure by logging, altering the UI, etc. } }) .withAdManagerAdViewOptions(new AdManagerAdViewOptions.Builder() // Methods in the AdManagerAdViewOptions.Builder class can be // used here to specify individual options settings. .build()) .build();
Kotlin
val adLoader = AdLoader.Builder(this, "/21775744923/example/native-and-banner") .forNativeAd { nativeAd -> // Show the ad. } .forAdManagerAdView({ adView -> // Show the banner ad. }, AdSize.BANNER, AdSize.MEDIUM_RECTANGLE) .withAdListener(object: AdListener() { override fun onAdFailedToLoad(adError: LoadAdError) { // Handle the failure by logging, altering the UI, etc. } }) .withAdManagerAdViewOptions(AdManagerAdViewOptions.Builder() // Methods in the AdManagerAdViewOptions.Builder class can be // used here to specify individual options settings. .build()) .build()
เมธอด forAdManagerAdView() จะเตรียม AdLoader ให้พร้อมรับโฆษณาแบนเนอร์
ต้องระบุรายการขนาดโฆษณาที่ถูกต้องซึ่งมีความยาวผันแปรได้ควบคู่ไปกับ OnAdManagerAdViewLoadedListener เมื่อเรียกใช้ forAdManagerAdView()
หากต้องการส่งคำขอโฆษณาที่ถูกต้อง คุณต้องระบุขนาดโฆษณาที่ถูกต้องอย่างน้อย 1 ขนาด เมื่อโหลดโฆษณาแบนเนอร์สำเร็จ ระบบจะเรียกใช้เมธอด onAdManagerAdViewLoaded() ของออบเจ็กต์ Listener ที่ระบุ
กำหนดขนาดโฆษณาให้เล็กกว่าหรือเท่ากับขนาดหน้าจอของอุปกรณ์
หากต้องการกำหนดขนาดหน้าจอสูงสุด ให้ใช้เมธอด Resources.getDisplayMetrics ขนาดออบเจ็กต์ AdSize ใช้ความหนาแน่นของพิกเซลอิสระ (dp) เมื่อเปรียบเทียบ ให้แปลงขนาดหน้าจอจากพิกเซลเป็น dp
ดูรายละเอียดเกี่ยวกับข้อจำกัดด้านขนาดได้ที่แก้ปัญหาการแสดงผลที่ไม่ได้รับโฆษณาใน Ad Manager
ตั้งค่าออบเจ็กต์ AdViewOptions
ฟังก์ชันสุดท้ายที่รวมอยู่ในการสร้าง AdLoader คือเมธอดที่ไม่บังคับอีกเมธอดหนึ่ง ซึ่งก็คือ withAdManagerAdViewOptions()
Java
.withAdManagerAdViewOptions(new AdManagerAdViewOptions.Builder() // Methods in the AdManagerAdViewOptions.Builder class can be // used here to specify individual banner options settings. .build()
Kotlin
.withAdManagerAdViewOptions(AdManagerAdViewOptions.Builder() // Methods in the AdManagerAdViewOptions.Builder class can be // used here to specify individual banner options settings. .build()
ออบเจ็กต์ AdManagerAdViewOptions ช่วยให้ผู้เผยแพร่โฆษณาสามารถตั้งค่าตัวเลือกเฉพาะสำหรับแบนเนอร์ที่โหลดโดย AdLoader เช่น
setManualImpressionsEnabled()- เปิดใช้การรายงานการแสดงผลด้วยตนเองสำหรับการจอง Google Ad Manager แอปที่ใช้การแสดงผลด้วยตนเองสามารถกำหนดเองได้ว่าควรบันทึกการแสดงผลเมื่อใด และสามารถทำได้โดยเรียกใช้
AdManagerAdView.recordManualImpression()