Skip to content

Monetisation

Mat Hopwood edited this page Nov 8, 2018 · 8 revisions

IGX provides two ways to help monetise games:

  • Adverts
  • In-app purchases

Monetisation with Adverts

Depending upon which ad provider you choose to work with, you will have access to rewarded or none rewarded adverts (usually video).

Setting up Adverts

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");

Checking Availability of Adverts

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

Requesting and Displaying Interstitial Adverts

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.

Requesting and Displaying Rewarded Video Adverts

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
});

Advert Errors

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.

Monetisation with Inn-App Purchases

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.

Setting up In-app Purchasing

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");

Checking availability of in-app purchasing

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

Retrieving a list of products

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);
    });

Purchasing a product

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
});

Help keep this project alive by donating paypal

Clone this wiki locally