नेटिव विज्ञापन

प्लैटफ़ॉर्म चुनें: Android iOS Android (लेगसी)

जब कोई नेटिव विज्ञापन लोड होता है, तो GMA Next-Gen SDK उससे जुड़े विज्ञापन फ़ॉर्मैट के लिए, लिसनर को शुरू करता है. इसके बाद, आपका ऐप्लिकेशन विज्ञापन दिखाने के लिए ज़िम्मेदार होता है. हालांकि, यह ज़रूरी नहीं है कि विज्ञापन तुरंत दिखाया जाए. सिस्टम के तय किए गए विज्ञापन फ़ॉर्मैट को आसानी से दिखाने के लिए, SDK टूल कुछ काम के संसाधन उपलब्ध कराता है. इनके बारे में यहां बताया गया है.

NativeAdView क्लास तय करें

NativeAdView क्लास तय करें. यह क्लास, ViewGroup क्लास है. साथ ही, यह NativeAdView क्लास के लिए टॉप लेवल कंटेनर है. हर नेटिव विज्ञापन व्यू में नेटिव विज्ञापन की ऐसेट होती हैं, जैसे कि MediaView व्यू एलिमेंट या Title व्यू एलिमेंट. यह NativeAdView ऑब्जेक्ट का चाइल्ड होना चाहिए.

एक्सएमएल लेआउट

अपने प्रोजेक्ट में एक्सएमएल NativeAdView जोड़ें:

<com.google.android.libraries.ads.mobile.sdk.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.libraries.ads.mobile.sdk.nativead.NativeAdView>

Jetpack Compose

  1. JetpackCompose Utilities फ़ोल्डर शामिल करें. इस फ़ोल्डर में, NativeAdView ऑब्जेक्ट और ऐसेट बनाने के लिए हेल्पर शामिल हैं.

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.
        }
    }

लोड किए गए नेटिव विज्ञापन को मैनेज करना

जब कोई नेटिव विज्ञापन लोड हो, तो कॉलबैक इवेंट को हैंडल करें, नेटिव विज्ञापन व्यू को बड़ा करें, और उसे व्यू हैरारकी (व्यू और व्यू ग्रुप के लेआउट का क्रम) में जोड़ें:

Kotlin

// Build an ad request with native ad options to customize the ad.
val adTypes = listOf(NativeAd.NativeAdType.NATIVE)
val adRequest = NativeAdRequest
  .Builder("ca-app-pub-3940256099942544/2247696110", adTypes)
  .build()

val adCallback =
  object : NativeAdLoaderCallback {
    override fun onNativeAdLoaded(nativeAd: NativeAd) {
      activity?.runOnUiThread {

        val nativeAdBinding = NativeAdBinding.inflate(layoutInflater)
        val adView = nativeAdBinding.root
        val frameLayout = myActivityLayout.nativeAdPlaceholder

        // Populate and register the native ad asset views.
        displayNativeAd(nativeAd, nativeAdBinding)

        // Remove all old ad views and add the new native ad
        // view to the view hierarchy.
        frameLayout.removeAllViews()
        frameLayout.addView(adView)
      }
    }
  }

// Load the native ad with our request and callback.
NativeAdLoader.load(adRequest, adCallback)

Java

// Build an ad request with native ad options to customize the ad.
List<NativeAd.NativeAdType> adTypes = Arrays.asList(NativeAd.NativeAdType.NATIVE);
NativeAdRequest adRequest = new NativeAdRequest
                                .Builder("ca-app-pub-3940256099942544/2247696110", adTypes)
                                .build();

NativeAdLoaderCallback adCallback = new NativeAdLoaderCallback() {
    @Override
    public void onNativeAdLoaded(NativeAd nativeAd) {
      if (getActivity() != null) {
        getActivity()
          .runOnUiThread(() -> {
            // Inflate the native ad view and add it to the view hierarchy.
            NativeAdBinding nativeAdBinding = NativeAdBinding.inflate(getLayoutInflater());
            NativeAdView adView = (NativeAdView) nativeAdBinding.getRoot();
            FrameLayout frameLayout = myActivityLayout.nativeAdPlaceholder;

            // Populate and register the native ad asset views.
            displayNativeAd(nativeAd, nativeAdBinding);

            // Remove all old ad views and add the new native ad
            // view to the view hierarchy.
            frameLayout.removeAllViews();
            frameLayout.addView(adView);
        });
      }
    }
};

// Load the native ad with our request and callback.
NativeAdLoader.load(adRequest, adCallback);

ध्यान दें कि किसी नेटिव विज्ञापन की सभी ऐसेट, NativeAdView लेआउट में रेंडर होनी चाहिए. जब नेटिव ऐसेट को नेटिव विज्ञापन व्यू लेआउट के बाहर रेंडर किया जाता है, तब GMA Next-Gen SDK चेतावनी को लॉग करने की कोशिश करता है.

विज्ञापन व्यू क्लास, हर ऐसेट के लिए इस्तेमाल किए जाने वाले व्यू को रजिस्टर करने के तरीके भी उपलब्ध कराती हैं. साथ ही, NativeAd ऑब्जेक्ट को रजिस्टर करने का तरीका भी उपलब्ध कराती हैं. व्यू को इस तरह रजिस्टर करने से, एसडीके अपने-आप टास्क मैनेज कर सकता है. जैसे:

  • क्लिक रिकॉर्ड करना
  • स्क्रीन पर पहला पिक्सल दिखने पर इंप्रेशन रिकॉर्ड करना
  • AdChoices ओवरले दिखाना

नेटिव विज्ञापन दिखाना

यहां दिए गए उदाहरण में, नेटिव विज्ञापन दिखाने का तरीका बताया गया है:

Kotlin

private fun displayNativeAd(nativeAd: NativeAd, nativeAdBinding : NativeAdBinding) {
  // Set the native ad view elements.
  val nativeAdView = nativeAdBinding.root
  nativeAdView.advertiserView = nativeAdBinding.adAdvertiser
  nativeAdView.bodyView = nativeAdBinding.adBody
  nativeAdView.callToActionView = nativeAdBinding.adCallToAction
  nativeAdView.headlineView = nativeAdBinding.adHeadline
  nativeAdView.iconView = nativeAdBinding.adAppIcon
  nativeAdView.priceView = nativeAdBinding.adPrice
  nativeAdView.starRatingView = nativeAdBinding.adStars
  nativeAdView.storeView = nativeAdBinding.adStore

  // Set the view element with the native ad assets.
  nativeAdBinding.adAdvertiser.text = nativeAd.advertiser
  nativeAdBinding.adBody.text = nativeAd.body
  nativeAdBinding.adCallToAction.text = nativeAd.callToAction
  nativeAdBinding.adHeadline.text = nativeAd.headline
  nativeAdBinding.adAppIcon.setImageDrawable(nativeAd.icon?.drawable)
  nativeAdBinding.adPrice.text = nativeAd.price
  nativeAd.starRating?.toFloat().let { value ->
    nativeAdBinding.adStars.rating = value
  }
  nativeAdBinding.adStore.text = nativeAd.store

  // Hide views for assets that don't have data.
  nativeAdBinding.adAdvertiser.visibility = getAssetViewVisibility(nativeAd.advertiser)
  nativeAdBinding.adBody.visibility = getAssetViewVisibility(nativeAd.body)
  nativeAdBinding.adCallToAction.visibility = getAssetViewVisibility(nativeAd.callToAction)
  nativeAdBinding.adHeadline.visibility = getAssetViewVisibility(nativeAd.headline)
  nativeAdBinding.adAppIcon.visibility = getAssetViewVisibility(nativeAd.icon)
  nativeAdBinding.adPrice.visibility = getAssetViewVisibility(nativeAd.price)
  nativeAdBinding.adStars.visibility = getAssetViewVisibility(nativeAd.starRating)
  nativeAdBinding.adStore.visibility = getAssetViewVisibility(nativeAd.store)

  // Inform GMA Next-Gen SDK that you have finished populating
  // the native ad views with this native ad.
  nativeAdView.registerNativeAd(nativeAd, nativeAdBinding.adMedia)
}

/**
* Determines the visibility of an asset view based on the presence of its asset.
*
* @param asset The native ad asset to check for nullability.
* @return [View.VISIBLE] if the asset is not null, [View.INVISIBLE] otherwise.
*/
private fun getAssetViewVisibility(asset: Any?): Int {
  return if (asset == null) View.INVISIBLE else View.VISIBLE
}

Java

private void displayNativeAd(ad: NativeAd, nativeAdBinding : NativeAdBinding) {
  // Set the native ad view elements.
  NativeAdView nativeAdView = nativeAdBinding.getRoot();
  nativeAdView.setAdvertiserView(nativeAdBinding.adAdvertiser);
  nativeAdView.setBodyView(nativeAdBinding.adBody);
  nativeAdView.setCallToActionView(nativeAdBinding.adCallToAction);
  nativeAdView.setHeadlineView(nativeAdBinding.adHeadline);
  nativeAdView.setIconView(nativeAdBinding.adAppIcon);
  nativeAdView.setPriceView(nativeAdBinding.adPrice);
  nativeAdView.setStarRatingView(nativeAdBinding.adStars);
  nativeAdView.setStoreView(nativeAdBinding.adStore);

  // Set the view element with the native ad assets.
  nativeAdBinding.adAdvertiser.setText(nativeAd.getAdvertiser());
  nativeAdBinding.adBody.setText(nativeAd.getBody());
  nativeAdBinding.adCallToAction.setText(nativeAd.getCallToAction());
  nativeAdBinding.adHeadline.setText(nativeAd.getHeadline());
  if (nativeAd.getIcon() != null) {
      nativeAdBinding.adAppIcon.setImageDrawable(nativeAd.getIcon().getDrawable());
  }
  nativeAdBinding.adPrice.setText(nativeAd.getPrice());
  if (nativeAd.getStarRating() != null) {
      nativeAdBinding.adStars.setRating(nativeAd.getStarRating().floatValue());
  }
  nativeAdBinding.adStore.setText(nativeAd.getStore());

  // Hide views for assets that don't have data.
  nativeAdBinding.adAdvertiser.setVisibility(getAssetViewVisibility(nativeAd.getAdvertiser()));
  nativeAdBinding.adBody.setVisibility(getAssetViewVisibility(nativeAd.getBody()));
  nativeAdBinding.adCallToAction.setVisibility(getAssetViewVisibility(nativeAd.getCallToAction()));
  nativeAdBinding.adHeadline.setVisibility(getAssetViewVisibility(nativeAd.getHeadline()));
  nativeAdBinding.adAppIcon.setVisibility(getAssetViewVisibility(nativeAd.getIcon()));
  nativeAdBinding.adPrice.setVisibility(getAssetViewVisibility(nativeAd.getPrice()));
  nativeAdBinding.adStars.setVisibility(getAssetViewVisibility(nativeAd.getStarRating()));
  nativeAdBinding.adStore.setVisibility(getAssetViewVisibility(nativeAd.getStore()));

  // Inform GMA Next-Gen SDK that you have finished populating
  // the native ad views with this native ad.
  nativeAdView.registerNativeAd(nativeAd, nativeAdBinding.adMedia);
}

/**
* Determines the visibility of an asset view based on the presence of its asset.
*
* @param asset The native ad asset to check for nullability.
* @return {@link View#VISIBLE} if the asset is not null, {@link View#INVISIBLE} otherwise.
*/
private int getAssetViewVisibility(Object asset) {
    return (asset == null) ? View.INVISIBLE : View.VISIBLE;
}

AdChoices ओवरले

SDK टूल, हर विज्ञापन व्यू में AdChoices ओवरले जोड़ता है. अपने नेटिव विज्ञापन व्यू के पसंदीदा कोने में, अपने-आप दिखने वाले AdChoices लोगो के लिए जगह छोड़ें. यह भी ज़रूरी है कि AdChoices ओवरले दिखे. इसलिए, बैकग्राउंड के लिए सही रंग और इमेज चुनें. ओवरले के दिखने और काम करने के तरीके के बारे में ज़्यादा जानने के लिए, नेटिव विज्ञापनों के फ़ील्ड की जानकारी देखें.

विज्ञापन एट्रिब्यूशन

आपको विज्ञापन एट्रिब्यूशन दिखाना होगा, ताकि यह पता चल सके कि व्यू एक विज्ञापन है. इस बारे में ज़्यादा जानने के लिए, नीति के दिशा-निर्देश पढ़ें.

हैंडल पर क्लिक

नेटिव विज्ञापन व्यू के ऊपर या अंदर मौजूद किसी भी व्यू पर, कस्टम क्लिक हैंडलर लागू न करें. विज्ञापन व्यू ऐसेट पर होने वाले क्लिक को SDK टूल तब तक मैनेज करता है, जब तक ऐसेट व्यू को सही तरीके से भरा और रजिस्टर किया जाता है.

क्लिक को सुनने के लिए, GMA Next-Gen SDK क्लिक कॉलबैक लागू करें:

Kotlin

private fun setEventCallback(nativeAd: NativeAd) {
  nativeAd.adEventCallback =
    object : NativeAdEventCallback {
      override fun onAdClicked() {
        Log.d(Constant.TAG, "Native ad recorded a click.")
      }
    }
}

Java

private void setEventCallback(NativeAd nativeAd) {
  nativeAd.setAdEventCallback(new NativeAdEventCallback() {
      @Override
      public void onAdClicked() {
        Log.d(Constant.TAG, "Native ad recorded a click.");
      }
  });
}

ImageScaleType

इमेज दिखाते समय, MediaView क्लास में ImageScaleType प्रॉपर्टी होती है. अगर आपको MediaView में किसी इमेज को स्केल करने का तरीका बदलना है, तो MediaView के setImageScaleType() तरीके का इस्तेमाल करके, उससे जुड़े ImageView.ScaleType को सेट करें:

Kotlin

nativeAdViewBinding.mediaView.imageScaleType = ImageView.ScaleType.CENTER_CROP

Java

nativeAdViewBinding.mediaView.setImageScaleType(ImageView.ScaleType.CENTER_CROP);

MediaContent

MediaContent क्लास में, नेटिव विज्ञापन के मीडिया कॉन्टेंट से जुड़ा डेटा होता है. इसे MediaView क्लास का इस्तेमाल करके दिखाया जाता है. जब MediaView mediaContent प्रॉपर्टी को MediaContent इंस्टेंस के साथ सेट किया जाता है, तब:

  • अगर कोई वीडियो ऐसेट उपलब्ध है, तो वह बफ़र होती है और MediaView में चलने लगती है. hasVideoContent() को देखकर, यह पता लगाया जा सकता है कि कोई वीडियो ऐसेट उपलब्ध है या नहीं.

  • अगर विज्ञापन में कोई वीडियो ऐसेट नहीं है, तो mainImage ऐसेट को डाउनलोड किया जाता है और उसे MediaView में रखा जाता है.

क्रिएटिव की खास जानकारी जोड़ना

बातचीत वाले और एआई चैट ऐप्लिकेशन में, GMA Next-Gen SDK विज्ञापन क्रिएटिव के लिए क्रिएटिव समरी ऐसेट (creativeSummary) उपलब्ध कराता है. अपने ऐप्लिकेशन के चैट फ़्लो में विज्ञापनों को इंटिग्रेट किया जा सकता है. इसके लिए, आपको एक शुरुआती मैसेज जोड़ना होगा.

अपने लेआउट में टेक्स्ट व्यू जोड़ना

अपने NativeAdView कंटेनर लेआउट में क्रिएटिव की खास जानकारी वाली ऐसेट के लिए, TextView जोड़ें:

<TextView
    android:id="@+id/ad_creative_summary"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="13sp" />

व्यू को पॉप्युलेट और रजिस्टर करना

TextView को creativeSummary से भरें और इसे अपने NativeAdView ऑब्जेक्ट पर creativeSummaryView प्रॉपर्टी के साथ रजिस्टर करें. व्यू को रजिस्टर करने से, क्रिएटिव की खास जानकारी वाली ऐसेट के लिए, क्लिक की अपने-आप ट्रैकिंग की सुविधा चालू हो जाती है.

Kotlin

private fun populateCreativeSummary(
  nativeAd: NativeAd,
  nativeAdBinding: NativeAdBinding,
  nativeAdView: NativeAdView,
) {
  val summary = nativeAd.creativeSummary
  if (summary == null) {
    nativeAdBinding.adCreativeSummary.visibility = View.GONE
    return
  }

  nativeAdBinding.adCreativeSummary.text = summary
  nativeAdBinding.adCreativeSummary.visibility = View.VISIBLE
  nativeAdView.creativeSummaryView = nativeAdBinding.adCreativeSummary
}

Java

private void populateCreativeSummary(
    NativeAd nativeAd, NativeAdBinding nativeAdBinding, NativeAdView nativeAdView) {
  String summary = nativeAd.getCreativeSummary();
  if (summary == null) {
    nativeAdBinding.adCreativeSummary.setVisibility(View.GONE);
    return;
  }

  nativeAdBinding.adCreativeSummary.setText(summary);
  nativeAdBinding.adCreativeSummary.setVisibility(View.VISIBLE);
  nativeAdView.setCreativeSummaryView(nativeAdBinding.adCreativeSummary);
}

क्लिक ट्रैकिंग और बदला गया टेक्स्ट

  • डायरेक्ट रेंडरिंग: SDK टूल से मिली creativeSummary स्ट्रिंग को रेंडर करते समय, उसे NativeAdView कंटेनर में मौजूद creativeSummaryView प्रॉपर्टी में रजिस्टर करें. इससे SDK टूल, क्लिक को हैंडल कर पाएगा.
  • बदला हुआ परिचय: अगर आपका ऐप्लिकेशन, क्रिएटिव की खास जानकारी में बदलाव करता है, उसे छोटा करता है या उसमें अपनी पसंद के मुताबिक टेक्स्ट जोड़ता है, तो बदले हुए टेक्स्ट को NativeAdView कंटेनर के बाहर रखें.

विज्ञापन को खत्म करना

नेटिव विज्ञापन दिखाने के बाद, उसे खत्म कर दें. यहां दिए गए उदाहरण में, नेटिव विज्ञापन को खत्म करने का तरीका बताया गया है:

Kotlin

nativeAd.destroy()

Java

nativeAd.destroy();

अगले चरण

इन विषयों के बारे में जानें:

उदाहरण

उदाहरण के तौर पर दिया गया ऐप्लिकेशन डाउनलोड करें और चलाएं. इससे GMA Next-Gen SDK के इस्तेमाल के बारे में पता चलता है.