นอกจากรูปแบบเนทีฟที่ระบบกำหนดแล้ว ผู้เผยแพร่โฆษณา Ad Manager ยังมีตัวเลือกในการสร้างรูปแบบโฆษณาเนทีฟของตนเองโดยการกำหนดรายการชิ้นงานที่กำหนดเอง ซึ่งเรียกว่ารูปแบบโฆษณาเนทีฟที่กำหนดเอง และใช้กับโฆษณาแบบจองล่วงหน้าได้ ซึ่งช่วยให้ผู้เผยแพร่โฆษณาส่งข้อมูลที่มีโครงสร้างที่กำหนดเองไปยังแอปของตนได้ โฆษณาเหล่านี้จะแสดงด้วยออบเจ็กต์ NativeCustomFormatAd
โหลดรูปแบบโฆษณาเนทีฟที่กำหนดเอง
คู่มือนี้อธิบายวิธีโหลดและแสดงรูปแบบโฆษณาเนทีฟที่กำหนดเอง
โหลดโฆษณาเนทีฟที่กำหนดเอง
หากต้องการโหลดโฆษณาเนทีฟที่กำหนดเอง ให้ทำดังนี้
รวมประเภท
NativeAdType.CUSTOM_NATIVEเป็นประเภทโฆษณาในNativeAdRequestตั้งค่ารหัสรูปแบบของโฆษณาเนทีฟที่กำหนดเอง
Kotlin
Java
รหัสรูปแบบโฆษณาเนทีฟที่กำหนดเอง
รหัสรูปแบบที่ใช้ระบุรูปแบบโฆษณาเนทีฟที่กำหนดเองจะอยู่ใน UI ของ Ad Manager ในส่วนเนทีฟภายในเมนูแบบเลื่อนลงการแสดงโฆษณา

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

คุณสามารถเพิ่ม แก้ไข และนำฟิลด์แต่ละรายการออกได้จากที่นี่ จดชื่อของชิ้นงานแต่ละรายการไว้ ชื่อคือคีย์ที่ใช้เพื่อรับข้อมูลของชิ้นงานแต่ละรายการเมื่อแสดงรูปแบบโฆษณาเนทีฟที่กำหนดเอง
รูปแบบโฆษณาเนทีฟที่กำหนดเองใน Display
รูปแบบโฆษณาเนทีฟที่กำหนดเองแตกต่างจากรูปแบบที่ระบบกำหนดตรงที่ผู้เผยแพร่โฆษณาสามารถกำหนดรายการชิ้นงานของตนเองที่ประกอบกันเป็นโฆษณาได้ ดังนั้นกระบวนการแสดงโฆษณาเนทีฟจึงแตกต่างจากรูปแบบที่ระบบกำหนดในบางประการ ดังนี้
- ชิ้นงานข้อความและชิ้นงานรูปภาพพร้อมใช้งานผ่าน Getter
getText()และgetImage()ที่ใช้ชื่อช่องเป็นพารามิเตอร์ - เนื่องจากไม่มีคลาส
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();
}
ดูข้อมูลเพิ่มเติมเกี่ยวกับวิธีปรับแต่งประสบการณ์การใช้งานวิดีโอของโฆษณาเนทีฟที่กำหนดเองได้ที่โฆษณาวิดีโอ
แสดงผลไอคอนตัวเลือกโฆษณาอื่นๆ
โฆษณาแบบจองล่วงหน้าที่แสดงในเขตเศรษฐกิจยุโรป (EEA) ต้องมีไอคอนตัวเลือกโฆษณาอื่นๆ และลิงก์ไปยังหน้าเกี่ยวกับโฆษณานี้ของ Google เพื่อสนับสนุนกฎหมายบริการดิจิทัล (DSA) เมื่อติดตั้งใช้งานโฆษณาเนทีฟที่กำหนดเอง คุณจะเป็นผู้รับผิดชอบในการแสดงผลไอคอนตัวเลือกโฆษณาอื่นๆ เราขอแนะนำให้คุณทำตามขั้นตอนเพื่อแสดงผลและตั้งค่าฟังก์ชันรอคลิกสำหรับไอคอนตัวเลือกโฆษณาอื่นๆ เมื่อแสดงผลชิ้นงานโฆษณาหลัก
ตัวอย่างต่อไปนี้ถือว่าคุณได้กำหนดองค์ประกอบ <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);
}
}
}
บันทึกการแสดงผลและรายงานการคลิก
แอปของคุณมีหน้าที่รับผิดชอบในการบันทึกการแสดงผลและรายงานกิจกรรมการคลิกไปยัง GMA Next-Gen SDK
การแสดงผลของบันทึก
หากต้องการบันทึกการแสดงผลสำหรับโฆษณาเนทีฟที่กำหนดเอง ให้เรียกใช้เมธอด 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 แต่ละรายการของโฆษณา ให้พยายามค้นหา Content Resolver และเริ่มรายการแรกที่แก้ไขได้
- เปิดเบราว์เซอร์และไปที่ 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 ติดตามของบุคคลที่สามที่ตั้งค่าไว้สําหรับครีเอทีฟโฆษณาโดยอัตโนมัติ และจัดการงานอื่นๆ เบื้องหลังโดยไม่ต้องใช้โค้ดเพิ่มเติม