原生廣告選項

選取平台: 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。

請使用 BOOL 值設定 GADNativeAdImageAdLoaderOptions disableImageLoading

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

  • 這個控制項停用時,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 圖示。

請使用 GADAdChoicesPosition 值設定 GADNativeAdViewAdOptions preferredAdChoicesPosition

  • 如未設定這個控制項,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;
}

影片控制項

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

初始靜音行為

初始靜音行為用來指定影片一開始是否播放聲音。

請使用 BOOL 值設定 GADVideoOptions startMuted

  • 根據預設,影片一開始會靜音播放。

  • 如果停用這個控制項,應用程式會要求影片一開始就播放聲音。

  • 如果啟用這個控制項,應用程式會要求影片開始播放時應靜音。

以下示範如何使影片一開始就播放聲音。

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 ]];

自訂播放控制項

您可以要求顯示自訂的影片輸入控制項,讓使用者選擇播放/暫停影片,或是將影片靜音。

請使用 BOOL 值設定 GADVideoOptions customControlsRequested

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

  • 控制項處於停用狀態時,影片會顯示 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 SDK 計算可視度時,就會將自訂控制項視為無害的遮蔽物。
  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 會叫用 GADNativeAdDelegatenativeAdDidRecordSwipeGestureClick: 委派方法,以及原本的 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 算繪廣告的廣告來源,不會回應自訂點擊手勢的設定。