นอกเหนือจากรูปแบบเนทีฟที่กำหนดโดยระบบแล้ว ผู้เผยแพร่โฆษณา Ad Manager ยังมีตัวเลือกในการสร้างรูปแบบโฆษณาเนทีฟของตนเองโดยการกำหนดรายการชิ้นงานที่กำหนดเอง
ซึ่งเรียกว่ารูปแบบโฆษณาเนทีฟที่กำหนดเอง และใช้กับโฆษณาที่จองไว้ได้ ซึ่งช่วยให้ผู้เผยแพร่โฆษณาส่ง Structured Data ที่กำหนดเองไปยังแอปของตนได้ โฆษณาเหล่านี้แสดงด้วยออบเจ็กต์
NativeCustomFormatAd
โหลดรูปแบบโฆษณาเนทีฟที่กำหนดเอง
คู่มือนี้อธิบายวิธีโหลดและแสดงรูปแบบโฆษณาเนทีฟที่กำหนดเอง
โหลดโฆษณาเนทีฟที่กำหนดเอง
หากต้องการโหลดโฆษณาเนทีฟที่กำหนดเอง ให้ทำดังนี้
รวม
NativeAdType.CUSTOM_NATIVE
ประเภทเป็นประเภทโฆษณาในNativeAdRequest
ตั้งรหัสรูปแบบของโฆษณาเนทีฟที่กำหนดเอง
Kotlin
Java
รหัสรูปแบบโฆษณาเนทีฟที่กำหนดเอง
คุณดูรหัสรูปแบบที่ใช้ระบุรูปแบบโฆษณาเนทีฟที่กำหนดเองได้ใน UI ของ Ad Manager ในส่วนเนทีฟภายในเมนูการแสดงผลแบบเลื่อนลง
รหัสรูปแบบโฆษณาเนทีฟที่กำหนดเองแต่ละรายการจะปรากฏข้างชื่อ การคลิกชื่อใดชื่อหนึ่งจะนำคุณไปยังหน้าจอรายละเอียดที่แสดงข้อมูลเกี่ยวกับฟิลด์ของรูปแบบ
คุณจะเพิ่ม แก้ไข และนำฟิลด์แต่ละรายการออกได้จากที่นี่ จดชื่อของชิ้นงานแต่ละรายการ ชื่อคือคีย์ที่ใช้เพื่อรับข้อมูลสำหรับ ชิ้นงานแต่ละรายการเมื่อแสดงรูปแบบโฆษณาเนทีฟที่กำหนดเอง
แสดงรูปแบบโฆษณาเนทีฟที่กำหนดเอง
รูปแบบโฆษณาเนทีฟที่กำหนดเองแตกต่างจากรูปแบบที่ระบบกำหนดตรงที่ผู้เผยแพร่โฆษณามีอำนาจในการกำหนดรายการชิ้นงานของตนเองที่ประกอบกันเป็นโฆษณา ดังนั้น กระบวนการแสดงโฆษณาจึงแตกต่างจากรูปแบบที่ระบบกำหนดไว้ในหลายๆ ด้าน ดังนี้
- ชิ้นงานข้อความและชิ้นงานรูปภาพพร้อมใช้งานผ่าน
getText()
และgetImage()
getters ที่ใช้ชื่อฟิลด์เป็นพารามิเตอร์ - เนื่องจากไม่มี
ViewGroup
คลาสเฉพาะเพื่อลงทะเบียนกับ Google คุณจึงต้องบันทึกการแสดงผลและการคลิกด้วยตนเอง - โฆษณาเนทีฟที่กำหนดเองมี
null
เนื้อหาสื่อหากโฆษณาไม่มีชิ้นงานวิดีโอ
ตัวอย่างนี้แสดงวิธีแสดง CustomNativeAd
Kotlin
private fun displayCustomNativeAd(customNativeAd: CustomNativeAd, context: Context) {
// Render the text elements.
// The `customNativeAdBinding` is the layout binding for the ad container that
// contains all `CustomNativeAd` assets.
customNativeAdBinding.headline.text = customNativeAd.getText("Headline")
customNativeAdBinding.caption.text = customNativeAd.getText("Caption")
// If the main asset is an image, render it with an ImageView.
val imageView = ImageView(context)
imageView.adjustViewBounds = true
imageView.setImageDrawable(customNativeAd.getImage("MainImage")?.drawable)
imageView.setOnClickListener { customNativeAd.performClick("MainImage") }
customNativeAdBinding.mediaPlaceholder.addView(imageView)
// Render the ad choices icon.
renderAdChoices(customNativeAd)
// Record an impression.
customNativeAd.recordImpression()
}
Java
private void displayCustomNativeAd(CustomNativeAd customNativeAd, Context context) {
// Render the text elements.
// The `customNativeAdBinding` is the layout binding for the ad container that
// contains all `CustomNativeAd` assets.
if (customNativeAdBinding != null) {
customNativeAdBinding.headline.setText(customNativeAd.getText("Headline"));
customNativeAdBinding.caption.setText(customNativeAd.getText("Caption"));
ImageView imageView = new ImageView(context);
imageView.setAdjustViewBounds(true);
imageView.setImageDrawable(customNativeAd.getImage("MainImage").getDrawable());
imageView.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
customNativeAd.performClick("MainImage");
}
});
customNativeAdBinding.mediaPlaceholder.addView(imageView);
// Render the ad choices icon.
renderAdChoices(customNativeAd);
// Record an impression.
customNativeAd.recordImpression();
}
}
วิดีโอเนทีฟสำหรับรูปแบบโฆษณาเนทีฟที่กำหนดเอง
เมื่อสร้างรูปแบบที่กำหนดเอง คุณจะมีตัวเลือกในการทำให้รูปแบบมีสิทธิ์สำหรับวิดีโอ
ในการติดตั้งใช้งานแอป คุณสามารถใช้ CustomNativeAd.getMediaContent()
เพื่อรับเนื้อหาสื่อ จากนั้นเรียก setMediaContent()
เพื่อตั้งค่าเนื้อหาสื่อในมุมมองสื่อ
หากโฆษณามีnull
เนื้อหาสื่อ ให้วางแผนสำรองเพื่อแสดง
โฆษณาที่ไม่มีวิดีโอ
ตัวอย่างต่อไปนี้จะตรวจสอบว่าโฆษณามีเนื้อหาวิดีโอหรือไม่ และแสดงรูปภาพ แทนหากไม่มีวิดีโอ
Kotlin
private fun displayVideoCustomNativeAd(customNativeAd: CustomNativeAd, context: Context) {
// Check whether the custom native ad has video content.
val mediaContent = customNativeAd.mediaContent
if (mediaContent != null && mediaContent.hasVideoContent) {
// Render the media content in a MediaView.
val mediaView = MediaView(context)
mediaView.mediaContent = mediaContent
customNativeAdBinding.mediaPlaceholder.addView(mediaView)
} else {
// Fall back to other assets defined on your custom native ad.
val imageView = ImageView(context)
imageView.adjustViewBounds = true
imageView.setImageDrawable(customNativeAd.getImage("MainImage")?.drawable)
customNativeAdBinding.mediaPlaceholder.addView(imageView)
}
// Record an impression.
customNativeAd.recordImpression()
}
Java
private void displayVideoCustomNativeAd(CustomNativeAd customNativeAd, Context context) {
// Check whether the custom native ad has video content.
MediaContent mediaContent = customNativeAd.getMediaContent();
if (mediaContent != null && mediaContent.getHasVideoContent()) {
// Render the media content in a MediaView.
MediaView mediaView = new MediaView(context);
mediaView.setMediaContent(mediaContent);
customNativeAdBinding.mediaPlaceholder.addView(mediaView);
} else {
// Fall back to other assets defined on your custom native ad.
ImageView imageView = new ImageView(context);
imageView.setAdjustViewBounds(true);
imageView.setImageDrawable(customNativeAd.getImage("MainImage").getDrawable());
customNativeAdBinding.mediaPlaceholder.addView(imageView);
}
// Record an impression.
customNativeAd.recordImpression();
}
ดูข้อมูลเพิ่มเติมเกี่ยวกับวิธีปรับแต่งประสบการณ์วิดีโอของโฆษณาเนทีฟที่กำหนดเองได้ที่โฆษณาวิดีโอ
แสดงผลไอคอนตัวเลือกโฆษณาอื่นๆ
ในส่วนของการสนับสนุนกฎหมายบริการดิจิทัล (DSA) โฆษณาแบบจองล่วงหน้าที่แสดงในเขตเศรษฐกิจยุโรป (EEA) ต้องมีไอคอนตัวเลือกโฆษณาอื่นๆ และลิงก์ไปยังหน้า "เกี่ยวกับโฆษณานี้" ของ Google เมื่อติดตั้งใช้งานโฆษณาเนทีฟที่กำหนดเอง คุณจะเป็นผู้รับผิดชอบในการแสดงผลไอคอนตัวเลือกโฆษณาอื่นๆ เราขอแนะนำให้คุณทำตามขั้นตอนเพื่อแสดงผลและตั้งค่า Listener การคลิกสำหรับไอคอนตัวเลือกโฆษณาอื่นๆ เมื่อแสดงผลชิ้นงานโฆษณาหลัก
ตัวอย่างต่อไปนี้ถือว่าคุณได้กำหนดองค์ประกอบ <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>
ตัวอย่างต่อไปนี้แสดงไอคอนตัวเลือกโฆษณาอื่นๆ และ กำหนดค่าลักษณะการคลิกที่เหมาะสม
Kotlin
private fun renderAdChoices(customNativeAd: CustomNativeAd) {
// Render the AdChoices image.
val adChoiceAsset = customNativeAd.getImage(NativeAdAssetNames.ASSET_ADCHOICES_CONTAINER_VIEW)
if (adChoiceAsset != null) {
customNativeAdBinding.adchoices.setImageDrawable(adChoiceAsset.drawable)
customNativeAdBinding.adchoices.visibility = View.VISIBLE
customNativeAdBinding.adchoices.setOnClickListener {
// Handle click. See the next section for more details.
customNativeAd.performClick(NativeAdAssetNames.ASSET_ADCHOICES_CONTAINER_VIEW)
}
} else {
customNativeAdBinding.adchoices.visibility = View.GONE
}
}
Java
private void renderAdChoices(CustomNativeAd customNativeAd) {
// Render the AdChoices image.
Image adChoiceAsset =
customNativeAd.getImage(NativeAdAssetNames.ASSET_ADCHOICES_CONTAINER_VIEW);
if (adChoiceAsset != null) {
if (customNativeAdBinding != null) {
customNativeAdBinding.adchoices.setImageDrawable(adChoiceAsset.getDrawable());
customNativeAdBinding.adchoices.setVisibility(View.VISIBLE);
customNativeAdBinding.adchoices.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
// Handle click.
customNativeAd.performClick(NativeAdAssetNames.ASSET_ADCHOICES_CONTAINER_VIEW);
}
});
}
} else {
if (customNativeAdBinding != null) {
customNativeAdBinding.adchoices.setVisibility(View.GONE);
}
}
}
บันทึกการแสดงผลและรายงานการคลิก
แอปของคุณมีหน้าที่บันทึกการแสดงผลและรายงานเหตุการณ์คลิกไปยัง SDK โฆษณาในอุปกรณ์เคลื่อนที่ของ Google (เบต้า)
บันทึกการแสดงผล
หากต้องการบันทึกการแสดงผลสำหรับโฆษณาเนทีฟที่กำหนดเอง ให้เรียกใช้เมธอด recordImpression()
ของโฆษณา
Kotlin
// Record an impression.
customNativeAd.recordImpression()
Java
// Record an impression.
customNativeAd.recordImpression();
หากแอปเรียกใช้เมธอด 2 ครั้งโดยไม่ตั้งใจสำหรับโฆษณาเดียวกัน SDK จะป้องกันไม่ให้บันทึกการแสดงผลที่ซ้ำกันสำหรับคำขอเดียวโดยอัตโนมัติ
รายงานการคลิก
หากต้องการรายงานไปยัง SDK ว่ามีการคลิกในมุมมองชิ้นงาน ให้เรียกใช้เมธอด performClick()
ของโฆษณา ระบุชื่อของชิ้นงานที่คลิก
โดยใช้สตริงเดียวกันกับที่คุณกำหนดไว้ใน UI ของ Ad Manager
Kotlin
imageView.setOnClickListener { customNativeAd.performClick("MainImage") }
Java
imageView.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
customNativeAd.performClick("MainImage");
}
});
โปรดทราบว่าคุณไม่จำเป็นต้องเรียกใช้เมธอดนี้สำหรับการดูทุกครั้งที่เชื่อมโยงกับโฆษณา
ของคุณ หากคุณมีฟิลด์อื่นชื่อ "คำบรรยายแทนเสียง" ซึ่งมีไว้เพื่อแสดง
แต่ผู้ใช้ไม่ได้คลิกหรือแตะ แอปของคุณก็ไม่จำเป็นต้องเรียกใช้ performClick
สำหรับมุมมองของชิ้นงานนั้น
ตอบสนองต่อการกระทำเมื่อคลิกที่กำหนดเอง
เมื่อมีการคลิกโฆษณารูปแบบที่กำหนดเอง SDK จะตอบกลับได้ 3 รูปแบบตามลำดับต่อไปนี้
- เรียกใช้
OnCustomClickListener
หากมีการระบุไว้ - สำหรับ URL ของ Deep Link ของโฆษณาแต่ละรายการ ให้พยายามค้นหาตัวแก้ไขเนื้อหา และเริ่มตัวแก้ไขแรกที่แก้ไขได้
- เปิดเบราว์เซอร์แล้วไปที่ URL ปลายทางของโฆษณา
หากต้องการใช้การคลิกที่กำหนดเอง ให้ระบุOnCustomClickListener
Kotlin
customNativeAd.onCustomClickListener =
object : OnCustomClickListener {
override fun onCustomClick(assetName: String) {
// Perform your custom action.
}
}
Java
customNativeAd.setOnCustomClickListener(
new OnCustomClickListener() {
@Override
public void onCustomClick(@NonNull String assetName) {
// Perform your custom action.
}
});
ในตอนแรก คุณอาจรู้สึกแปลกใจที่ตัวฟังการคลิกที่กำหนดเองมีอยู่ เพราะท้ายที่สุดแล้ว แอปของคุณเพิ่งบอก SDK ว่ามีการคลิกเกิดขึ้น แล้วทำไม SDK ต้อง กลับมารายงานเรื่องนั้นให้แอปทราบด้วย
การไหลเวียนของข้อมูลนี้มีประโยชน์ด้วยเหตุผลหลายประการ แต่ที่สำคัญที่สุดคือช่วยให้ SDK ควบคุมการตอบสนองต่อการคลิกได้ โดยสามารถ ส่ง Ping โดยอัตโนมัติไปยัง URL ติดตามของบุคคลที่สามที่ตั้งค่าไว้สำหรับ ครีเอทีฟโฆษณา เป็นต้น และจัดการงานอื่นๆ เบื้องหลังโดยไม่ต้องใช้ โค้ดเพิ่มเติม