Welcome to the Facebook Instant Games SDK Tutorials. This series of tutorials covers many aspects of using the Facebook Instant Games SDK.
In this tutorial we will take a look at earning money from your game with adverts using the Facebook Instant Games SDK.
Introduction
Facebook Instant Games offers two forms of adverts that can be used to earn money from your game, these include:
- Interstitials – These are static adverts or sometimes videos that allow the user to skip them after a few seconds.
- Rewarded videos – These are videos that usually last 15+ seconds and cannot be skipped by the user. These are generally best used in situations where you want to award the player some kind of boost, item or currency. Rewarded videos generally have a higher eCPM that interstitials.
In order to earn money from adverts you must add and set up the Audience Network product to your game in the developer dashboard. You must must also have created a verified business on Facebook. See https://www.facebook.com/business/help/1710077379203657 for details.
Once you have added Audience Network to your game, you can visit the monetisation manager for your business. Quickest way to get to the monetisation manager is to select the Audience Network section of your instant game in the developer dashboard then click the Monetisation Manager link.
In the monetisation manager you can create ad placements that can be shown in your game. After you have created placements for interstitial and rewarded ads you should make a note of the ad placement ID’s because you will need them to request adverts from code.
Note that you will also have to submit your ads for review in the monetisation section of audience network. Do not launch your game until ads have also been approved or you are at risk of not generating revenue from your adverts.
Checking for Support
Before requesting any adverts in-game its best to check that they are available. You can do this by querying their support by calling FBInstant.getSupportedAPIs():
[sourcecode language=”js”]
var supportedAPIs = FBInstant.getSupportedAPIs();
if (supportedAPIs.includes("getInterstitialAdAsync"))
interstitialAdsSupported = true;
if (supportedAPIs.includes("getRewardedVideoAsync"))
videoAdsSupported = true;
[/sourcecode]
Once you know that you have support you can begin requesting adverts to show the player.
Preloading Adverts
Ads should be preloaded (usually on boot) to ensure that they are available to show at key points in your game, this ensures that there is no interruption in-game play and the user is not sat around waiting for the advert to load. You can preload both interstitial and rewarded video adverts using FBInstant.getInterstitialAdAsync() and FBInstant.getRewardedVideoAsync((). Lets take a look at two examples:
[sourcecode language=”js”]
function PreloadRewardedVideoAd(done_callback, placement_id)
{
var adInstance;
FBInstant.getRewardedVideoAsync(
placement_id
).then(function(ad_instance) {
adInstance = ad_instance;
return adInstance.loadAsync();
}).then(function() {
done_callback(adInstance, null);
}).catch(function(err){
done_callback(null, err);
});
};
[/sourcecode]
getRewardedVideoAsync() creates an ad instance associated with the supplied placement_id (placement ID is available in the placements section for your game in the monetisation manager). Once the ad instance is created the advert is loaded by calling loadAsync() on the ad instance. If an error occurs during the loading process then you can check the returned error. There are a number of reasons why an advert does not load but the most common reason is that there was no ad available to show, this is called no fill. You can check the returned error for ADS_NO_FILL and request another ad after a short period of time.
Now lets take a look at an example showing how to preload an interstitial ad:
[sourcecode language=”js”]
function PreloadInterstitialAd(done_callback, placement_id)
{
var adInstance;
FBInstant.getInterstitialAdAsync(
placement_id
).then(function(ad_instance) {
adInstance = ad_instance;
return adInstance.loadAsync();
}).then(function() {
done_callback(adInstance, null);
}).catch(function(err){
done_callback(null, err);
});
};
[/sourcecode]
The code above is almost identical to loaded a rewarded video, the only difference is the call to getInterstitialAdAsync() instead of getRewardedVideoAsync().
Showing Adverts
Once an advert has loaded and you have the ad instance you can simply call showAync() on the ad instance to show the advert to the user:
[sourcecode language=”js”]
function ShowAd(ad_instance, done_callback)
{
ad_instance.showAsync()
.then(function() {
done_callback(true, null);
}).catch(function(err) {
done_callback(false, err);
});
};
[/sourcecode]
Ad Request Limitations
You may not request adverts more than once every 30 seconds, its a good idea to put a timer in your code to ensure that you do not break the rule.
You cannot hold more than 3 preloaded adverts without showing them. Doing so will cause future ad requests to fail with the ADS_TOO_MANY_INSTANCES error.