Complete example code

Miquel Martín edited this page Apr 29, 2016 · 3 revisions

Note that the tappx ads are configured inside onDeviceReady(). This is because only after device ready the Tappx Cordova plugin will be working.

    function initAds() {
      if (tappx) {
        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;

        tappx.setOptions({
          publisherId:      admobid.banner,
          interstitialAdId: admobid.interstitial,
          tappxIdiOS:       "/XXXXXXXXX/Pub-XXXX-iOS-IIII",
          tappxIdAndroid:   "/XXXXXXXXX/Pub-XXXX-Android-AAAA",
          tappxShare:       0.5
        });

        registerAdEvents();

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

    function onAdLoaded(e) {
      if (e.adType === tappx.AD_TYPE.INTERSTITIAL) {
        tappx.showInterstitialAd();
        showNextInterstitial = setTimeout(function() {
          tappx.requestInterstitialAd();
        }, 2 * 60 * 1000); // 2 minutes
      }
    }

    // optional, in case respond to events
    function registerAdEvents() {
      document.addEventListener(tappx.events.onAdLoaded, onAdLoaded);
      document.addEventListener(tappx.events.onAdFailedToLoad, function (e) {});
      document.addEventListener(tappx.events.onAdOpened, function (e) {});
      document.addEventListener(tappx.events.onAdClosed, function (e) {});
      document.addEventListener(tappx.events.onAdLeftApplication, function (e) {});
      document.addEventListener(tappx.events.onInAppPurchaseRequested, function (e) {});
    }

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

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

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

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