// Initialize Adsgram with the configured block ID const AdController = window.Adsgram.init({ blockId: "16505" }); // Store reward callback function let rewardCallback = null; let errorCallback = null; // Function to show ad (called from Dart) function showAd() { return AdController.show().then((result) => { // user watch ad till the end or close it in interstitial format // your code to reward user for rewarded format console.log('Ad completed successfully', result); // Call reward callback if set if (window.rewardCallback && typeof window.rewardCallback === 'function') { window.rewardCallback(); } return result; }).catch((result) => { // user get error during playing ad // do nothing or whatever you want console.error('Ad failed', result); // Call error callback if set if (window.errorCallback && typeof window.errorCallback === 'function') { window.errorCallback(JSON.stringify(result, null, 4)); } throw result; }); } // Function to show ad with specific block ID function showAdWithBlockId(blockId) { const dynamicController = window.Adsgram.init({ blockId: blockId }); return dynamicController.show().then((result) => { console.log('Ad completed successfully for block', blockId, result); // Call reward callback if set if (window.rewardCallback && typeof window.rewardCallback === 'function') { window.rewardCallback(); } return result; }).catch((result) => { console.error('Ad failed for block', blockId, result); // Call error callback if set if (window.errorCallback && typeof window.errorCallback === 'function') { window.errorCallback(JSON.stringify(result, null, 4)); } throw result; }); } // Function to set reward callback from Dart function setRewardCallback(callback) { window.rewardCallback = callback; } // Function to set error callback from Dart function setErrorCallback(callback) { window.errorCallback = callback; }