3

I'm using AdMob plugin in Ionic and with this code I show an 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 intersitial ad every 2 minutes? Someone told me to add this: setInterval(showInterstitial,1*60*1000), but I don't know where to add?

2
  • did the answers provided help you solve your issue?
    – Nikola
    Jul 2, 2015 at 20:05
  • Which Admob plugin are you using?
    – Paul
    Mar 30, 2016 at 6:40

4 Answers 4

3

If you would like to show it every 2 minutes you should use:

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

and you should add it just before the closing bracket of your initAdd function:

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);

 }

You can see a simple setInterval usage on this jsFiddle example:

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

setInterval(a, 2*1000);

The reason why you shouldn't call it like this (note the brackets after a): setInterval(a(), 2*1000); is that then your function would be called only once (you would see only one alert popping up). Example on jsFiddle:

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

setInterval(a(), 2*1000);

Hope this helps clear things a bit.

2
  • Which Admob plugin did you write this code from? I ask because there are several.
    – Paul
    Mar 30, 2016 at 6:41
  • I used this one. You can read the full tutorial here.
    – Nikola
    Mar 30, 2016 at 6:57
2

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

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
});

BTW, showing interstitial ad based on time interval is not a good idea, as it may bring poor user experience and violate Google rules.

It will be better to prepareInterstitial() in background, then showInterstitial() when some page or state change, for example, when gamer over and user click the OK button.

1

By using the plugin at https://github.com/appfeel/admob-google-cordova you could listen to 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);
0

As by now this is illegal in admob, your id will get probably disabled for this, as well as showing add on load, back buttons, to many interstitial for simple apps,etc Bottom line is, if you wanna make any money at all, you have to show interstitial ads,as admob pays for click not for views, and no one ever clicks on banner.

So best practice would be show ad after X-clicks(set up "click counter") and cap your ids in admob it self. Or your account will bet banned like my did

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.