自訂廣告格式
自訂原生廣告格式就像系統定義的對應項目一樣,系統會使用 GADAdLoader
物件載入自訂原生廣告格式。在初始化 GADAdLoader
時在 adTypes
陣列中加入 GADAdLoaderAdTypeCustomNative
常數,會設為在載入廣告時要求自訂原生格式。
GADCustomNativeAdLoaderDelegate
載入自訂格式的通訊協定有兩種方法。GADAdLoader
會使用第一個參數來找出應要求的格式 ID:
Swift
public func customNativeAdFormatIDs(for adLoader: GADAdLoader) -> [Any]
Objective-C
- (NSArray *)customNativeAdFormatIDsForAdLoader:(GADAdLoader *)adLoader;
每個自訂原生廣告格式都有對應的格式 ID,可供識別。呼叫此方法時,您的應用程式應傳回陣列,其中包含準備好顯示廣告的格式 ID。
載入自訂原生廣告時,系統會傳送第二則訊息,類似於系統定義格式的廣告格式:
Swift
public func adLoader(_ adLoader: GADAdLoader, didReceive customNativeAd: GADCustomNativeAd)
Objective-C
- (void)adLoader:(GADAdLoader *)adLoader didReceiveCustomNativeAd:(GADCustomNativeAd *)customNativeAd;
格式 ID
您可以在 Ad Manager 使用者介面中「廣告放送」下拉式選單的「原生」部分,找到用於專門參照自訂原生廣告格式的格式 ID:
每個自訂原生廣告的格式 ID 都會顯示在名稱旁邊。 按一下其中一個名稱,即可前往詳細資料畫面,其中顯示格式欄位的相關資訊:
您可以在這裡新增、編輯及移除個別欄位。記下每個資產的名稱。名稱是在顯示自訂原生廣告格式時,用來取得每個素材資源資料的鍵。
顯示自訂原生廣告格式
自訂原生廣告格式與系統定義的廣告格式不同,發布商有權定義自己的廣告素材資源清單。因此,顯示自訂原生廣告的程序和系統定義格式的顯示程序略有不同:
GADCustomNativeAd
適合用來處理您建立的任何自訂原生廣告格式,因此沒有已命名的素材資源存取工具。而是提供imageForKey:
和stringForKey:
等方法,將欄位名稱做為引數。- 目前沒有可與
GADCustomNativeAd
搭配使用的專屬廣告檢視畫面類別 (例如GADNativeAdView
)。您可以自由使用任何適合使用者體驗的檢視畫面。 - 由於沒有專屬廣告瀏覽類別,因此您不必註冊任何用來顯示廣告素材資源的檢視畫面。
以下是可顯示簡易自訂原生廣告的廣告檢視畫面範例:
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: GADCustomNativeAd) { ... } }
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: GADCustomNativeAd) { 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"]; } ...
自訂原生廣告格式的原生影片
建立自訂格式時,您可以選擇將該格式用於影片。
在應用程式實作中,您可以使用 GADCustomNativeAd.mediaView
屬性取得影片檢視畫面。然後將這個檢視區塊加入檢視區塊階層。如果廣告沒有影片內容,請制定替代計畫,以便在沒有影片的情況下放送廣告。
以下範例會檢查廣告是否包含影片內容,並在沒有影片時,顯示原位圖片:
Swift
... /// Populates the ad view with the custom native ad object. func populate(withCustomNativeAd customNativeAd: GADCustomNativeAd) { 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]]; [self.mainPlaceholder addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[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"];
請注意,您不需要針對每個與廣告相關的素材資源檢視畫面呼叫這個方法。如果有另一個名為「Caption」的素材資源 (例如會顯示但未點選或輕觸的素材資源),應用程式就不需要針對該檢視畫面呼叫 performClickOnAssetWithKey:
。
回應自訂點擊動作
GADCustomNativeAd
具有類型為 GADNativeAdCustomClickHandler
的屬性 customClickHandler
Swift
typealias GADNativeAdCustomClickHandler = (assetID: String) -> Void
Objective-C
typedef void (^GADNativeAdCustomClickHandler)(NSString *assetID);
這是一個區塊 (Objective-C) / 閉包 (Swift),可接受 assetID
做為輸入參數,用來識別使用者點選的資產。
點擊自訂原生廣告時,SDK 會有三種可能的回應,依序嘗試:
- 在 Objective-C 中叫用
customClickHandler
區塊,或在 Swift 中叫用該區塊 (如果已設定)。 - 迴圈廣告的深層連結網址,並開啟可找到相符應用程式的第一個網址。
- 開啟瀏覽器,前往廣告的傳統到達網頁網址。
customClickHandler
屬性接受 Objective-C 中的區塊,並在 Swift 中封閉。如果您設定了封鎖或關閉,SDK 就會執行該區塊,且不會採取進一步動作。不過,如果您設定了 nil 值,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:
/6499/example/native
這套工具已設為放送範例應用程式安裝和內容廣告,以及含有以下素材資源的自訂原生廣告格式:
- 標題 (文字)
- MainImage (圖片)
- 說明文字 (文字)
原生候補廣告
如要測試原生候補廣告的行為,請使用這個Ad Manager 廣告單元:
/6499/example/native-backfill
這個 SDK 會放送包含 AdChoices 重疊廣告的範例應用程式安裝廣告和內容廣告。
別忘了,在上線前將程式碼更新為參照實際廣告單元和格式 ID!