原生廣告選項

選取平台: Android iOS

原生廣告有許多進階功能,可供您進行額外的自訂作業,打造最佳廣告體驗。本指南說明如何使用原生廣告的進階功能。

必要條件

素材資源控制選項

本節將詳細說明如何自訂原生廣告中的廣告素材資源。您可以選擇指定媒體素材資源的偏好顯示比例,以及圖片素材資源的下載和顯示方式。

偏好的媒體顯示比例控制項

媒體顯示比例控制項,可讓您指定偏好的廣告素材顯示比例。

使用 GADMediaAspectRatio 設定 GADNativeAdMediaAdLoaderOptions mediaAspectRatio

  • 如果未設定,傳回的廣告媒體顯示比例可能為任意數值。

  • 設定後,您就能指定偏好的顯示比例,提升使用者體驗。

以下範例會指示 SDK 優先傳回特定顯示比例的圖片或影片。

Swift

let nativeOptions = NativeAdMediaAdLoaderOptions()
nativeOptions.mediaAspectRatio = .any

adLoader = AdLoader(
  adUnitID: "nativeAdUnitID",
  rootViewController: self,
  adTypes: [.native],
  options: [nativeOptions])

Objective-C

GADNativeAdMediaAdLoaderOptions *nativeOptions = [[GADNativeAdMediaAdLoaderOptions alloc] init];
nativeOptions.mediaAspectRatio = GADMediaAspectRatioAny;

self.adLoader = [[GADAdLoader alloc] initWithAdUnitID:"nativeAdUnitID"
                                   rootViewController:self
                                              adTypes:@[ GADAdLoaderAdTypeNative ]
                                              options:@[ nativeOptions ]];

nativeAdUnitID 替換為廣告單元 ID。

圖片下載控制項

圖片下載控制項可讓您決定,要 SDK 傳回圖片素材資源或只傳回 URI。

GADNativeAdImageAdLoaderOptions disableImageLoading 設為 BOOL 值。

  • 圖片下載控制項預設為停用。

  • 停用時,Google Mobile Ads SDK 會為您填入圖片和 URI。

  • 啟用時,SDK 只會填入 URI,您可自行決定是否要下載實際圖片。

以下範例會指示 SDK 只傳回 URI。

Swift

let nativeOptions = NativeAdImageAdLoaderOptions()
nativeOptions.isImageLoadingDisabled = true

adLoader = AdLoader(
  adUnitID: "nativeAdUnitID",
  rootViewController: self,
  adTypes: [.native],
  options: [nativeOptions])

Objective-C

GADNativeAdImageAdLoaderOptions *nativeOptions = [[GADNativeAdImageAdLoaderOptions alloc] init];
nativeOptions.disableImageLoading = YES;

self.adLoader = [[GADAdLoader alloc] initWithAdUnitID:"nativeAdUnitID"
                                   rootViewController:self
                                              adTypes:@[ GADAdLoaderAdTypeNative ]
                                              options:@[ nativeOptions ]];

圖片酬載控制項

部分廣告會顯示一系列圖片,而不只是一張。使用這項功能,指出應用程式是否已準備好顯示所有圖片,或只顯示一張圖片。

  • 圖片酬載控制項預設為停用。

  • 停用時,應用程式會指示 SDK 對含有系列圖片的素材資源,僅提供第一張圖片。

  • 啟用時,應用程式會表示已準備好,為含有多張圖片的素材資源顯示所有圖片。

以下範例會指示 SDK 傳回多個圖片素材資源。

Swift

let nativeOptions = NativeAdImageAdLoaderOptions()
nativeOptions.shouldRequestMultipleImages = true

adLoader = AdLoader(
  adUnitID: "nativeAdUnitID",
  rootViewController: self,
  adTypes: [.native],
  options: [nativeOptions])

Objective-C

GADNativeAdImageAdLoaderOptions *nativeOptions = [[GADNativeAdImageAdLoaderOptions alloc] init];
nativeOptions.shouldRequestMultipleImages = YES;

self.adLoader = [[GADAdLoader alloc] initWithAdUnitID:"nativeAdUnitID"
                                   rootViewController:self
                                              adTypes:@[ GADAdLoaderAdTypeNative ]
                                              options:@[ nativeOptions ]];

AdChoices 刊登位置

本節將詳細說明如何放置 AdChoices 疊加層。您可以選擇將其放置在四個角落之一,或在自訂檢視區塊中算繪。

AdChoices 位置控制項

您可以透過 AdChoices 位置控制項,選擇要在哪個角落算繪 AdChoices 圖示。

GADNativeAdViewAdOptions preferredAdChoicesPosition 設為 GADAdChoicesPosition 值。

  • 如未設定,AdChoices 圖示位置會設為右上角。

  • 如已設定,AdChoices 會放置在您自訂的位置。

以下範例說明如何自訂 AdChoices 圖片位置。

Swift

let nativeOptions = NativeAdViewAdOptions()
nativeOptions.preferredAdChoicesPosition = .topRightCorner

adLoader = AdLoader(
  adUnitID: "nativeAdUnitID",
  rootViewController: self,
  adTypes: [.native],
  options: [nativeOptions])

Objective-C

GADNativeAdViewAdOptions *nativeOptions = [[GADNativeAdViewAdOptions alloc] init];
nativeOptions.preferredAdChoicesPosition = GADAdChoicesPositionTopRightCorner;

self.adLoader = [[GADAdLoader alloc] initWithAdUnitID:"nativeAdUnitID"
                                   rootViewController:self
                                              adTypes:@[ GADAdLoaderAdTypeNative ]
                                              options:@[ nativeOptions ]];

AdChoices 自訂檢視區塊

AdChoices 自訂檢視區塊功能可讓您自訂 AdChoices 圖示位置。這與 AdChoices 位置控制項不同,此控制項只允許指定放在畫面的四個角落之一。

在算繪前,使用 GADAdChoicesView 設定 GADNativeAd.adChoicesView 屬性,AdChoices 內容就會在 GADAdChoicesView 內算繪。

以下範例說明如何自訂 AdChoices 檢視區塊。AdChoices 圖示會在 GADAdChoicesView 內算繪:

Swift

private func createAdChoicesView(nativeAdView: NativeAdView) {
  // 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
}

Objective-C

- (void)createAdChoicesViewWithNativeAdView:(GADNativeAdView *)nativeAdView {
  // Define a custom position for the AdChoices icon.
  CGRect customRect = CGRectMake(100, 100, 15, 15);
  GADAdChoicesView *customAdChoicesView = [[GADAdChoicesView alloc] initWithFrame:customRect];
  [nativeAdView addSubview:customAdChoicesView];
  nativeAdView.adChoicesView = customAdChoicesView;
}

影片控制選項

本節將詳細說明如何自訂影片廣告的播放體驗。您可以選擇設定初始靜音狀態,並實作自訂播放控制項。

初始靜音行為

您可以透過「開始靜音行為」,停用或啟用影片的起始音訊。

GADVideoOptions startMuted 設為 BOOL 值。

  • 系統會預設啟用開始靜音行為。

  • 停用時,應用程式會要求影片在開始時播放音訊。

  • 啟用時,應用程式會要求影片開始播放時靜音。

以下範例說明如何在影片開始時播放音訊。

Swift

let videoOptions = VideoOptions()
videoOptions.shouldStartMuted = false

adLoader = AdLoader(
  adUnitID: "nativeAdUnitID",
  rootViewController: self,
  adTypes: [.native],
  options: [videoOptions])

Objective-C

GADVideoOptions *videoOptions = [[GADVideoOptions alloc] init];
videoOptions.startMuted = NO;

self.adLoader = [[GADAdLoader alloc] initWithAdUnitID:"nativeAdUnitID"
                                   rootViewController:self
                                              adTypes:@[ GADAdLoaderAdTypeNative ]
                                              options:@[ videoOptions ]];

自訂播放控制項

您可以要求自訂影片輸入控制項,播放、暫停或將影片設為靜音。

GADVideoOptions customControlsRequested 設為 BOOL 值。

  • 自訂播放控制項預設為停用。

  • 停用時,影片會顯示 SDK 算繪輸入控制項。

如果廣告含有影片內容且已啟用自訂控制項,您應連同廣告顯示自訂控制項,因為廣告本身不會顯示任何控制項。然後,控制項可以呼叫以下項目的相關方法:

GADVideoController

以下範例說明如何要求含有自訂播放控制項的影片。

Swift

let videoOptions = VideoOptions()
videoOptions.areCustomControlsRequested = true

adLoader = AdLoader(
  adUnitID: "nativeAdUnitID",
  rootViewController: self,
  adTypes: [.native],
  options: [videoOptions])

Objective-C

GADVideoOptions *videoOptions = [[GADVideoOptions alloc] init];
videoOptions.customControlsRequested = YES;

self.adLoader = [[GADAdLoader alloc] initWithAdUnitID:"nativeAdUnitID"
                                   rootViewController:self
                                              adTypes:@[ GADAdLoaderAdTypeNative ]
                                              options:@[ videoOptions ]];

檢查是否已啟用自訂控制項

由於在請求時,系統無法判斷傳回的廣告是否允許自訂影片控制項,因此您必須檢查是否已啟用自訂控制項。

Swift

private func checkCustomControlsEnabled(nativeAd: NativeAd) -> Bool {
  let videoController = nativeAd.mediaContent.videoController
  return videoController.areCustomControlsEnabled
}

Objective-C

- (BOOL)checkCustomControlsEnabledWithNativeAd:(GADNativeAd *)nativeAd {
  GADVideoController *videoController = nativeAd.mediaContent.videoController;
  return videoController.customControlsEnabled;
}

算繪自訂影片控制項

請按照下列最佳做法,算繪自訂影片控制項:

  1. 將自訂控制項檢視區塊,算繪為原生廣告檢視區塊的子項。採用這種做法後,Open Measurement 可視度計算會將自訂控制項視為友善遮擋物。
  2. 避免在整個媒體檢視區塊上算繪不可見的疊加元素。疊加元素會阻擋媒體檢視區塊上的點擊,對原生廣告成效造成負面影響。請改為建立小型疊加元素,大小只要足以容納控制項即可。

自訂點擊手勢

自訂點擊手勢是原生廣告功能,可將廣告瀏覽畫面上的滑動動作註冊為廣告點擊。這項功能適用於使用滑動手勢導覽內容的應用程式。本指南說明如何為原生廣告啟用自訂點擊手勢。

使用所選滑動方向,初始化 GADNativeAdCustomClickGestureOptions 執行個體。此外,您也需要指出是否要將輕觸視為點擊。

  • 自訂點擊手勢預設為停用。

  • 停用時,只有輕觸會計為點擊。

  • 啟用時,系統會將滑動手勢計為點擊,您也可以指定輕觸是否仍要計為點擊。

以下範例說明如何導入向右滑動的自訂滑動手勢,並保留一般輕觸行為。

Swift

let swipeGestureOptions = NativeAdCustomClickGestureOptions(
  swipeGestureDirection: .right,
  tapsAllowed: true)

adLoader = AdLoader(
  adUnitID: "nativeAdUnitID",
  rootViewController: self,
  adTypes: [.native],
  options: [swipeGestureOptions])

Objective-C

GADNativeAdCustomClickGestureOptions *swipeGestureOptions =
    [[GADNativeAdCustomClickGestureOptions alloc]
        initWithSwipeGestureDirection:UISwipeGestureRecognizerDirectionRight
                          tapsAllowed:YES];

self.adLoader = [[GADAdLoader alloc] initWithAdUnitID:"nativeAdUnitID"
                                   rootViewController:self
                                              adTypes:@[ GADAdLoaderAdTypeNative ]
                                              options:@[ swipeGestureOptions ]];

監聽滑動手勢事件

系統記錄到滑動手勢點擊時,Google Mobile Ads SDK 會在 GADNativeAdDelegate 上叫用 nativeAdDidRecordSwipeGestureClick: 委派方法,以及現有的 nativeAdDidRecordClick: 委派方法。

Swift

// Called when a swipe gesture click is recorded, as configured in
// NativeAdCustomClickGestureOptions.
func nativeAdDidRecordSwipeGestureClick(_ nativeAd: NativeAd) {
  print("A swipe gesture click has occurred.")
}

// Called when a swipe gesture click or a tap click is recorded.
func nativeAdDidRecordClick(_ nativeAd: NativeAd) {
  print("A swipe gesture click or tap click has occurred.")
}

Objective-C

// Called when a swipe gesture click is recorded, as configured in
// GADNativeAdCustomClickGestureOptions.
- (void)nativeAdDidRecordSwipeGestureClick:(GADNativeAd *)nativeAd {
  NSLog(@"A swipe gesture click has occurred.");
}

// Called when a swipe gesture click or a tap click is recorded.
- (void)nativeAdDidRecordClick:(GADNativeAd *)nativeAd {
  NSLog(@"A swipe gesture click or tap click has occurred.");
}

中介服務

自訂點擊手勢僅適用於 Google Mobile Ads SDK 算繪的原生廣告。如果廣告來源需要第三方 SDK 才能算繪,就不會回應自訂點擊方向設定。