-
Notifications
You must be signed in to change notification settings - Fork 9
Monetisation
IGX provides two ways to help monetise games:
- Adverts
- In-app purchases
Depending upon which ad provider you choose to work with, you will have access to rewarded or none rewarded adverts (usually video).
Depending upon which ad provider you choose you will need to provide different options to FBInstants.options.adOptions. The two options that are a must are callbacks which fire when an ad is started or finished, this enables you to pause your game and music / audio in the background as this is usually a requirement for showing video ads:
FBInstant.options.adsOptions.startedCallback = PauseAudio;
FBInstant.options.adsOptions.finishedCallback = ResumeAudio;
Here we assign functions that pause audio when an ad is started and resume audio when the ad is finished.
Once all ad options are set up you can create an instant of the AdsService module for your chosen ad vendor. Lets take a look at an example which creates an ad service that is serviced by Crazy Games.
new AdsService("crazygames");
Before we can request any adverts we need to ensure that adverts are supported:
var supportedAPIs = FBInstant.getSupportedAPIs();
if (supportedAPIs.includes("getInterstitialAdAsync"))
// Interstitial ads are supported
if (supportedAPIs.includes("getRewardedVideoAsync"))
// Rewarded video ads are supported
An example showing how to request an interstitial advert:
var preloadedAd; // Has to be stored so we can play it at a later time
FBInstant.getInterstitialAdAsync(placement_id,
).then(function(interstitial) {
preloadedAd = interstitial;
return interstitial.loadAsync();
}).then(function() {
// Interstitial preloaded
}).catch(function(err){
// Interstitial failed to preload
}
);
An example showing how to play an interstitial advert:
preloadedAd.showAsync()
.then(function() {
// Ad shown
}).catch(function(e) {
// Error showing ad
});
Note that most ad providers do not use the placement_id parameter.
An example showing how to request a rewarded video advert:
var preloadedAd; // Has to be stored so we can play it at a later time
FBInstant.getRewardedVideoAsync(placement_id,
).then(function(rewarded) {
preloadedAd = rewarded;
return rewarded.loadAsync();
}).then(function() {
// Rewarded ad preloaded
}).catch(function(err){
// Rewarded ad failed to preload
}
);
An example showing how to play a rewarded video advert:
preloadedAd.showAsync()
.then(function() {
// Ad shown
}).catch(function(e) {
// Error showing ad
});
If an error occurs during the preloading or showing of an advert the returned promise will provide the error that occurred. You should always check for ads returning errors because so many things can go wrong whilst preloading or showing an advert. Also, note that many ad providers do not provide advert preloading so the preloadinng process will always return without failure in those cases.
Users can purchase items from within your game using in-app purchases, this enables you to sell virtual products such as extra games, features and items to the user for real money.
Depending upon which payments provider you choose you will need to provide different options to FBInstants.options.paymentsOptions. Some require you to set up the product catalogue client side (PayPal for example), e.g.:
FBInstant.options.paymentsOptions.products["product1"] = new PaymentsService.Product("Product1", "product1", "My first product", "", 1, "USD");
FBInstant.options.paymentsOptions.products["product2"] = new PaymentsService.Product("Product2", "product2", "My second product", "", 1, "USD");
The constructor to Product accepts the following
- title
- product_id
- description
- image_url
- price
- currency_code
Note that not all in-app purchasing vendors will utilise all of these Product properties.
Once all purchasing options are set up you can create an instant of the PaymentsService module for your chosen payments vendor. Lets take a look at an example which creates a payment service that is serviced by PayPal Checkout.
new PaymentsService("paypal");
Before we can perform any purchases we need to ensure that purchasing is supported:
var supportedAPIs = FBInstant.getSupportedAPIs();
if (supportedAPIs.includes("payments.purchaseAsync"))
// Purchasing is supported
Below is an example that shows how to query the list of available products:
FBInstant.payments.getCatalogAsync().then(function(catalog)
{
if (done_callback !== undefined)
done_callback(catalog);
});
Below is an example that shows how to begin the purchase process:
FBInstant.payments.purchaseAsync(
{
productID: product_id,
developerPayload: payload, // payload is not supported by most vendors
}).then(function(purchase)
{
// Purchase successful
}).catch(function(e) {
// Purchase error
});
