2

I have set up my Admob to serve ads to specific every 30 seconds within my cordova/ionic based App. Is there a way to capture when a new ad is sent or when an add rotates from the API? Or even a way to capture when a user clicks on an ad?

I know Adsense and the Admob administration provides all these details and reports, but I am looking for a way to do a basic capture per user - ie: how many ads were served for a particular user during a single session of the app, did the particular user click on any of the ads, etc...

rolinger
  • 2,787
  • 1
  • 31
  • 53

1 Answers1

2

You can do it with the events provided by cordova-admob. See here: https://github.com/appfeel/admob-google-cordova/wiki/Events

Here you go a complete example of using events:

var isAppForeground = true;

function onAdLoaded(e) {
  // Called when an ad is received.
  if (isAppForeground) {
    if (e.adType === admob.AD_TYPE.INTERSTITIAL) {
      admob.showInterstitialAd();
    }
  }
}

function onAdClosed(e) {
  // Called when the user is about to return to the application after clicking on an ad. Please note that onResume event is raised when an interstitial is closed.
  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;
  }
}

function registerAdEvents() {
  // See https://github.com/appfeel/admob-google-cordova/wiki/Events
  document.addEventListener(admob.events.onAdLoaded, onAdLoaded);
  document.addEventListener(admob.events.onAdClosed, onAdClosed);

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

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 onDeviceReady() {
  document.removeEventListener('deviceready', onDeviceReady, false);
  initAds();

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

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

document.addEventListener("deviceready", onDeviceReady, false);
Miquel
  • 8,339
  • 11
  • 59
  • 82