Skip to main content

Full-screen ads

The four full-screen formats share one lifecycle: load an ad, assign callbacks, call show() once.

FormatClassLifetime after loadingReward
InterstitialInterstitialAd1 hourNo
RewardedRewardedAd1 hourYes
Rewarded interstitialRewardedInterstitialAd1 hourYes
App openAppOpenAd4 hoursNo

Load​

Each class has a static load(adUnitId, options?) that resolves with a loaded ad:

import { Admob, InterstitialAd } from 'nitro-ui-admob'

const ad = await InterstitialAd.load(Admob.testAdUnitId('interstitial'), {
keywords: ['games'],
contentUrl: 'https://example.com/levels/3',
})

options is an AdmobRequestOptions: keywords, contentUrl, neighboringContentUrls. Loading starts the SDK if it is not running. A failed load rejects with "<code>: <message>" from the SDK; see errors.

Show​

ad.onAdShown = () => pauseGame()
ad.onAdDismissed = () => resumeGame()
ad.onAdFailedToShow = (error) => {
console.warn(error.code, error.message)
resumeGame()
}
ad.show()

show() presents the ad over the current screen. It throws when:

ConditionMessage
The ad was already shownThis ad was already shown. Load a new ad.
Date.now() is past expiresAtThis ad expired. Load a new ad.

Check freshness yourself with ad.expiresAt, in milliseconds since the Unix epoch, or let AdPreloader replace expired ads for you.

Events​

CallbackCalled when
onAdShownThe ad starts covering the screen.
onAdDismissedThe user dismissed the ad.
onAdFailedToShowThe ad could not be presented.
onAdImpressionThe SDK recorded an impression.
onAdClickedThe SDK recorded a click.
onAdPaidThe SDK estimated the impression's revenue. See revenue.

Rewarded formats add onUserEarnedReward; see rewarded ads.

:::info Callbacks are cleared after the ad closes

Once the ad is dismissed or fails to show, every callback on it is set back to undefined. This breaks reference cycles through callbacks that capture the ad, so you do not need to clear them yourself. Assign callbacks again only on a new ad.

:::

Only one at a time​

Admob.isShowingFullScreenAd is true while any full-screen ad from this package is on screen. startAppOpenAds uses it to avoid stacking an app open ad over another ad.