原生廣告具備多項進階功能,讓您能進一步自訂,打造最佳廣告體驗。本指南將說明如何運用這些進階功能。
前置作業
- 整合原生廣告格式。
素材資源控制選項
設定原生廣告素材資源的控制選項。
偏好的媒體顯示比例控制選項
「媒體顯示比例控制選項」可用於指定偏好的廣告素材顯示比例。
使用 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。
使用 BOOL
值設定 GADNativeAdImageAdLoaderOptions disableImageLoading
。
圖片下載控制選項預設為停用。
停用時,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 圖示。
使用 GADAdChoicesPosition
值設定 GADNativeAdViewAdOptions preferredAdChoicesPosition
。
如未設定,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 定位控制選項,就只能從四個角落擇一顯示圖示。
在顯示廣告之前,請先將 GADNativeAd.adChoicesView
屬性設為 GADAdChoicesView
,這樣 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;
}
影片控制選項
設定影片素材資源的播放行為。
初始靜音行為
初始靜音行為可供指定影片一開始是否有聲音。
使用 BOOL
值設定 GADVideoOptions startMuted
。
根據預設,影片一開始為靜音播放。
停用時,應用程式會要求一開始就播放影片聲音。
啟用後,應用程式會要求影片以靜音狀態開始播放。
以下示範如何讓影片從一開始就播放聲音。
GADVideoOptions *nativeOptions = [[GADVideoOptions alloc] init];
nativeOptions.startMuted = NO;
GADAdLoader* adLoader = [[GADAdLoader alloc] initWithAdUnitID:@"/21775744923/example/native"
rootViewController:self
adTypes:@[ GADAdLoaderAdTypeNative ]
options:@[ nativeOptions ]];
自訂播放控制項
您可以要求顯示自訂的影片輸入控制項,讓使用者播放、暫停或靜音影片。
使用 BOOL
值設定 GADVideoOptions customControlsRequested
。
自訂播放控制項預設為停用。
停用時,影片會顯示 SDK 提供的輸入控制項。
- 啟用後,您可以使用
GADVideoController play
、GADVideoController pause
和GADVideoController setMute
控制影片廣告。
如果廣告含有影片內容並啟用自訂控制項,您應該在廣告旁顯示自訂控制項,因為廣告本身不會顯示任何控制項。這些控制項可呼叫
GADVideoController
的相關方法。
以下示範如何要求影片顯示自訂播放控制項。
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
}
顯示自訂影片控制選項
請按照下列最佳做法顯示自訂影片控制選項:
- 將自訂控制項檢視區塊顯示為原生廣告檢視區塊的子項。這樣一來,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:@"/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 顯示廣告,則不支援自訂點擊手勢的設定。