原生廣告提供許多進階功能,可供您進行額外的自訂作業,盡可能提供最佳廣告體驗。本指南說明如何使用原生廣告的進階功能。
必要條件
- 整合原生廣告格式。
素材資源控制選項
設定原生廣告素材控制選項。
首選媒體長寬比控制選項
媒體長寬比控制選項可讓您指定廣告素材的偏好長寬比。
使用 GADMediaAspectRatio
設定 GADNativeAdMediaAdLoaderOptions mediaAspectRatio
。
如果未設定,傳回的廣告可以有任何媒體顯示比例。
設定後,您就能指定偏好的長寬比類型,進而提升使用者體驗。
以下範例會指示 SDK 優先傳回特定顯示比例的圖片或影片。
GADNativeAdMediaAdLoaderOptions *nativeOption = [[GADNativeAdMediaAdLoaderOptions alloc] init];
nativeOption.mediaAspectRatio = GADMediaAspectRatioAny;
GADAdLoader* adLoader = [[GADAdLoader alloc] initWithAdUnitID:@"/21775744923/example/native"
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:@"/21775744923/example/native"
rootViewController:self
adTypes:@[ GADAdLoaderAdTypeNative ]
options:@[ nativeOption ]];
圖片有效負載控制項預設為停用。
停用後,應用程式會指示 SDK 僅提供含有系列的第一張圖片。
啟用後,應用程式會指出已準備好顯示任何有多張圖片的素材資源。
下列範例會指示 SDK 傳回多個圖片資產。
GADNativeAdImageAdLoaderOptions *nativeOption = [[GADNativeAdImageAdLoaderOptions alloc] init];
nativeOption.shouldRequestMultipleImages = YES;
GADAdLoader* adLoader = [[GADAdLoader alloc] initWithAdUnitID:@"/21775744923/example/native"
rootViewController:self
adTypes:@[ GADAdLoaderAdTypeNative ]
options:@[ nativeOption ]];
AdChoices 刊登位置
自訂 AdChoices 圖示顯示方式。
AdChoices 位置控制選項
您可以透過「AdChoices」AdChoices位置控制選項,選擇要在哪個角落顯示 AdChoices 圖示。
設定 GADNativeAdViewAdOptions preferredAdChoicesPosition
,並提供 GADAdChoicesPosition
值。
如果未設定,AdChoices 圖示位置會設為右上角。
如果已設定,AdChoices 會放置在要求的自訂位置。
以下範例說明如何設定自訂 AdChoices 圖片位置。
GADNativeAdViewAdOptions *nativeOptions = [[GADNativeAdViewAdOptions alloc] init];
nativeOptions.preferredAdChoicesPosition = GADAdChoicesPositionTopLeftCorner;
GADAdLoader* adLoader = [[GADAdLoader alloc] initWithAdUnitID:@"/21775744923/example/native"
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:@"/21775744923/example/native"
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:@"/21775744923/example/native"
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
}
顯示自訂影片控制項
請按照下列最佳做法顯示自訂影片控制項:
- 將自訂控制項檢視區塊顯示為原生廣告檢視區塊的子項。採用這種做法後,開放式評估可視度計算會將自訂控制項視為友善的阻礙。
- 避免在整個媒體檢視區塊上算繪不可見的疊加層。重疊內容會阻擋媒體檢視畫面上的點擊,對原生廣告成效造成負面影響。請改為建立小型疊加層,大小只要足以容納控制項即可。
自訂點擊手勢
自訂點擊手勢是原生廣告功能,可將廣告瀏覽畫面上的滑動動作註冊為廣告點擊。這項功能適用於使用滑動手勢導覽內容的應用程式。本指南說明如何為原生廣告啟用自訂點擊手勢。
使用所選滑動方向初始化 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:@"/21775744923/example/native"
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 才能算繪,就不會回應自訂點擊方向設定。