जब कोई नेटिव विज्ञापन लोड होता है, तब Google Mobile Ads SDK, विज्ञापन के उस फ़ॉर्मैट के लिए लिसनर को शुरू करता है. इसके बाद, विज्ञापन दिखाने की ज़िम्मेदारी आपके ऐप्लिकेशन की होती है. हालांकि, यह ज़रूरी नहीं है कि वह विज्ञापन तुरंत दिखाए. सिस्टम की ओर से तय किए गए विज्ञापन फ़ॉर्मैट को आसानी से दिखाने के लिए, SDK टूल कुछ काम के संसाधन उपलब्ध कराता है. इनके बारे में यहां बताया गया है.
NativeAdView
क्लास को परिभाषित करना
NativeAdView
क्लास को परिभाषित करें. यह क्लास, एक ViewGroup
क्लास है. साथ ही, यह NativeAdView
क्लास के लिए टॉप लेवल का कंटेनर है. हर नेटिव विज्ञापन व्यू में नेटिव विज्ञापन ऐसेट होती हैं. जैसे, MediaView
व्यू एलिमेंट या Title
व्यू एलिमेंट. यह NativeAdView
ऑब्जेक्ट का चाइल्ड होना चाहिए.
एक्सएमएल लेआउट
अपने प्रोजेक्ट में कोई XML NativeAdView
जोड़ें:
<com.google.android.gms.ads.nativead.NativeAdView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical">
<LinearLayout
android:orientation="horizontal">
<ImageView
android:id="@+id/ad_app_icon" />
<TextView
android:id="@+id/ad_headline" />
</LinearLayout>
<!--Add remaining assets such as the image and media view.-->
</LinearLayout>
</com.google.android.gms.ads.nativead.NativeAdView>
Jetpack Compose
JetpackComposeDemo/compose-util मॉड्यूल शामिल करें. इसमें NativeAdView
और उसकी ऐसेट को कंपोज़ करने के लिए हेल्पर शामिल हैं.
compose-util
मॉड्यूल का इस्तेमाल करके, NativeAdView
लिखें:
import com.google.android.gms.compose_util.NativeAdAttribution
import com.google.android.gms.compose_util.NativeAdView
@Composable
/** Display a native ad with a user defined template. */
fun DisplayNativeAdView(nativeAd: NativeAd) {
NativeAdView {
// Display the ad attribution.
NativeAdAttribution(text = context.getString("Ad"))
// Add remaining assets such as the image and media view.
}
}
लोड किए गए नेटिव विज्ञापन को मैनेज करना
नेटिव विज्ञापन लोड होने पर, कॉलबैक इवेंट को मैनेज करें, नेटिव विज्ञापन व्यू को बड़ा करें, और उसे व्यू हैरारकी में जोड़ें:
Java
AdLoader.Builder builder = new AdLoader.Builder(this, "/21775744923/example/native")
.forNativeAd(new NativeAd.OnNativeAdLoadedListener() {
@Override
public void onNativeAdLoaded(NativeAd nativeAd) {
// Assumes you have a placeholder FrameLayout in your View layout
// (with ID fl_adplaceholder) where the ad is to be placed.
FrameLayout frameLayout =
findViewById(R.id.fl_adplaceholder);
// Assumes that your ad layout is in a file call native_ad_layout.xml
// in the res/layout folder
NativeAdView adView = (NativeAdView) getLayoutInflater()
.inflate(R.layout.native_ad_layout, null);
// This method sets the assets into the ad view.
displayNativeAd(nativeAd, adView);
frameLayout.removeAllViews();
frameLayout.addView(adView);
}
});
Kotlin
val builder = AdLoader.Builder(this, "/21775744923/example/native")
.forNativeAd { nativeAd ->
// Assumes you have a placeholder FrameLayout in your View layout
// (with ID fl_adplaceholder) where the ad is to be placed.
val frameLayout: FrameLayout = findViewById(R.id.fl_adplaceholder)
// Assumes that your ad layout is in a file call native_ad_layout.xml
// in the res/layout folder
val adView = layoutInflater
.inflate(R.layout.native_ad_layout, null) as NativeAdView
// This method sets the assets into the ad view.
displayNativeAd(nativeAd, adView)
frameLayout.removeAllViews()
frameLayout.addView(adView)
}
Jetpack Compose
@Composable
/** Load and display a native ad. */
fun NativeScreen() {
var nativeAd by remember { mutableStateOf<NativeAd?>(null) }
val context = LocalContext.current
var isDisposed by remember { mutableStateOf(false) }
DisposableEffect(Unit) {
// Load the native ad when we launch this screen
loadNativeAd(
context = context,
onAdLoaded = { ad ->
// Handle the native ad being loaded.
if (!isDisposed) {
nativeAd = ad
} else {
// Destroy the native ad if loaded after the screen is disposed.
ad.destroy()
}
},
)
// Destroy the native ad to prevent memory leaks when we dispose of this screen.
onDispose {
isDisposed = true
nativeAd?.destroy()
nativeAd = null
}
}
// Display the native ad view with a user defined template.
nativeAd?.let { adValue -> DisplayNativeAdView(adValue) }
}
fun loadNativeAd(context: Context, onAdLoaded: (NativeAd) -> Unit) {
val adLoader =
AdLoader.Builder(context, NATIVE_AD_UNIT_ID)
.forNativeAd { nativeAd -> onAdLoaded(nativeAd) }
.withAdListener(
object : AdListener() {
override fun onAdFailedToLoad(error: LoadAdError) {
Log.e(TAG, "Native ad failed to load: ${error.message}")
}
override fun onAdLoaded() {
Log.d(TAG, "Native ad was loaded.")
}
override fun onAdImpression() {
Log.d(TAG, "Native ad recorded an impression.")
}
override fun onAdClicked() {
Log.d(TAG, "Native ad was clicked.")
}
}
)
.build()
adLoader.loadAd(AdRequest.Builder().build())
}
ध्यान दें कि किसी नेटिव विज्ञापन की सभी ऐसेट, NativeAdView
लेआउट में रेंडर की जानी चाहिए. Google Mobile Ads SDK, नेटिव ऐसेट को नेटिव विज्ञापन व्यू लेआउट से बाहर रेंडर किए जाने पर चेतावनी लॉग करने की कोशिश करता है.
विज्ञापन व्यू क्लास, हर ऐसेट के लिए इस्तेमाल किए गए व्यू को रजिस्टर करने के तरीके भी उपलब्ध कराती हैं. साथ ही, NativeAd
ऑब्जेक्ट को रजिस्टर करने का तरीका भी उपलब्ध कराती हैं.
इस तरह से व्यू रजिस्टर करने पर, SDK टूल इन जैसे टास्क अपने-आप मैनेज कर सकता है:
- रिकॉर्डिंग पर किए गए क्लिक
- स्क्रीन पर पहला पिक्सल दिखने पर इंप्रेशन रिकॉर्ड करना
- नेटिव बैकफ़िल क्रिएटिव के लिए AdChoices ओवरले दिखाना—फ़िलहाल, यह सुविधा चुनिंदा पब्लिशर के लिए उपलब्ध है
नेटिव विज्ञापन दिखाना
यहां दिए गए उदाहरण में, नेटिव विज्ञापन दिखाने का तरीका बताया गया है:
Java
private void displayNativeAd(ViewGroup parent, NativeAd ad) {
// Inflate a layout and add it to the parent ViewGroup.
LayoutInflater inflater = (LayoutInflater) parent.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
NativeAdView adView = (NativeAdView) inflater
.inflate(R.layout.ad_layout_file, parent);
// Locate the view that will hold the headline, set its text, and call the
// NativeAdView's setHeadlineView method to register it.
TextView headlineView = adView.findViewById<TextView>(R.id.ad_headline);
headlineView.setText(ad.getHeadline());
adView.setHeadlineView(headlineView);
// Repeat the process for the other assets in the NativeAd
// using additional view objects (Buttons, ImageViews, etc).
// If you use a MediaView, call theNativeAdView.setMediaView() method
// before calling the NativeAdView.setNativeAd() method.
MediaView mediaView = (MediaView) adView.findViewById(R.id.ad_media);
adView.setMediaView(mediaView);
// Register the native ad with its ad view.
adView.setNativeAd(ad);
// Ensure that the parent view doesn't already contain an ad view.
parent.removeAllViews();
// Place the AdView into the parent.
parent.addView(adView);
}
Kotlin
fun displayNativeAd(parent: ViewGroup, ad: NativeAd) {
// Inflate a layout and add it to the parent ViewGroup.
val inflater = parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)
as LayoutInflater
val adView = inflater.inflate(R.layout.ad_layout_file, parent) as NativeAdView
// Locate the view that will hold the headline, set its text, and use the
// NativeAdView's headlineView property to register it.
val headlineView = adView.findViewById<TextView>(R.id.ad_headline)
headlineView.text = ad.headline
adView.headlineView = headlineView
// Repeat the process for the other assets in the NativeAd using
// additional view objects (Buttons, ImageViews, etc).
val mediaView = adView.findViewById<MediaView>(R.id.ad_media)
adView.mediaView = mediaView
// Call the NativeAdView's setNativeAd method to register the
// NativeAdObject.
adView.setNativeAd(ad)
// Ensure that the parent view doesn't already contain an ad view.
parent.removeAllViews()
// Place the AdView into the parent.
parent.addView(adView)
}
Jetpack Compose
@Composable
/** Display a native ad with a user defined template. */
fun DisplayNativeAdView(nativeAd: NativeAd) {
val context = LocalContext.current
Box(modifier = Modifier.padding(8.dp).wrapContentHeight(Alignment.Top)) {
// Call the NativeAdView composable to display the native ad.
NativeAdView {
// Inside the NativeAdView composable, display the native ad assets.
Column(Modifier.align(Alignment.TopStart).wrapContentHeight(Alignment.Top)) {
// Display the ad attribution.
NativeAdAttribution(text = context.getString(R.string.attribution))
Row {
// If available, display the icon asset.
nativeAd.icon?.let { icon ->
NativeAdIconView(Modifier.padding(5.dp)) {
icon.drawable?.toBitmap()?.let { bitmap ->
Image(bitmap = bitmap.asImageBitmap(), "Icon")
}
}
}
Column {
// If available, display the headline asset.
nativeAd.headline?.let {
NativeAdHeadlineView {
Text(text = it, style = MaterialTheme.typography.headlineLarge)
}
}
// If available, display the star rating asset.
nativeAd.starRating?.let {
NativeAdStarRatingView {
Text(text = "Rated $it", style = MaterialTheme.typography.labelMedium)
}
}
}
}
// If available, display the body asset.
nativeAd.body?.let { NativeAdBodyView { Text(text = it) } }
// Display the media asset.
NativeAdMediaView(Modifier.fillMaxWidth().height(500.dp).fillMaxHeight())
Row(Modifier.align(Alignment.End).padding(5.dp)) {
// If available, display the price asset.
nativeAd.price?.let {
NativeAdPriceView(Modifier.padding(5.dp).align(Alignment.CenterVertically)) {
Text(text = it)
}
}
// If available, display the store asset.
nativeAd.store?.let {
NativeAdStoreView(Modifier.padding(5.dp).align(Alignment.CenterVertically)) {
Text(text = it)
}
}
// If available, display the call to action asset.
// Note: The Jetpack Compose button implements a click handler which overrides the native
// ad click handler, causing issues. Use the NativeAdButton which does not implement a
// click handler. To handle native ad clicks, use the NativeAd AdListener onAdClicked
// callback.
nativeAd.callToAction?.let { callToAction ->
NativeAdCallToActionView(Modifier.padding(5.dp)) { NativeAdButton(text = callToAction) }
}
}
}
}
}
}
AdChoices ओवरले
जब बैकफ़िल विज्ञापन दिखाया जाता है, तब SDK टूल, AdChoices ओवरले को विज्ञापन व्यू के तौर पर जोड़ता है. अगर आपका ऐप्लिकेशन, नेटिव विज्ञापन बैकफ़िल का इस्तेमाल करता है, तो अपने नेटिव विज्ञापन व्यू के पसंदीदा कोने में, AdChoices लोगो के अपने-आप जुड़ने के लिए जगह छोड़ें. यह भी ज़रूरी है कि AdChoices ओवरले दिखे. इसलिए, बैकग्राउंड के लिए सही रंग और इमेज चुनें. ओवरले के दिखने और काम करने के तरीके के बारे में ज़्यादा जानकारी के लिए, प्रोग्राम के हिसाब से नेटिव विज्ञापन लागू करने के दिशा-निर्देश देखें.
प्रोग्रामैटिक नेटिव विज्ञापनों के लिए विज्ञापन एट्रिब्यूशन
प्रोग्राम के हिसाब से दिखाए जाने वाले नेटिव विज्ञापनों को दिखाते समय, आपको विज्ञापन एट्रिब्यूशन दिखाना होगा. इससे यह पता चलेगा कि व्यू एक विज्ञापन है. इस बारे में ज़्यादा जानने के लिए, नीति के दिशा-निर्देश पढ़ें.
क्लिक मैनेज करना
नेटिव विज्ञापन व्यू के ऊपर या उसके अंदर मौजूद किसी भी व्यू पर, कस्टम क्लिक हैंडलर लागू न करें. विज्ञापन व्यू ऐसेट पर होने वाले क्लिक को SDK टूल मैनेज करता है. इसके लिए, आपको ऐसेट व्यू को सही तरीके से पॉप्युलेट और रजिस्टर करना होगा.
क्लिक के बारे में सुनने के लिए, Google Mobile Ads SDK के क्लिक कॉलबैक को लागू करें:
Java
AdLoader adLoader = new AdLoader.Builder(context, "/21775744923/example/native")
// ...
.withAdListener(new AdListener() {
@Override
public void onAdFailedToLoad(LoadAdError adError) {
// Handle the failure by logging.
}
@Override
public void onAdClicked() {
// Log the click event or other custom behavior.
}
})
.build();
Kotlin
val adLoader = AdLoader.Builder(this, "/21775744923/example/native")
// ...
.withAdListener(object : AdListener() {
override fun onAdFailedToLoad(adError: LoadAdError) {
// Handle the failure.
}
override fun onAdClicked() {
// Log the click event or other custom behavior.
}
})
.build()
ImageScaleType
इमेज दिखाते समय, MediaView
क्लास में ImageScaleType
प्रॉपर्टी होती है. अगर आपको MediaView
में इमेज को स्केल करने का तरीका बदलना है, तो MediaView
के setImageScaleType()
तरीके का इस्तेमाल करके, उससे जुड़ा ImageView.ScaleType
सेट करें:
Java
mediaView.setImageScaleType(ImageView.ScaleType.CENTER_CROP);
Kotlin
mediaView.imageScaleType = ImageView.ScaleType.CENTER_CROP
MediaContent
MediaContent
क्लास में, नेटिव विज्ञापन के मीडिया कॉन्टेंट से जुड़ा डेटा होता है. इसे MediaView
क्लास का इस्तेमाल करके दिखाया जाता है. जब MediaContent
इंस्टेंस के साथ MediaView
mediaContent
प्रॉपर्टी सेट की जाती है, तब:
अगर कोई वीडियो ऐसेट उपलब्ध है, तो उसे बफ़र किया जाता है और वह
MediaView
में चलने लगती है.hasVideoContent()
देखकर पता लगाया जा सकता है कि कोई वीडियो ऐसेट उपलब्ध है या नहीं.अगर विज्ञापन में वीडियो ऐसेट नहीं है, तो
mainImage
ऐसेट डाउनलोड हो जाती है और उसेMediaView
में रख दिया जाता है.
∂## विज्ञापन को मिटाना
नेटिव विज्ञापन दिखाने के बाद, उसे डिस्ट्रॉय करें. यहां दिए गए उदाहरण में, नेटिव विज्ञापन को हटाने का तरीका बताया गया है:
Java
nativeAd.destroy();
Kotlin
nativeAd.destroy()
नेटिव विज्ञापन कोड की जांच करना
सीधे तौर पर बेचे जाने वाले विज्ञापन
अगर आपको यह आज़माना है कि सीधे तौर पर बेचे गए नेटिव विज्ञापन कैसे दिखते हैं, तो इस Ad Manager विज्ञापन यूनिट आईडी का इस्तेमाल करें:
/21775744923/example/native
इसे ऐप्लिकेशन इंस्टॉल करने को बढ़ावा देने वाले विज्ञापन और कॉन्टेंट विज्ञापन के सैंपल दिखाने के लिए कॉन्फ़िगर किया गया है. साथ ही, इसे कस्टम नेटिव विज्ञापन फ़ॉर्मैट के तौर पर भी कॉन्फ़िगर किया गया है. इसमें ये ऐसेट शामिल हैं:
- हेडलाइन (टेक्स्ट)
- MainImage (image)
- कैप्शन (टेक्स्ट)
कस्टम नेटिव विज्ञापन फ़ॉर्मैट के लिए, टेंप्लेट आईडी 10063170
है.
नेटिव बैकफ़िल विज्ञापन
Ad Exchange बैकफ़िल की सुविधा, चुनिंदा पब्लिशर के लिए उपलब्ध है. नेटिव बैकफ़िल विज्ञापनों के व्यवहार की जांच करने के लिए, इस Ad Manager विज्ञापन यूनिट का इस्तेमाल करें:
/21775744923/example/native-backfill
यह ऐप्लिकेशन इंस्टॉल करने और कॉन्टेंट दिखाने वाले विज्ञापनों के सैंपल दिखाता है. इनमें AdChoices ओवरले शामिल होता है.
लाइव होने से पहले, अपने कोड को अपडेट करना न भूलें, ताकि वह आपकी विज्ञापन यूनिट और टेंप्लेट आईडी को रेफ़र कर सके.
GitHub पर मौजूद उदाहरण
नेटिव विज्ञापनों को लागू करने का पूरा उदाहरण:
अगले चरण
इन विषयों के बारे में जानें: