原生廣告有許多進階功能,可供您進行額外的自訂作業,打造最佳廣告體驗。本指南說明如何使用原生廣告的進階功能。
必備條件
- 整合原生廣告格式。
素材資源控制項
設定原生廣告素材資源控制項。
偏好的媒體顯示比例控制項
媒體顯示比例控制項,可讓您指定偏好的廣告素材顯示比例。
使用 GADMediaAspectRatio
設定 GADNativeAdMediaAdLoaderOptions mediaAspectRatio
。
如果未設定,傳回的廣告媒體顯示比例可能為任意數值。
設定後,您就能指定偏好的顯示比例,提升使用者體驗。
以下範例會指示 SDK 優先傳回特定顯示比例的圖片或影片。
GADNativeAdMediaAdLoaderOptions *nativeOption = [[GADNativeAdMediaAdLoaderOptions alloc] init];
nativeOption.mediaAspectRatio = GADMediaAspectRatioAny;
GADAdLoader* adLoader = [[GADAdLoader alloc] initWithAdUnitID:@"ca-app-pub-3940256099942544/3986624511"
rootViewController:self
adTypes:@[ GADAdLoaderAdTypeNative ]
options:@[ nativeOption ]];
圖片下載控制項
圖片下載控制項可讓您決定,要 SDK 傳回圖片素材資源或只傳回 URI。
將 GADNativeAdImageAdLoaderOptions disableImageLoading
設為 BOOL
值。
圖片下載控制項預設為停用。
停用時,Google Mobile Ads SDK 會為您填入圖片和 URI。
啟用時,SDK 只會填入 URI,您可自行決定是否要下載實際圖片。
以下範例會指示 SDK 只傳回 URI。
GADNativeAdImageAdLoaderOptions *nativeOption = [[GADNativeAdImageAdLoaderOptions alloc] init];
nativeOption.disableImageLoading = YES;
GADAdLoader* adLoader = [[GADAdLoader alloc] initWithAdUnitID:@"ca-app-pub-3940256099942544/3986624511"
rootViewController:self
adTypes:@[ GADAdLoaderAdTypeNative ]
options:@[ nativeOption ]];
圖片酬載控制項預設為停用。
停用時,應用程式會指示 SDK 對含有系列圖片的素材資源,僅提供第一張圖片。
啟用時,應用程式會表示已準備好,為含有多張圖片的素材資源顯示所有圖片。
以下範例會指示 SDK 傳回多個圖片素材資源。
GADNativeAdImageAdLoaderOptions *nativeOption = [[GADNativeAdImageAdLoaderOptions alloc] init];
nativeOption.shouldRequestMultipleImages = YES;
GADAdLoader* adLoader = [[GADAdLoader alloc] initWithAdUnitID:@"ca-app-pub-3940256099942544/3986624511"
rootViewController:self
adTypes:@[ GADAdLoaderAdTypeNative ]
options:@[ nativeOption ]];
AdChoices 刊登位置
自訂 AdChoices 圖示顯示方式。
AdChoices 位置控制項
您可以透過 AdChoices 位置控制項,選擇要在哪個角落算繪 AdChoices 圖示。
將 GADNativeAdViewAdOptions preferredAdChoicesPosition
設為 GADAdChoicesPosition
值。
如未設定,AdChoices 圖示位置會設為右上角。
如已設定,AdChoices 會放置在您自訂的位置。
以下範例說明如何自訂 AdChoices 圖片位置。
GADNativeAdViewAdOptions *nativeOptions = [[GADNativeAdViewAdOptions alloc] init];
nativeOptions.preferredAdChoicesPosition = GADAdChoicesPositionTopLeftCorner;
GADAdLoader* adLoader = [[GADAdLoader alloc] initWithAdUnitID:@"ca-app-pub-3940256099942544/3986624511"
rootViewController:self
adTypes:@[ GADAdLoaderAdTypeNative ]
options:@[ nativeOptions ]];
AdChoices 自訂檢視區塊
AdChoices 自訂檢視區塊功能可讓您自訂 AdChoices 圖示位置。這與 AdChoices 位置控制項不同,此控制項只允許指定放在畫面的四個角落之一。
在算繪前,使用 GADAdChoicesView
設定 GADNativeAd.adChoicesView
屬性,AdChoices 內容就會在 GADAdChoicesView
內算繪。
以下範例說明如何自訂 AdChoices 檢視區塊。AdChoices 圖示會在 GADAdChoicesView
內算繪。
Objective-C
- (void)adLoader:(GADAdLoader *)adLoader didReceiveNativeAd:(GADNativeAd *)nativeAd {
...
GADAdChoicesView *customAdChoicesView =
[[GADAdChoicesView alloc] initWithFrame: CGRectMake(..., ..., ..., ...)];
[nativeAdView addSubview:customAdChoicesView];
nativeAdView.adChoicesView = customAdChoicesView;
// 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;
}
Swift
func adLoader(_ adLoader: AdLoader, didReceive nativeAd: NativeAd) {
refreshAdButton.isEnabled = true
...
// Define a custom position for the AdChoices icon.
let customRect = CGRect(x: 100, y: 100, width: 15, height: 15)
let customAdChoicesView = AdChoicesView(frame: customRect)
nativeAdView.addSubview(customAdChoicesView)
nativeAdView.adChoicesView = customAdChoicesView
// 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;
}
影片控制項
設定影片素材資源的行為。
開始靜音行為
您可以透過「開始靜音行為」,停用或啟用影片的起始音訊。
將 GADVideoOptions startMuted
設為 BOOL
值。
系統會預設啟用開始靜音行為。
停用時,應用程式會要求影片在開始時播放音訊。
啟用時,應用程式會要求影片開始播放時靜音。
以下範例說明如何在影片開始時播放音訊。
GADVideoOptions *nativeOptions = [[GADVideoOptions alloc] init];
nativeOptions.startMuted = NO;
GADAdLoader* adLoader = [[GADAdLoader alloc] initWithAdUnitID:@"ca-app-pub-3940256099942544/3986624511"
rootViewController:self
adTypes:@[ GADAdLoaderAdTypeNative ]
options:@[ nativeOptions ]];
自訂播放控制項
您可以要求自訂影片輸入控制項,播放、暫停或將影片設為靜音。
將 GADVideoOptions customControlsRequested
設為 BOOL
值。
自訂播放控制項預設為停用。
停用時,影片會顯示 SDK 算繪輸入控制項。
- 啟用時,您可以使用
GADVideoController play
、GADVideoController pause
和GADVideoController setMute
控制影片廣告。
如果廣告含有影片內容且已啟用自訂控制項,您應連同廣告顯示自訂控制項,因為廣告本身不會顯示任何控制項。然後,控制項可以呼叫以下項目的相關方法:
以下範例說明如何要求含有自訂播放控制項的影片。
GADVideoOptions *nativeOptions = [[GADVideoOptions alloc] init];
nativeOptions.customControlsRequested = YES;
GADAdLoader* adLoader = [[GADAdLoader alloc] initWithAdUnitID:@"ca-app-pub-3940256099942544/3986624511"
rootViewController:self
adTypes:@[ GADAdLoaderAdTypeNative ]
options:@[ nativeOptions ]];
檢查是否已啟用自訂控制項
由於在請求時,系統無法判斷傳回的廣告是否允許自訂影片控制項,因此您必須檢查是否已啟用自訂控制項。
Objective-C
- (void)adLoader:(GADAdLoader *)adLoader
didReceiveNativeAd:(GADNativeAd*)nativeAd {
GADVideoController *videoController = nativeAd.mediaContent.videoController;
BOOL canShowCustomControls = videoController.customControlsEnabled;
}
Swift
func adLoader(_ adLoader: AdLoader, didReceive nativeAd: NativeAd) {
let videoController = nativeAd.mediaContent.videoController
let canShowCustomControls = videoController?.customControlsEnabled() == true
}
算繪自訂影片控制項
請按照下列最佳做法,算繪自訂影片控制項:
- 將自訂控制項檢視區塊,算繪為原生廣告檢視區塊的子項。採用這種做法後,Open Measurement 可視度計算會將自訂控制項視為友善遮擋物。
- 避免在整個媒體檢視區塊上算繪不可見的疊加元素。疊加元素會阻擋媒體檢視區塊上的點擊,對原生廣告成效造成負面影響。請改為建立小型疊加元素,大小只要足以容納控制項即可。
自訂點擊手勢
自訂點擊手勢是原生廣告功能,可將廣告瀏覽畫面上的滑動動作註冊為廣告點擊。這項功能適用於使用滑動手勢導覽內容的應用程式。本指南說明如何為原生廣告啟用自訂點擊手勢。
使用所選滑動方向,初始化 GADNativeAdCustomClickGestureOptions
執行個體。此外,您也需要指出是否要將輕觸視為點擊。
自訂點擊手勢預設為停用。
停用時,只有輕觸會計為點擊。
啟用時,系統會將滑動手勢計為點擊,您也可以指定輕觸是否仍要計為點擊。
以下範例說明如何導入向右滑動的自訂滑動手勢,並保留一般輕觸行為。
GADNativeAdCustomClickGestureOptions *swipeGestureOptions = [[GADNativeAdCustomClickGestureOptions alloc]
initWithSwipeGestureDirection:UISwipeGestureRecognizerDirectionRight
tapsAllowed:YES];
// The following sample ad unit ID has been enabled for custom click gestures
// and can be used for testing.
self.adLoader = [[GADAdLoader alloc]
initWithAdUnitID:@"ca-app-pub-3940256099942544/3986624511"
rootViewController:self
adTypes:@[ GADAdLoaderAdTypeNative ]
options:@[ swipeGestureOptions ]];
監聽滑動手勢事件
系統記錄到滑動手勢點擊時,Google Mobile Ads SDK 會在 GADNativeAdDelegate
上叫用 nativeAdDidRecordSwipeGestureClick:
委派方法,以及現有的 nativeAdDidRecordClick:
委派方法。
#pragma mark - GADNativeAdDelegate implementation
// Called when a swipe gesture click is recorded.
- (void)nativeAdDidRecordSwipeGestureClick:(GADNativeAd *)nativeAd {
NSLog(@"A swipe gesture click has occurred.");
}
// Called when a swipe gesture click or a tap click is recorded, as configured in
// GADNativeAdCustomClickGestureOptions.
- (void)nativeAdDidRecordClick:(GADNativeAd *)nativeAd {
NSLog(@"A swipe gesture click or tap click has occurred.");
}
中介服務
自訂點擊手勢僅適用於 Google Mobile Ads SDK 算繪的原生廣告。如果廣告來源需要第三方 SDK 才能算繪,就不會回應自訂點擊方向設定。