เหตุการณ์ที่กำหนดเองของโฆษณาเนทีฟ

ข้อกำหนดเบื้องต้น

ตั้งค่าเหตุการณ์ที่กําหนดเองให้เสร็จ

ขอโฆษณาเนทีฟ

เมื่อระบบเข้าถึงรายการโฆษณาของเหตุการณ์ที่กําหนดเองในเชนสื่อกลางตามลำดับขั้น มีการเรียกใช้เมธอด loadNativeAd:adConfiguration:completionHandler: ใน ชื่อคลาสที่คุณระบุเมื่อสร้าง กิจกรรม ในกรณีนี้ เมธอดนั้นอยู่ใน SampleCustomEvent ซึ่งจะเรียกใช้ เมธอด loadNativeAd:adConfiguration:completionHandler: ใน SampleCustomEventNative

หากต้องการขอโฆษณาเนทีฟ ให้สร้างหรือแก้ไขคลาสที่ใช้ GADMediationAdapter และ loadNativeAd:adConfiguration:completionHandler: ถ้า มีคลาสที่ขยาย GADMediationAdapter อยู่แล้ว, ใช้ loadNativeAd:adConfiguration:completionHandler:ที่นั่น นอกจากนี้ ให้สร้าง คลาสใหม่สำหรับใช้งาน GADMediationNativeAd

ในตัวอย่างเหตุการณ์ที่กำหนดเอง ติดตั้งใช้งาน SampleCustomEvent อินเทอร์เฟซ GADMediationAdapter แล้วจึงมอบสิทธิ์ให้ SampleCustomEventNative

Swift

import GoogleMobileAds

class SampleCustomEvent: NSObject, GADMediationAdapter {

  fileprivate var nativeAd: SampleCustomEventNativeAd?

  func loadNativeAd(
    for adConfiguration: GADMediationNativeAdConfiguration,
    completionHandler: @escaping GADMediationNativeAdLoadCompletionHandler
  ) {
    self.nativeAd = SampleCustomEventNativeAd()
    self.nativeAd?.loadNativeAd(
      for: adConfiguration, completionHandler: completionHandler)
  }
}

Objective-C

#import "SampleCustomEvent.h"

@implementation SampleCustomEvent

SampleCustomEventNativeAd *sampleNativeAd;

- (void)loadNativeAdForAdConfiguration:
            (GADMediationNativeAdConfiguration *)adConfiguration
                     completionHandler:
                         (GADMediationNativeAdLoadCompletionHandler)
                             completionHandler {
  sampleNative = [[SampleCustomEventNativeAd alloc] init];
  [sampleNative loadNativeAdForAdConfiguration:adConfiguration
                             completionHandler:completionHandler];
}

SampleCustomEventNative` มีหน้าที่ดังนี้

  • กำลังโหลดโฆษณาเนทีฟ

  • กำลังใช้โปรโตคอล GADMediationNativeAd

  • การรับและการรายงาน Callback ของเหตุการณ์โฆษณาไปยัง SDK โฆษณาในอุปกรณ์เคลื่อนที่ของ Google

พารามิเตอร์ที่ไม่บังคับซึ่งกำหนดไว้ใน UI ของ AdMob คือ ที่รวมอยู่ในการกำหนดค่าโฆษณา พารามิเตอร์นี้สามารถเข้าถึงได้ adConfiguration.credentials.settings[@"parameter"] พารามิเตอร์นี้คือ มักจะเป็นตัวระบุหน่วยโฆษณาที่ SDK เครือข่ายโฆษณาต้องใช้เมื่อ การเริ่มต้นออบเจ็กต์โฆษณา

Swift

class SampleCustomEventNativeAd: NSObject, GADMediationNativeAd {
  /// The Sample Ad Network native ad.
  var nativeAd: SampleNativeAd?

  /// The ad event delegate to forward ad rendering events to the Google Mobile
  /// Ads SDK.
  var delegate: GADMediationNativeAdEventDelegate?

  /// Completion handler called after ad load
  var completionHandler: GADMediationNativeLoadCompletionHandler?

  func loadNativeAd(
    for adConfiguration: GADMediationNativeAdConfiguration,
    completionHandler: @escaping GADMediationNativeLoadCompletionHandler
  ) {
    let adLoader = SampleNativeAdLoader()
    let sampleRequest = SampleNativeAdRequest()

    // The Google Mobile Ads SDK requires the image assets to be downloaded
    // automatically unless the publisher specifies otherwise by using the
    // GADNativeAdImageAdLoaderOptions object's disableImageLoading property. If
    // your network doesn't have an option like this and instead only ever
    // returns URLs for images (rather than the images themselves), your adapter
    // should download image assets on behalf of the publisher. This should be
    // done after receiving the native ad object from your network's SDK, and
    // before calling the connector's adapter:didReceiveMediatedNativeAd: method.
    sampleRequest.shouldDownloadImages = true
    sampleRequest.preferredImageOrientation = NativeAdImageOrientation.any
    sampleRequest.shouldRequestMultipleImages = false
    let options = adConfiguration.options
    for loaderOptions: GADAdLoaderOptions in options {
      if let imageOptions = loaderOptions as? GADNativeAdImageAdLoaderOptions {
        sampleRequest.shouldRequestMultipleImages =
          imageOptions.shouldRequestMultipleImages
        // If the GADNativeAdImageAdLoaderOptions' disableImageLoading property is
        // YES, the adapter should send just the URLs for the images.
        sampleRequest.shouldDownloadImages = !imageOptions.disableImageLoading
      } else if let mediaOptions = loaderOptions
        as? GADNativeAdMediaAdLoaderOptions
      {
        switch mediaOptions.mediaAspectRatio {
        case GADMediaAspectRatio.landscape:
          sampleRequest.preferredImageOrientation =
            NativeAdImageOrientation.landscape
        case GADMediaAspectRatio.portrait:
          sampleRequest.preferredImageOrientation =
            NativeAdImageOrientation.portrait
        default:
          sampleRequest.preferredImageOrientation = NativeAdImageOrientation.any
        }
      }
    }
    // This custom event uses the server parameter to carry an ad unit ID, which
    // is the most common use case.
    adLoader.delegate = self
    adLoader.adUnitID =
      adConfiguration.credentials.settings["parameter"] as? String
    self.completionHandler = completionHandler
    adLoader.fetchAd(sampleRequest)
  }
}

Objective-C

#import "SampleCustomEventNativeAd.h"

@interface SampleCustomEventNativeAd () <SampleNativeAdDelegate,
                                         GADMediationNativeAd> {
  /// The sample native ad.
  SampleNativeAd *_nativeAd;

  /// The completion handler to call when the ad loading succeeds or fails.
  GADMediationNativeLoadCompletionHandler _loadCompletionHandler;

  /// The ad event delegate to forward ad rendering events to the Google Mobile
  /// Ads SDK.
  id<GADMediationNativeAdEventDelegate> _adEventDelegate;
}
@end

- (void)loadNativeAdForAdConfiguration:
            (GADMediationNativeAdConfiguration *)adConfiguration
                     completionHandler:(GADMediationNativeLoadCompletionHandler)
                                           completionHandler {
  __block atomic_flag completionHandlerCalled = ATOMIC_FLAG_INIT;
  __block GADMediationNativeLoadCompletionHandler originalCompletionHandler =
      [completionHandler copy];

  _loadCompletionHandler = ^id<GADMediationNativeAdEventDelegate>(
      _Nullable id<GADMediationNativeAd> ad, NSError *_Nullable error) {
    // Only allow completion handler to be called once.
    if (atomic_flag_test_and_set(&completionHandlerCalled)) {
      return nil;
    }

    id<GADMediationNativeAdEventDelegate> delegate = nil;
    if (originalCompletionHandler) {
      // Call original handler and hold on to its return value.
      delegate = originalCompletionHandler(ad, error);
    }

    // Release reference to handler. Objects retained by the handler will also
    // be released.
    originalCompletionHandler = nil;

    return delegate;
  };

  SampleNativeAdLoader *adLoader = [[SampleNativeAdLoader alloc] init];
  SampleNativeAdRequest *sampleRequest = [[SampleNativeAdRequest alloc] init];

  // The Google Mobile Ads SDK requires the image assets to be downloaded
  // automatically unless the publisher specifies otherwise by using the
  // GADNativeAdImageAdLoaderOptions object's disableImageLoading property. If
  // your network doesn't have an option like this and instead only ever returns
  // URLs for images (rather than the images themselves), your adapter should
  // download image assets on behalf of the publisher. This should be done after
  // receiving the native ad object from your network's SDK, and before calling
  // the connector's adapter:didReceiveMediatedNativeAd: method.
  sampleRequest.shouldDownloadImages = YES;

  sampleRequest.preferredImageOrientation = NativeAdImageOrientationAny;
  sampleRequest.shouldRequestMultipleImages = NO;
  sampleRequest.testMode = adConfiguration.isTestRequest;

  for (GADAdLoaderOptions *loaderOptions in adConfiguration.options) {
    if ([loaderOptions isKindOfClass:[GADNativeAdImageAdLoaderOptions class]]) {
      GADNativeAdImageAdLoaderOptions *imageOptions =
          (GADNativeAdImageAdLoaderOptions *)loaderOptions;
      sampleRequest.shouldRequestMultipleImages =
          imageOptions.shouldRequestMultipleImages;

      // If the GADNativeAdImageAdLoaderOptions' disableImageLoading property is
      // YES, the adapter should send just the URLs for the images.
      sampleRequest.shouldDownloadImages = !imageOptions.disableImageLoading;
    } else if ([loaderOptions
                   isKindOfClass:[GADNativeAdMediaAdLoaderOptions class]]) {
      GADNativeAdMediaAdLoaderOptions *mediaOptions =
          (GADNativeAdMediaAdLoaderOptions *)loaderOptions;
      switch (mediaOptions.mediaAspectRatio) {
        case GADMediaAspectRatioLandscape:
          sampleRequest.preferredImageOrientation =
              NativeAdImageOrientationLandscape;
          break;
        case GADMediaAspectRatioPortrait:
          sampleRequest.preferredImageOrientation =
              NativeAdImageOrientationPortrait;
          break;
        default:
          sampleRequest.preferredImageOrientation = NativeAdImageOrientationAny;
          break;
      }
    } else if ([loaderOptions isKindOfClass:[GADNativeAdViewAdOptions class]]) {
      _nativeAdViewAdOptions = (GADNativeAdViewAdOptions *)loaderOptions;
    }
  }

  // This custom event uses the server parameter to carry an ad unit ID, which
  // is the most common use case.
  NSString *adUnit = adConfiguration.credentials.settings[@"parameter"];
  adLoader.adUnitID = adUnit;
  adLoader.delegate = self;

  [adLoader fetchAd:sampleRequest];
}

ไม่ว่าโฆษณาจะดึงข้อมูลสำเร็จหรือพบข้อผิดพลาด จะโทรหา GADMediationNativeAdLoadCompletionHandler ในกรณีที่ประสบความสำเร็จ ผ่านคลาสที่ใช้งาน GADMediationNativeAd ด้วยค่า nil สำหรับพารามิเตอร์ข้อผิดพลาด ในกรณีที่เกิดข้อผิดพลาด ให้ส่งต่อข้อผิดพลาดที่คุณ ที่พบ

โดยปกติแล้ว วิธีการเหล่านี้จะใช้ภายใน Callback จาก SDK ของบุคคลที่สามที่อะแดปเตอร์ของคุณใช้ สำหรับตัวอย่างนี้ SDK ตัวอย่าง มี SampleNativeAdDelegate ที่มี Callback ที่เกี่ยวข้อง:

Swift

func adLoader(
  _ adLoader: SampleNativeAdLoader, didReceive nativeAd: SampleNativeAd
) {
  extraAssets = [
    SampleCustomEventConstantsSwift.awesomenessKey: nativeAd.degreeOfAwesomeness
      ?? ""
  ]

  if let image = nativeAd.image {
    images = [GADNativeAdImage(image: image)]
  } else {
    let imageUrl = URL(fileURLWithPath: nativeAd.imageURL)
    images = [GADNativeAdImage(url: imageUrl, scale: nativeAd.imageScale)]
  }
  if let mappedIcon = nativeAd.icon {
    icon = GADNativeAdImage(image: mappedIcon)
  } else {
    let iconURL = URL(fileURLWithPath: nativeAd.iconURL)
    icon = GADNativeAdImage(url: iconURL, scale: nativeAd.iconScale)
  }

  adChoicesView = SampleAdInfoView()
  self.nativeAd = nativeAd
  if let handler = completionHandler {
    delegate = handler(self, nil)
  }
}

func adLoader(
  _ adLoader: SampleNativeAdLoader,
  didFailToLoadAdWith errorCode: SampleErrorCode
) {
  let error =
    SampleCustomEventUtilsSwift.SampleCustomEventErrorWithCodeAndDescription(
      code: SampleCustomEventErrorCodeSwift
        .SampleCustomEventErrorAdLoadFailureCallback,
      description:
        "Sample SDK returned an ad load failure callback with error code: \(errorCode)"
    )
  if let handler = completionHandler {
    delegate = handler(nil, error)
  }
}

Objective-C

- (void)adLoader:(SampleNativeAdLoader *)adLoader
    didReceiveNativeAd:(SampleNativeAd *)nativeAd {
  if (nativeAd.image) {
    _images = @[ [[GADNativeAdImage alloc] initWithImage:nativeAd.image] ];
  } else {
    NSURL *imageURL = [[NSURL alloc] initFileURLWithPath:nativeAd.imageURL];
    _images = @[ [[GADNativeAdImage alloc] initWithURL:imageURL
                                                 scale:nativeAd.imageScale] ];
  }

  if (nativeAd.icon) {
    _icon = [[GADNativeAdImage alloc] initWithImage:nativeAd.icon];
  } else {
    NSURL *iconURL = [[NSURL alloc] initFileURLWithPath:nativeAd.iconURL];
    _icon = [[GADNativeAdImage alloc] initWithURL:iconURL
                                            scale:nativeAd.iconScale];
  }

  // The sample SDK provides an AdChoices view (SampleAdInfoView). If your SDK
  // provides image and click through URLs for its AdChoices icon instead of an
  // actual UIView, the adapter is responsible for downloading the icon image
  // and creating the AdChoices icon view.
  _adChoicesView = [[SampleAdInfoView alloc] init];
  _nativeAd = nativeAd;

  _adEventDelegate = _loadCompletionHandler(self, nil);
}

- (void)adLoader:(SampleNativeAdLoader *)adLoader
    didFailToLoadAdWithErrorCode:(SampleErrorCode)errorCode {
  NSError *error = SampleCustomEventErrorWithCodeAndDescription(
      SampleCustomEventErrorAdLoadFailureCallback,
      [NSString stringWithFormat:@"Sample SDK returned an ad load failure "
                                 @"callback with error code: %@",
                                 errorCode]);
  _adEventDelegate = _loadCompletionHandler(nil, error);
}

แมปโฆษณาเนทีฟ

SDK ต่างๆ มีรูปแบบที่เป็นเอกลักษณ์สำหรับโฆษณาเนทีฟ อาจมีคนกลับมา วัตถุที่มี "ชื่อ" เช่น ในขณะที่อีกโฆษณาอาจมี "headline" นอกจากนี้ วิธีการที่ใช้ติดตามการแสดงผลและการประมวลผล จำนวนคลิกอาจแตกต่างกันไปในแต่ละ SDK

ในการแก้ไขปัญหาเหล่านี้ เมื่อเหตุการณ์ที่กำหนดเองได้รับออบเจ็กต์โฆษณาเนทีฟจาก SDK ที่ใช้สื่อกลาง ควรใช้คลาสที่ใช้งาน GADMediationNativeAd เช่น SampleCustomEventNativeAd ไปยัง "แมป" ออบเจ็กต์โฆษณาเนทีฟของ SDK ที่ใช้สื่อกลาง เพื่อให้ตรงกับอินเทอร์เฟซที่ Google Mobile Ads SDK คาดไว้

ตอนนี้เราจะมาดูรายละเอียดการนำไปใช้งานสำหรับ SampleCustomEventNativeAd

จัดเก็บการแมป

คาดว่า GADMediationNativeAd จะใช้พร็อพเพอร์ตี้บางอย่างที่ แมปจากพร็อพเพอร์ตี้ของ SDK อื่นๆ ดังนี้

Swift

var nativeAd: SampleNativeAd?

var headline: String? {
  return nativeAd?.headline
}

var images: [GADNativeAdImage]?

var body: String? {
  return nativeAd?.body
}

var icon: GADNativeAdImage?

var callToAction: String? {
  return nativeAd?.callToAction
}

var starRating: NSDecimalNumber? {
  return nativeAd?.starRating
}

var store: String? {
  return nativeAd?.store
}

var price: String? {
  return nativeAd?.price
}

var advertiser: String? {
  return nativeAd?.advertiser
}

var extraAssets: [String: Any]? {
  return [
    SampleCustomEventConstantsSwift.awesomenessKey:
      nativeAd?.degreeOfAwesomeness
      ?? ""
  ]
}

var adChoicesView: UIView?

var mediaView: UIView? {
  return nativeAd?.mediaView
}

Objective-C

/// Used to store the ad's images. In order to implement the
/// GADMediationNativeAd protocol, we use this class to return the images
/// property.
NSArray<GADNativeAdImage *> *_images;

/// Used to store the ad's icon. In order to implement the GADMediationNativeAd
/// protocol, we use this class to return the icon property.
GADNativeAdImage *_icon;

/// Used to store the ad's ad choices view. In order to implement the
/// GADMediationNativeAd protocol, we use this class to return the adChoicesView
/// property.
UIView *_adChoicesView;

- (nullable NSString *)headline {
  return _nativeAd.headline;
}

- (nullable NSArray<GADNativeAdImage *> *)images {
  return _images;
}

- (nullable NSString *)body {
  return _nativeAd.body;
}

- (nullable GADNativeAdImage *)icon {
  return _icon;
}

- (nullable NSString *)callToAction {
  return _nativeAd.callToAction;
}

- (nullable NSDecimalNumber *)starRating {
  return _nativeAd.starRating;
}

- (nullable NSString *)store {
  return _nativeAd.store;
}

- (nullable NSString *)price {
  return _nativeAd.price;
}

- (nullable NSString *)advertiser {
  return _nativeAd.advertiser;
}

- (nullable NSDictionary<NSString *, id> *)extraAssets {
  return
      @{SampleCustomEventExtraKeyAwesomeness : _nativeAd.degreeOfAwesomeness};
}

- (nullable UIView *)adChoicesView {
  return _adChoicesView;
}

- (nullable UIView *)mediaView {
  return _nativeAd.mediaView;
}

- (BOOL)hasVideoContent {
  return self.mediaView != nil;
}

เครือข่ายสื่อกลางบางเครือข่ายอาจให้ชิ้นงานเพิ่มเติมนอกเหนือไปจากที่กําหนดโดย SDK โฆษณาในอุปกรณ์เคลื่อนที่ของ Google โปรโตคอล GADMediationNativeAd มีเมธอด ที่เรียกว่า extraAssets ซึ่ง Google Mobile Ads SDK ใช้เพื่อเรียกข้อมูล "เพิ่มเติม" เหล่านี้ เนื้อหาจากโปรแกรมสร้างแผนที่ของคุณ

แมปชิ้นงานรูปภาพ

การแมปเนื้อหารูปภาพมีความซับซ้อนกว่าการแมปข้อมูลที่ง่ายกว่า ประเภท เช่น NSString หรือ double ระบบอาจดาวน์โหลดรูปภาพโดยอัตโนมัติ หรือ แสดงผลเป็นค่า URL ความหนาแน่นของพิกเซลอาจแตกต่างกันไปด้วย

SDK โฆษณาบนอุปกรณ์เคลื่อนที่ของ Google มีฟังก์ชัน GADNativeAdImage ชั้นเรียน ข้อมูลชิ้นงานรูปภาพ (ไม่ว่าจะเป็น UIImage จริงหรือไม่ ออบเจ็กต์หรือเพียง NSURL ค่า) ควรส่งไปยัง SDK โฆษณาในอุปกรณ์เคลื่อนที่ของ Google โดยใช้ชั้นเรียนนี้

ต่อไปนี้เป็นวิธีที่ชั้นเรียนของผู้ทำแผนที่จะจัดการกับการสร้าง GADNativeAdImage เพื่อระงับ ภาพไอคอน:

Swift

if let image = nativeAd.image {
  images = [GADNativeAdImage(image: image)]
} else {
  let imageUrl = URL(fileURLWithPath: nativeAd.imageURL)
  images = [GADNativeAdImage(url: imageUrl, scale: nativeAd.imageScale)]
}

Objective-C

if (nativeAd.image) {
  _images = @[ [[GADNativeAdImage alloc] initWithImage:nativeAd.image] ];
} else {
  NSURL *imageURL = [[NSURL alloc] initFileURLWithPath:nativeAd.imageURL];
  _images = @[ [[GADNativeAdImage alloc] initWithURL:imageURL
                                               scale:nativeAd.imageScale] ];
}

เหตุการณ์การแสดงผลและการคลิก

ทั้ง SDK โฆษณาในอุปกรณ์เคลื่อนที่ของ Google และ SDK ที่ใช้สื่อกลางจำเป็นต้องทราบเมื่อ การแสดงผลหรือการคลิกเกิดขึ้น แต่มี SDK เพียง 1 รายการเท่านั้นที่ต้องติดตามเหตุการณ์เหล่านี้ มี มี 2 วิธีที่ต่างกันซึ่งเหตุการณ์ที่กำหนดเองจะใช้ได้ ขึ้นอยู่กับว่า SDK ที่ใช้สื่อกลางรองรับการติดตามการแสดงผลและการคลิกโดยตัวมันเอง

ติดตามคลิกและการแสดงผลด้วย Google Mobile Ads SDK

หาก SDK ที่ใช้สื่อกลางไม่ดำเนินการแสดงผลและการติดตามการคลิกของตนเอง แต่ Google Mobile Ads SDK เป็นวิธีบันทึกการคลิกและการแสดงผล ติดตามเหตุการณ์เหล่านี้และแจ้งให้อะแดปเตอร์ทราบ โปรโตคอล GADMediationNativeAd ประกอบด้วย 2 วิธี ได้แก่ didRecordImpression: และ didRecordClickOnAssetWithName:view:viewController:ที่เหตุการณ์ที่กำหนดเองสามารถ ใช้เพื่อเรียกใช้เมธอดที่เกี่ยวข้องในออบเจ็กต์โฆษณาเนทีฟที่ใช้สื่อกลาง

Swift

func didRecordImpression() {
  nativeAd?.recordImpression()
}

func didRecordClickOnAsset(
  withName assetName: GADUnifiedNativeAssetIdentifier,
  view: UIView,
  wController: UIViewController
) {
  nativeAd?.handleClick(on: view)
}

Objective-C

- (void)didRecordImpression {
  if (self.nativeAd) {
    [self.nativeAd recordImpression];
  }
}

- (void)didRecordClickOnAssetWithName:(GADUnifiedNativeAssetIdentifier)assetName
                                 view:(UIView *)view
                       viewController:(UIViewController *)viewController {
  if (self.nativeAd) {
    [self.nativeAd handleClickOnView:view];
  }
}

เนื่องจากคลาสที่ใช้ GADMediationNativeAd มีการอ้างอิงไปยังออบเจ็กต์โฆษณาเนทีฟของ SDK ตัวอย่าง ซึ่งสามารถเรียก ที่เหมาะสมในออบเจ็กต์ดังกล่าวเพื่อรายงานการคลิกหรือการแสดงผล โปรดทราบว่า เมธอด didRecordClickOnAssetWithName:view:viewController: ใช้เวลาเพียง พารามิเตอร์: ออบเจ็กต์ View ที่ตรงกับชิ้นงานโฆษณาเนทีฟที่ได้รับ คลิก

ติดตามคลิกและการแสดงผลด้วย SDK ที่ใช้สื่อกลาง

SDK ที่ใช้สื่อกลางบางรายการอาจต้องการติดตามการคลิกและการแสดงผลด้วยตัวเอง ใน ในกรณีนี้ คุณควรใช้ handlesUserClicks และ handlesUserImpressions เมธอดดังที่แสดงในข้อมูลโค้ดด้านล่าง ด้วยการส่งคืน YES คุณระบุว่าเหตุการณ์ที่กำหนดเองเป็นผู้รับผิดชอบในการติดตาม เหตุการณ์เหล่านี้ และจะแจ้ง SDK โฆษณาในอุปกรณ์เคลื่อนที่ของ Google เมื่อเกิดเหตุการณ์เหล่านี้ขึ้น

เหตุการณ์ที่กำหนดเองที่ลบล้างการติดตามการคลิกและการแสดงผลสามารถใช้ฟังก์ชัน didRenderInView: ข้อความเพื่อส่งมุมมองของโฆษณาเนทีฟไปยัง SDK ที่ใช้สื่อกลาง ให้ SDK ที่ใช้สื่อกลางทำการติดตามจริงได้ ตัวอย่าง SDK จากโปรเจ็กต์ตัวอย่างเหตุการณ์ที่กำหนดเองของเรา (ซึ่งเป็นโค้ดของข้อมูลโค้ดของคู่มือนี้ มีการใช้แล้ว) ไม่ได้ใช้วิธีนี้" แต่ถ้าใช่ โค้ดเหตุการณ์ที่กำหนดเอง จะเรียกเมธอด setNativeAdView:view: ดังที่แสดงในข้อมูลโค้ดด้านล่าง

Swift

func handlesUserClicks() -> Bool {
  return true
}
func handlesUserImpressions() -> Bool {
  return true
}

func didRender(
  in view: UIView, clickableAssetViews: [GADNativeAssetIdentifier: UIView],
  nonclickableAssetViews: [GADNativeAssetIdentifier: UIView],
  viewController: UIViewController
) {
  // This method is called when the native ad view is rendered. Here you would pass the UIView
  // back to the mediated network's SDK.
  self.nativeAd?.setNativeAdView(view)
}

Objective-C

- (BOOL)handlesUserClicks {
  return YES;
}

- (BOOL)handlesUserImpressions {
  return YES;
}

- (void)didRenderInView:(UIView *)view
       clickableAssetViews:(NSDictionary<GADNativeAssetIdentifier, UIView *> *)
                               clickableAssetViews
    nonclickableAssetViews:(NSDictionary<GADNativeAssetIdentifier, UIView *> *)
                               nonclickableAssetViews
            viewController:(UIViewController *)viewController {
  // This method is called when the native ad view is rendered. Here you would
  // pass the UIView back to the mediated network's SDK. Playing video using
  // SampleNativeAd's playVideo method
  [_nativeAd setNativeAdView:view];
}

ส่งต่อเหตุการณ์สื่อกลางไปยัง SDK โฆษณาในอุปกรณ์เคลื่อนที่ของ Google

เมื่อโทรหา GADMediationNativeLoadCompletionHandler ด้วยโฆษณาที่โหลด ผู้แทน GADMediationNativeAdEventDelegate ที่แสดงผล จากนั้นอะแดปเตอร์สามารถใช้เพื่อส่งต่อเหตุการณ์การนำเสนอจาก SDK ของบุคคลที่สามกับ Google Mobile Ads SDK

เหตุการณ์ที่กำหนดเองจะต้องส่งต่อ Callback เหล่านี้ให้มากที่สุดเท่าที่จะเป็นไปได้ เพื่อให้แอปของคุณได้รับเหตุการณ์ที่เทียบเท่าเหล่านี้จาก SDK โฆษณาในอุปกรณ์เคลื่อนที่ ต่อไปนี้เป็นตัวอย่างของการใช้ Callback

เท่านี้ก็เสร็จสิ้นการใช้งานเหตุการณ์ที่กำหนดเองสำหรับโฆษณาเนทีฟแล้ว ตัวอย่างแบบเต็ม มีให้บริการใน GitHub