JavaScript ब्रिज API
विज्ञापन
अपने इशारे पर interstitial या rewarded विज्ञापन दिखाना, और शेल को बताना कि नेविगेशन कब शुरू और पूरा होता है ताकि वह बीच में न टोके।
AppMintAds.appOpen हेल्पर#
window.AppMintAds.appOpen.setEnabled(on)
उदाहरण
app-open विज्ञापन को रोकता या फिर चालू करता है, वह full-screen विज्ञापन जो लॉन्च पर और यूज़र के दूसरे ऐप से लौटने पर दिखता है।
लौटाता है: कुछ नहीं। ज़रूरत: Ad Control → Developer-controlled और एक app-open ad unit ID।
जब यूज़र जानबूझकर थोड़ी देर के लिए ऐप छोड़े (पेमेंट ऐप, कैमरा, शेयर शीट), तब इसे रोक दें, ताकि वापस आने पर विज्ञापन न दिखे:
var ads = window.AppMintAds;
async function payWithUpi(link) {
if (ads) ads.appOpen.setEnabled(false);
try {
await openPaymentApp(link); // your own code; the user switches apps and returns
} finally {
if (ads) ads.appOpen.setEnabled(true);
}
}जाँचें कि बिल्ड में app-open यूनिट है भी या नहीं:
var hasAppOpen = !!(window.AppMintAds && window.AppMintAds.config().formats.app_open);जब यूज़र फ़ोटो चुने तब इसे रोक दें, क्योंकि सिस्टम फ़ोटो पिकर उन्हें थोड़ी देर के लिए ऐप से बाहर ले जाता है:
var ads = window.AppMintAds;
var input = document.getElementById('photo-input'); // <input type="file" accept="image/*" hidden>
var pausedForPicker = false;
document.getElementById('pick-photo').addEventListener('click', function () {
if (ads) { ads.appOpen.setEnabled(false); pausedForPicker = true; }
input.click();
});
input.addEventListener('change', function () {
if (input.files[0]) uploadPhoto(input.files[0]); // your own code
});
// Back in the app (a photo was picked or the picker was cancelled): turn it on again.
document.addEventListener('visibilitychange', function () {
if (document.hidden || !pausedForPicker) return;
pausedForPicker = false;
setTimeout(function () { ads.appOpen.setEnabled(true); }, 1000);
});नोट्स: यह डिफ़ॉल्ट रूप से चालू है। कोल्ड स्टार्ट पर पेज तैयार होने पर, या 15 सेकंड के भीतर लोड पूरा होने पर विज्ञापन दिखता है; बाद का लोड अगली बार के लिए रख लिया जाता है। 4 घंटे से पुराना app-open विज्ञापन कभी नहीं दिखाया जाता। आप खुद भी एक दिखा सकते हैं AppMintAds.show('app_open').
AppMintAds.banner हेल्पर#
window.AppMintAds.banner.attach(target, opts) / .detach(slotOrElement)
उदाहरण
आपके चुने हुए एलिमेंट के ऊपर, पेज में कहीं भी banner या 300×250 medium-rectangle विज्ञापन लगाता है।
लौटाता है: attach विज्ञापन लोड होने पर {ok:true, slot, width, height} (CSS px) का Promise लौटाता है, या {ok:false, slot?, code, message?}। detach कुछ नहीं लौटाता। ज़रूरत: Ad Control → Developer-controlled और एक banner (या medium rectangle) ad unit ID।
एलिमेंट सिर्फ़ जगह रिज़र्व करता है। विज्ञापन उसके ऊपर बनाया जाता है और पेज के स्क्रॉल या साइज़ बदलने पर उसके साथ चलता है:
<div id="ad-slot"></div>
<script>
var ads = window.AppMintAds;
var slotEl = document.getElementById('ad-slot');
if (ads) {
ads.banner.attach(slotEl, { size: 'banner' }).then(function (res) {
// On success the helper sets slotEl.style.minHeight to res.height (pass keepHeight:true to stop that).
if (!res.ok) { // no_fill, network, too_many_slots, ...
if (res.slot) ads.banner.detach(res.slot); // a failed slot still counts toward the limit of two
slotEl.hidden = true;
}
});
} else {
slotEl.hidden = true; // browser: no ads here
}
</script>Medium rectangle (एक समय में एक), और banner को फिर हटाना:
var box = document.querySelector('.article-end-ad');
var mrecSlot = null;
if (window.AppMintAds) {
window.AppMintAds.banner.attach(box, { size: 'medium_rectangle' }).then(function (res) {
if (res.ok) mrecSlot = res.slot;
else { if (res.slot) window.AppMintAds.banner.detach(res.slot); box.hidden = true; }
});
}
function leaveArticle() {
if (window.AppMintAds && mrecSlot) window.AppMintAds.banner.detach(mrecSlot); // or detach(box)
mrecSlot = null;
}जब कोई विज्ञापन उपलब्ध न हो तो स्लॉट को बाद में फिर आज़माएँ, पहले फ़ेल हुआ स्लॉट खाली कर दें ताकि वह दो में से एक जगह न घेरे रहे:
function fillSlot(el, triesLeft) {
var ads = window.AppMintAds;
if (!ads) { el.hidden = true; return; } // browser: no ads here
ads.banner.attach(el, { size: 'banner' }).then(function (res) {
if (res.ok) return;
if (res.slot) ads.banner.detach(res.slot);
if ((res.code === 'no_fill' || res.code === 'network') && triesLeft > 0) {
setTimeout(function () { fillSlot(el, triesLeft - 1); }, 60000);
} else {
el.hidden = true; // ad_free, disabled, consent, ...
}
});
}
fillSlot(document.getElementById('footer-ad'), 2);नोट्स: target एक एलिमेंट या CSS selector है; size 'banner' (डिफ़ॉल्ट, adaptive चौड़ाई, ज़्यादा से ज़्यादा 90 dp ऊँचा), 'medium_rectangle' या 'mrec' है। एक समय में ज़्यादा से ज़्यादा दो स्लॉट, और जिस स्लॉट का विज्ञापन लोड नहीं हुआ वह भी डिटैच करने तक गिना जाता है। detach को एलिमेंट या attach का स्लॉट id चाहिए, selector नहीं। एलिमेंट को DOM से हटाने पर भी वह डिटैच हो जाता है। कोड: no_element, duplicate_slot, too_many_slots, detached, no_fill, network, invalid_unit, ad_free, disabled, consent, automatic_mode, internal.
AppMintAds.config हेल्पर#
window.AppMintAds.config()
उदाहरण
पेज को बताता है कि बिल्ड कौन सा ad control मोड इस्तेमाल करता है और कौन से विज्ञापन फ़ॉर्मैट दे सकता है।
लौटाता है: एक ऑब्जेक्ट, तुरंत: {control: 'automatic' | 'developer', formats: {banner, mrec, interstitial, rewarded, rewarded_interstitial, app_open}} (हर फ़ॉर्मैट true / false)। अंदरूनी त्रुटि पर यह {control:'automatic', formats:{}} लौटाता है। ज़रूरत: कुछ नहीं; window.AppMintAds सिर्फ़ इंस्टॉल किए ऐप के अंदर होता है।
सिर्फ़ वही विज्ञापन UI दिखाएँ जो यह बिल्ड सच में दे सकता है:
var ads = window.AppMintAds; // undefined in a browser
var cfg = ads ? ads.config() : null;
var developer = !!(cfg && cfg.control === 'developer');
document.getElementById('watch-ad').hidden = !(developer && cfg.formats.rewarded);
document.getElementById('ad-slot').hidden = !(developer && cfg.formats.banner);"bonus" बटन के लिए बिल्ड में जो rewarded फ़ॉर्मैट हो, वह चुनें, और दोनों न हों तो बटन छुपा दें:
var ads = window.AppMintAds;
var cfg = ads ? ads.config() : null;
var bonusBtn = document.getElementById('bonus');
var bonusType = null;
if (cfg && cfg.control === 'developer') {
if (cfg.formats.rewarded) bonusType = 'rewarded';
else if (cfg.formats.rewarded_interstitial) bonusType = 'rewarded_interstitial';
}
bonusBtn.hidden = !bonusType;
bonusBtn.addEventListener('click', async function () {
var res = await ads.show(bonusType); // inside the tap: plays at once
if (res.earned) addCoins(res.reward.amount);
});लेख के अंत के लिए विज्ञापन का साइज़ चुनें: बिल्ड में medium rectangle हो तो वह, वरना banner:
var ads = window.AppMintAds;
var cfg = ads ? ads.config() : null;
var box = document.getElementById('article-end-ad');
var size = null;
if (cfg && cfg.control === 'developer') {
size = cfg.formats.mrec ? 'medium_rectangle' : (cfg.formats.banner ? 'banner' : null);
}
if (!size) box.hidden = true;
else ads.banner.attach(box, { size: size }).then(function (res) {
if (!res.ok) { if (res.slot) ads.banner.detach(res.slot); box.hidden = true; }
});नोट्स: कोई फ़ॉर्मैट true तब होता है जब विज्ञापन चालू हों और बिल्ड में उसके लिए ad unit ID हो। Automatic मोड (control: 'automatic') में हर load / show / banner.attach कॉल का जवाब code: 'automatic_mode' होता है; वहाँ WebToApk.showRewardedAd() / showInterstitialAd() इस्तेमाल करें। विज्ञापन पेज से चलाने के लिए बिल्ड विज़ार्ड में Ad Control → Developer-controlled चुनें।
AppMintAds.isReady हेल्पर#
window.AppMintAds.isReady(type)
उदाहरण
बताता है कि इस टाइप का full-screen विज्ञापन लोड है और अभी दिखाया जा सकता है या नहीं।
लौटाता है: true / false, तुरंत। type: 'interstitial', 'rewarded', 'rewarded_interstitial' या 'app_open'। ज़रूरत: Ad Control → Developer-controlled और उस टाइप के लिए एक ad unit ID।
appmint:ad स्टेटस इवेंट से "Watch ad" बटन को विज्ञापन के साथ तालमेल में रखें:
var ads = window.AppMintAds;
var btn = document.getElementById('watch-ad');
function refresh() { btn.disabled = !(ads && ads.isReady('rewarded')); }
if (ads) {
refresh();
window.addEventListener('appmint:ad', function (e) {
if (e.detail.type === 'rewarded') refresh(); // phases: loaded, failed, closed, ...
});
} else {
btn.hidden = true; // browser: no ads here
}नतीजों वाली स्क्रीन पर "अपने सिक्के दोगुने करें" तभी दें जब rewarded विज्ञापन पहले से तैयार हो:
function showResults(coins) {
var ads = window.AppMintAds;
var offer = document.getElementById('double-coins');
renderResults(coins); // your own screen
offer.hidden = !(ads && ads.isReady('rewarded'));
offer.onclick = async function () {
offer.hidden = true;
var res = await ads.show('rewarded'); // inside the tap: plays at once
addCoins(res.earned ? coins : 0); // the bonus: the same amount again
};
}हिंट बटन में, जो भी rewarded फ़ॉर्मैट अभी लोड है वही चलाएँ:
document.getElementById('hint').addEventListener('click', async function () {
var ads = window.AppMintAds;
if (!ads) return; // browser: no ads here
var type = ads.isReady('rewarded') ? 'rewarded'
: ads.isReady('rewarded_interstitial') ? 'rewarded_interstitial'
: null;
if (!type) { showMessage('No hint video right now. Try again in a minute.'); return; }
var res = await ads.show(type);
if (res.earned) revealHint();
});नोट्स: banner और mrec हमेशा false जवाब देते हैं (वे अटैच होते हैं, दिखाए नहीं जाते)। 60-सेकंड cooldown में चल रहा interstitial false जवाब देता है; 4 घंटे से पुराना app-open विज्ञापन false जवाब देता है। Automatic मोड में यह हमेशा false.
AppMintAds.load हेल्पर#
window.AppMintAds.load(type)
उदाहरण
एक full-screen विज्ञापन लोड करता है ताकि बाद का show(type) उसे तुरंत चला सके।
लौटाता है: एक Promise जो resolve होता है (कभी reject नहीं) {ok:true} के साथ, {ok:true, cached:true} अगर एक पहले से इंतज़ार में हो, या {ok:false, code, reason, message?, adCode?}। ज़रूरत: Ad Control → Developer-controlled और उस टाइप के लिए एक ad unit ID (interstitial, rewarded, rewarded_interstitial, app_open).
var ads = window.AppMintAds;
var btn = document.getElementById('watch-ad');
btn.disabled = true;
async function prepareRewarded() {
if (!ads) { btn.hidden = true; return; } // browser: no ads
var res = await ads.load('rewarded');
if (res.ok) { btn.disabled = false; return; }
if (res.code === 'no_fill' || res.code === 'network') setTimeout(prepareRewarded, 30000);
else console.log('rewarded ad unavailable:', res.code, res.message);
}
prepareRewarded();हर अनुरोध की पूरी ज़िंदगी appmint:ad इवेंट पर भी स्ट्रीम होती है:
window.addEventListener('appmint:ad', function (e) {
var a = e.detail; // {type, phase, slot?, code?, adCode?, message?, width?, height?, reward?, earned?}
// phase: loading, loaded, failed, opened, impression, clicked, closed, rewarded, show_failed, opt_in, declined
if (a.phase === 'failed') console.log(a.type, 'failed to load:', a.code, a.adCode, a.message);
});
// The same object also goes to window.onAppMintAd, if you define that function.
window.onAppMintAd = function (a) { console.log('ad', a.type, a.phase); };interstitial को लेवल शुरू होते ही लोड करें, ताकि लेवल ख़त्म होने पर वह तैयार रहे, और जब यूज़र विज्ञापन हटा दे तो माँगना बंद कर दें:
var ads = window.AppMintAds;
var interstitialsOff = false;
async function startLevel(n) {
runLevel(n); // your own game
if (!ads || interstitialsOff) return;
var res = await ads.load('interstitial');
if (!res.ok && (res.code === 'ad_free' || res.code === 'disabled' || res.code === 'automatic_mode')) {
interstitialsOff = true; // no interstitials in this app session
}
}
async function levelEnded() {
if (ads && !interstitialsOff && ads.isReady('interstitial')) await ads.show('interstitial');
showLevelSummary();
}नोट्स: कोड: no_fill, network, invalid_unit, internal (AdMob से), automatic_mode, unknown_type, ad_free, disabled, consent, और banner / mrec के लिए use_slot (AppMintAds.banner.attach इस्तेमाल करें)। ऐप हर full-screen टाइप को ad unit ID होने पर पहले से लोड कर लेता है और हर show के बाद अगला लोड करता है, इसलिए load ज़्यादातर यह जानने के लिए है कि बटन कब चालू किया जा सकता है।
AppMintAds.show हेल्पर#
window.AppMintAds.show(type)
उदाहरण
लोड किया हुआ full-screen विज्ञापन दिखाता है: interstitial, rewarded, rewarded_interstitial या app_open.
लौटाता है: एक Promise जो विज्ञापन बंद होने पर resolve होता है (कभी reject नहीं): {ok:true, shown:true, earned, reward?} (rewarded फ़ॉर्मैट के लिए reward = {type, amount}), या कुछ न दिखा हो तो {ok:false, shown:false, code, reason, message?}। ज़रूरत: Ad Control → Developer-controlled और उस टाइप के लिए एक ad unit ID।
Rewarded: इसे यूज़र के अपने टैप के भीतर कॉल करें। वह टैप यूज़र की opt-in है, इसलिए विज्ञापन तुरंत चलता है। इनाम तभी दें जब earned true हो:
var ads = window.AppMintAds;
document.getElementById('watch-ad').addEventListener('click', async function () {
if (!ads) return; // browser: no ads here
var res = await ads.show('rewarded');
if (res.earned) addCoins(res.reward.amount);
else if (res.reason === 'ad_free') addCoins(50); // user paid to remove ads: reward anyway
else if (res.shown) showMessage('Watch to the end to get the reward.');
else if (res.reason !== 'declined') showMessage('No ad available right now.');
});Interstitial: किसी स्वाभाविक ब्रेक पर। जवाब कुछ भी हो, आगे बढ़ें:
async function levelFinished() {
if (window.AppMintAds && window.AppMintAds.isReady('interstitial')) {
await window.AppMintAds.show('interstitial'); // resolves when the ad closes
}
showNextLevel();
}Rewarded interstitial आपकी अपनी intro स्क्रीन के पीछे, साफ़ "No thanks" के साथ। "Watch" का टैप ही opt-in है:
var ads = window.AppMintAds;
var intro = document.getElementById('free-life-intro'); // your own dialog: Watch / No thanks
function offerFreeLife() {
if (ads && ads.isReady('rewarded_interstitial')) intro.hidden = false;
else gameOver();
}
document.getElementById('free-life-watch').addEventListener('click', async function () {
intro.hidden = true;
var res = await ads.show('rewarded_interstitial');
if (res.earned) continueWithExtraLife();
else gameOver();
});
document.getElementById('free-life-skip').addEventListener('click', function () {
intro.hidden = true;
gameOver();
});नोट्स: जो rewarded या rewarded-interstitial show() यूज़र के टैप से नहीं आता (टाइमर, पेज लोड, रूट बदलना), उसे पहले "Watch an ad? / No thanks" स्क्रीन मिलती है, और "No thanks" का जवाब declined होता है। rewarded फ़ॉर्मैट को कभी इस तरह ट्रिगर न करें। दूसरे कोड: automatic_mode, unknown_type, ad_free, disabled, consent, not_full_screen, showing, cooldown (interstitials के बीच 60 s), not_loaded (अगली बार के लिए नया लोड शुरू होता है), show_failed, internal। Automatic मोड में इस्तेमाल करें WebToApk.showRewardedAd().
adsAppOpenEnable ब्रिज#
window.WebToApk.adsAppOpenEnable(enabled: Boolean)
पेज को दूसरे ऐप से वापस आने पर app-open विज्ञापन रोकने/फिर चालू करने देता है (यूनिट सेट होने पर डिफ़ॉल्ट रूप से चालू)।
उदाहरण
AppMintAds.appOpen.setEnabled(on) का नेटिव हिस्सा: यह app-open विज्ञापन को रोकता या फिर चालू करता है। पेज इस्तेमाल करते हैं AppMintAds.appOpen.setEnabled.
लौटाता है: कुछ नहीं। ज़रूरत: Ad Control → Developer-controlled और एक app-open ad unit ID।
function setAppOpenAds(on) {
if (window.AppMintAds) window.AppMintAds.appOpen.setEnabled(on);
}
// Pause while the user is in the middle of something, resume afterwards.
function startCheckout() { setAppOpenAds(false); showCheckout(); }
function checkoutDone() { setAppOpenAds(true); }फ़ोटो चुनने पर ऐप सिस्टम पिकर के लिए छूट जाता है, और वापस आने को वापसी गिना जाएगा। इसके आसपास app-open विज्ञापन को रोक दें:
var input = document.getElementById('photo');
function appOpenAds(on) {
if (window.AppMintAds) window.AppMintAds.appOpen.setEnabled(on);
}
input.addEventListener('click', function () { appOpenAds(false); });
function pickerDone() { setTimeout(function () { appOpenAds(true); }, 1000); }
input.addEventListener('change', pickerDone);
input.addEventListener('cancel', pickerDone);आपके अपने पेड प्लान के सदस्य इसे कभी नहीं देखते। यह स्विच सेव नहीं होता, इसलिए हर लॉन्च पर इसे जल्दी सेट करें:
// Runs from a <head> script: window.AppMintAds is already there at document start.
var plan = null;
try { plan = localStorage.getItem('plan'); } catch (e) {}
if (window.AppMintAds && plan === 'pro') {
window.AppMintAds.appOpen.setEnabled(false);
}नोट्स: रॉ सिग्नेचर adsAppOpenEnable(enabled)। app-open यूनिट सेट होने पर डिफ़ॉल्ट रूप से चालू। app-open विज्ञापन कोल्ड स्टार्ट पर दिखता है (अगर 15 सेकंड के भीतर लोड हो) और जब यूज़र दूसरे ऐप से वापस आता है; 4 घंटे से पुराना विज्ञापन कभी नहीं दिखाया जाता।
adsConfig ब्रिज#
window.WebToApk.adsConfig(): String
`{control, formats:{banner, mrec, interstitial, rewarded, rewarded_interstitial, app_open}}` - वे विज्ञापन फ़ॉर्मैट जो यह बिल्ड दे सकता है।
उदाहरण
पढ़ता है कि यह बिल्ड कौन से विज्ञापन फ़ॉर्मैट दे सकता है और कौन सा ad control मोड इस्तेमाल करता है। पेज आम तौर पर AppMintAds.config() कॉल करते हैं, जो इसे आपके लिए पार्स करता है।
लौटाता है: एक JSON string: {"control":"automatic"|"developer","formats":{"banner":bool,"mrec":bool,"interstitial":bool,"rewarded":bool,"rewarded_interstitial":bool,"app_open":bool}}। कोई फ़ॉर्मैट तब true होता है जब विज्ञापन चालू हों और बिल्ड में उसके लिए ad unit ID हो। ज़रूरत: कुछ नहीं।
पहले पब्लिक API:
if (window.AppMintAds) {
var cfg = window.AppMintAds.config(); // already parsed; {control:'automatic', formats:{}} on error
if (cfg.control === 'developer' && cfg.formats.rewarded) {
document.getElementById('watch-ad').hidden = false;
}
}सीधा कॉल, अगर आप इस्तेमाल नहीं करते AppMintAds:
var b = window.WebToApk;
if (b && typeof b.adsConfig === 'function') {
try {
var cfg = JSON.parse(b.adsConfig());
console.log(cfg.control, cfg.formats);
} catch (e) { console.log('could not read ad config', e); }
}जगह सिर्फ़ उन फ़ॉर्मैट के लिए रिज़र्व करें जो बिल्ड में सच में हैं, और बाकी में खाली बॉक्स पेज को लौटा दें:
var box = document.getElementById('sidebar-ad');
var cfg = window.AppMintAds ? window.AppMintAds.config() : { control: 'automatic', formats: {} };
if (cfg.control === 'developer' && cfg.formats.mrec) {
window.AppMintAds.banner.attach(box, { size: 'mrec' });
} else if (cfg.control === 'developer' && cfg.formats.banner) {
window.AppMintAds.banner.attach(box, { size: 'banner' });
} else {
box.remove(); // browser, Automatic mode, or no unit: no empty gap
}नोट्स: Automatic मोड में formats फिर भी बताता है कि कौन सी units हैं, लेकिन AppMintAds.load/show का जवाब code: 'automatic_mode' होता है; वहाँ WebToApk.showRewardedAd() / showInterstitialAd() इस्तेमाल करें।
adsIsReady ब्रिज#
window.WebToApk.adsIsReady(type: String): Boolean
True जब एक full-screen [type] लोड हो और अभी दिखाया जा सकता हो।
उदाहरण
बताता है कि एक टाइप का full-screen विज्ञापन लोड है और अभी दिख सकता है (Developer-controlled विज्ञापन)। पेज आम तौर पर कॉल करते हैं AppMintAds.isReady(type).
लौटाता है: true / false, तुरंत। type 'interstitial', 'rewarded', 'rewarded_interstitial' या 'app_open' है; 'banner' / 'mrec' हमेशा false जवाब देते हैं। ज़रूरत: Ad Control → Developer-controlled और उस टाइप के लिए एक ad unit ID।
पहले पब्लिक API:
var watchBtn = document.getElementById('watch-ad');
function refresh() {
watchBtn.disabled = !(window.AppMintAds && window.AppMintAds.isReady('rewarded'));
}
refresh();
window.addEventListener('appmint:ad', refresh); // loaded / closed / failed all change readinessसीधा कॉल:
var b = window.WebToApk;
var ready = !!(b && typeof b.adsIsReady === 'function' && b.adsIsReady('interstitial'));लेवल के अंत में interstitial तभी दिखाएँ जब कोई तैयार हो, और जब न हो तो खिलाड़ी को कभी न रोकें:
function onLevelComplete() {
var ads = window.AppMintAds;
if (ads && ads.isReady('interstitial')) {
// resolves after the ad closes, or at once with {ok:false, code} if it could not show
ads.show('interstitial').then(startNextLevel);
} else {
startNextLevel(); // not loaded yet, or inside the 60-second cooldown
}
}नोट्स: 60 सेकंड के cooldown में चल रहा interstitial false जवाब देता है। 4 घंटे से पुराना app-open विज्ञापन false जवाब देता है (Google कहता है इसे न दिखाएँ)। Automatic मोड में यह हमेशा false होता है; वहाँ WebToApk.isRewardedAdReady() इस्तेमाल करें।
adsLoad ब्रिज#
window.WebToApk.adsLoad(type: String, callbackId: String)
एक full-screen [type] लोड करता है; [callbackId] का promise नतीजे के साथ resolve होता है।
उदाहरण
AppMintAds.load(type) का नेटिव हिस्सा: यह full-screen विज्ञापन लोड करना शुरू करता है और shim के callback का जवाब देता है। पेज AppMintAds.load(type) इस्तेमाल करते हैं, जो Promise लौटाता है; इसे सीधे कॉल न करें।
लौटाता है: (AppMintAds.load के ज़रिए) {ok:true} का Promise, {ok:true, cached:true} अगर एक पहले से लोड हो, या {ok:false, code, reason, message?, adCode?}। ज़रूरत: Ad Control → Developer-controlled और उस टाइप के लिए एक ad unit ID।
var watchBtn = document.getElementById('watch-ad');
watchBtn.disabled = true;
if (window.AppMintAds) {
window.AppMintAds.load('rewarded').then(function (res) {
if (res.ok) watchBtn.disabled = false;
else console.log('rewarded ad not loaded:', res.code, res.message);
});
} else {
watchBtn.hidden = true; // browser: there are no ads here
}जो लोड किसी गुज़रते कारण से फ़ेल हुआ उसे दोबारा आज़माएँ, और उन कारणों पर रुक जाएँ जिन्हें रीट्राई ठीक नहीं कर सकता:
function loadWithRetry(type, attempt) {
attempt = attempt || 0;
return window.AppMintAds.load(type).then(function (res) {
if (res.ok) return res;
if ((res.code === 'no_fill' || res.code === 'network') && attempt < 3) {
return new Promise(function (wait) { setTimeout(wait, 5000 * (attempt + 1)); })
.then(function () { return loadWithRetry(type, attempt + 1); });
}
return res; // invalid_unit, consent, ad_free, disabled ...
});
}
if (window.AppMintAds) {
loadWithRetry('rewarded').then(function (res) {
document.getElementById('watch-ad').disabled = !res.ok;
});
}गेम सेशन शुरू होने से पहले दो फ़ॉर्मैट तैयार रखें, और जब यूज़र के पास कोई विज्ञापन ही न हो तो हर विज्ञापन बटन छुपा दें:
if (window.AppMintAds) {
Promise.all([
window.AppMintAds.load('interstitial'),
window.AppMintAds.load('rewarded_interstitial')
]).then(function (results) {
var noAds = results.some(function (r) { return r.code === 'ad_free' || r.code === 'disabled'; });
if (noAds) hideAllAdButtons(); // paid to remove ads, or ads are off in this build
results.forEach(function (r) {
if (!r.ok && r.adCode !== undefined) console.log('ad load failed', r.code, r.adCode, r.message);
});
});
}नोट्स: फ़ेल होने के कोड: no_fill, network, invalid_unit, internal (AdMob से, adCode के साथ), और automatic_mode, unknown_type, ad_free, disabled, consent, और banner / mrec के लिए use_slot (ये AppMintAds.banner.attach से लोड होते हैं)। ऐप ऐसे हर full-screen टाइप को पहले से लोड करता है जिसकी unit ID है, और हर show के बाद दोबारा लोड करता है। रॉ सिग्नेचर adsLoad(type, callbackId) है; इसका जवाब एक आंतरिक resolver को जाता है, इसलिए सीधे कॉल से आपको कोई नतीजा नहीं मिलता।
adsShow ब्रिज#
window.WebToApk.adsShow(type: String, callbackId: String, userGesture: Boolean)
लोड किया हुआ full-screen [type] दिखाता है; बंद होने पर resolve होता है, rewarded फ़ॉर्मैट के लिए earned/reward के साथ। [userGesture] shim की `navigator.userActivation` रीडिंग है - इसके बिना माँगे गए rewarded फ़ॉर्मैट को पहले AdMob की intro स्क्रीन मिलती है (DeveloperAds.offerRewarded देखें)।
उदाहरण
AppMintAds.show(type) का नेटिव हिस्सा: यह लोड किया हुआ full-screen विज्ञापन दिखाता है और बंद होने पर जवाब देता है। पेज AppMintAds.show(type) इस्तेमाल करते हैं; इसे सीधे कॉल न करें।
लौटाता है: (AppMintAds.show के ज़रिए) विज्ञापन बंद होने के बाद {ok:true, shown:true, earned, reward?} का Promise (rewarded फ़ॉर्मैट के लिए reward = {type, amount}), या {ok:false, shown:false, code, reason, message?}। ज़रूरत: Ad Control → Developer-controlled और उस टाइप के लिए एक ad unit ID।
इसे यूज़र के टैप से कॉल करें। shim तीसरे आर्गुमेंट के रूप में navigator.userActivation.isActive देता है (userGesture):
document.getElementById('watch-ad').addEventListener('click', function () {
if (!window.AppMintAds) return;
window.AppMintAds.show('rewarded').then(function (res) {
if (res.earned) addCoins(res.reward.amount);
else if (res.reason === 'ad_free') addCoins(50); // paid to remove ads: reward anyway
else if (res.reason !== 'declined') showMessage('No ad available right now.');
});
});यूज़र के अपने "Double my bonus" टैप से rewarded interstitial पेश करें, और जवाब जो भी हो, आगे बढ़ें:
document.getElementById('double-bonus').addEventListener('click', function () {
if (!window.AppMintAds) return goToNextLevel();
window.AppMintAds.show('rewarded_interstitial').then(function (res) {
if (res.earned) doubleLevelBonus();
else if (!res.shown && res.code !== 'declined') console.log('no ad:', res.code);
goToNextLevel(); // shown or not, the player keeps going
});
});जब भी कोई full-screen विज्ञापन स्क्रीन पर हो, appmint:ad स्ट्रीम से अपना गेम म्यूज़िक रोकें:
window.addEventListener('appmint:ad', function (e) {
var d = e.detail; // {type, phase, code?, adCode?, message?, earned?, reward?}
if (d.type === 'banner' || d.type === 'mrec') return;
if (d.phase === 'opened') music.pause();
if (d.phase === 'closed' || d.phase === 'show_failed') music.play();
});
function onChapterSaved() {
if (window.AppMintAds) window.AppMintAds.show('interstitial'); // a natural break
}नोट्स: रॉ सिग्नेचर adsShow(type, callbackId, userGesture)। जब rewarded या rewarded_interstitial के लिए userGesture false हो, तो ऐप पहले "Watch an ad? / No thanks" पूछता है और "no" का जवाब declined होता है। दूसरे कोड: automatic_mode, unknown_type, ad_free, disabled, consent, not_full_screen, showing, cooldown (interstitial, 60 s), not_loaded (अगली बार के लिए लोड शुरू होता है), show_failed, internal.
adsSlotAttach ब्रिज#
window.WebToApk.adsSlotAttach(slotId: String, type: String, widthCss: Int, callbackId: String)
पेज के स्लॉट [slotId] के लिए banner/mrec ओवरले बनाता है, जिसकी चौड़ाई [widthCss] के हिसाब से होती है; लोड होने पर विज्ञापन के साइज़ के साथ resolve होता है।
उदाहरण
AppMintAds.banner.attach(element, {size}) का नेटिव हिस्सा: यह पेज के एलिमेंट के ऊपर banner या medium-rectangle विज्ञापन बनाता है। पेज AppMintAds.banner.attach इस्तेमाल करते हैं; इसे सीधे कॉल न करें।
लौटाता है: (AppMintAds.banner.attach के ज़रिए) विज्ञापन लोड होने पर CSS पिक्सल में {ok:true, slot, width, height} का Promise, या {ok:false, slot, code, message?}। ज़रूरत: Ad Control → Developer-controlled और एक banner (या medium rectangle) ad unit ID।
<div id="ad-slot"></div>
<script>
if (window.AppMintAds) {
window.AppMintAds.banner.attach('#ad-slot', { size: 'banner' }).then(function (res) {
if (!res.ok) console.log('no banner:', res.code); // e.g. no_fill, network, too_many_slots
});
}
</script>नतीजों वाली स्क्रीन पर एक medium rectangle, जो विज्ञापन न आने पर अपनी जगह वापस दे देता है:
var box = document.getElementById('result-ad');
if (window.AppMintAds) {
window.AppMintAds.banner.attach(box, { size: 'mrec' }).then(function (res) {
if (res.ok) return; // the box now has min-height = res.height
window.AppMintAds.banner.detach(res.slot); // free the slot for another banner
box.hidden = true;
if (res.code === 'too_many_slots') console.log('detach a banner first:', res.message);
});
} else {
box.hidden = true;
}appmint:ad स्ट्रीम से हर स्लॉट के इम्प्रेशन और क्लिक गिनें, उस स्लॉट id के हिसाब से जो attach ने लौटाया:
var slotNames = {};
if (window.AppMintAds) {
window.AppMintAds.banner.attach('#footer-ad', { size: 'banner' }).then(function (res) {
if (res.ok) slotNames[res.slot] = 'footer';
});
}
window.addEventListener('appmint:ad', function (e) {
var d = e.detail; // banner events carry {type, phase, slot}
if (d.slot && (d.phase === 'impression' || d.phase === 'clicked')) {
track('ad_' + d.phase, slotNames[d.slot] || d.slot);
}
});नोट्स: रॉ सिग्नेचर adsSlotAttach(slotId, type, widthCss, callbackId); type 'banner' या 'mrec' होता है। WebView पेज के अंदर नेटिव व्यू नहीं रख सकता, इसलिए विज्ञापन आपके एलिमेंट के ऊपर बनता है और स्क्रॉल व साइज़ बदलने पर उसके साथ चलता है (adsSlotRect देखें)। एक समय में ज़्यादा से ज़्यादा दो स्लॉट, और medium rectangle सिर्फ़ एक। अतिरिक्त कोड: duplicate_slot, too_many_slots, detached, internal, साथ में लोड कोड (no_fill, network, invalid_unit, ad_free, disabled, consent, automatic_mode).
adsSlotDetach ब्रिज#
window.WebToApk.adsSlotDetach(slotId: String)
banner ओवरले हटाता है।
उदाहरण
AppMintAds.banner.detach(slotOrElement) का नेटिव हिस्सा: यह banner ओवरले हटाता है और उसका स्लॉट खाली करता है। पेज इस्तेमाल करते हैं AppMintAds.banner.detach.
लौटाता है: कुछ नहीं। ज़रूरत: Ad Control → Developer-controlled.
ऐसी स्क्रीन से पहले banner हटा दें जहाँ विज्ञापन ठीक नहीं बैठता (वीडियो प्लेयर, चेकआउट):
var slotEl = document.getElementById('ad-slot');
function openPlayer() {
if (window.AppMintAds) window.AppMintAds.banner.detach(slotEl); // the element, or the slot id from attach()
showPlayer();
}
function closePlayer() {
hidePlayer();
if (window.AppMintAds) window.AppMintAds.banner.attach(slotEl, { size: 'banner' });
}सिंगल-पेज ऐप में, जिस रूट में banner है उसके हटने पर banner खाली करें। attach एलिमेंट को तुरंत मार्क कर देता है, इसलिए विज्ञापन अभी लोड हो रहा हो तब भी एलिमेंट से डिटैच करना काम करता है:
function onRouteChange(route) {
if (!window.AppMintAds) return renderRoute(route);
var old = document.getElementById('article-ad');
if (old) window.AppMintAds.banner.detach(old);
renderRoute(route);
if (route === '/articles') {
window.AppMintAds.banner.attach('#article-ad', { size: 'banner' });
}
}"Remove Ads" खरीदारी के बाद ओवरले चले जाते हैं; स्लॉट भी डिटैच करें और उनकी रिज़र्व की हुई जगह हटा दें:
window.addEventListener('appmint:premium', function (e) {
if (!e.detail.premium || !window.AppMintAds) return;
document.querySelectorAll('[data-appmint-ad-slot]').forEach(function (el) {
window.AppMintAds.banner.detach(el);
el.remove();
});
});नोट्स: रॉ सिग्नेचर adsSlotDetach(slotId)। detach वह एलिमेंट या slot string लेता है जो attach ने लौटाया, CSS selector नहीं। अपने विज्ञापन का इंतज़ार कर रहा attach Promise {ok:false, code:'detached'} जवाब देता है। एलिमेंट को DOM से हटाने पर भी स्लॉट डिटैच हो जाता है।
adsSlotRect ब्रिज#
window.WebToApk.adsSlotRect(slotId: String, x: Float, y: Float, w: Float, h: Float, visible: Boolean)
आंतरिक: स्लॉट एलिमेंट का मौजूदा viewport आयत (CSS px) - ओवरले को हिलाता है।
उदाहरण
आंतरिक: banner ओवरले को उसके एलिमेंट की मौजूदा जगह पर ले जाता है। AppMintAds.banner हेल्पर इसे scroll, resize, visibility बदलने और हर 500 ms पर आपके लिए कॉल करता है; पेज AppMintAds.banner.attach इस्तेमाल करते हैं और इसे कभी कॉल नहीं करते।
लौटाता है: कुछ नहीं। ज़रूरत: Ad Control → Developer-controlled, और इससे बना एक स्लॉट AppMintAds.banner.attach.
पेज को बस स्लॉट एलिमेंट को लेआउट में जगह देनी होती है। हेल्पर उसे नापता है और विज्ञापन को उसके ऊपर रखता है:
<main>
<article id="story">…</article>
<div id="ad-slot" style="margin: 16px 0"></div> <!-- the ad follows this box while scrolling -->
</main>
<script>
if (window.AppMintAds) window.AppMintAds.banner.attach('#ad-slot', { size: 'banner' });
</script>स्क्रॉल होती लिस्ट के अंदर banner, पेज में नहीं: हेल्पर capture phase में scroll सुनता है, इसलिए विज्ञापन अंदरूनी कंटेनर के साथ भी चलता है:
<div id="feed" style="height: 100vh; overflow-y: auto">
<div class="card">…</div>
<div class="card">…</div>
<div id="feed-ad" style="margin: 12px 0"></div>
<div class="card">…</div>
</div>
<script>
if (window.AppMintAds) window.AppMintAds.banner.attach('#feed-ad', { size: 'banner' });
</script>विज्ञापन पेज के ऊपर बनता है, इसलिए आपका अपना डायलॉग उसके नीचे आ जाएगा। डायलॉग खुला होने पर स्लॉट छुपा दें:
var slotEl = document.getElementById('ad-slot');
var dialog = document.getElementById('settings-dialog');
function openSettings() {
slotEl.style.visibility = 'hidden'; // the helper hides the overlay on its next tick
dialog.showModal();
}
dialog.addEventListener('close', function () {
slotEl.style.visibility = ''; // and puts it back over the box
});नोट्स: रॉ सिग्नेचर adsSlotRect(slotId, x, y, w, h, visible), CSS पिक्सल में, viewport के सापेक्ष। जब एलिमेंट स्क्रीन से बाहर हो, उसका साइज़ शून्य हो, उसमें visibility: hidden हो, या पेज बैकग्राउंड में हो, तो ओवरले छुपा रहता है। अगर एलिमेंट DOM से निकल जाए, तो हेल्पर स्लॉट को खुद डिटैच कर देता है।
isInterstitialAdReady ब्रिज#
window.WebToApk.isInterstitialAdReady(): Boolean
True जब interstitial लोड है और उसके cooldown के पार है (ठीक वही जो showInterstitialAd() स्वीकार करेगा - कोई ऐसा "ready" नहीं जो फिर दिखाने से मना कर दे)।
उदाहरण
बताता है कि interstitial विज्ञापन लोड है और उसके 60-सेकंड cooldown के पार है, ताकि showInterstitialAd() उसे अभी दिखा दे (Automatic ad control)।
लौटाता है: true / false, तुरंत। ज़रूरत: विज्ञापन चालू और Full Screen Ad → Interstitial Ad.
function onLevelDone() {
var b = window.WebToApk;
if (b && typeof b.isInterstitialAdReady === 'function' && b.isInterstitialAdReady()) {
b.showInterstitialAd(); // wait for 'appmint:ad-closed' before continuing
} else {
startNextLevel(); // nothing ready: never block the user
}
}
window.addEventListener('appmint:ad-closed', function (e) {
if (e.detail.adType === 'interstitial') startNextLevel();
});हर तीसरी पूरी की गई क्विज़ के बाद विज्ञापन दिखाएँ, और सिर्फ़ तब जब वह सच में दिखे:
var finished = 0;
function onQuizFinished() {
finished++;
var b = window.WebToApk;
var ready = !!(b && typeof b.isInterstitialAdReady === 'function' && b.isInterstitialAdReady());
if (finished % 3 === 0 && ready) b.showInterstitialAd();
showResults(); // the results are there when the ad closes
}"Ad in 3…" वाली छोटी सूचना तभी दिखाएँ जब विज्ञापन तैयार हो, फिर उसके बंद होने या न दिख पाने पर आगे बढ़ें:
var afterAd = null;
function adBreakThen(next) {
var b = window.WebToApk;
if (!b || typeof b.isInterstitialAdReady !== 'function' || !b.isInterstitialAdReady()) return next();
var note = document.getElementById('ad-break'), n = 3;
note.hidden = false; note.textContent = 'Ad in 3…';
var t = setInterval(function () {
if (--n > 0) { note.textContent = 'Ad in ' + n + '…'; return; }
clearInterval(t); note.hidden = true;
afterAd = next;
b.showInterstitialAd();
}, 1000);
}
function runAfterAd(e) {
if (e.detail.adType !== 'interstitial' || !afterAd) return;
var next = afterAd; afterAd = null; next();
}
window.addEventListener('appmint:ad-closed', runAfterAd);
window.addEventListener('appmint:ad-unavailable', runAfterAd);नोट्स: यहाँ true का मतलब है कि show को not_loaded या cooldown की वजह से मना नहीं किया जाएगा। सहमति या "Remove Ads" खरीदारी फिर भी इसे रोक सकती है (appmint:ad-unavailable)। Developer-controlled बिल्ड में इस्तेमाल करें AppMintAds.isReady('interstitial').
isRewardedAdReady ब्रिज#
window.WebToApk.isRewardedAdReady(): Boolean
True जब rewarded विज्ञापन लोड है और अभी दिखाया जा सकता है। पेज अपने "Watch ad" बटन को इसी से चालू/बंद करे (दिखाने से पहले poll करें या कॉल करें)।
उदाहरण
बताता है कि rewarded विज्ञापन लोड है और अभी दिखाया जा सकता है (Automatic ad control)।
लौटाता है: true / false, तुरंत। ज़रूरत: विज्ञापन चालू और Full Screen Ad → Rewarded Ad.
"Watch an ad" बटन तभी चालू करें जब कोई विज्ञापन तैयार हो:
var btn = document.getElementById('earn');
function refreshWatchButton() {
var b = window.WebToApk;
var ready = !!(b && typeof b.isRewardedAdReady === 'function' && b.isRewardedAdReady());
btn.disabled = !ready;
btn.textContent = ready ? 'Watch an ad for 10 coins' : 'Loading ad…';
}
refreshWatchButton();
setInterval(refreshWatchButton, 2000); // an ad can finish loading at any time
window.addEventListener('appmint:ad-closed', refreshWatchButton);गेम ओवर पर "Continue" तभी दें जब कोई विज्ञापन तैयार हो, ताकि बटन कहीं न ले जाए ऐसा न हो:
var continueBtn = document.getElementById('continue-with-ad');
function onGameOver() {
var b = window.WebToApk;
continueBtn.hidden = !(b && typeof b.isRewardedAdReady === 'function' && b.isRewardedAdReady());
showGameOverPanel(); // "Restart" is always there
}
continueBtn.addEventListener('click', function () { // the user's own tap
continueBtn.hidden = true;
window.WebToApk.showRewardedAd();
});
window.addEventListener('appmint:reward', function () { revivePlayer(); });टैप के समय जाँचें और यूज़र को बताएँ कि क्या हो रहा है, ऐसा show चलाने के बजाय जिसे मना कर दिया जाएगा:
document.getElementById('hint').addEventListener('click', function () {
var b = window.WebToApk;
if (!b || typeof b.isRewardedAdReady !== 'function') return showMessage('Hints need the app.');
if (typeof b.isPremium === 'function' && b.isPremium()) return revealHint(); // paid to remove ads
if (b.isRewardedAdReady()) b.showRewardedAd();
else showMessage('The ad is still loading. Try again in a few seconds.');
});
window.addEventListener('appmint:reward', function () { revealHint(); });नोट्स: ब्राउज़र में, खरीदारी से विज्ञापन हटने पर, और पहला विज्ञापन लोड होने से पहले false लौटाता है। विज्ञापन दिखने के बाद अगला अपने-आप लोड होता है। Developer-controlled बिल्ड में इस्तेमाल करें AppMintAds.isReady('rewarded').
notifyUserAction ब्रिज#
window.WebToApk.notifyUserAction()
आंतरिक (इंजेक्ट किए गए टैप काउंटर द्वारा कॉल किया जाता है, क्रिएटर के कोड से नहीं): पेज में एक सार्थक यूज़र इंटरैक्शन हुआ। यह "हर N क्रियाओं पर interstitial" की गिनती में सिर्फ़ तब गिना जाता है जब कभी कोई URL-आधारित नेविगेशन देखा न गया हो - DOM के show/hide से स्क्रीन बदलने वाले single-HTML ऐप का URL कभी नहीं बदलता, इसलिए इसके बिना उनका नेविगेशन काउंटर हमेशा 0 रहता था और कॉन्फ़िगर किए विज्ञापन कभी नहीं दिखे (TicTrek शिकायत वाली श्रेणी, 2026-07-26)। URL/SPA ऐप पर कोई असर नहीं: उनका पहला असली नेविगेशन गिनती को हमेशा के लिए URL संकेतों पर वापस ले जाता है।
उदाहरण
"हर N क्रियाओं पर full-screen विज्ञापन" सेटिंग के लिए एक सार्थक यूज़र क्रिया गिनता है। ऐप इसे अपने टैप काउंटर से खुद कॉल करता है; पेज को आम तौर पर इसे कभी कॉल नहीं करना पड़ता।
लौटाता है: कुछ नहीं। ज़रूरत: विज्ञापन चालू, Full Screen Ad और Trigger Mode → After Navigations, और Automatic ad control।
यह कैसे काम करता है: जो पेज DOM एलिमेंट छुपाकर और दिखाकर स्क्रीन बदलता है, उसका URL कभी नहीं बदलता, इसलिए ऐप "नेविगेशन" नहीं देख सकता। इसलिए ऐप एक टैप काउंटर डालता है (हर 2 सेकंड में ज़्यादा से ज़्यादा एक गिनती) जो यह मेथड कॉल करता है। जैसे ही ऐप एक असली URL बदलाव देखता है (पेज लोड, pushState या #hash), वह URL बदलाव गिनने लगता है और यह मेथड गिनना बंद कर देता है।
अगर आपकी स्क्रीन के बदलाव टैप से नहीं आते (जैसे कीबोर्ड या गेमपैड की चाल), तो उन्हें खुद रिपोर्ट करें:
function goToScreen(name) {
showScreen(name); // your own DOM show/hide
var b = window.WebToApk;
if (b && typeof b.notifyUserAction === 'function') b.notifyUserAction();
}
document.addEventListener('keydown', function (e) {
if (e.key === 'Enter') goToScreen('next');
});कार्ड-स्टाइल ऐप जिसमें यूज़र स्वाइप करके अगली स्क्रीन पर जाता है (स्वाइप बटन का टैप नहीं है, इसलिए टैप काउंटर उसे नहीं देखता):
var startX = null;
document.addEventListener('touchstart', function (e) { startX = e.touches[0].clientX; }, { passive: true });
document.addEventListener('touchend', function (e) {
if (startX === null) return;
var dx = e.changedTouches[0].clientX - startX;
startX = null;
if (Math.abs(dx) < 80) return; // not a swipe
showScreen(dx < 0 ? 'next' : 'previous'); // your own DOM show/hide
var b = window.WebToApk;
if (b && typeof b.notifyUserAction === 'function') b.notifyUserAction();
});गेमपैड से खेला जाने वाला HTML5 गेम, जो A बटन से शुरू किए हर लेवल पर एक क्रिया गिनता है:
var aWasDown = false;
function pollPad() {
var pad = navigator.getGamepads ? navigator.getGamepads()[0] : null;
var aDown = !!(pad && pad.buttons[0] && pad.buttons[0].pressed);
if (aDown && !aWasDown && isOnLevelSelect()) { // your own game state
startSelectedLevel();
var b = window.WebToApk;
if (b && typeof b.notifyUserAction === 'function') b.notifyUserAction();
}
aWasDown = aDown;
requestAnimationFrame(pollPad);
}
window.addEventListener('gamepadconnected', function () { requestAnimationFrame(pollPad); }, { once: true });नोट्स: विज्ञापन बंद होने पर, खरीदारी से हटाए जाने पर, या बिल्ड में Ad Control → Developer-controlled होने पर यह कुछ नहीं करता (वहाँ आप AppMintAds.show() खुद कॉल करते हैं)। इस तरह पहुँचा rewarded विज्ञापन हमेशा "Watch an ad? / No thanks" सवाल के साथ पेश किया जाता है, ज़बरदस्ती नहीं। इसे हर टैप पर कॉल न करें; इससे विज्ञापन आपकी सेट की गई सीमा से ज़्यादा बार दिखेंगे।
showInterstitialAd ब्रिज#
window.WebToApk.showInterstitialAd()
अभी interstitial दिखाएँ (जैसे लेवल के बीच / किसी सोची-समझी क्रिया पर)। अभी दिखाया नहीं जा सकता → छोड़ दिया जाता है + 'appmint:ad-unavailable' कारण के साथ।
उदाहरण
अभी एक full-screen interstitial विज्ञापन दिखाता है, जैसे गेम के दो लेवल के बीच (Automatic ad control)।
लौटाता है: कुछ नहीं। विज्ञापन बंद होने पर appmint:ad-closed फ़ायर होता है, {adType:'interstitial', earned:false} के साथ। न दिख पाने पर appmint:ad-unavailable फ़ायर होता है, {adType:'interstitial', reason} के साथ। ज़रूरत: विज्ञापन चालू और Full Screen Ad → Interstitial Ad। अगर सिर्फ़ आपका पेज तय करे कि कब, तो Trigger Mode → On Demand चुनें।
var bridge = window.WebToApk;
var waitingForAd = false;
function levelFinished() {
if (bridge && typeof bridge.isInterstitialAdReady === 'function' && bridge.isInterstitialAdReady()) {
waitingForAd = true;
bridge.showInterstitialAd(); // the next level starts when the ad closes
} else {
showNextLevel(); // no ad ready: just continue
}
}
window.addEventListener('appmint:ad-closed', function (e) {
if (e.detail.adType === 'interstitial' && waitingForAd) { waitingForAd = false; showNextLevel(); }
});
window.addEventListener('appmint:ad-unavailable', function (e) {
// reason: ad_free | disabled | consent | not_loaded | cooldown | show_failed
if (e.detail.adType === 'interstitial' && waitingForAd) { waitingForAd = false; showNextLevel(); }
});विज्ञापन से पहले गेम और उसकी आवाज़ रोकें, और किसी भी जवाब पर फिर चालू करें:
var pausedForAd = false;
function adBetweenRounds() {
var b = window.WebToApk;
if (!b || typeof b.showInterstitialAd !== 'function') return;
pausedForAd = true;
pauseGame(); music.pause();
b.showInterstitialAd();
}
function resumeAfterAd(e) {
if (e.detail.adType !== 'interstitial' || !pausedForAd) return;
pausedForAd = false;
music.play(); resumeGame();
}
window.addEventListener('appmint:ad-closed', resumeAfterAd);
window.addEventListener('appmint:ad-unavailable', resumeAfterAd);विज्ञापन के बाद लेख खोलें, एक Promise के रूप में जो विज्ञापन दिखने पर true और छोड़ दिए जाने पर false के साथ resolve होता है:
function interstitialThen() {
return new Promise(function (resolve) {
var b = window.WebToApk;
if (!b || typeof b.isInterstitialAdReady !== 'function' || !b.isInterstitialAdReady()) return resolve(false);
function done(e) {
if (e.detail.adType !== 'interstitial') return;
window.removeEventListener('appmint:ad-closed', done);
window.removeEventListener('appmint:ad-unavailable', done);
resolve(e.type === 'appmint:ad-closed');
}
window.addEventListener('appmint:ad-closed', done);
window.addEventListener('appmint:ad-unavailable', done);
b.showInterstitialAd();
});
}
interstitialThen().then(function () { openArticle(articleId); });नोट्स: ऐप दो interstitials के बीच कम से कम 60 सेकंड रखता है; उस अंतराल के भीतर कॉल का जवाब cooldown होता है। isInterstitialAdReady() में cooldown पहले से शामिल है, इसलिए पहले उसे जाँचने से इवेंट से बचा जा सकता है। interstitials स्वाभाविक ब्रेक पर दिखाएँ, हर टैप पर कभी नहीं। Developer-controlled बिल्ड में इस्तेमाल करें AppMintAds.show('interstitial').
showRewardedAd ब्रिज#
window.WebToApk.showRewardedAd()
अभी rewarded विज्ञापन दिखाएँ (यूज़र की पहल से - rewarded दिखाने का इकलौता पॉलिसी-अनुकूल तरीका)। विज़ार्ड में इसे ट्रिगर मोड "On Demand" के साथ जोड़ें ताकि rewarded विज्ञापन मिलें और कोई अपने-आप app-open/नेविगेशन विज्ञापन न हो। इनाम deliverRewardToPage() से पेज को लौटाया जाता है: window.addEventListener('appmint:reward', e => { /* e.detail = {type,amount} */ }) विज्ञापन बंद होने पर 'appmint:ad-closed' {adType:'rewarded', earned} के साथ फ़ायर होता है - अगर यूज़र जल्दी बाहर निकल गया तो earned false होता है, तब कुछ न दें। अभी दिखाया नहीं जा सकता → छोड़ दिया जाता है + 'appmint:ad-unavailable' कारण के साथ फ़ायर होता है।
उदाहरण
अभी rewarded विज्ञापन दिखाता है, यूज़र के अपने "Watch an ad" टैप से (Automatic ad control)।
लौटाता है: कुछ नहीं। नतीजा बाद में इवेंट के रूप में आता है: विज्ञापन पूरा देखा जाने पर appmint:reward {type, amount}, बंद होने पर appmint:ad-closed {adType:'rewarded', earned}, या कुछ भी न दिखा सकने पर appmint:ad-unavailable {adType:'rewarded', reason}। ज़रूरत: विज्ञापन चालू, Full Screen Ad → Rewarded Ad, और Trigger Mode → On Demand ताकि कुछ अपने-आप न दिखे।
इनाम सिर्फ़ reward हैंडलर में दें, टैप पर कभी नहीं:
var btn = document.getElementById('earn');
var bridge = window.WebToApk;
function refresh() {
btn.disabled = !(bridge && typeof bridge.isRewardedAdReady === 'function' && bridge.isRewardedAdReady());
}
refresh();
setInterval(refresh, 2000);
btn.addEventListener('click', function () { // a real user tap: the only place to call it
if (!bridge || typeof bridge.showRewardedAd !== 'function') return;
btn.disabled = true;
bridge.showRewardedAd();
});
// GRANT HERE ONLY: the ad was watched to the end.
window.addEventListener('appmint:reward', function (e) {
addCoins(e.detail.amount); // e.detail = {type, amount}
});
// The ad closed. earned === false means the user left early: grant nothing.
window.addEventListener('appmint:ad-closed', function (e) {
if (e.detail.adType === 'rewarded' && !e.detail.earned) showMessage('No reward: the ad was closed early.');
refresh();
});
// Nothing could be shown.
window.addEventListener('appmint:ad-unavailable', function (e) {
// e.detail = {adType, reason}; reason: ad_free | disabled | consent | not_loaded | show_failed
if (e.detail.reason === 'ad_free') addCoins(1); // the user paid to remove ads: reward anyway
else showMessage('No ad right now. Try again in a moment.');
refresh();
});वही तीन पल पेज हुक भी कॉल करते हैं, अगर आप हर एक के लिए एक फ़ंक्शन पसंद करें (हर एक को वही ऑब्जेक्ट मिलता है जो e.detail):
window.onAppMintReward = function (reward) { addCoins(reward.amount); }; // {type, amount}
window.onAppMintAdClosed = function (info) { console.log(info.adType, info.earned); };
window.onAppMintAdUnavailable = function (info) { console.log(info.adType, info.reason); };कॉल और उसके इवेंट को एक Promise में लपेटें, ताकि हर "Watch an ad" बटन सामान्य async कॉल जैसा लगे:
function watchRewardedAd() { // call it inside the user's tap
return new Promise(function (resolve) {
var b = window.WebToApk;
if (!b || typeof b.showRewardedAd !== 'function') return resolve({ earned: false, reason: 'no_app' });
function done(e) {
if (e.detail.adType !== 'rewarded') return;
window.removeEventListener('appmint:ad-closed', done);
window.removeEventListener('appmint:ad-unavailable', done);
resolve(e.type === 'appmint:ad-closed' ? { earned: e.detail.earned } : { earned: false, reason: e.detail.reason });
}
window.addEventListener('appmint:ad-closed', done);
window.addEventListener('appmint:ad-unavailable', done);
b.showRewardedAd();
});
}
document.getElementById('revive').addEventListener('click', function () {
watchRewardedAd().then(function (r) {
if (r.earned || r.reason === 'ad_free') revivePlayer();
else if (r.reason) showMessage('No ad right now.');
});
});नोट्स: इसे सिर्फ़ click हैंडलर से कॉल करें। टाइमर, पेज लोड या रूट बदलने से कभी नहीं, और कोई मुख्य फ़ीचर इसके पीछे न रखें। not_loaded भी नया लोड शुरू करता है, इसलिए अगला टैप आम तौर पर चल जाता है। Ad Control → Developer-controlled बिल्ड में इसके बजाय AppMintAds.show('rewarded') इस्तेमाल करें, जो Promise से जवाब देता है।