HTML to APK Builder

ANDROIDAPI REFERENCE

AndroidAPI: AdMob banner, interstitial and rewarded ads with consent

Reference for showing AdMob ads from JavaScript: banners, interstitials, rewarded ads, ad events, the UMP consent gate and privacy options.

Updated:

Ads use your own Google AdMob account. Set them up in Advanced Build → Ads: turn on AdMob, enter your AdMob App ID and the ad unit IDs, and choose automatic banner or interstitial behaviour if you want it. JavaScript cannot enable ads, change the App ID or bypass consent; it can only control ads that the project already allows.

When the project’s ads test mode is on, Google’s test ad units are used. Never click your own live ads.

In regions where consent is required (for example the EEA and UK), Google’s consent form (UMP) is shown first. Until the user answers, ad requests wait. Requests you make during that time are held, one per format, and you receive adDeferred. They run automatically when ads are allowed. Do not retry in a loop.

Method Returns / event
adsInitialize admobInitialized, adInitFailed, adResult
adsCanRequestAds boolean
adsGetConsentStatus JSON string
adsIsPrivacyOptionsRequired boolean
adsShowPrivacyOptionsForm privacyOptionsResult
adsLoadBanner, adsShowBanner, adsHideBanner, isBannerAdLoaded ad events, boolean
adsLoadInterstitial, adsShowInterstitial, isInterstitialAdLoaded ad events, boolean
adsLoadRewarded, adsShowRewarded, isRewardedAdLoaded ad events, rewardEarned, boolean

Older names still work: loadBannerAd, showBannerAd, hideBannerAd, loadInterstitialAd, showInterstitialAd, loadRewardedAd, showRewardedAd.

Ad events

Event Meaning
adLoaded {type, adUnitId} The ad is ready. type is banner, interstitial or rewarded.
adFailedToLoad {type, adUnitId, code, message} No ad was available or the request failed. Try again later, not immediately.
adDeferred Waiting for consent or the SDK; the request will run later. This is not “loaded”.
adShowedFullScreenContent An interstitial or rewarded ad is on screen. Pause your game or audio.
adDismissed The full-screen ad was closed. Resume.
adFailedToShow The ad could not be shown (for example, not loaded yet).
rewardEarned {type, amount} The user earned the reward. type here is the reward name from AdMob.
consentStatusChanged The consent state changed; check adsCanRequestAds() again.

Common error codes: ads_disabled, consent_pending, consent_or_sdk_pending, consent_required, already_loading, activity_not_resumed.

adsInitialize

AndroidAPI.adsInitialize()

Starts the Mobile Ads SDK with the project’s configuration. Most projects do not need to call it, because loading an ad initializes the SDK. Events: admobInitialized, adInitFailed, or adResult with an error when ads are disabled.

adsCanRequestAds

AndroidAPI.adsCanRequestAds() // → true / false

true when consent is resolved and ads may be requested. Use it to decide whether to show an “ad” button, such as “Watch an ad for a hint”.

adsGetConsentStatus

JSON.parse(AndroidAPI.adsGetConsentStatus())

Returns success, error, canRequestAds, consentStatus, privacyOptionsRequired, privacyOptionsStatus, inProgress, debugEnabled. Useful for a settings screen or debugging.

adsIsPrivacyOptionsRequired and adsShowPrivacyOptionsForm

AndroidAPI.adsIsPrivacyOptionsRequired() // → true / false
AndroidAPI.adsShowPrivacyOptionsForm()

Where consent applies, the user must be able to change their choice later. If the first method returns true, show a “Privacy settings” button in your app that opens the form. Event: privacyOptionsResult.

privacyButton.hidden = !AndroidAPI.adsIsPrivacyOptionsRequired();
privacyButton.onclick = () => AndroidAPI.adsShowPrivacyOptionsForm();

adsResetConsent() exists only for testing with debug settings enabled in the project. In a normal build it returns debug_disabled.

AndroidAPI.adsLoadBanner()                    // project banner unit, bottom
AndroidAPI.adsLoadBanner(adUnitId, position)  // position: "top" or "bottom"
AndroidAPI.adsShowBanner(position)
AndroidAPI.adsHideBanner()
AndroidAPI.isBannerAdLoaded() // → true / false

The banner is a native bar at the top or bottom of the screen, above your page. Leave space for it so it does not cover buttons:

window.addEventListener('adLoaded', e => {
  if (e.detail.type === 'banner') document.body.style.paddingBottom = '60px';
});
AndroidAPI.adsLoadBanner('', 'bottom'); // empty id = project banner unit

Hide the banner on screens where it would interfere, such as full-screen games, with adsHideBanner().

Interstitial ads

AndroidAPI.adsLoadInterstitial()          // project interstitial unit
AndroidAPI.adsLoadInterstitial(adUnitId)
AndroidAPI.isInterstitialAdLoaded()       // → true / false
AndroidAPI.adsShowInterstitial()

A full-screen ad. Load it early, then show it at a natural break, for example between game levels. Google’s policies do not allow ads that appear unexpectedly or while the user is in the middle of an action.

AndroidAPI.adsLoadInterstitial();
function onLevelComplete() {
  if (AndroidAPI.isInterstitialAdLoaded()) AndroidAPI.adsShowInterstitial();
  else nextLevel();
}
window.addEventListener('adDismissed', e => {
  if (e.detail.type === 'interstitial') { nextLevel(); AndroidAPI.adsLoadInterstitial(); }
});

Rewarded ads

AndroidAPI.adsLoadRewarded()          // project rewarded unit
AndroidAPI.adsLoadRewarded(adUnitId)
AndroidAPI.isRewardedAdLoaded()       // → true / false
AndroidAPI.adsShowRewarded()
AndroidAPI.adsShowRewarded(callbackName) // also calls window[callbackName](amount, type)

The user chooses to watch an ad in exchange for a reward. Give the reward only on rewardEarned. Closing the ad (adDismissed) does not mean the user earned it. Use either the event or the named callback, not both, or you will give the reward twice.

AndroidAPI.adsLoadRewarded();
window.addEventListener('adLoaded', e => {
  if (e.detail.type === 'rewarded') hintButton.disabled = false;
});
window.addEventListener('rewardEarned', e => {
  coins += e.detail.amount;
  updateCoins();
});
window.addEventListener('adDismissed', e => {
  if (e.detail.type === 'rewarded') { hintButton.disabled = true; AndroidAPI.adsLoadRewarded(); }
});
hintButton.onclick = () => {
  if (AndroidAPI.isRewardedAdLoaded()) AndroidAPI.adsShowRewarded();
};

Before publishing

  • Add an app-ads.txt file to your developer website as AdMob instructs.
  • Declare ad use and the advertising ID in the Play Console’s App content section.
  • If your app is for children, set the child-directed options in the project’s ads step; they cannot be changed from HTML.