Show interstitial ad via AdMob in Ionic every 2 minutes

I am using AdMob plugin on Ionic and with this code I am displaying Interstital ad:

 function initAd(){
     // it will display smart banner at top center, using the default options
     if(AdMob) AdMob.createBanner( {
         adId: admobid.banner,
         bannerId: admobid.banner,
         position: AdMob.AD_POSITION.BOTTOM_CENTER,
         autoShow: true,
         isTesting: false,
         success: function() {
             console.log('banner created');
         },
         error: function() {
             console.log('failed to create banner');
         }
     });


    window.AdMob.prepareInterstitial({
        adId:admobid.interstitial, autoShow:false
    });
    window.AdMob.showInterstitial();
}

      

Is there a way to show an interpersonal ad every 2 minutes? Someone told me to add this: setInterval(showInterstitial,1*60*1000)

but I don't know where to add?

+3


source to share


4 answers


If you want to show it every 2 minutes you should use:

setInterval(window.AdMob.showInterstitial, 2*60*1000);

      

and you have to add it just before the closing parenthesis of your function initAdd

:

function initAd(){


 // it will display smart banner at top center, using the default options
 if(AdMob) AdMob.createBanner( {
                          adId: admobid.banner,
                          bannerId: admobid.banner,
                          position:AdMob.AD_POSITION.BOTTOM_CENTER,
                          autoShow: true,
                          isTesting: false,
                          success: function(){
                          console.log('banner created');
                          },
                          error: function(){
                         console.log('failed to create banner');
                          }
                          } );

                                       window.AdMob.prepareInterstitial( 
                           {adId:admobid.interstitial, autoShow:false} );
    window.AdMob.showInterstitial();
  
  
  
  //!!!add the code here!!! - so, just paste what I wrote above:
  setInterval(window.AdMob.showInterstitial, 2*60*1000);

 }
      

Run codeHide result


You can see a simple use of setInterval in this jsFiddle example :



function a(){
    alert("hi every 2 seconds");
};

setInterval(a, 2*1000);
      

Run codeHide result


The reason why you shouldn't call it that way (note the parentheses after a

): setInterval(a(), 2*1000);

is that then your function will only be called once (you will only see one warning popped up). JsFiddle example :

function a(){
    alert("hi every 2 seconds");
};

setInterval(a(), 2*1000);
      

Run codeHide result


Hope this helps a little to understand.

+3


source


Using the plugin https://github.com/appfeel/admob-google-cordova you can listen for the onAdLoaded and onAdClosed event and make autoShowInterstitial false:



var isAppForeground = true;

function initAds() {
  if (admob) {
    var adPublisherIds = {
      ios : {
        banner : "ca-app-pub-XXXXXXXXXXXXXXXX/BBBBBBBBBB",
        interstitial : "ca-app-pub-XXXXXXXXXXXXXXXX/IIIIIIIIII"
      },
      android : {
        banner : "ca-app-pub-XXXXXXXXXXXXXXXX/BBBBBBBBBB",
        interstitial : "ca-app-pub-XXXXXXXXXXXXXXXX/IIIIIIIIII"
      }
    };

    var admobid = (/(android)/i.test(navigator.userAgent)) ? adPublisherIds.android : adPublisherIds.ios;

    admob.setOptions({
      publisherId:          admobid.banner,
      interstitialAdId:     admobid.interstitial,
      autoShowInterstitial: false
    });

    registerAdEvents();

  } else {
    alert('AdMobAds plugin not ready');
  }
}

function onAdLoaded(e) {
  if (isAppForeground) {
    if (e.adType === admob.AD_TYPE.INTERSTITIAL) {
      admob.showInterstitialAd();
    }
  }
}

function onAdClosed(e) {
  if (isAppForeground) {
    if (e.adType === admob.AD_TYPE.INTERSTITIAL) {
      setTimeout(admob.requestInterstitialAd, 1000 * 60 * 2);
    }
  }
}

function onPause() {
  if (isAppForeground) {
    admob.destroyBannerView();
    isAppForeground = false;
  }
}

function onResume() {
  if (!isAppForeground) {
    setTimeout(admob.createBannerView, 1);
    setTimeout(admob.requestInterstitialAd, 1);
    isAppForeground = true;
  }
}

// optional, in case respond to events
function registerAdEvents() {
  document.addEventListener(admob.events.onAdLoaded, onAdLoaded);
  document.addEventListener(admob.events.onAdClosed, onAdClosed);

  document.addEventListener("pause", onPause, false);
  document.addEventListener("resume", onResume, false);
}

function onDeviceReady() {
  document.removeEventListener('deviceready', onDeviceReady, false);
  initAds();

  // display a banner at startup
  admob.createBannerView();

  // request an interstitial
  admob.requestInterstitialAd();
}

document.addEventListener("deviceready", onDeviceReady, false);

      

+2


source


I am the author of the cordova admob plugin if you are using Ionic ngCordova. Here is my suggestion for your goal.

var interstitialReady = false;

// update the state when ad preloaded
document.addEventListener('onAdLoaded', function(e){
    if(e.adType == 'interstitial') {
        interstitialReady = true;
    }
});

// when dismissed, preload one for next show
document.addEventListener('onAdDismiss', function(e){
    if(e.adType == 'interstitial') {
        interstitialReady = false;
        AdMob.prepareInterstitial({
           adId:admobid.interstitial, 
           autoShow:false
        });
    }
});

setInterval(function(){
    if(interstitialReady) AdMob.showInterstitial();
}, 2*60*1000);

// preload the first ad
AdMob.prepareInterstitial({
    adId:admobid.interstitial, 
    autoShow:false
});

      

By the way, it is not a good idea to run an interstitial ad based on a time interval, as it can lead to a bad user experience and violate Google's policies.

Better to prepare Interstitial () in the background, then showInterstitial () when any page or state changes, such as when the player and user click OK.

+2


source


As it is currently illegal in admob, your id will probably be disabled for this, and will also show uploads, return buttons to many interstitial for simple apps, etc. Bottom line: If you want to make any money at all, you must serve interstitial ads, since admob pays per click, not views, and no one clicks on the banner.

So the best practice would be to show ads after X-clicks (setting "click count") and limit your IDs to admob it self. Or your account will be banned like me.

0


source







All Articles