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 generic OpenGL-based Android Java games as well as Cocos2d-x.

Helpful primers

If you're integrating Google Mobile Ads using OpenGL with Android code, Get Started in Android Studio 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 Android 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. This has the following benefits:

  1. Provides a better user experience; users don't want to be distracted by ads during gameplay
  2. Improves performance; ads may affect the FPS rate in games
  3. 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.

Most OpenGL-based Android games use some form of SurfaceView. Your main activity will look something like this:

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        SurfaceView gameView = new SurfaceView(this);
        setContentView(gameView);
    }
}

The following example uses a RelativeLayout to pin a smart banner ad at the bottom of the screen:

public class MainActivity extends Activity {
    private AdView adView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        SurfaceView gameView = new SurfaceView(this);

        // Create and load the AdView.
        adView = new AdView(this);
        adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
        adView.setAdSize(AdSize.SMART_BANNER);

        // Create a RelativeLayout as the main layout and add the gameView.
        RelativeLayout mainLayout = new RelativeLayout(this);
        mainLayout.addView(gameView);

        // Add adView to the bottom of the screen.
        RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        mainLayout.addView(adView, adParams);

        // Set the RelativeLayout as the main layout.
        setContentView(mainLayout);
    }
}

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:

private void showBanner() {
    adView.setVisibility(View.VISIBLE);
    adView.loadAd(new AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build());
}

Once you call the showBanner method, your game will have a banner ad.

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

private void hideBanner() {
    adView.setVisibility(View.GONE);
}

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

Cocos2d-x

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

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

Requirements

To use rewarded video ads, you must have the following:

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

Import AnySDK into your game

Complete the AnySDK Quick Start 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 against the AnySDK ads system.

AdMob supports the AD_TYPE_BANNER and AD_TYPE_FULLSCREEN formats. The following methods are commonly used to integrate AdMob.

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 by doing the following:.

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. the following 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 will default 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, which notifies you of all events.

AdMob supports the following ad events:

  • kAdsReceived
  • kAdsShown
  • kAdsDismissed
  • kNetworkError
  • kUnknownError

A typical skeleton implementation of your onAdsResult method would look 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 ads plugin's ad listener:

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

Use the AnySDK package tool

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 process for Android is described below:

  1. In the SDK Management section of your AnySDK app configuration, specify AdMob as your ad provider, and select which formats your app supports.

  2. In the Parameter Config section, enter the Android ad unit IDs for your banners and/or interstitials, as well as the size and position of any banners you're using.

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

  3. Finally, in the Publishing tab, select Browse and find your app's .apk file. 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 Get Started guide with the following difference:

  • For Android, add the required libraries into the libs/ folder of your project prior to using the AnySDK package tool.

DoubleClick for Publishers support

AnySDK will accept 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 the banner best practices that apply to 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.
Why are ads not clickable in versions of Unity older than 5?
Because versions of Unity earlier than 5.0 are missing required metadata. To make ads clickable, add the unityplayer.ForwardNativeEventsToDalvik metadata tag to your main Unity activity.
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