ใช้รูปแบบโฆษณาเนทีฟที่กำหนดเอง

เลือกแพลตฟอร์ม: Android ใหม่ Android iOS

รูปแบบโฆษณาที่กำหนดเอง

รูปแบบโฆษณาเนทีฟที่กำหนดเองจะโหลดโดยใช้ออบเจ็กต์ GADAdLoader เช่นเดียวกับรูปแบบโฆษณาเนทีฟที่ระบบกำหนด การใส่ค่าคงที่ GADAdLoaderAdTypeCustomNative ในอาร์เรย์ adTypes เมื่อ เริ่มต้น GADAdLoader จะกำหนดค่าให้ขอรูปแบบเนทีฟที่กำหนดเอง เมื่อโหลดโฆษณา

GADCustomNativeAdLoaderDelegate

โปรโตคอลสำหรับการโหลดรูปแบบที่กำหนดเองมี 2 เมธอด GADAdLoader ใช้เมธอดแรกเพื่อดูว่าควรขอรหัสรูปแบบใด

Swift

public func customNativeAdFormatIDs(for adLoader: AdLoader) -> [Any]

Objective-C

- (NSArray *)customNativeAdFormatIDsForAdLoader:(AdLoader *)adLoader;

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

ข้อความที่ 2 จะส่งเมื่อโหลดโฆษณาเนทีฟที่กำหนดเองแล้ว ซึ่งคล้ายกับข้อความสำหรับรูปแบบที่ระบบกำหนด

Swift

public func adLoader(_ adLoader: AdLoader,
    didReceive customNativeAd: CustomNativeAd)

Objective-C

- (void)adLoader:(AdLoader *)adLoader
    didReceiveCustomNativeAd:(CustomNativeAd *)customNativeAd;

รหัสรูปแบบ

คุณดูรหัสรูปแบบที่ใช้เพื่ออ้างอิงรูปแบบโฆษณาเนทีฟที่กำหนดเองได้โดยเฉพาะ ใน UI ของ Ad Manager ในส่วนเนทีฟ ภายในเมนูแบบเลื่อนลงการแสดงโฆษณา:

รหัสรูปแบบของโฆษณาเนทีฟที่กำหนดเองแต่ละรายการจะปรากฏข้างชื่อ การคลิกชื่อใดชื่อหนึ่งจะนำคุณไปยังหน้าจอรายละเอียดที่แสดงข้อมูลเกี่ยวกับช่องของรูปแบบ

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

การแสดงรูปแบบโฆษณาเนทีฟที่กำหนดเอง

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

  1. เนื่องจาก GADCustomNativeAd มีไว้เพื่อจัดการรูปแบบโฆษณาเนทีฟที่กำหนดเองที่คุณสร้างขึ้น จึงไม่มีตัวเข้าถึงชิ้นงานที่มีชื่อ แต่จะมีเมธอด เช่น imageForKey: และ stringForKey: ที่ใช้ชื่อของช่องเป็นอาร์กิวเมนต์
  2. ไม่มีคลาสของมุมมองโฆษณาเฉพาะ เช่น GADNativeAdView ที่จะใช้กับ GADCustomNativeAd คุณสามารถใช้มุมมองใดก็ได้ที่เหมาะสมกับประสบการณ์ของผู้ใช้
  3. เนื่องจากไม่มีคลาสของมุมมองโฆษณาเฉพาะ คุณจึงไม่จำเป็นต้องลงทะเบียนมุมมองใดๆ ที่ใช้เพื่อแสดงชิ้นงานของโฆษณา

ตัวอย่างต่อไปนี้แสดงมุมมองโฆษณาที่แสดงโฆษณาเนทีฟที่กำหนดเองอย่างง่ายได้

MySimpleNativeAdView.h

Swift

import UIKit
import GoogleMobileAds

/// Custom native ad view class with format ID 10063170.
class MySimpleNativeAdView: UIView {

  /// Weak references to this ad's asset views.
  @IBOutlet weak var headlineView: UILabel!
  @IBOutlet weak var mainImageView: UIImageView!
  @IBOutlet weak var captionView: UILabel!

  ...

  /// Populates the ad view with the custom native ad object.
  func populateWithCustomNativeAd(_ customNativeAd: CustomNativeAd) {
    ...
  }
}

Objective-C

@import UIKit;
@import GoogleMobileAds;

/// View representing a custom native ad format with format ID 10063170.
@interface MySimpleNativeAdView : UIView

// Weak references to this ad's asset views.
@property(weak, nonatomic) IBOutlet UILabel *headlineView;
@property(weak, nonatomic) IBOutlet UIImageView *mainImageView;
@property(weak, nonatomic) IBOutlet UILabel *captionView;

/// Populates the ad view with the custom native ad object.
- (void)populateWithCustomNativeAd:(GADCustomNativeAd *)customNativeAd;

@end

MySimpleNativeAdView.m (excerpt)

Swift

...
func populateWithCustomNativeAd(_ customNativeAd: CustomNativeAd) {
  self.customNativeAd = customNativeAd

  // Populate the custom native ad assets.
  headlineView.text = self.customNativeAd.stringForKey("Headline")
  mainImageView.image = self.customNativeAd.imageForKey("MainImage")?.image
  captionView.text = self.customNativeAd.stringForKey("Caption")
}
...

Objective-C

...
- (void)populateWithCustomNativeAd:(GADCustomNativeAd *)customNativeAd {
  self.customNativeAd = customNativeAd;

  // Populate the custom native ad assets.
  self.headlineView.text = [customNativeAd stringForKey:@"Headline"];
  self.mainImageView.image = [customNativeAd imageForKey:@"MainImage"].image;
  self.captionView.text = [customNativeAd stringForKey:@"Caption"];
}
...

แสดงผลไอคอนตัวเลือกโฆษณาอื่นๆ

โฆษณาแบบจองล่วงหน้าที่แสดงในเขตเศรษฐกิจยุโรป (EEA) ต้องมีไอคอนตัวเลือกโฆษณาอื่นๆ และลิงก์ไปยังหน้า เกี่ยวกับโฆษณานี้ของ Google ซึ่งเป็นส่วนหนึ่งของการสนับสนุนกฎหมายบริการดิจิทัล (DSA) เมื่อติดตั้งใช้งานโฆษณาเนทีฟที่กำหนดเอง คุณจะเป็นผู้รับผิดชอบในการแสดงผลไอคอนตัวเลือกโฆษณาอื่นๆ โดยต้องทำตามขั้นตอนเพื่อแสดงผลและตั้งค่า Listener การคลิกสำหรับไอคอนตัวเลือกโฆษณาอื่นๆ เมื่อแสดงผลชิ้นงานโฆษณาหลัก

ตัวอย่างต่อไปนี้แสดงผลไอคอนตัวเลือกโฆษณาอื่นๆ และกำหนดค่าลักษณะการทำงานของการคลิกที่เหมาะสม

Swift

class MySimpleNativeAdView: UIView {
  @IBOutlet weak var adChoicesView: UIImageView!

  override func awakeFromNib() {
    super.awakeFromNib()

    // Enable clicks on AdChoices.
    adChoicesView.addGestureRecognizer(
      UITapGestureRecognizer(
        target: self,
        action: #selector(performClickOnAdChoices(_:))))
    adChoicesView.isUserInteractionEnabled = true
  }

  @objc func performClickOnAdChoices(_ sender: UIImage!) {
    customNativeAd.performClickOnAsset(withKey:
      NativeAssetIdentifier.adChoicesViewAsset.rawValue)
  }

  func populate(withCustomNativeAd customNativeAd: CustomNativeAd) {

    // Render the AdChoices image.
    let adChoicesKey = NativeAssetIdentifier.adChoicesViewAsset.rawValue
    let adChoicesImage = customNativeAd.image(forKey: adChoicesKey)?.image
    adChoicesView.image = adChoicesImage
    adChoicesView.isHidden = adChoicesImage == nil
    ...
  }
}

Objective-C

@interface MySimpleNativeAdView ()

@property(nonatomic, weak) IBOutlet UIImageView *adChoicesView;

@end

@implementation MySimpleNativeAdView

- (void)awakeFromNib {
  [super awakeFromNib];
  // Enable clicks on AdChoices.
  [self.adChoicesView addGestureRecognizer:[[UITapGestureRecognizer alloc]
                                            initWithTarget:self
                                            action:@selector(performClickOnAdChoices:)]];
  self.adChoicesView.userInteractionEnabled = YES;
}

- (void)performClickOnAdChoices:(UITapGestureRecognizer *)sender {
    [self.customNativeAd performClickOnAssetWithKey:GADNativeAdChoicesViewAsset];
}

- (void)populateWithCustomNativeAd:(GADCustomNativeAd *)customNativeAd {
  // Render the AdChoices image.
  GADNativeAdImage *adChoicesAsset = [customNativeAd
    imageForKey:GADNativeAdChoicesViewAsset];
  self.adChoicesView.image = adChoicesAsset.image;
  self.adChoicesView.hidden = (adChoicesAsset == nil);
  ...
}

วิดีโอเนทีฟสำหรับรูปแบบโฆษณาเนทีฟที่กำหนดเอง

เมื่อสร้างรูปแบบที่กำหนดเอง, คุณสามารถเลือกให้รูปแบบมีสิทธิ์ใช้วิดีโอได้

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

ตัวอย่างด้านล่างจะตรวจสอบว่าโฆษณามีเนื้อหาวิดีโอหรือไม่ และแสดงรูปภาพแทนหากไม่มีวิดีโอ

Swift

...
  /// Populates the ad view with the custom native ad object.
  func populate(withCustomNativeAd customNativeAd: CustomNativeAd) {
    if customNativeAd.videoController.hasVideoContent(),
      let mediaView = customNativeAd.mediaView {
      updateMainView(mediaView)
    } else {
      // Assumes your native format has an image asset with the name MainImage.
      let image: UIImage? = customNativeAd.image(forKey: "MainImage")?.image
      updateMainView(UIImageView(image: image))
    }
  }

  private func updateMainView(_ mainView:UIView) {
    // Assumes you have a placeholder view for your media content.
    // Remove all the placeholder's subviews.
    for subview: UIView in mainPlaceholder.subviews {
      subview.removeFromSuperview()
    }
    mainPlaceholder.addSubview(mainView)
    // Size the media view to fill our container size.
    mainView.translatesAutoresizingMaskIntoConstraints = false
    let viewDictionary: [AnyHashable: Any] = ["mainView":mainView]
    mainPlaceholder.addConstraints(NSLayoutConstraint.constraints(
      withVisualFormat: "H:|[mainView]|", options: [], metrics: nil,
      views: viewDictionary as? [String : Any] ?? [String : Any]()))
    mainPlaceholder.addConstraints(NSLayoutConstraint.constraints(
      withVisualFormat: "V:|[mainView]|", options: [], metrics: nil,
      views: viewDictionary as? [String : Any] ?? [String : Any]()))
  }
...

Objective-C

...
- (void)populateWithCustomNativeAd:(GADCustomNativeAd *)ad {
  UIView *mainView = nil;
  if (ad.videoController.hasVideoContent) {
    mainView = ad.mediaView;
  } else {
    // Assumes your native format has an image asset with the name MainImage.
    UIImage *image = [ad imageForKey:@"MainImage"].image;
    mainView = [[UIImageView alloc] initWithImage:image];
  }
  // Assumes you have a placeholder view for your media content.
  for (UIView *subview in self.mainPlaceholder.subviews) {
    [subview removeFromSuperview];
  }
  [self.mainPlaceholder addSubview:mainView];

  // Size the main view to fill our container size.
  [mainView setTranslatesAutoresizingMaskIntoConstraints:NO];
  NSDictionary *viewDictionary = NSDictionaryOfVariableBindings(mainView);
  [self.mainPlaceholder
      addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[mainView]|"
                                                             options:0
                                                             metrics:nil
                                                               views:viewDictionary]];
}
...

ดูข้อมูลเพิ่มเติมเกี่ยวกับวิธีปรับแต่งประสบการณ์การใช้งานวิดีโอของโฆษณาเนทีฟที่กำหนดเองได้ที่ GADVideoController

ดาวน์โหลดตัวอย่างการแสดงผลที่กำหนดเองของ Ad Manager เพื่อดูตัวอย่างการทำงานของวิดีโอเนทีฟ

การจัดการการคลิกและการแสดงผลโฆษณาเนทีฟที่กำหนดเอง

สำหรับรูปแบบโฆษณาเนทีฟที่กำหนดเอง แอปของคุณมีหน้าที่รับผิดชอบในการบันทึกการแสดงผลและรายงานเหตุการณ์การคลิกไปยัง SDK

การบันทึกการแสดงผล

หากต้องการบันทึกการแสดงผลสำหรับโฆษณาเนทีฟที่กำหนดเอง ให้เรียกเมธอด recordImpression ใน GADCustomNativeAd ที่เกี่ยวข้อง

Swift

myCustomNativeAd.recordImpression()

Objective-C

[myCustomNativeAd recordImpression];

SDK จะป้องกันไม่ให้มีการบันทึกการแสดงผลที่ซ้ำกันสำหรับคำขอเดียว หากแอปเรียกเมธอดหลายครั้งสำหรับโฆษณาเดียวกันโดยไม่ได้ตั้งใจ

การรายงานการคลิก

หากต้องการรายงานไปยัง SDK ว่ามีการคลิกในมุมมองชิ้นงาน ให้เรียกเมธอด performClickOnAssetWithKey: ใน GADCustomNativeAd ที่เกี่ยวข้อง แล้วส่งชื่อของชิ้นงานที่ถูกคลิก ตัวอย่างเช่น หากคุณมีชิ้นงานในรูปแบบที่กำหนดเองชื่อ "MainImage" และต้องการรายงานการคลิกในมุมมองที่สอดคล้องกับชิ้นงานนั้น โค้ดจะมีลักษณะดังนี้

Swift

myCustomNativeAd.performClickOnAsset(withKey: "MainImage")

Objective-C

[myCustomNativeAd performClickOnAssetWithKey:@"MainImage"];

โปรดทราบว่าคุณไม่จำเป็นต้องเรียกเมธอดนี้สำหรับมุมมองชิ้นงานทุกรายการที่เชื่อมโยงกับโฆษณา ตัวอย่างเช่น หากคุณมีชิ้นงานอื่นชื่อ "Caption" ที่มีไว้เพื่อแสดงแต่ผู้ใช้ไม่ได้คลิกหรือแตะ แอปก็ไม่จำเป็นต้องเรียก performClickOnAssetWithKey: สำหรับมุมมองนั้น

การตอบสนองต่อการกระทำที่กำหนดเองเมื่อมีการคลิก

GADCustomNativeAd มีพร็อพเพอร์ตี้ customClickHandler ซึ่งมีประเภทเป็น GADNativeAdCustomClickHandler

Swift

typealias NativeAdCustomClickHandler = (assetID: String) -> Void

Objective-C

typedef void (^GADNativeAdCustomClickHandler)(NSString *assetID);

นี่คือบล็อก (Objective-C) / Closure (Swift) ที่รับ assetID เป็นพารามิเตอร์อินพุต ซึ่งระบุชิ้นงานที่ถูกคลิก

เมื่อมีการคลิกโฆษณาเนทีฟที่กำหนดเอง SDK จะตอบสนองได้ 3 แบบตามลำดับต่อไปนี้

  1. เรียกใช้บล็อก customClickHandler ใน Objective-C หรือ Closure ใน Swift หากมีการตั้งค่าไว้
  2. วนซ้ำ URL ของดีปลิงก์ของโฆษณาและเปิด URL แรกที่พบแอปที่ตรงกัน
  3. เปิดเบราว์เซอร์และไปยัง URL ปลายทางแบบดั้งเดิมของโฆษณา

พร็อพเพอร์ตี้ customClickHandler รับบล็อกใน Objective-C และ Closure ใน Swift หากคุณตั้งค่าบล็อกหรือ Closure SDK จะเรียกใช้และไม่ดำเนินการใดๆ เพิ่มเติม อย่างไรก็ตาม หากคุณตั้งค่าเป็น nil SDK จะกลับไปใช้ดีปลิงก์และ/หรือ URL ปลายทางที่ลงทะเบียนไว้กับโฆษณา

ตัวแฮนเดิลการคลิกที่กำหนดเองช่วยให้แอปตัดสินใจเองได้ว่าจะดำเนินการใดดีที่สุดเพื่อตอบสนองต่อการคลิก ไม่ว่าจะเป็นการอัปเดต UI, การแสดงตัวควบคุมมุมมองอื่น หรือเพียงแค่บันทึกการคลิก ตัวอย่างต่อไปนี้แสดงการแจ้งเตือน

Swift

myCustomNativeAd.customClickHandler = { assetID in
  if assetID == "MainImage" {
    let alertView = UIAlertView(title: "Custom Click",
        message: "You just clicked on the image!",
        delegate: self,
        cancelButtonTitle: "OK")
    alertView.alertViewStyle = .default
    alertView.show()
  }
}
myCustomNativeAd.performClickOnAsset(withKey: "MainImage")

Objective-C

[self.customNativeAd setCustomClickHandler:^(NSString *assetID){
  if ([assetID isEqualToString:@"MainImage"]) {
    [[[UIAlertView alloc] initWithTitle:@"Custom Click"
                                message:@"You just clicked on the image!"
                               delegate:self
                      cancelButtonTitle:@"OK"
                      otherButtonTitles:nil] show];
  }
}];
[self.customNativeAd performClickOnAssetWithKey:@"MainImage"];

การทดสอบโค้ดโฆษณาเนทีฟ

โฆษณาแบบขายตรง

หากต้องการทดสอบลักษณะของโฆษณาเนทีฟแบบขายตรง คุณสามารถใช้รหัสหน่วยโฆษณา Ad Manager นี้

/21775744923/example/native

หน่วยโฆษณานี้ได้รับการกำหนดค่าให้แสดงโฆษณาเพื่อการติดตั้งแอปตัวอย่างและโฆษณาแบบคอนเทนต์ รวมถึงรูปแบบโฆษณาเนทีฟที่กำหนดเองที่มีชิ้นงานต่อไปนี้

  • บรรทัดแรก (ข้อความ)
  • MainImage (รูปภาพ)
  • คำบรรยายภาพ (ข้อความ)

โฆษณาแบ็กฟิลล์เนทีฟ

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

/21775744923/example/native-backfill

หน่วยโฆษณานี้จะแสดงโฆษณาเพื่อการติดตั้งแอปตัวอย่างและโฆษณาแบบคอนเทนต์ที่มีการวางซ้อนตัวเลือกโฆษณาอื่นๆ

อย่าลืมอัปเดตโค้ดเพื่ออ้างอิงหน่วยโฆษณาและรหัสรูปแบบจริงก่อนที่จะเผยแพร่