Game Developers: Ads Best Practices

This guide discusses best practices for integrating banner and interstitial ads into games using the Google Mobile Ads SDK. There are both cross-platform and platform-specific integration options for game developers.

The cross-platform options include the following:

This guide covers integration with platform-specific OpenGL-based iOS games as well as Cocos2d-x.

Helpful primers

If you're integrating Google Mobile Ads using OpenGL with iOS code, Get Started and Interstitial Ads are good resources for learning how to use the SDK.

If you're using an OpenGL-based game engine but integrating ads with the native iOS SDKs, it is recommended to show banner ads only when your game is paused or completed, or on screens where the user isn't actively playing the game. Doing this has the following benefits:

  • Provides a better user experience (users don't want to be distracted by ads while playing the game).
  • Improves performance (ads may affect the number of frames per second (FPS) displayed).
  • Discourages accidental clicks.

The following example shows how a banner ad can be added with minimal code. It's your responsibility to load the ad at the right time.

This example is based on SpriteKit but applies to any framework. When creating a new Xcode project for a SpriteKit game, here is what the boilerplate code looks like in your GameViewController's viewDidLoad() method:

Objective-C

- (void)viewDidLoad {
  [super viewDidLoad];

  // Configure the view.
  SKView *skView = (SKView *)self.view;
  skView.showsFPS = YES;
  skView.showsNodeCount = YES;

  // Sprite Kit applies additional optimizations to improve rendering performance.
  skView.ignoresSiblingOrder = YES;

  // Create and configure the scene.
  GameScene *scene = [GameScene unarchiveFromFile:@"GameScene"];
  scene.scaleMode = SKSceneScaleModeAspectFill;

  // Present the scene.
  [skView presentScene:scene];
}

Swift

override func viewDidLoad() {
  super.viewDidLoad()

  if let scene = GameScene(fileNamed:"GameScene") {
    // Configure the view.
    let skView = view as! SKView
    skView.showsFPS = true
    skView.showsNodeCount = true

    // Sprite Kit applies additional optimizations to improve rendering performance.
    skView.ignoresSiblingOrder = true

    // Set the scale mode to scale to fit the window.
    scene.scaleMode = .AspectFill

    // Present the scene.
    skView.presentScene(scene)
  }
}

After adding the SDK to your project, update the viewDidLoad() method to include a banner ad. In this case, the smart banner format is used and is initialized at the top left of the screen:

Objective-C

- (void)viewDidLoad {
  [super viewDidLoad];

  // Create a banner ad and add it to the view hierarchy.
  self.bannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeSmartBannerPortrait];
  self.bannerView.hidden = YES;
  self.bannerView.adUnitID = @"ca-app-pub-3940256099942544/2934735716";
  self.bannerView.rootViewController = self;
  [self.view addSubview:self.bannerView];</b>

  // Configure the view.
  SKView *skView = (SKView *)self.view;
  skView.showsFPS = YES;
  skView.showsNodeCount = YES;

  // Sprite Kit applies additional optimizations to improve rendering performance.
  skView.ignoresSiblingOrder = YES;

  // Create and configure the scene.
  GameScene *scene = [GameScene unarchiveFromFile:@"GameScene"];
  scene.scaleMode = SKSceneScaleModeAspectFill;

  // Present the scene.
  [skView presentScene:scene];
}

Swift

override func viewDidLoad() {
  super.viewDidLoad()

  // Create a banner ad and add it to the view hierarchy.
  bannerView = GADBannerView(adSize: kGADAdSizeSmartBannerPortrait)
  bannerView.hidden = true
  bannerView.adUnitID = "ca-app-pub-3940256099942544/2934735716"
  bannerView.rootViewController = self
  view.addSubview(bannerView)

  if let scene = GameScene(fileNamed:"GameScene") {
    // Configure the view.
    let skView = view as! SKView
    skView.showsFPS = true
    skView.showsNodeCount = true

    // Sprite Kit applies additional optimizations to improve rendering performance.
    skView.ignoresSiblingOrder = true

    // Set the scale mode to scale to fit the window.
    scene.scaleMode = .AspectFill

    // Present the scene.
    skView.presentScene(scene)
  }
}

To resolve compilation errors, import GADBannerView at the top of the file and add a bannerView property:

Objective-C

#import "GADBannerView.h"

@interface GameViewController()

@property(nonatomic, strong) GADBannerView *bannerView;

@end

Swift

import GoogleMobileAds

class GameViewController: UIViewController {
  var bannerView: GADBannerView!
  ...
}

Your app is now prepared to display banners. The only step that remains is to make the ad visible and make an ad request. This can be done in a helper method called showBanner():

Objective-C

- (void)showBanner {
  self.bannerView.hidden = NO;
  GADRequest *request = [GADRequest request];
  request.testDevices = @[@"2077ef9a63d2b398840261c8221a0c9b"];
  [self.bannerView loadRequest:request];
}

Swift

func showBanner() {
  bannerView.hidden = false
  let request = GADRequest()
  request.testDevices = ["2077ef9a63d2b398840261c8221a0c9b"]
  bannerView.loadRequest(request)
}

Once you call showBanner(), your game has a banner ad:

During gameplay, you'll want to hide the ad. This can be done by calling this hideBanner() method:

Objective-C

- (void)hideBanner {
  self.bannerView.hidden = YES;
}

Swift

func hideBanner() {
  bannerView.hidden = true
}

When the game is over, call the showBanner() method again to refresh the ad and make it visible.

Cocos2d-x

If you're using any of the Cocos2d-x game engines, you can monetize your iOS apps by integrating the AnySDK framework and constructing your app to use the ads system.

This guide shows the AnySDK integration for Cocos2d-x apps. These concepts also apply to the entire suite of Cocos2d-x engines.

Requirements

  • Latest version of Cocos2d-x
  • AnySDK framework v1.2.3 or higher
  • AnySDK package tool

Import AnySDK into your game

Complete the Obtain AnySDK Framework guide, which walks you through importing AnySDK at both the C++ and Android levels.

AnySDK ads system API

Once AnySDK has been successfully imported, you can refer to the ads system guide to write code using the AnySDK ads system.

AdMob supports the ad types AD_TYPE_BANNER and AD_TYPE_FULLSCREEN to specify banner and fullscreen format ads respectively. Some methods that are commonly used to integrate AdMob are described in the following sections.

To request and show a banner ad, use the showAds() method with the AD_TYPE_BANNER ad type:

AdsPlugin* ads = AgentManager::getInstance()->getAdsPlugin();
ads->showAds(AD_TYPE_BANNER);

To stop showing a banner, use the hideAds() method:

ads->hideAds(AD_TYPE_BANNER);

Interstitial ads

You should preload interstitials using the preloadAds() method with the AD_TYPE_FULLSCREEN ad type before you explicitly show them:

AdsPlugin* ads = AgentManager::getInstance()->getAdsPlugin();
ads->preloadAds(AD_TYPE_FULLSCREEN);

Later, when you want to show an interstitial, use the showAds call:

ads->showAds(AD_TYPE_FULLSCREEN);

Use multiple banners or interstitials

If you need to use multiple banner sizes or different ad unit IDs, you can pass an index to the showAds(), hideAds(), and preloadAds() methods. Here are some examples:

ads->showAds(AD_TYPE_BANNER, 2); // show banner #2
ads->hideAds(AD_TYPE_BANNER, 2); // hide banner #2
ads->showAds(AD_TYPE_FULLSCREEN, 2); // preload interstitial #2
ads->preloadAds(AD_TYPE_FULLSCREEN, 2); // show interstitial #2

If no index is specified, it defaults to 1.

When you configure AdMob later with the AnySDK package tool, you'll be able to set an ad unit ID, ad size, and position for each banner and an ad unit ID for each interstitial.

Listen for ad events

To be notified of ad events, such as when an ad is successfully received, have your class implement AdsListener. It has a single method, onAdsResult(), that notifies you of all events.

AdMob supports the following ad events:

  • kAdsReceived
  • kAdsShown
  • kAdsDismissed
  • kNetworkError
  • kUnknownError

A typical skeleton implementation of your onAdsResult() method looks like this:

void onAdsResult(AdsResultCode code, const char* msg) {
  switch(code) {
    case kAdsReceived:
      // Ad has been received.
      break;
    case kAdsShown:
      // Ad is presenting a full screen view.
      break;
    case kAdsDismissed:
      // A full screen ad view is being dismissed.
      break;
    case kNetworkError:
      // Ad failed due to network error.
      break;
    case kUnknownError:
      // Ad failed.
      break;
    default:
      break;
  }
}

From here, you can decide what action to take for each event.

Finally, remember to tell AnySDK which class should receive ad events by setting the plugin's ad listener:

AgentManager::getInstance()->getAdsPlugin()->setAdsListener(this);

Enable AdMob to fill your ad space

Once your app is configured to use the AnySDK ads system, use the AnySDK package tool to enable AdMob to fill your ad space. You'll have to configure AnySDK separately for Android and iOS; the iOS process is described below:

  1. In the Config tab, go to the SDK Management section of your configuration dialog, specify iOS–AdMob as your ad provider, and select which formats your app supports by checking the appropriate boxes in the SDK LIST pane.

  2. In the Config tab, go to the Parameter Config section and enter your iOS ad unit IDs for your banners and/or interstitials as well as size and position if you're using banners.

    If your app is set up to use multiple banners (see the Use multiple banners or interstitials section), click the + tab to configure ad units for each of your banners. Do the same for interstitials.

  3. In the Publishing tab, click the Browse button at the bottom of the screen and select the top level directory of your Cocos2d-x project.

    When asked which .xcodeproj to use, select the project under the proj.ios_mac/ directory and click OK. Then click Start.

    That's it! This step generates a version of your project with AdMob included!

AdMob mediation support

To perform mediation with the AnySDK framework, you must download and include both the SDK and adapter libraries for each third-party network as described in the Mediation guide with the following difference:

  • For iOS, add the required libraries into your Xcode project prior to using the AnySDK package tool.

DoubleClick for Publishers support

AnySDK accepts DFP ad units, but you're limited to the ad size constants provided by AnySDK. Custom ad sizes and custom targeting are not supported.

FAQ

I'm using a game engine that is not mentioned here. How do I integrate Google Mobile Ads?
See Banner ads in OpenGL iOS games, which provides best practices for implementing ads in any game.
Where do I go for help with Unity?
For questions about the Google Mobile Ads Unity plugin, see the Google Mobile Ads SDK developer forum. To file bugs, add an issue to the issue tracker on GitHub. For general Unity questions, consult the Unity community.
Where do I go for help with Cocos2d-x?
Use the AnySDK contact form for help with integrating the AnySDK framework, using the AnySDK package tool, and filing bugs.

Send feedback about...

AdMob by Google
AdMob by Google