Full-screen ads
The four full-screen formats share one lifecycle: load an ad, assign callbacks, call show() once.
| Format | Class | Lifetime after loading | Reward |
|---|---|---|---|
| Interstitial | InterstitialAd | 1 hour | No |
| Rewarded | RewardedAd | 1 hour | Yes |
| Rewarded interstitial | RewardedInterstitialAd | 1 hour | Yes |
| App open | AppOpenAd | 4 hours | No |
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:
| Condition | Message |
|---|---|
| The ad was already shown | This ad was already shown. Load a new ad. |
Date.now() is past expiresAt | This 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
| Callback | Called when |
|---|---|
onAdShown | The ad starts covering the screen. |
onAdDismissed | The user dismissed the ad. |
onAdFailedToShow | The ad could not be presented. |
onAdImpression | The SDK recorded an impression. |
onAdClicked | The SDK recorded a click. |
onAdPaid | The 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.