सिस्टम के तय किए गए नेटिव फ़ॉर्मैट के अलावा, Ad Manager पब्लिशर के पास ऐसेट की कस्टम सूचियां तय करके, अपने नेटिव विज्ञापन फ़ॉर्मैट बनाने का विकल्प होता है. इन्हें कस्टम नेटिव विज्ञापन फ़ॉर्मैट कहा जाता है. इनका इस्तेमाल, रिज़र्व किए गए विज्ञापनों के साथ किया जा सकता है. इससे पब्लिशर, अपने ऐप्लिकेशन में किसी भी तरह का स्ट्रक्चर्ड डेटा पास कर सकते हैं. इन विज्ञापनों को
NativeCustomFormatAd
ऑब्जेक्ट से दिखाया जाता है.
कस्टम नेटिव विज्ञापन फ़ॉर्मैट लोड करना
इस गाइड में, कस्टम नेटिव विज्ञापन फ़ॉर्मैट को लोड करने और दिखाने का तरीका बताया गया है.
कस्टम नेटिव विज्ञापन लोड करना
कस्टम नेटिव विज्ञापन लोड करने के लिए, यह तरीका अपनाएं:
NativeAdRequestमें विज्ञापन के टाइप के तौर परNativeAdType.CUSTOM_NATIVEटाइप को शामिल करें.कस्टम नेटिव विज्ञापन का फ़ॉर्मैट आईडी सेट करें.
Kotlin
Java
कस्टम नेटिव विज्ञापन फ़ॉर्मैट का आईडी
कस्टम नेटिव विज्ञापन फ़ॉर्मैट की पहचान करने के लिए इस्तेमाल किया जाने वाला फ़ॉर्मैट आईडी, Ad Manager के यूज़र इंटरफ़ेस (यूआई) में डिलीवरी ड्रॉप-डाउन के अंदर नेटिव सेक्शन में मिल सकता है:

हर कस्टम नेटिव विज्ञापन फ़ॉर्मैट का आईडी, उसके नाम के बगल में दिखता है. किसी नाम पर क्लिक करने से, आपको ज़्यादा जानकारी वाली स्क्रीन पर ले जाया जाता है. इस स्क्रीन पर, फ़ॉर्मैट के फ़ील्ड की जानकारी दिखती है:

यहां से, अलग-अलग फ़ील्ड जोड़े जा सकते हैं, उनमें बदलाव किया जा सकता है, और उन्हें हटाया जा सकता है. हर ऐसेट का नाम नोट करें. कस्टम नेटिव विज्ञापन फ़ॉर्मैट दिखाते समय, हर ऐसेट का डेटा पाने के लिए, नाम का इस्तेमाल कुंजी के तौर पर किया जाता है.
कस्टम नेटिव विज्ञापन फ़ॉर्मैट दिखाना
कस्टम नेटिव विज्ञापन फ़ॉर्मैट, सिस्टम के तय किए गए फ़ॉर्मैट से अलग होते हैं. इनमें पब्लिशर के पास, विज्ञापन बनाने वाली ऐसेट की अपनी सूची तय करने का विकल्प होता है. इसलिए, कस्टम फ़ॉर्मैट दिखाने की प्रोसेस, सिस्टम के तय किए गए फ़ॉर्मैट से कुछ मामलों में अलग होती है:
- टेक्स्ट और इमेज ऐसेट,
getText()औरgetImage()गेटर के ज़रिए उपलब्ध होती हैं. ये गेटर, फ़ील्ड के नाम को पैरामीटर के तौर पर लेते हैं. - Google के साथ रजिस्टर करने के लिए, कोई खास
ViewGroupक्लास नहीं है. इसलिए, आपको इंप्रेशन और क्लिक को मैन्युअल तरीके से रिकॉर्ड करना होगा. - अगर कस्टम नेटिव विज्ञापन में कोई वीडियो ऐसेट नहीं है, तो उसमें
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();
}
कस्टम नेटिव विज्ञापन के वीडियो अनुभव को पसंद के मुताबिक बनाने के तरीके के बारे में ज़्यादा जानने के लिए, वीडियो विज्ञापन देखें.
AdChoices आइकॉन को रेंडर करना
डिजिटल सर्विसेज़ ऐक्ट (डीएसए) का पालन करने के लिए, यूरोपियन इकनॉमिक एरिया (ईईए) में रिज़र्वेशन करके दिखाए जाने वाले विज्ञापनों में AdChoices आइकॉन और Google के 'विज्ञापन के बारे में जानकारी' पेज का लिंक शामिल होना चाहिए. कस्टम नेटिव विज्ञापनों को लागू करते समय, AdChoices आइकॉन को रेंडर करने की ज़िम्मेदारी आपकी होती है. हमारा सुझाव है कि विज्ञापन की मुख्य ऐसेट को रेंडर करते समय, AdChoices आइकॉन के लिए क्लिक लिसनर को रेंडर और सेट करने के लिए ज़रूरी कदम उठाएं.
यहां दिए गए उदाहरण में यह माना गया है कि आपने अपनी व्यू हैरारकी (व्यू और व्यू ग्रुप के लेआउट का क्रम) में, AdChoices लोगो को होल्ड करने के लिए <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>
यहां दिए गए उदाहरणों में, AdChoices आइकॉन को रेंडर किया गया है और क्लिक करने पर होने वाली सही कार्रवाई को कॉन्फ़िगर किया गया है.
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();
अगर आपका ऐप्लिकेशन गलती से एक ही विज्ञापन के लिए, इस तरीके को दो बार कॉल करता है, तो SDK टूल, एक ही अनुरोध के लिए डुप्लीकेट इंप्रेशन को रिकॉर्ड होने से अपने-आप रोकता है.
क्लिक की रिपोर्ट करना
SDK टूल को यह रिपोर्ट करने के लिए कि किसी ऐसेट व्यू पर क्लिक किया गया है, विज्ञापन के performClick() तरीके को कॉल करें. उस एसेट का नाम दें जिस पर क्लिक किया गया था. इसके लिए, उसी स्ट्रिंग का इस्तेमाल करें जिसे आपने 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 टूल से तीन तरह के जवाब मिल सकते हैं. ये जवाब इस क्रम में मिलते हैं:
- अगर कोई
OnCustomClickListenerदिया गया है, तो उसे शुरू करें. - विज्ञापन के हर डीप लिंक यूआरएल के लिए, कॉन्टेंट रिज़ॉल्वर का पता लगाने की कोशिश करें और सबसे पहले रिज़ॉल्व होने वाले यूआरएल को शुरू करें.
- कोई ब्राउज़र खोलें और विज्ञापन के डेस्टिनेशन यूआरएल पर जाएं.
कस्टम क्लिक ऐक्शन लागू करने के लिए, 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 टूल, क्लिक के जवाब को कंट्रोल कर पाता है. उदाहरण के लिए, यह क्रिएटिव के लिए सेट किए गए तीसरे पक्ष के ट्रैकिंग यूआरएल को अपने-आप पिंग कर सकता है. साथ ही, बिना किसी अतिरिक्त कोड के, पर्दे के पीछे होने वाले अन्य टास्क को मैनेज कर सकता है.