নেটিভ উন্নত

প্ল্যাটফর্ম নির্বাচন করুন: Android iOS

যখন একটি নেটিভ বিজ্ঞাপন লোড হয়, তখন Google মোবাইল বিজ্ঞাপন SDK শ্রোতাকে সংশ্লিষ্ট বিজ্ঞাপন বিন্যাসের জন্য আহ্বান করে। আপনার অ্যাপটি তখন বিজ্ঞাপনটি প্রদর্শনের জন্য দায়ী, যদিও এটি অবিলম্বে তা করতে হবে না। সিস্টেম-সংজ্ঞায়িত বিজ্ঞাপন ফর্ম্যাটগুলিকে সহজে প্রদর্শন করতে, SDK কিছু দরকারী সংস্থান অফার করে, যেমনটি নীচে বর্ণনা করা হয়েছে।

NativeAdView ক্লাস সংজ্ঞায়িত করুন

একটি NativeAdView ক্লাস সংজ্ঞায়িত করুন। এই ক্লাসটি একটি ViewGroup ক্লাস এবং এটি একটি NativeAdView ক্লাসের জন্য শীর্ষ স্তরের ধারক৷ প্রতিটি নেটিভ বিজ্ঞাপন ভিউতে নেটিভ বিজ্ঞাপন সম্পদ থাকে, যেমন MediaView ভিউ এলিমেন্ট বা Title ভিউ এলিমেন্ট, যা অবশ্যই NativeAdView অবজেক্টের চাইল্ড হতে হবে।

XML লেআউট

আপনার প্রকল্পে একটি 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>

জেটপ্যাক রচনা

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

লোড করা নেটিভ বিজ্ঞাপনটি পরিচালনা করুন

যখন একটি নেটিভ বিজ্ঞাপন লোড হয়, তখন কলব্যাক ইভেন্ট পরিচালনা করুন, নেটিভ অ্যাড ভিউকে স্ফীত করুন এবং এটিকে ভিউ হায়ারার্কিতে যোগ করুন:

জাভা

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);
        }
});

কোটলিন

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

জেটপ্যাক রচনা

@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 মোবাইল বিজ্ঞাপন SDK একটি সতর্কতা লগ করার চেষ্টা করে যখন নেটিভ এ্যাড ভিউ লেআউটের বাইরে নেটিভ অ্যাসেট রেন্ডার করা হয়।

বিজ্ঞাপন ভিউ ক্লাসগুলি প্রতিটি পৃথক সম্পত্তির জন্য ব্যবহৃত ভিউ নিবন্ধন করার জন্য ব্যবহৃত পদ্ধতি এবং একটি NativeAd অবজেক্ট নিজেই নিবন্ধন করার জন্য ব্যবহার করা হয়। এইভাবে ভিউ নিবন্ধন করলে SDK স্বয়ংক্রিয়ভাবে কাজগুলি পরিচালনা করতে দেয় যেমন:

  • রেকর্ডিং ক্লিক
  • স্ক্রিনে প্রথম পিক্সেল দৃশ্যমান হলে ইম্প্রেশন রেকর্ড করা
  • নেটিভ ব্যাকফিল ক্রিয়েটিভের জন্য AdChoices ওভারলে প্রদর্শন করা হচ্ছে—বর্তমানে প্রকাশকদের একটি নির্বাচিত গোষ্ঠীর মধ্যে সীমাবদ্ধ

নেটিভ বিজ্ঞাপন প্রদর্শন করুন

নিম্নলিখিত উদাহরণটি দেখায় কিভাবে একটি নেটিভ বিজ্ঞাপন প্রদর্শন করতে হয়:

জাভা

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);
}

কোটলিন

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

জেটপ্যাক রচনা

@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 ওভারলে

একটি AdChoices ওভারলে SDK দ্বারা একটি বিজ্ঞাপন দৃশ্য হিসাবে যোগ করা হয় যখন একটি ব্যাকফিল বিজ্ঞাপন ফেরত দেওয়া হয়। যদি আপনার অ্যাপ নেটিভ বিজ্ঞাপন ব্যাকফিল ব্যবহার করে, তাহলে স্বয়ংক্রিয়ভাবে সন্নিবেশিত AdChoices লোগোর জন্য আপনার নেটিভ বিজ্ঞাপন ভিউয়ের পছন্দের কোণে জায়গা ছেড়ে দিন। এছাড়াও, এটি গুরুত্বপূর্ণ যে AdChoices ওভারলে দেখা যায়, তাই সঠিকভাবে ব্যাকগ্রাউন্ডের রং এবং ছবি বেছে নিন। ওভারলে এর চেহারা এবং ফাংশন সম্পর্কে আরও তথ্যের জন্য, প্রোগ্রামেটিক নেটিভ বিজ্ঞাপন বাস্তবায়ন নির্দেশিকা পড়ুন।

প্রোগ্রামেটিক নেটিভ বিজ্ঞাপনের জন্য বিজ্ঞাপন অ্যাট্রিবিউশন

প্রোগ্রামেটিক নেটিভ বিজ্ঞাপনগুলি প্রদর্শন করার সময়, আপনাকে অবশ্যই একটি বিজ্ঞাপন অ্যাট্রিবিউশন প্রদর্শন করতে হবে যাতে বোঝা যায় যে দৃশ্যটি একটি বিজ্ঞাপন। আমাদের নীতি নির্দেশিকা আরো জানুন.

ক্লিক হ্যান্ডেল

নেটিভ অ্যাড ভিউয়ের উপর বা এর মধ্যে কোনও ভিউতে কোনও কাস্টম ক্লিক হ্যান্ডলার প্রয়োগ করবেন না। বিজ্ঞাপন ভিউ অ্যাসেটে ক্লিকগুলি SDK দ্বারা পরিচালিত হয় যতক্ষণ না আপনি সঠিকভাবে সম্পদের ভিউ পূরণ এবং নিবন্ধন করেন।

ক্লিকগুলি শুনতে, Google মোবাইল বিজ্ঞাপন SDK ক্লিক কলব্যাক প্রয়োগ করুন:

জাভা

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();

কোটলিন

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()

ইমেজস্কেল টাইপ

ছবি প্রদর্শন করার সময় MediaView ক্লাসে একটি ImageScaleType বৈশিষ্ট্য রয়েছে। আপনি যদি MediaView একটি চিত্রকে কীভাবে স্কেল করা হয় তা পরিবর্তন করতে চান, MediaView এর setImageScaleType() পদ্ধতি ব্যবহার করে সংশ্লিষ্ট ImageView.ScaleType সেট করুন:

জাভা

mediaView.setImageScaleType(ImageView.ScaleType.CENTER_CROP);

কোটলিন

mediaView.imageScaleType = ImageView.ScaleType.CENTER_CROP

মিডিয়া বিষয়বস্তু

MediaContent ক্লাস নেটিভ বিজ্ঞাপনের মিডিয়া বিষয়বস্তুর সাথে সম্পর্কিত ডেটা ধারণ করে, যা MediaView ক্লাস ব্যবহার করে প্রদর্শিত হয়। যখন MediaView mediaContent সম্পত্তি একটি MediaContent উদাহরণের সাথে সেট করা হয়:

  • যদি একটি ভিডিও সম্পদ উপলব্ধ থাকে, এটি বাফার করা হয় এবং MediaView ভিতরে বাজানো শুরু করে। আপনি hasVideoContent() চেক করে একটি ভিডিও সম্পদ উপলব্ধ কিনা তা বলতে পারেন৷

  • যদি বিজ্ঞাপনটিতে একটি ভিডিও সম্পদ না থাকে, তাহলে mainImage সম্পদ ডাউনলোড করা হয় এবং পরিবর্তে MediaView ভিতরে রাখা হয়।

∂## একটি বিজ্ঞাপন ধ্বংস করুন

আপনি একটি নেটিভ বিজ্ঞাপন দেখানোর পরে, বিজ্ঞাপনটি ধ্বংস করুন। নিম্নলিখিত উদাহরণ একটি নেটিভ বিজ্ঞাপন ধ্বংস করে:

জাভা

nativeAd.destroy();

কোটলিন

nativeAd.destroy()

নেটিভ বিজ্ঞাপন কোড পরীক্ষা করুন

সরাসরি বিক্রি বিজ্ঞাপন

আপনি যদি সরাসরি-বিক্রীত নেটিভ বিজ্ঞাপনগুলি কেমন তা পরীক্ষা করতে চান, আপনি এই অ্যাড ম্যানেজার বিজ্ঞাপন ইউনিট আইডি ব্যবহার করতে পারেন:

/21775744923/example/native

এটি নমুনা অ্যাপ ইনস্টল এবং বিষয়বস্তু বিজ্ঞাপনের পাশাপাশি নিম্নলিখিত সম্পদগুলির সাথে একটি কাস্টম নেটিভ বিজ্ঞাপন বিন্যাস পরিবেশনের জন্য কনফিগার করা হয়েছে:

  • শিরোনাম (পাঠ্য)
  • মূল ছবি (ছবি)
  • ক্যাপশন (পাঠ্য)

কাস্টম নেটিভ বিজ্ঞাপন ফরম্যাটের টেমপ্লেট আইডি হল 10063170

নেটিভ ব্যাকফিল বিজ্ঞাপন

Ad Exchange ব্যাকফিল প্রকাশকদের একটি নির্বাচিত গোষ্ঠীর মধ্যে সীমাবদ্ধ৷ নেটিভ ব্যাকফিল বিজ্ঞাপনের আচরণ পরীক্ষা করতে, এই অ্যাড ম্যানেজার বিজ্ঞাপন ইউনিট ব্যবহার করুন:

/21775744923/example/native-backfill

এটি নমুনা অ্যাপ ইনস্টল এবং বিষয়বস্তু বিজ্ঞাপন পরিবেশন করে যা AdChoices ওভারলে অন্তর্ভুক্ত করে।

লাইভ হওয়ার আগে আপনার আসল বিজ্ঞাপন ইউনিট এবং টেমপ্লেট আইডি উল্লেখ করতে আপনার কোড আপডেট করতে ভুলবেন না।

গিটহাবের উদাহরণ

নেটিভ বিজ্ঞাপনের সম্পূর্ণ প্রয়োগের উদাহরণ:

জাভা কোটলিন জেটপ্যাক কম্পোজ

পরবর্তী পদক্ষেপ

নিম্নলিখিত বিষয়গুলি অন্বেষণ করুন: