Code Monkey home page Code Monkey logo

admob-google-cordova's Introduction

admob-google-cordova

Monetize your apps with AdMob ads for Cordova Android/iOS. With this Cordova AdMob plugin you can show AdMob ads as easy as:

admob.createBannerView({publisherId: "ca-app-pub-xxx/7078073011"});

Or

admob.requestInterstitialAd({interstitialAdId: "ca-app-pub-xxx/7078073011", autoShowInterstitial: true});

Platform SDK supported

  • iOS, using AdMob SDK for iOS, v6.11.1
  • Android, using Google Play Service for Android, v4.4

How to use

To install this plugin, follow the Command-line Interface Guide.

cordova plugin add com.admob.google

Or,

cordova plugin add https://github.com/appfeel/admob-google-cordova.git

In your onDeviceReady callback:

    
    function onDeviceReady() {
      document.removeEventListener('deviceready', onDeviceReady, false);
      admob.createBannerView({ 'publisherId' : 'ca-app-pub-xxx/7078073011' });
    }
    
    document.addEventListener("deviceready", onDeviceReady, false);

Note: ensure you have a proper AdMob account and create an Id for your app.

Quick example with cordova CLI

    cordova create <project_folder> com.<company_name>.<app_name> <AppName>
    cd <project_folder>
    cordova platform add android
    cordova platform add ios

    // cordova will handle dependency automatically
    cordova plugin add com.admob.google

    // now remove the default www content, copy the demo html file to www
    rm -rf www/*;
    cp plugins/com.admob.google/test/* www/

    cordova prepare; cordova run android; cordova run ios;
    // or import into Xcode / eclipse

Javascript API

APIs:

setOptions(options, success, fail);

createBannerView(options, success, fail);
showAd(true/false, success, fail); 
destroyBannerView();

requestInterstitialAd(options, success, fail);
showInterstitialAd();

Complete example code

Call the following code inside onDeviceReady(), because only after device ready you will have the plugin working.

    function initAds() {
      if (admob) {
        var adPublisherIds = {
          ios : {
            banner: 'ca-app-pub-xxx/7078073011',
            interstitial: 'ca-app-pub-xxx/8554806210'
          },
          android : {
            banner: 'ca-app-pub-xxx/4806197152',
            interstitial: 'ca-app-pub-xxx/9791193812'
          }
        };
    	  
        var admobid = (/(android)/i.test(navigator.userAgent)) ? adPublisherIds.android : adPublisherIds.ios;
            
        admob.setOptions({
          publisherId: admobid.banner,
          interstitialAdId: admobid.interstitial,
          bannerAtTop: false, // set to true, to put banner at top
          overlap: false, // set to true, to allow banner overlap webview
          offsetTopBar: false, // set to true to avoid ios7 status bar overlap
          isTesting: false, // receiving test ads (do not test with real ads as your account will be banned)
          autoShowBanner: true, // auto show banners ad when loaded
          autoShowInterstitial: true // auto show interstitials ad when loaded
        });

        registerAdEvents();
        
      } else {
        alert('AdMobAds plugin not ready');
      }
    }
    
    function onAdLoaded(e) {
      if (e.adType === admob.AD_TYPE.INTERSTITIAL) {
        admob.showInterstitialAd();
        showNextInterstitial = setTimeout(function() {
          admob.requestInterstitialAd();
        }, 2 * 60 * 1000); // 2 minutes
      }
    }
    
    // optional, in case respond to events
    function registerAdEvents() {
      document.addEventListener(admob.events.onAdLoaded, onAdLoaded);
      document.addEventListener(admob.events.onAdFailedToLoad, function (e) {});
      document.addEventListener(admob.events.onAdOpened, function (e) {});
      document.addEventListener(admob.events.onAdClosed, function (e) {});
      document.addEventListener(admob.events.onAdLeftApplication, function (e) {});
      document.addEventListener(admob.events.onInAppPurchaseRequested, function (e) {});
    }
        
    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);

See the working example code in demo under test folder.

Donate

You can use this cordova plugin for free. To support this project, donation is welcome.
Donation can be accepted in either of following ways:

  • Share 2% Ad traffic. (It's not mandatory. If you are unwilling to share, please fork and remove the donation code.)

Credits

floatinghotpot/cordova-plugin-admob

aliokan/cordova-plugin-admob

Screenshots

iPhone:

ScreenShot

iPad Banner Portrait:

ScreenShot

iPad Banner Landscape:

ScreenShot

admob-google-cordova's People

Contributors

appfeel avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.