自訂廣告格式
與系統定義的格式相同,自訂原生廣告格式也是使用 GADAdLoader
物件載入。在初始化 GADAdLoader
時,將 GADAdLoaderAdTypeCustomNative
常數納入 adTypes
陣列,即可設定在載入廣告時請求自訂原生格式。
GADCustomNativeAdLoaderDelegate
載入自訂格式的通訊協定有兩種方法。第一個用於GADAdLoader
,找出應要求哪些格式 ID:
Swift
public func customNativeAdFormatIDs(for adLoader: AdLoader) -> [Any]
Objective-C
- (NSArray *)customNativeAdFormatIDsForAdLoader:(AdLoader *)adLoader;
每個自訂原生廣告格式都有對應的格式 ID,可做為識別依據。呼叫這個方法時,應用程式應傳回陣列,其中包含準備顯示的廣告格式 ID。
自訂原生廣告載入後,系統會傳送第二則訊息,與系統定義格式的訊息類似:
Swift
public func adLoader(_ adLoader: AdLoader, didReceive customNativeAd: CustomNativeAd)
Objective-C
- (void)adLoader:(AdLoader *)adLoader didReceiveCustomNativeAd:(CustomNativeAd *)customNativeAd;
格式 ID
如要以專屬 ID 參照自訂原生廣告格式,請前往 Ad Manager 使用者介面,在「放送」下拉式選單的「原生」部分中查看:
每個自訂原生廣告的格式 ID 都會顯示在名稱旁邊。 點選其中一個名稱會前往詳細資料畫面,顯示格式欄位的相關資訊:
您可以在這裡新增、編輯及移除個別欄位。請記下每個資產的「名稱」。顯示自訂原生廣告格式時,系統會使用名稱做為鍵,取得每個素材資源的資料。
放送自訂原生廣告格式
自訂原生廣告格式與系統定義的格式不同,發布商可自行定義廣告素材資源清單。因此,顯示自訂原生廣告的程序與系統定義格式的程序有幾項不同之處:
- 由於
GADCustomNativeAd
旨在處理您建立的任何自訂原生廣告格式,因此沒有具名資產存取子。而是提供imageForKey:
和stringForKey:
等方法,並以欄位名稱做為引數。 - 沒有專用的廣告檢視類別 (例如
GADNativeAdView
) 可搭配GADCustomNativeAd
使用。您可以自由使用任何適合使用者體驗的檢視畫面。 - 由於沒有專屬的廣告檢視類別,因此您不需要註冊任何用於顯示廣告素材資源的檢視畫面。
以下是可顯示簡單自訂原生廣告的廣告檢視區塊範例:
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 (節錄)
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"]; } ...
顯示 AdChoices 圖示
為支援《數位服務法》(DSA),在歐洲經濟區放送的預訂廣告必須包含 AdChoices 圖示和Google 的「關於這則廣告」頁面連結。導入自訂原生廣告時,您必須顯示 AdChoices 圖示。顯示主要廣告素材資源時,請務必遵循相關步驟來顯示及設定 AdChoices 圖示的點擊事件監聽器。
下列範例會算繪 AdChoices 圖示,並設定適當的點擊行為。
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 回報點擊事件。
記錄曝光次數
如要記錄自訂原生廣告的曝光次數,只要在對應的 GADCustomNativeAd
呼叫 recordImpression
方法即可:
Swift
myCustomNativeAd.recordImpression()
Objective-C
[myCustomNativeAd recordImpression];
如果應用程式不慎對同一個廣告多次呼叫方法,SDK 會防止系統針對單一請求記錄重複的曝光次數。
回報點擊
如要向 SDK 回報素材資源檢視畫面有點擊事件,請在對應的 GADCustomNativeAd
呼叫 performClickOnAssetWithKey:
方法,並傳遞受點擊的素材資源名稱。舉例來說,如果您在名為「MainImage」的自訂格式中有素材資源,且想回報與該素材資源相應的檢視畫面點擊次數,程式碼會如下所示:
Swift
myCustomNativeAd.performClickOnAsset(withKey: "MainImage")
Objective-C
[myCustomNativeAd performClickOnAssetWithKey:@"MainImage"];
請注意,您不需要針對與廣告相關聯的每個素材資源檢視畫面呼叫這個方法。舉例來說,如果您有另一個名為「說明」的素材資源,該素材資源應會顯示,但使用者不會點選或輕觸,則應用程式不需要為該檢視區塊呼叫 performClickOnAssetWithKey:
。
回應自訂點擊動作
GADCustomNativeAd
具有類型為 GADNativeAdCustomClickHandler
的屬性 customClickHandler
Swift
typealias NativeAdCustomClickHandler = (assetID: String) -> Void
Objective-C
typedef void (^GADNativeAdCustomClickHandler)(NSString *assetID);
這是接受 assetID
做為輸入參數的區塊 (Objective-C) / 閉包 (Swift),用來識別點選的資產。
如果自訂原生廣告發生點擊,SDK 可能會依下列順序嘗試回應:
- 在 Objective-C 中叫用
customClickHandler
區塊,或在 Swift 中叫用閉包 (如有設定)。 - 逐一檢查廣告的深層連結網址,並開啟第一個找到相符應用程式的網址。
- 開啟瀏覽器,前往廣告的傳統到達網頁網址。
customClickHandler
屬性接受 Objective-C 中的區塊和 Swift 中的閉包。如果設定封鎖或關閉,SDK 會執行該動作,不會採取進一步行動。不過,如果您設定空值,SDK 會改用向廣告註冊的深層連結和/或到達網址。
自訂點擊事件處理常式可讓應用程式自行決定點擊事件的最佳回應方式,無論是更新 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 廣告單元 ID:
/21775744923/example/native
這個範例已設定為放送應用程式安裝和內容廣告, 以及包含下列素材資源的原生自訂廣告格式:
- 廣告標題 (文字)
- MainImage (圖片)
- 說明文字 (文字)
原生候補廣告
如要測試原生候補廣告的行為,請使用這個 Ad Manager 廣告單元:
/21775744923/example/native-backfill
系統會放送包含 AdChoices 重疊廣告的應用程式安裝和內容廣告範例。
請務必先更新程式碼,參照實際的廣告單元和格式 ID,再正式發布!