Navigation

Part 4 - Showing the Ad

With an ad preloaded it's time to display it.

You do this by using the showAd method for interstitial ads:

this.facebook.showAd('INTERSTITIAL_PLACEMENT_ID');

and this is the method for rewarded video ads:

this.facebook.showVideo('REWARDED_VIDEO_PLACEMENT_ID');

As with loading ads, the methods take a Placement ID, which is the long code you were given when creating the Ad Placement in the previous step. It can only accept a single ID.

The Ad Appears

Assuming there were no problems (see the events below) the ad will now appear over your game. At this point, Facebook Messenger takes control. It places a dark overlay above your game, making it look faded out, and the ad will appear over the top and take user input.

For all intents and purposes, you should now consider your game paused and minimize what it's doing in the background.

Show Ad

While the Ad is open you don't have much control, all you can really do is wait for the adfinished event, which informs you that the user has closed the ad down:

this.facebook.on('adfinished', function () {

    //  Ad closed.
    //  Resume your game.

});

Error Events

Even if the ad preloads, it doesn't always mean you can display it. There are 3 events you can listen for, all of which are emitted after calling showAd or showVideoAd, if the call fails for any reason.

The events are:

this.facebook.on('adsnotloaded', function () {

    //  The ad for the given placement ID is no longer loaded

});

this.facebook.on('adratelimited', function () {

    //  You're trying to show too many ads and have been rate limited

});

this.facebook.on('adshowerror', function (error) {

    //  Something else went wrong, inspect the error to find out what

});

As with the preload errors, how you deal with these is up to you. But, if you've put your game into a paused state, expecting to get the adfinished event, you should at the very least listen for the adshowerror event, too, and resume the game in that handler as well.