รูปแบบโฆษณาเนทีฟที่กำหนดเอง
นอกเหนือจากรูปแบบโฆษณาเนทีฟที่ระบบกำหนดแล้ว ผู้เผยแพร่โฆษณา Ad Manager ยังมีตัวเลือกในการสร้างรูปแบบโฆษณาเนทีฟของตนเองโดยกำหนดรายการเนื้อหาที่กำหนดเอง โฆษณาประเภทนี้เรียกว่ารูปแบบโฆษณาเนทีฟที่กำหนดเอง และใช้กับโฆษณาที่จองไว้ได้ วิธีนี้ช่วยให้ผู้เผยแพร่โฆษณาส่งข้อมูลที่มีโครงสร้างที่กำหนดเองไปยังแอปของตนได้ โฆษณาเหล่านี้แสดงผลด้วยออบเจ็กต์ NativeCustomFormatAd
โหลดรูปแบบโฆษณาเนทีฟที่กำหนดเอง
คู่มือนี้อธิบายวิธีโหลดและแสดงรูปแบบโฆษณาเนทีฟที่กำหนดเอง
สร้าง AdLoader
เช่นเดียวกับโฆษณาเนทีฟ ระบบจะโหลดรูปแบบโฆษณาเนทีฟที่กำหนดเองโดยใช้คลาส AdLoader
ดังนี้
Java
AdLoader adLoader = new AdLoader.Builder(context, "/6499/example/native") .forCustomFormatAd("10063170", new NativeCustomFormatAd.OnCustomFormatAdLoadedListener() { @Override public void onCustomFormatAdLoaded(NativeCustomFormatAd ad) { // Show the custom format and record an impression. } }, new NativeCustomFormatAd.OnCustomClickListener() { @Override public void onCustomClick(NativeCustomFormatAd ad, String s) { // Handle the click action } }) .withAdListener( ... ) .withNativeAdOptions( ... ) .build();
Kotlin
val adLoader = AdLoader.Builder(this, "/6499/example/native") .forCustomFormatAd("10063170", { ad -> // Show the custom format and record an impression. }, { ad, s -> // Handle the click action }) .withAdListener( ... ) .withNativeAdOptions( ... ) .build()
เมธอด forCustomFormatAd
จะกำหนดค่า AdLoader
ให้ขอรูปแบบโฆษณาเนทีฟที่กำหนดเอง โดยจะมีการส่งพารามิเตอร์ 3 ตัวเข้าไปในเมธอด นั่นคือ
- รหัสของรูปแบบโฆษณาเนทีฟที่กำหนดเองที่
AdLoader
ควรขอ รูปแบบโฆษณาเนทีฟที่กำหนดเองแต่ละรูปแบบจะมีรหัสเชื่อมโยงอยู่ พารามิเตอร์นี้ระบุรูปแบบที่แอปต้องการให้AdLoader
ขอ - จะมีการเรียกใช้
OnCustomFormatAdLoadedListener
เมื่อโหลดโฆษณาสำเร็จ - การเรียก (ไม่บังคับ)
OnCustomClickListener
เมื่อผู้ใช้แตะหรือคลิกที่โฆษณา ดูข้อมูลเพิ่มเติมเกี่ยวกับการฟังนี้ได้ในส่วน "การจัดการการคลิกและการแสดงผล" ด้านล่าง
เนื่องจากหน่วยโฆษณา 1 หน่วยตั้งค่าให้แสดงรูปแบบโฆษณาได้มากกว่า 1 รูปแบบ ระบบจึงอาจเรียกใช้ forCustomFormatAd
หลายครั้งด้วยรหัสรูปแบบที่ไม่ซ้ำกัน เพื่อเตรียมตัวโหลดโฆษณาสำหรับรูปแบบโฆษณาเนทีฟที่กำหนดเองที่เป็นไปได้มากกว่า 1 รูปแบบ
รหัสรูปแบบโฆษณาเนทีฟที่กำหนดเอง
รหัสรูปแบบที่ใช้ระบุรูปแบบโฆษณาเนทีฟที่กำหนดเองได้ใน UI ของ Ad Manager ในส่วนโฆษณาเนทีฟภายในเมนูแบบเลื่อนลงการแสดงโฆษณา โดยมีรายละเอียดดังนี้
รหัสรูปแบบโฆษณาเนทีฟที่กำหนดเองแต่ละรหัสจะปรากฏข้างชื่อ การคลิกชื่อใดชื่อหนึ่งจะนำคุณไปยังหน้าจอรายละเอียดที่แสดงข้อมูลเกี่ยวกับช่องของรูปแบบ ดังนี้
คุณจะเพิ่ม แก้ไข และนำช่องแต่ละช่องออกจากที่นี่ได้ จดชื่อของแต่ละเนื้อหา ชื่อเป็นคีย์ที่ใช้รับข้อมูลของเนื้อหาแต่ละรายการ เมื่อแสดงรูปแบบโฆษณาเนทีฟที่กำหนดเอง
รูปแบบโฆษณาเนทีฟที่กำหนดเองในเครือข่ายดิสเพลย์
รูปแบบโฆษณาเนทีฟที่กำหนดเองแตกต่างจากรูปแบบที่ระบบกำหนดตรงที่ผู้เผยแพร่โฆษณามีอำนาจในการกำหนดรายการเนื้อหาของตนเองที่จะประกอบเป็นโฆษณา ดังนั้น กระบวนการแสดงโฆษณาจึงแตกต่างจากรูปแบบที่ระบบกำหนดใน 2-3 รูปแบบ ดังนี้
- เนื่องจากคลาส
NativeCustomFormatAd
มีไว้เพื่อจัดการรูปแบบโฆษณาเนทีฟที่กำหนดเองแบบใดก็ตามที่คุณกำหนดไว้ใน Ad Manager จึงไม่มีชื่อ "getters" สำหรับเนื้อหา แต่จะมีเมธอดอย่างgetText
และgetImage
ที่ใช้ชื่อช่องเป็นพารามิเตอร์ - ไม่มีคลาสการดูโฆษณาเฉพาะอย่าง
NativeAdView
เพื่อใช้กับNativeCustomFormatAd
คุณสามารถใช้เลย์เอาต์ใดก็ได้ที่เหมาะกับประสบการณ์ของผู้ใช้ - เนื่องจากไม่มีคลาส
ViewGroup
โดยเฉพาะ คุณจึงไม่จำเป็นต้องบันทึก มุมมองที่คุณใช้เพื่อแสดงเนื้อหาของโฆษณา วิธีนี้ช่วยประหยัดโค้ดได้ 2-3 บรรทัดเมื่อแสดงโฆษณา แต่ก็หมายความว่าคุณจะต้องทำงานเพิ่มเติมอีกเล็กน้อยเพื่อรองรับการคลิกในภายหลัง
ต่อไปนี้คือตัวอย่างฟังก์ชันที่แสดง NativeCustomFormatAd
Java
public void displayCustomFormatAd (ViewGroup parent, NativeCustomFormatAd customFormatAd) { // Inflate a layout and add it to the parent ViewGroup. LayoutInflater inflater = (LayoutInflater) parent.getContext() .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View adView = inflater.inflate(R.layout.custom_format_ad, parent); // Locate the TextView that will hold the value for "Headline" and // set its text. TextView myHeadlineView = (TextView) adView.findViewById(R.id.headline); myHeadlineView.setText(customFormatAd.getText("Headline")); // Locate the ImageView that will hold the value for "MainImage" and // set its drawable. Button myMainImageView = (ImageView) adView.findViewById(R.id.main_image); myMainImageView.setImageDrawable( customFormatAd.getImage("MainImage").getDrawable()); ... // Continue locating views and displaying assets until finished. ... }
Kotlin
public fun displayCustomFormatAd (parent: ViewGroup, customFormatAd: NativeCustomFormatAd) { val adView = layoutInflater .inflate(R.layout.ad_simple_custom_format, null) val myHeadlineView = adView.findViewById<TextView>(R.id.headline) myHeadlineView.setText(customFormatAd.getText("Headline")); // Locate the ImageView that will hold the value for "MainImage" and // set its drawable. val myMainImageView = adView.findViewById(R.id.main_image); myMainImageView.setImageDrawable( customFormatAd.getImage("MainImage").drawable); ... // Continue locating views and displaying assets until finished. ... }
แสดงผลไอคอนตัวเลือกโฆษณาอื่นๆ
การรองรับกฎหมายบริการดิจิทัล (DSA) กำหนดให้โฆษณาแบบจองล่วงหน้าที่แสดงในเขตเศรษฐกิจยุโรป (EEA) ต้องมีไอคอนตัวเลือกโฆษณาอื่นๆ และลิงก์ไปยังหน้า "เกี่ยวกับโฆษณานี้" ของ Google เมื่อคุณติดตั้งโฆษณาเนทีฟที่กำหนดเอง คุณจะต้องเป็นผู้รับผิดชอบในการแสดงผลไอคอนตัวเลือกโฆษณาอื่นๆ เราขอแนะนำให้คุณทำตามขั้นตอนในการแสดงผลและตั้งค่าเครื่องมือฟังคลิกสำหรับไอคอนตัวเลือกโฆษณาอื่นๆ เมื่อแสดงผลชิ้นงานโฆษณาหลัก
ตัวอย่างต่อไปนี้ถือว่าคุณได้กำหนดองค์ประกอบ <ImageView />
ในลำดับชั้นการแสดงผลเพื่อเก็บโลโก้ตัวเลือกโฆษณาอื่นๆ
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:id="@+id/adChoices"
android:layout_width="15dp"
android:layout_height="15dp"
android:adjustViewBounds="true"
android:contentDescription="AdChoices icon." />
</LinearLayout>
ตัวอย่างต่อไปนี้แสดงไอคอนตัวเลือกโฆษณาอื่นๆ และ กำหนดค่าลักษณะการคลิกที่เหมาะสม
Java
private AdSimpleCustomTemplateBinding customTemplateBinding;
private void populateAdView(final NativeCustomFormatAd nativeCustomFormatAd) {
// Render the AdChoices icon.
String adChoicesKey = NativeAdAssetNames.ASSET_ADCHOICES_CONTAINER_VIEW;
NativeAd.Image adChoicesAsset = nativeCustomFormatAd.getImage(adChoicesKey);
if (adChoicesAsset == null) {
customTemplateBinding.adChoices.setVisibility(View.GONE);
} else {
customTemplateBinding.adChoices.setVisibility(View.VISIBLE);
customTemplateBinding.adChoices.setImageDrawable(adChoicesAsset.getDrawable());
// Enable clicks on AdChoices.
customTemplateBinding.adChoices.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
nativeCustomFormatAd.performClick(adChoicesKey);
}
});
}
...
}
Kotlin
private lateinit var customTemplateBinding: AdSimpleCustomTemplateBinding
private fun populateAdView(nativeCustomFormatAd: NativeCustomFormatAd) {
// Render the AdChoices icon.
val adChoicesKey = NativeAdAssetNames.ASSET_ADCHOICES_CONTAINER_VIEW
val adChoicesAsset = nativeCustomFormatAd.getImage(adChoicesKey)
if (adChoicesAsset == null) {
customTemplateBinding.adChoices.visibility = View.GONE
} else {
customTemplateBinding.adChoices.setImageDrawable(adChoicesAsset.drawable)
customTemplateBinding.adChoices.visibility = View.VISIBLE
// Enable clicks on AdChoices.
customTemplateBinding.adChoices.setOnClickListener {
nativeCustomFormatAd.performClick(adChoicesKey)
}
}
...
}
วิดีโอเนทีฟสำหรับรูปแบบโฆษณาเนทีฟที่กำหนดเอง
เมื่อสร้างรูปแบบที่กำหนดเอง คุณจะมีตัวเลือกในการทำให้รูปแบบมีสิทธิ์แสดงในวิดีโอ
ในการใช้งานแอป คุณสามารถใช้ NativeCustomFormatAd.getMediaContent()
เพื่อรับเนื้อหาสื่อ จากนั้นเรียกใช้ setMediaContent()
เพื่อตั้งค่าเนื้อหาสื่อในมุมมองสื่อไปยังมุมมองสื่อ
หากโฆษณาไม่มีเนื้อหาวิดีโอ ให้เลือกแผนอื่นเพื่อแสดงโฆษณาที่ไม่มีวิดีโอ
ตัวอย่างด้านล่างเพื่อตรวจสอบว่าโฆษณามีเนื้อหาวิดีโอหรือไม่ และแสดงรูปภาพแทนที่โฆษณานั้นในกรณีที่วิดีโอไม่พร้อมใช้งาน
Java
// Called when a custom native ad loads. @Override public void onCustomFormatAdLoaded(final NativeCustomFormatAd ad) { MediaContent mediaContent = ad.getMediaContent(); // Assumes you have a FrameLayout in your view hierarchy with the id media_placeholder. FrameLayout mediaPlaceholder = (FrameLayout) findViewById(R.id.media_placeholder); // Apps can check the MediaContent's hasVideoContent property to determine if the // NativeCustomFormatAd has a video asset. if (mediaContent != null && mediaContent.hasVideoContent()) { MediaView mediaView = new MediaView(mediaPlaceholder.getContext()); mediaView.setMediaContent(mediaContent); mediaPlaceholder.addView(mediaView); // Create a new VideoLifecycleCallbacks object and pass it to the VideoController. The // VideoController will call methods on this object when events occur in the video // lifecycle. VideoController vc = mediaContent.getVideoController(); vc.setVideoLifecycleCallbacks( new VideoController.VideoLifecycleCallbacks() { @Override public void onVideoEnd() { // Publishers should allow native ads to complete video playback before // refreshing or replacing them with another ad in the same UI location. super.onVideoEnd(); } }); } else { ImageView mainImage = new ImageView(this); mainImage.setAdjustViewBounds(true); mainImage.setImageDrawable(ad.getImage("MainImage").getDrawable()); mediaPlaceholder.addView(mainImage); mainImage.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View view) { ad.performClick("MainImage"); } }); } }
Kotlin
// Called when a custom native ad loads. NativeCustomFormatAd.OnCustomFormatAdLoadedListener { ad -> val mediaContent = ad.mediaContent // Apps can check the MediaContent's hasVideoContent property to determine if the // NativeCustomFormatAd has a video asset. if (mediaContent != null && mediaContent.hasVideoContent()) { val mediaView = MediaView(mediaPlaceholder.getContest()) mediaView.mediaContent = mediaContent val videoController = mediaContent.videoController // Create a new VideoLifecycleCallbacks object and pass it to the VideoController. The // VideoController will call methods on this object when events occur in the video // lifecycle. if (videoController != null) { videoController.videoLifecycleCallbacks = object : VideoController.VideoLifecycleCallbacks() { override fun onVideoEnd() { // Publishers should allow native ads to complete video playback before refreshing // or replacing them with another ad in the same UI location. super.onVideoEnd() } } } } else { val mainImage = ImageView(this) mainImage.adjustViewBounds = true mainImage.setImageDrawable(ad.getImage("MainImage")?.drawable) mainImage.setOnClickListener { ad.performClick("MainImage") } customTemplateBinding.simplecustomMediaPlaceholder.addView(mainImage) } }
ดูข้อมูลเพิ่มเติมเกี่ยวกับวิธีปรับแต่งประสบการณ์การใช้งานวิดีโอของโฆษณาเนทีฟที่กำหนดเองได้ที่ MediaContent
ดาวน์โหลดตัวอย่างการแสดงผลที่กำหนดเองของ Ad Manager เพื่อดูตัวอย่างการทำงานจริงของวิดีโอเนทีฟ
จำนวนคลิกและการแสดงผลของรูปแบบโฆษณาเนทีฟที่กำหนดเอง
เมื่อใช้รูปแบบโฆษณาเนทีฟที่กำหนดเอง แอปของคุณจะทำหน้าที่บันทึก การแสดงผลและการรายงานเหตุการณ์การคลิกไปยัง SDK โฆษณาในอุปกรณ์เคลื่อนที่ของ Google
บันทึกการแสดงผล
หากต้องการบันทึกการแสดงผลสำหรับโฆษณารูปแบบที่กำหนดเอง ให้เรียกใช้เมธอด recordImpression
ใน NativeCustomFormatAd
ที่เกี่ยวข้อง
myCustomFormatAd.recordImpression();
หากแอปเรียกใช้เมธอด 2 ครั้งสำหรับโฆษณาเดียวกันโดยไม่ได้ตั้งใจ SDK จะป้องกันไม่ให้มีการบันทึกการแสดงผลที่ซ้ำกันสำหรับคำขอเดียวโดยอัตโนมัติ
รายงานการคลิก
หากต้องการรายงานไปยัง SDK ว่ามีการคลิกเกิดขึ้นในข้อมูลพร็อพเพอร์ตี้เนื้อหา ให้เรียกเมธอด performClick
ใน NativeCustomFormatAd
ที่เกี่ยวข้อง แล้วส่งต่อชื่อของเนื้อหาที่มีการคลิก ตัวอย่างเช่น หากคุณมีเนื้อหาในรูปแบบที่กำหนดเองชื่อ "MainImage" และต้องการรายงานการคลิก ImageView
ซึ่งตรงกับเนื้อหานั้น โค้ดของคุณจะมีลักษณะดังนี้
myCustomFormatAd.performClick("MainImage");
โปรดทราบว่าคุณไม่จำเป็นต้องเรียกใช้วิธีนี้สำหรับทุกข้อมูลพร็อพเพอร์ตี้ที่เชื่อมโยงกับโฆษณาของคุณ หากคุณมีช่องอีกช่องหนึ่งชื่อ "คำบรรยายภาพ" ซึ่งตั้งใจให้แสดง แต่ผู้ใช้ไม่ได้คลิกหรือแตะ แอปจะไม่จำเป็นต้องเรียก performClick
สำหรับการดูเนื้อหานั้น
ตอบสนองต่อการคลิกที่กำหนดเอง
เมื่อมีการคลิกในโฆษณารูปแบบที่กำหนดเอง SDK จะมีการตอบสนองที่เป็นไปได้ 3 แบบ ซึ่งพยายามทำตามลำดับต่อไปนี้
- เรียกใช้
OnCustomClickListener
จากAdLoader
หากระบุไว้ - สำหรับ URL แบบ Deep Link ของแต่ละ URL ให้พยายามหาตัวแก้ไขเนื้อหา แล้วเริ่มใช้รีโซลเวอร์แรกที่แปลงค่านั้น
- เปิดเบราว์เซอร์และไปที่ URL ปลายทางแบบดั้งเดิมของโฆษณา
เมธอด forCustomFormatAd
ยอมรับ OnCustomClickListener
หากคุณส่งออบเจ็กต์ Listener เข้ามา SDK จะเรียกใช้เมธอด onCustomClick
แทนและไม่ดําเนินการใดๆ เพิ่มเติม อย่างไรก็ตาม หากคุณส่งค่า Null เป็นผู้ฟัง SDK จะกลับไปที่ Deep Link และ/หรือ URL ปลายทางที่ลงทะเบียนไว้กับโฆษณา
Listener การคลิกที่กำหนดเองอนุญาตให้แอปตัดสินใจเลือกการดำเนินการที่ดีที่สุดเพื่อตอบสนองต่อการคลิก ไม่ว่าจะเป็นการอัปเดต UI, การเปิดตัวกิจกรรมใหม่ หรือแค่การบันทึกการคลิก ต่อไปนี้เป็นตัวอย่างที่บันทึกว่ามีการคลิกเกิดขึ้น
Java
AdLoader adLoader = new AdLoader.Builder(context, "/6499/example/native") .forCustomFormatAd("10063170", new NativeCustomFormatAd.OnCustomFormatAdLoadedListener() { // Display the ad. }, new NativeCustomFormatAd.OnCustomClickListener() { @Override public void onCustomClick(NativeCustomFormatAd ad, String assetName) { Log.i("MyApp", "A custom click just happened for " + assetName + "!"); } }).build();
Kotlin
val adLoader = AdLoader.Builder(this, "/6499/example/native") .forCustomFormatAd("10063170", { ad -> // Display the ad. }, { ad, assetName -> Log.i("MyApp", "A custom click just happened for $assetName!") }).build()
ตอนแรกอาจดูแปลกๆ ที่มี Listener การคลิกที่กำหนดเองอยู่ เพราะจริงๆ แล้ว แอปของคุณเพิ่งบอก SDK ว่าเกิดการคลิก แล้วเหตุใด SDK จึงควรดำเนินการ และรายงานเรื่องนี้ให้แอปทราบ
การรับส่งข้อมูลนี้มีประโยชน์ด้วยเหตุผล 2-3 ประการ แต่ที่สำคัญที่สุดคือช่วยให้ SDK ยังคงควบคุมการตอบสนองต่อการคลิกได้ คำสั่งนี้อาจใช้คำสั่ง ping กับ URL ติดตามของบุคคลที่สามที่ตั้งค่าไว้สำหรับครีเอทีฟโฆษณาโดยอัตโนมัติ และจัดการงานอื่นๆ เบื้องหลังได้โดยไม่ต้องมีโค้ดเพิ่มเติม