這份文件已更新。
中文(香港) 的翻譯尚未完成。
英文更新時間:2023年8月8日
中文(香港) 更新時間:2020年12月7日

即時遊戲的應用程式內部廣告

您可以向玩家顯示廣告,讓遊戲成為獲利來源。目前有兩種格式的 Audience Network 廣告已經和即時遊戲整合:獎勵式影片和插頁廣告。本節的內容將會引導您進行整合廣告獲利方式與即時遊戲的流程。

格式適用情況

平台插頁廣告獎勵插頁廣告獎勵式影片

Android

iOS

行動版網頁

桌面版網頁

必要條件

  • 您已在營利管理工具中與遊戲相關聯的屬性內,建立至少一個廣告版位。
  • 您可以在行動裝置或 Facebook.com 上測試您建置的內容。

注意:Audience Network 目前在某些地區受到限制。請在 Audience Network 新手入門流程中,查看您的資格是否符合。

如需詳細資訊,請參閱 Audience Network 讓即時遊戲成為獲利來源的 5 個步驟

設定 Facebook 應用程式

下載範例

如果您已設定應用程式使用營利管理工具,並已建立廣告版位,可以下載我們的示範內容,查看下列的範例程式碼如何實際運作。

下載廣告示範版(.zip)

顯示插頁廣告

插頁廣告是應用程式在正常切換點時顯示的全螢幕廣告。插頁廣告有自動播放或點擊播放兩種格式。插頁廣告一載入後,就會開始播放自動播放的廣告;這些廣告預設可在 3 秒後跳過。廣告的時間長度上限是 2 分鐘。

下列程式碼片段示範如何顯示插頁廣告

您必須先建立廣告版位並使用插頁廣告顯示格式,這些程式碼才能發揮作用。您應針對應用程式的各個廣告單位使用不同的廣告版位。

預先載入廣告

var preloadedInterstitial = null;

FBInstant.getInterstitialAdAsync(
  '123123123123_123123123123' // Your Ad Placement Id
).then(function(interstitial) {
  // Load the Ad asynchronously
  preloadedInterstitial = interstitial;
  return preloadedInterstitial.loadAsync();
}).then(function() {
  console.log('Interstitial preloaded');
}).catch(function(err){
  console.error('Interstitial failed to preload: ' + err.message);
});

顯示廣告

preloadedInterstitial.showAsync()
.then(function() {
  // Perform post-ad success operation
  console.log('Interstitial ad finished successfully');        
})
.catch(function(e) {
  console.error(e.message);
});

顯示獎勵插頁廣告

獎勵插頁廣告是向用戶提供激勵以換取觀看廣告的全螢幕廣告。這類廣告不需要用戶選擇是否觀看。

下列程式碼片段示範如何顯示獎勵插頁廣告。

您必須先建立廣告版位並使用獎勵插頁廣告顯示格式,這些程式碼才能發揮作用。獎勵插頁廣告適用於 Facebook Gaming、行動版網頁和桌面版網頁應用程式。您應針對應用程式的各個廣告單位使用不同的廣告版位。

預先載入廣告

var preloadedRewardedInterstitial = null;

FBInstant.getRewardedInterstitialAsync(
  '123123123123_123123123123' // Your Ad Placement Id
).then(function(rewarded) {
  // Load the Ad asynchronously
  preloadedRewardedInterstitial = rewarded;
  return preloadedRewardedInterstitial.loadAsync();
}).then(function() {
  console.log('Rewarded interstitial preloaded');
}).catch(function(err){
  console.error('Rewarded interstitial failed to preload: ' + err.message);
});

顯示廣告

preloadedRewardedInterstitial.showAsync()
.then(function() {
  // Perform post-ad success operation
  console.log('Rewarded interstitial watched successfully');        
})
.catch(function(e) {
  console.error(e.message);
});

顯示獎勵式影片廣告

擁有全螢幕體驗的獎勵式影片廣告,可讓用戶自行選擇是否要透過觀看影片來取得獎勵(例如虛擬貨幣、應用程式內部商品或獨享內容)。

下列程式碼片段示範如何顯示獎勵式影片廣告

您必須先建立廣告版位並使用獎勵式影片顯示格式,這些程式碼才能發揮作用。您應針對應用程式的各個廣告單位使用不同的廣告版位。

預先載入廣告

var preloadedRewardedVideo = null;

FBInstant.getRewardedVideoAsync(
  '456456456456_456456456456' // Your Ad Placement Id
).then(function(rewarded) {
  // Load the Ad asynchronously
  preloadedRewardedVideo = rewarded;
  return preloadedRewardedVideo.loadAsync();
}).then(function() {
  console.log('Rewarded video preloaded');
}).catch(function(err){
  console.error('Rewarded video failed to preload: ' + err.message);
});

顯示廣告

preloadedRewardedVideo.showAsync()
.then(function() {
  // Perform post-ad success operation
  console.log('Rewarded video watched successfully');        
})
.catch(function(e) {
  console.error(e.message);
});

廣告實例的生命週期

預先載入三個相同類型的廣告實例而不顯示這些廣告實例,將會造成嘗試建立新廣告的動作失敗。成功載入廣告後,廣告的有效期間為 60 分鐘。

下列程式碼可協助您瞭解其運作方式,假設所有內容皆已正確設定(有效的廣告版位編號等)且沒有網路問題:

var preloadedInterstitial1 = null;
var preloadedInterstitial2 = null;
var preloadedInterstitial3 = null;
var preloadedRewardedVideo = null;

// PRELOAD PHASE
FBInstant.getInterstitialAdAsync(
  '123123123123_123123123123'
).then(function(interstitial) {
  // This should get called, meaning you now have 1 ad instance.
  preloadedInterstitial1 = interstitial;
  return preloadedInterstitial1.loadAsync();
}).then(function() {
  // This should get called unless we do not have ads to serve.
  console.log('Interstitial 1 preloaded')
}).catch(function(err){
  // This would be called only if we do not have ads to serve.
  // The error would then be ADS_NO_FILL.
  // You can try to reload the ad after some time (ideally over 30 seconds).
  // If you get ADS_NO_FILL 3 times in a row, the instance will be disabled.
  console.error('Interstitial 1 failed to preload: ' + err.message);
  // You can try to reload after 30 seconds (2nd attempt)
  setTimeout(function () { handleAdsNoFill(preloadedInterstitial1, 2); }, 30 * 1000);
});

// Here is how the function to handle ADS_NO_FILL would look like
function handleAdsNoFill (adInstance, attemptNumber) {
  if (attemptNumber > 3) {
    // You can assume we will not have to serve in the current session, no need to try
    // to load another ad.
    return;
  } else {
    adInstance.loadAsync().then(function() {
      // This should get called if we finally have ads to serve.
      console.log('Interstitial preloaded')
    }).catch(function(err){
      console.error('Interstitial failed to preload: ' + err.message);
      // You can try to reload after 30 seconds
      setTimeout(function () {
        handleAdsNoFill(adInstance, attemptNumber+1);
      }, 30 * 1000);
    });
  }
}

// As we covered ADS_NO_FILL, from now on let's assume we have ads to serve.
FBInstant.getInterstitialAdAsync(
  // You can re-use the same Ad Placement Id as long as it is of the valid type:
  // interstitial or rewarded video (here, it has to be interstitial)
  '123123123123_123123123123'
).then(function(interstitial) {
  // This should get called, meaning you now have 2 ad instances.
  preloadedInterstitial2 = interstitial;
  return preloadedInterstitial2.loadAsync();
}).then(function() {
  console.log('Interstitial 2 preloaded')
});

FBInstant.getRewardedVideoAsync(
  '456456456456_456456456456'
).then(function(rewarded) {
  // This should get called, meaning you now have 3 ad instances.
  preloadedRewardedVideo = rewarded;
  return preloadedRewardedVideo.loadAsync();
}).then(function() {
  console.log('Rewarded video preloaded')
});

FBInstant.getInterstitialAdAsync(
  '123123123123_123123123123'
).catch(function(err){
  // This should be called now because you already have 3 ad instances.
  // The error would then be ADS_TOO_MANY_INSTANCES.
  console.error('Interstitial 3 failed to preload: ' + err.message);
});

// SHOWING ADS (at a timing that makes sense in the workflow of your game)
preloadedInterstitial1.showAsync()
.then(function() {
  // This should get called, meaning you now have 2 ad instances.
  console.log('Interstitial ad 1 finished successfully');        
});

// A bit later, assuming you did not preload a new instance yet.
preloadedInterstitial2.showAsync()
.then(function() {
  // This should get called, meaning you now have 1 ad instance.
  console.log('Interstitial ad finished successfully');        
});

// A bit later, assuming you did not preload a new instance yet.
preloadedRewardedVideo.showAsync()
.then(function() {
  // This should get called, meaning you now have no ad instance.
  console.log('Rewarded video watched successfully');        
});

最佳作法與疑難排解

下列說明即時遊戲的特定資訊。請同時參閱我們的 Audience Network 洞察報告

設定適當的廣告時間長度。請確定遊戲中的廣告不會打斷玩家的遊戲體驗。

預先載入廣告。不要等到要求發出才執行動作,變成同時預載和顯示廣告,而是確定已先預載某些 AdInstance,如此當玩家要求檢視廣告時,就不用等太久。

檢查是否支援。在某些情況中,玩家的連線階段不支援廣告(例如,如果玩家是透過桌上型電腦的瀏覽器玩遊戲時)。您應該在要求顯示廣告之前,一律檢查 FBInstant.getSupportedAPIs() 是否支援廣告。

建置妥善的「無法呈現」狀況處理機制。某些狀況可能會造成廣告無法呈現。在此情況中,Promise 將會拒絕並丟出 ADS_NO_FILL 錯誤訊息。請確定您的遊戲能夠妥善地處理此狀況、顯示友善的訊息,並等待 30 秒後再要求另一個相同類型的廣告。

提交之前測試您的廣告。身為應用程式的開發人員,您甚至可以在提交應用程式接受 Audience Network 審查之前,在手機上要求顯示廣告,來確認整合是否能夠順利運作。

追蹤廣告曝光次數。您可以使用自訂事件showAsync 解析時追蹤廣告曝光次數。

請勿保留過多 AdInstancegetRewardedVideoAsyncgetInterstitialAdAsync 這兩種方法皆會傳回可預先載入的 AdInstance。預先載入 3 個(含)以上相同類型的 AdInstance 而不顯示這些 AdInstance,將會造成後續建立新 AdInstance 的動作失敗。這表示在尚未呼叫 showAsync 或玩家尚未重新啟動 Messenger 之前,呼叫 getRewardedVideoAsyncgetInterstitialAdAsync 的動作將會失敗,並丟出錯誤碼 ADS_TOO_MANY_INSTANCES