بومی پیشرفته

پلتفرم مورد نظر را انتخاب کنید: اندروید، اندروید (بتا)، iOS

وقتی یک تبلیغ بومی بارگذاری می‌شود، 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 رندر شوند. 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(nativeAd) {
      // 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 هنگام بازگشت یک تبلیغ backfill اضافه می‌شود. اگر برنامه شما از backfill تبلیغات بومی استفاده می‌کند، در گوشه دلخواه خود از نمای تبلیغ بومی، فضایی را برای لوگوی AdChoices که به طور خودکار درج می‌شود، در نظر بگیرید. همچنین، مهم است که پوشش AdChoices دیده شود، بنابراین رنگ‌ها و تصاویر پس‌زمینه را به طور مناسب انتخاب کنید. برای اطلاعات بیشتر در مورد ظاهر و عملکرد پوشش، به دستورالعمل‌های پیاده‌سازی تبلیغات بومی برنامه‌ریزی‌شده مراجعه کنید.

انتساب آگهی برای تبلیغات بومی برنامه‌ریزی‌شده

هنگام نمایش تبلیغات بومی برنامه‌ریزی‌شده، باید یک انتساب تبلیغ نمایش دهید تا مشخص شود که این نمایش یک تبلیغ است. برای اطلاعات بیشتر به دستورالعمل‌های خط‌مشی ما مراجعه کنید.

مدیریت کلیک‌ها

هیچ کنترل‌کننده کلیک سفارشی را روی هیچ نمایی، چه در داخل و چه در نمای تبلیغات بومی، پیاده‌سازی نکنید. کلیک‌ها روی دارایی‌های نمای تبلیغات توسط SDK مدیریت می‌شوند، مادامی که نماهای دارایی را به درستی پر و ثبت کنید.

برای گوش دادن به کلیک‌ها، فراخوانی کلیک Google Mobile Ads 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 تغییر دهید، ImageView.ScaleType مربوطه را با استفاده از متد setImageScaleType() از MediaView تنظیم کنید:

جاوا

mediaView.setImageScaleType(ImageView.ScaleType.CENTER_CROP);

کاتلین

mediaView.imageScaleType = ImageView.ScaleType.CENTER_CROP

محتوای رسانه‌ای

کلاس MediaContent داده‌های مربوط به محتوای رسانه‌ای تبلیغ بومی را که با استفاده از کلاس MediaView نمایش داده می‌شود، در خود نگه می‌دارد. وقتی ویژگی mediaContent MediaView با یک نمونه MediaContent تنظیم می‌شود:

  • اگر یک فایل ویدیویی موجود باشد، بافر شده و درون MediaView شروع به پخش می‌کند. می‌توانید با بررسی hasVideoContent() متوجه شوید که آیا یک فایل ویدیویی موجود است یا خیر.

  • اگر تبلیغ حاوی فایل ویدیویی نباشد، فایل mainImage دانلود شده و در داخل MediaView قرار می‌گیرد.

از بین بردن یک تبلیغ

بعد از اینکه یک تبلیغ بومی نمایش دادید، آن را از بین ببرید. مثال زیر یک تبلیغ بومی را از بین می‌برد:

جاوا

nativeAd.destroy();

کاتلین

nativeAd.destroy()

کد تبلیغات بومی را آزمایش کنید

تبلیغات فروش مستقیم

اگر می‌خواهید آزمایش کنید که تبلیغات بومی فروش مستقیم چگونه هستند، می‌توانید از این شناسه واحد تبلیغاتی Ad Manager استفاده کنید:

/21775744923/example/native

این افزونه طوری پیکربندی شده است که تبلیغات نمونه نصب اپلیکیشن و تبلیغات محتوایی و همچنین یک قالب تبلیغاتی بومی سفارشی با ویژگی‌های زیر را ارائه دهد:

  • تیتر (متن)
  • تصویر اصلی (تصویر)
  • عنوان (متن)

شناسه قالب برای قالب تبلیغات بومی سفارشی 10063170 است.

تبلیغات محلی برای پر کردن مجدد

قابلیت پر کردن مجدد تبلیغات Ad Exchange محدود به گروه منتخبی از ناشران است. برای آزمایش رفتار تبلیغات پر کردن مجدد بومی، از این واحد تبلیغاتی Ad Manager استفاده کنید:

/21775744923/example/native-backfill

این سرویس، نمونه‌هایی از تبلیغات نصب اپلیکیشن و محتوا را ارائه می‌دهد که شامل پوشش AdChoices هستند.

به یاد داشته باشید که قبل از انتشار، کد خود را به‌روزرسانی کنید تا به شناسه‌های واحد تبلیغاتی و قالب واقعی خود ارجاع دهید.

مثال‌ها در گیت‌هاب

پیاده‌سازی کامل مثال تبلیغات بومی:

جاوا کاتلین JetpackCompose

مراحل بعدی

مباحث زیر را بررسی کنید: