Skip to main content

App open ads

startAppOpenAds shows an app open ad each time the app returns from the background, following Google's guidance:

  • The ad is preloaded with AdPreloader and replaced before it expires (app open ads last four hours).
  • Nothing shows on a cold start.
  • Nothing shows over another full-screen ad from this package.
  • minimumInterval caps how often ads show.
import { Admob, startAppOpenAds } from 'nitro-ui-admob'

const stopAppOpenAds = startAppOpenAds({
adUnitId: Admob.testAdUnitId('appOpen'),
minimumInterval: 30 * 60 * 1000, // at most every 30 minutes
onWillShow: (ad) => {
ad.onAdDismissed = () => console.log('App open ad dismissed')
},
onLoadError: (error) => console.warn(error),
})

// Later, for example when the user buys an ad-free upgrade:
stopAppOpenAds()

Options​

OptionTypeDescription
adUnitIdstringThe AdMob app open ad unit ID.
requestOptionsAdmobRequestOptionsTargeting for the ad requests.
minimumIntervalnumberMinimum time between two app open ads, in milliseconds. Default 0.
onWillShow(ad: AppOpenAd) => voidCalled with each ad right before it is shown, to set its callbacks.
onLoadError(error: unknown) => voidCalled with each failed load. Loads are retried with backoff.

When an ad shows​

On each return from the background (Admob.addOnAppForegroundListener):

  1. If a full-screen ad is on screen, or less than minimumInterval passed since the last app open ad, nothing happens.
  2. If no ad is ready, nothing happens; the preloader keeps loading.
  3. Otherwise onWillShow(ad) runs, then the ad is shown, and the next one starts loading.

Build your own​

startAppOpenAds is a small composition of public APIs. For different rules, combine them yourself:

import { AdPreloader, Admob, AppOpenAd } from 'nitro-ui-admob'

const preloader = new AdPreloader(() => AppOpenAd.load(adUnitId))
preloader.start()
const subscription = Admob.addOnAppForegroundListener(() => {
if (Admob.isShowingFullScreenAd || !shouldShowAppOpenAd()) return
preloader.take()?.show()
})

// Cleanup:
subscription.remove()
preloader.stop()