小游戏的应用内广告

您可以通过向玩家展示广告来实现游戏变现。目前,系统会将两种格式的 Audience Network 广告与小游戏进行集成,这两种广告分别为激励视频广告和插屏广告。本部分将指导您完成广告变现与小游戏的集成过程。

格式可用性

开放平台插屏广告激励插屏广告激励视频广告

Android

iOS

移动网页

桌面端网页

前提条件

  • 在与变现管理工具中的游戏相关的资产中,您至少需已创建一个广告版位。
  • 您可以在移动设备或 Facebook.com 上测试您的编译版本。

注意:目前,Audience Network 在某些地区受限。请在 Audience Network 注册流程中检查资格。

有关更多信息,请参阅“利用 Audience Network 实现小游戏变现的 5 个步骤”指南。

配置 Facebook 应用

下载示例

如果您已利用变现管理工具配置应用,并已创建广告版位,则可以下载我们的演示,查看下方示例代码的实际应用:

下载广告演示 (.zip)

显示插屏广告

插屏广告是在应用的自然切换点处显示的一种全屏广告。插屏广告分为“自动播放”或“点击播放”两种格式。自动播放广告会在插屏广告加载后开始播放;默认情况下,可以在 3 秒后跳过广告。广告时长最长为两分钟。

下方代码片段介绍了如何显示插屏广告的示例。

要让此代码正常运行,您首先需要以插屏广告展示格式创建一个广告版位。您应该为应用中的每个广告单元使用不同的版位。

预加载广告

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 的后续尝试失败。这意味着在调用 showAsync 或玩家重启 Messenger 之前,调用 getRewardedVideoAsyncgetInterstitialAdAsync 将失败,并会返回错误代码 ADS_TOO_MANY_INSTANCES