Skip to main content

Overview

This guide walks you through displaying your first ad using AdMob Plus. We’ll use a banner ad as an example, but the concepts apply to all ad types.
Make sure you’ve completed the installation steps before continuing.

Choose Your Framework

Select your framework to see framework-specific examples:

Initialize the SDK

First, initialize the AdMob SDK when your app starts. This should be done once in your app’s lifecycle.
index.js
document.addEventListener('deviceready', onDeviceReady, false)

async function onDeviceReady() {
  try {
    // Start the AdMob SDK
    const result = await admob.start()
    console.log('AdMob SDK initialized:', result.version)

    // Optional: Configure test devices
    await admob.configure({
      testDeviceIds: ['YOUR_TEST_DEVICE_ID']
    })
  } catch (error) {
    console.error('Failed to initialize AdMob:', error)
  }
}
Use test ad units during development. See test ads documentation for test device IDs.

Display a Banner Ad

Create and show a banner ad at the bottom of the screen:
async function showBannerAd() {
  const banner = new admob.BannerAd({
    adUnitId: 'ca-app-pub-3940256099942544/6300978111', // Test ad unit
    position: 'bottom'
  })

  // Listen for ad events
  banner.on('load', (event) => {
    console.log('Banner ad loaded:', event.ad.id)
  })

  banner.on('loadfail', (event) => {
    console.error('Banner ad failed to load:', event)
  })

  // Load and show the banner
  await banner.show()
}

Display an Interstitial Ad

Load and show a full-screen interstitial ad:
async function showInterstitialAd() {
  const interstitial = new admob.InterstitialAd({
    adUnitId: 'ca-app-pub-3940256099942544/1033173712' // Test ad unit
  })

  // Listen for ad events
  interstitial.on('load', () => {
    console.log('Interstitial ad loaded')
  })

  interstitial.on('dismiss', () => {
    console.log('Interstitial ad dismissed')
  })

  try {
    // Load the ad first
    await interstitial.load()
    // Then show it
    await interstitial.show()
  } catch (error) {
    console.error('Failed to show interstitial:', error)
  }
}

Display a Rewarded Ad

Show a rewarded ad and handle the reward:
async function showRewardedAd() {
  const rewarded = new admob.RewardedAd({
    adUnitId: 'ca-app-pub-3940256099942544/5224354917' // Test ad unit
  })

  // Handle the reward
  rewarded.on('reward', (event) => {
    console.log('User earned reward:', event.reward)
    // Grant the reward to the user
    grantReward(event.reward.amount, event.reward.type)
  })

  rewarded.on('dismiss', () => {
    console.log('Rewarded ad dismissed')
  })

  try {
    await rewarded.load()
    await rewarded.show()
  } catch (error) {
    console.error('Failed to show rewarded ad:', error)
  }
}

function grantReward(amount, type) {
  console.log(`Granting ${amount} ${type} to user`)
  // Your reward logic here
}

TypeScript Example

For TypeScript projects, leverage the full type safety:
import type { BannerAd, InterstitialAd, RewardedAd } from 'admob-plus-cordova'

declare const admob: import('admob-plus-cordova').AdMob

async function showTypedBannerAd(): Promise<void> {
  const banner: BannerAd = new admob.BannerAd({
    adUnitId: 'ca-app-pub-3940256099942544/6300978111',
    position: 'bottom',
  })

  banner.on('load', (event) => {
    console.log('Loaded:', event.ad.id)
  })

  await banner.show()
}

Test Ad Units

During development, always use Google’s test ad units to avoid policy violations:
Banner: ca-app-pub-3940256099942544/6300978111
Interstitial: ca-app-pub-3940256099942544/1033173712
Rewarded: ca-app-pub-3940256099942544/5224354917
Rewarded Interstitial: ca-app-pub-3940256099942544/6978759866
App Open: ca-app-pub-3940256099942544/5662855259
Never click on your own live ads. This violates AdMob policies and can result in account suspension. Always use test ads during development.

Common Patterns

Pre-loading Ads

For better user experience, pre-load ads before showing them:
const interstitial = new InterstitialAd({
  adUnitId: 'your-ad-unit-id'
})

// Pre-load when app starts or at a natural break
await interstitial.load()

// Show immediately when needed (e.g., level complete)
await interstitial.show()

Error Handling

Always handle errors gracefully:
try {
  await ad.load()
  await ad.show()
} catch (error) {
  // Ad failed to load or show
  console.error('Ad error:', error)
  // Continue with app flow
  proceedWithoutAd()
}

Event Listeners

Listen to ad lifecycle events:
ad.on('load', () => console.log('Ad loaded'))
ad.on('loadfail', (err) => console.error('Ad load failed:', err))
ad.on('show', () => console.log('Ad shown'))
ad.on('showfail', (err) => console.error('Ad show failed:', err))
ad.on('dismiss', () => console.log('Ad dismissed'))
ad.on('impression', () => console.log('Ad impression recorded'))

Best Practices

1

Initialize Early

Call start() as early as possible in your app lifecycle to reduce ad loading times.
2

Pre-load Ads

Load ads before you need to show them. This ensures ads are ready when users reach natural transition points.
3

Use Test Ads

Always use test ad units during development. Switch to your real ad units only for production builds.
4

Handle Failures

Ad loading can fail due to network issues or inventory. Always handle errors and continue your app flow.
5

Respect User Experience

Don’t show ads too frequently. Follow AdMob policies and show ads at natural breaks.

Next Steps

Now that you’ve shown your first ad, explore more features:

Banner Ads

Learn about banner ad positioning and sizing

Interstitial Ads

Implement full-screen interstitial ads

Rewarded Ads

Add rewarded video ads with incentives

API Reference

Explore the complete API documentation

Build docs developers (and LLMs) love