מודעות מותאמות מסוג Advanced

הצגת פורמט של מודעות מותאמות שהוגדר על ידי המערכת

כשמודעה מותאמת נטענת, האפליקציה מקבלת אובייקט של מודעה מותאמת דרך GADAdLoaderDelegate הודעות פרוטוקול. לאחר מכן האפליקציה אחראית להציג את המודעה (אבל לא בהכרח לעשות זאת מיד). כדי להקל על הצגת פורמטים של מודעות שהוגדרו על ידי המערכת, ערכת ה-SDK כוללת מספר המשאבים.

GADNativeAdView

בGADNativeAd יש 'צפייה במודעה' תואמת class: GADNativeAdView. הסיווג הזה של צפייה במודעה הוא UIView שבו בעלי אתרים צריכים להשתמש כדי להציג את המודעה. למשל, GADNativeAdView יחיד יכול להציג מופע יחיד של GADNativeAd. כל אחד מ-UIView האובייקטים ששימש להצגת המודעה הנכסים צריכים להיות תצוגות משנה של אובייקט GADNativeAdView הזה.

לדוגמה, אם הוצגה מודעה בUITableView, היררכיית התצוגה של אחד מהתאים עשויה להיראות כך:

הכיתה GADNativeAdView מספקת גם IBOutlets שמשמש להרשמה את התצוגה המפורטת שבה נעשה שימוש לכל נכס בנפרד, ושיטה לרישום GADNativeAd לאובייקט עצמו. רישום התצוגות בדרך הזו מאפשר ל-SDK: לטפל במשימות כמו:

  • מתבצע תיעוד של קליקים.
  • תיעוד חשיפות (כשהפיקסל הראשון גלוי במסך).
  • מוצגת שכבת-העל AdChoices.

שכבת-על AdChoices

למודעות מותאמות עקיפות (מוצגות דרך Ad Manager מילוי חוסרים (backfill) או דרך Ad Exchange או AdSense), שכבת-על של AdChoices מתווספת על ידי ערכת ה-SDK. להשאיר את המרחב במרחב המשותף הרצוי פינה בתצוגת המודעה המותאמת עבור הלוגו של AdChoices שאנחנו מוסיפים באופן אוטומטי. כמו כן, יוצרים לוודא ששכבת-העל AdChoices ממוקמת בתוכן שמאפשר להציג את הסמל קל לראות אותם. למידע נוסף על המראה והתפקוד של שכבת-העל, אפשר לעיין הנחיות להטמעה של מודעות מותאמות פרוגרמטיות.

שיוך מודעה למודעות מותאמות פרוגרמטיות

כשמציגים מודעות מותאמות פרוגרמטיות, צריך להציג את השיוך של המודעה לציין שהצפייה היא פרסומת. הנחיות המדיניות זמינות בדף הזה.

קוד לדוגמה

בואו נראה איך להציג מודעות מותאמות באמצעות צפיות שנטענות באופן דינמי מקובצי xib. זו גישה שימושית מאוד כשמשתמשים בGADAdLoaders מוגדר כך שיבקש מספר פורמטים.

פריסה של ה-UIViews

השלב הראשון הוא פריסה של UIViews שיציגו נכסים שמצורפים למודעות מותאמות. אפשר לעשות זאת ב-API של הכלי ליצירת ממשקים, כמו שמבצעים בכל יצירה של ממשק אחר קובץ xib. כך הפריסה של מודעת נייטיב המודעה יכולה להיראות:

שימו לב לערך 'סיווג מותאם אישית' בפינה השמאלית העליונה של התמונה. ההגדרה היא

GADNativeAdView זהו הסיווג של הצפייה במודעה שמשמש להצגת GADNativeAd.

צריך גם להגדיר את המחלקה המותאמת אישית עבור GADMediaView, שנעשה בו שימוש כדי להציג את הסרטון או התמונה עבור המודעה.

לאחר הצגת הצפיות והקציתם את הסיווג המתאים של צפיות במודעות הפריסה, צריך לקשר את שקעי הנכסים בתצוגת המודעה אל UIViews שיצרת. כך מקשרים את שקעי הנכסים הדיגיטליים בתצוגת המודעה אל UIViews שנוצרו למודעה:

בחלונית השקעים, השקעים ב-GADNativeAdView קושרו אל את UIViews שהוטמעה בבונה הממשקים. כך אפשר ערכת ה-SDK יכולה לדעת איזה נכס מוצג ב-UIView. חשוב גם לזכור שערוצי הפרסום האלה מייצגים את הדעות שניתן ללחוץ עליהן במודעה.

מציגים את המודעה

אחרי השלמת הפריסה וחיבור השקעים, צריך להוסיף את הקוד הבא לאפליקציה שמציגה מודעה לאחר שהיא נטענת:

Swift

// Mark: - GADNativeAdLoaderDelegate
func adLoader(_ adLoader: GADAdLoader, didReceive nativeAd: GADNativeAd) {
  print("Received native ad: \(nativeAd)")
  refreshAdButton.isEnabled = true
  // Create and place ad in view hierarchy.
  let nibView = Bundle.main.loadNibNamed("NativeAdView", owner: nil, options: nil)?.first
  guard let nativeAdView = nibView as? GADNativeAdView else {
    return
  }
  setAdView(nativeAdView)

  // Set ourselves as the native ad delegate to be notified of native ad events.
  nativeAd.delegate = self

  // Populate the native ad view with the native ad assets.
  // The headline and mediaContent are guaranteed to be present in every native ad.
  (nativeAdView.headlineView as? UILabel)?.text = nativeAd.headline
  nativeAdView.mediaView?.mediaContent = nativeAd.mediaContent

  // This app uses a fixed width for the GADMediaView and changes its height to match the aspect
  // ratio of the media it displays.
  if let mediaView = nativeAdView.mediaView, nativeAd.mediaContent.aspectRatio > 0 {
    let heightConstraint = NSLayoutConstraint(
      item: mediaView,
      attribute: .height,
      relatedBy: .equal,
      toItem: mediaView,
      attribute: .width,
      multiplier: CGFloat(1 / nativeAd.mediaContent.aspectRatio),
      constant: 0)
    heightConstraint.isActive = true
  }

  // These assets are not guaranteed to be present. Check that they are before
  // showing or hiding them.
  (nativeAdView.bodyView as? UILabel)?.text = nativeAd.body
  nativeAdView.bodyView?.isHidden = nativeAd.body == nil

  (nativeAdView.callToActionView as? UIButton)?.setTitle(nativeAd.callToAction, for: .normal)
  nativeAdView.callToActionView?.isHidden = nativeAd.callToAction == nil

  (nativeAdView.iconView as? UIImageView)?.image = nativeAd.icon?.image
  nativeAdView.iconView?.isHidden = nativeAd.icon == nil

  (nativeAdView.starRatingView as? UIImageView)?.image = imageOfStars(
    fromStarRating: nativeAd.starRating)
  nativeAdView.starRatingView?.isHidden = nativeAd.starRating == nil

  (nativeAdView.storeView as? UILabel)?.text = nativeAd.store
  nativeAdView.storeView?.isHidden = nativeAd.store == nil

  (nativeAdView.priceView as? UILabel)?.text = nativeAd.price
  nativeAdView.priceView?.isHidden = nativeAd.price == nil

  (nativeAdView.advertiserView as? UILabel)?.text = nativeAd.advertiser
  nativeAdView.advertiserView?.isHidden = nativeAd.advertiser == nil

  // For the SDK to process touch events properly, user interaction should be disabled.
  nativeAdView.callToActionView?.isUserInteractionEnabled = false

  // Associate the native ad view with the native ad object. This is
  // required to make the ad clickable.
  // Note: this should always be done after populating the ad views.
  nativeAdView.nativeAd = nativeAd
}

SwiftUI

יצירת מודל תצוגה

יצירת מודל תצוגה שטוען מודעה מותאמת ומפרסמים את נתוני המודעות המותאמות שינויים:

import GoogleMobileAds

class NativeAdViewModel: NSObject, ObservableObject, GADNativeAdLoaderDelegate {
  @Published var nativeAd: GADNativeAd?
  private var adLoader: GADAdLoader!

  func refreshAd() {
    adLoader = GADAdLoader(
      adUnitID: "ca-app-pub-3940256099942544/3986624511",
      // The UIViewController parameter is optional.
      rootViewController: nil,
      adTypes: [.native], options: nil)
    adLoader.delegate = self
    adLoader.load(GADRequest())
  }

  func adLoader(_ adLoader: GADAdLoader, didReceive nativeAd: GADNativeAd) {
    // Native ad data changes are published to its subscribers.
    self.nativeAd = nativeAd
    nativeAd.delegate = self
  }

  func adLoader(_ adLoader: GADAdLoader, didFailToReceiveAdWithError error: Error) {
    print("\(adLoader) failed with error: \(error.localizedDescription)")
  }
}

יצירת UIViewRepresentable

לכתוב UIViewRepresentable למשך GADNativeView, ולהירשם לשינויים בנתוניםViewModel class:

private struct NativeAdView: UIViewRepresentable {
  typealias UIViewType = GADNativeAdView

  // Observer to update the UIView when the native ad value changes.
  @ObservedObject var nativeViewModel: NativeAdViewModel

  func makeUIView(context: Context) -> GADNativeAdView {
    return
      Bundle.main.loadNibNamed(
        "NativeAdView",
        owner: nil,
        options: nil)?.first as! GADNativeAdView
  }

  func updateUIView(_ nativeAdView: GADNativeAdView, context: Context) {
    guard let nativeAd = nativeViewModel.nativeAd else { return }

    // Each UI property is configurable using your native ad.
    (nativeAdView.headlineView as? UILabel)?.text = nativeAd.headline

    nativeAdView.mediaView?.mediaContent = nativeAd.mediaContent

    (nativeAdView.bodyView as? UILabel)?.text = nativeAd.body

    (nativeAdView.iconView as? UIImageView)?.image = nativeAd.icon?.image

    (nativeAdView.starRatingView as? UIImageView)?.image = imageOfStars(from: nativeAd.starRating)

    (nativeAdView.storeView as? UILabel)?.text = nativeAd.store

    (nativeAdView.priceView as? UILabel)?.text = nativeAd.price

    (nativeAdView.advertiserView as? UILabel)?.text = nativeAd.advertiser

    (nativeAdView.callToActionView as? UIButton)?.setTitle(nativeAd.callToAction, for: .normal)

    // For the SDK to process touch events properly, user interaction should be disabled.
    nativeAdView.callToActionView?.isUserInteractionEnabled = false

    // Associate the native ad view with the native ad object. This is required to make the ad
    // clickable.
    // Note: this should always be done after populating the ad views.
    nativeAdView.nativeAd = nativeAd
  }

הוספת התצוגה להיררכיית התצוגות

הקוד הבא מדגים את ההוספה של UIViewRepresentable לתצוגה המפורטת היררכיה:

struct NativeContentView: View {
  // Single source of truth for the native ad data.
  @StateObject private var nativeViewModel = NativeAdViewModel()

  var body: some View {
    ScrollView {
      VStack(spacing: 20) {
        NativeAdView(nativeViewModel: nativeViewModel)  // Updates when the native ad data changes.
          .frame(minHeight: 300)  // minHeight determined from xib.

Objective-C

#pragma mark GADNativeAdLoaderDelegate implementation

- (void)adLoader:(GADAdLoader *)adLoader didReceiveNativeAd:(GADNativeAd *)nativeAd {
  NSLog(@"Received native ad: %@", nativeAd);
  self.refreshButton.enabled = YES;

  // Create and place ad in view hierarchy.
  GADNativeAdView *nativeAdView =
      [[NSBundle mainBundle] loadNibNamed:@"NativeAdView" owner:nil options:nil].firstObject;
  [self setAdView:nativeAdView];

  // Set the mediaContent on the GADMediaView to populate it with available
  // video/image asset.
  nativeAdView.mediaView.mediaContent = nativeAd.mediaContent;

  // Populate the native ad view with the native ad assets.
  // The headline is present in every native ad.
  ((UILabel *)nativeAdView.headlineView).text = nativeAd.headline;

  // These assets are not guaranteed to be present. Check that they are before
  // showing or hiding them.
  ((UILabel *)nativeAdView.bodyView).text = nativeAd.body;
  nativeAdView.bodyView.hidden = nativeAd.body ? NO : YES;

  [((UIButton *)nativeAdView.callToActionView)setTitle:nativeAd.callToAction
                                              forState:UIControlStateNormal];
  nativeAdView.callToActionView.hidden = nativeAd.callToAction ? NO : YES;

    ((UIImageView *)nativeAdView.iconView).image = nativeAd.icon.image;
  nativeAdView.iconView.hidden = nativeAd.icon ? NO : YES;

  ((UIImageView *)nativeAdView.starRatingView).image = [self imageForStars:nativeAd.starRating];
  nativeAdView.starRatingView.hidden = nativeAd.starRating ? NO : YES;

  ((UILabel *)nativeAdView.storeView).text = nativeAd.store;
  nativeAdView.storeView.hidden = nativeAd.store ? NO : YES;

  ((UILabel *)nativeAdView.priceView).text = nativeAd.price;
  nativeAdView.priceView.hidden = nativeAd.price ? NO : YES;

  ((UILabel *)nativeAdView.advertiserView).text = nativeAd.advertiser;
  nativeAdView.advertiserView.hidden = nativeAd.advertiser ? NO : YES;

  // In order for the SDK to process touch events properly, user interaction
  // should be disabled.
  nativeAdView.callToActionView.userInteractionEnabled = NO;

  // Associate the native ad view with the native ad object. This is
  // required to make the ad clickable.
  nativeAdView.nativeAd = nativeAd;
}

הקישור המלא ב-GitHub

בואו לראות את הדוגמה המלאה של שילוב מודעות מותאמות ב-Swift, ב-SwiftUI וב- כדי לעשות את זה, לוחצים על הקישור המתאים ב-GitHub.

דוגמה לעיבוד מותאם אישית באמצעות SWIFT דוגמה למודעות מותאמות של SwiftUI דוגמה לעיבוד מותאם אישית מסוג Objective-C

GADMediaView

נכסי וידאו ותמונה מוצגים למשתמשים דרך GADMediaView זהו UIView שאפשר להגדיר בקובץ xib או ליצור אותו באופן דינמי. צריך למקם אותו בהיררכיית התצוגות של GADNativeAdView, בדומה ל- כל תצוגת נכס אחרת.

בדומה לכל הצפיות בנכסים, תצוגת המדיה צריכה לכלול את התוכן שלה מאוכלס. הוא מוגדר באמצעות mediaContent בנכס GADMediaView. mediaContent נכס של GADNativeAd מכיל תוכן מדיה שאפשר להעביר GADMediaView.

הנה קטע טקסט מתוך דוגמה לעיבוד מותאם אישית (Swift | יעד ג') שמראה איך לאכלס את GADMediaView בנכסי המודעות המותאמות באמצעות GADMediaContent מ-GADNativeAd:

Swift

nativeAdView.mediaView?.mediaContent = nativeAd.mediaContent

Objective-C

nativeAdView.mediaView.mediaContent = nativeAd.mediaContent;

עליך לוודא שבקובץ של הכלי לבניית הממשק של תצוגת המודעה המותאמת, הסיווג המותאם אישית של הצפיות שהוגדר הוא GADMediaView וקישרת אותו אל שקע חשמל mediaView.

שינוי מצב תוכן התמונה

הכיתה GADMediaView מכבדת את UIView contentMode כשמציגים תמונות. אם רוצים לשנות את גודל התמונה GADMediaView, מגדירים את הערכים המתאימים UIViewContentMode בנכס contentMode של GADMediaView כדי לעשות את זה.

לדוגמה, כדי למלא את השדה GADMediaView כשמוצגת תמונה (למודעה אין ):

Swift

nativeAdView.mediaView?.contentMode = .aspectFill

Objective-C

nativeAdView.mediaView.contentMode = UIViewContentModeAspectFill;

GADMediaContent

GADMediaContent מכילה את הנתונים הקשורים לתוכן המדיה של המודעה המותאמת, מוצגים באמצעות המחלקה GADMediaView. כשהיא מוגדרת ב-GADMediaView מאפיין mediaContent:

  • אם נכס וידאו זמין, הוא נשמר במאגר נתונים זמני ומתחיל לפעול GADMediaView כדי לדעת אם נכס הווידאו זמין, אפשר לבדוק hasVideoContent

  • אם המודעה לא מכילה נכס וידאו, תתבצע הורדה של הנכס מסוג mainImage ומוקמו בתוך GADMediaView במקום זאת.

השלבים הבאים

מידע נוסף על פרטיות המשתמשים