Cordova, Phonegap Ionic and Intel XDK AdMob ads plugin
Maintained by appfeel
Made in Barcelona with Love and Code
Monetize your Cordova/Phonegap/Ionic(Angular)/XDK apps with AdMob ads, using latest Google AdMob SDK. With this Cordova/Phonegap/XDK plugin you can show AdMob ads as easy as:
admob.createBannerView({publisherId: "ca-app-pub-XXXXXXXXXXXXXXXX/BBBBBBBBBB"});
Or
admob.requestInterstitialAd({
publisherId: "ca-app-pub-XXXXXXXXXXXXXXXX/BBBBBBBBBB",
interstitialAdId: "ca-app-pub-XXXXXXXXXXXXXXXX/IIIIIIIIII"
});
Connectivity detection: When a new connection to WiFi or 3G/GPRS is detected, the ads are automatically shown if you asked to show them before and there was no connection.
Integration with tappx: the developers community that exchanges advertisement to promote their apps. You can advertise your app for FREE. You can decide how much inventory you divert to Tappx.
Back-filling: your lost inventory is delivered to tappx (you must have a free account).
Google Libraries externalitzation: The libraries are fetched from external repository, to help it keeping updated.
Intel XDK: Added support to Intel XDK platform and uploaded examples on how to integrate it.
It was really easy to integrate, thanks.
To install this plugin, follow the Command-line Interface Guide. You can use one of the following command lines:
cordova plugin add cordova-admob
cordova plugin add https://github.com/appfeel/admob-google-cordova.git
To use in Phonegap Build, place the following tag in your config.xml
file:
<gap:plugin name="cordova-admob" source="npm" />
To start showing ads, place the following code in your onDeviceReady
callback. Replace corresponding id's with yours:
Note: ensure you have a proper AdMob and tappx accounts and get your publisher id's.
function onDeviceReady() {
document.removeEventListener('deviceready', onDeviceReady, false);
// Set AdMobAds options:
admob.setOptions({
publisherId: "ca-app-pub-XXXXXXXXXXXXXXXX/BBBBBBBBBB", // Required
interstitialAdId: "ca-app-pub-XXXXXXXXXXXXXXXX/IIIIIIIIII", // Optional
tappxIdiOS: "/XXXXXXXXX/Pub-XXXX-iOS-IIII", // Optional
tappxIdAndroid: "/XXXXXXXXX/Pub-XXXX-Android-AAAA", // Optional
tappxShare: 0.5 // Optional
});
// Start showing banners (atomatic when autoShowBanner is set to true)
admob.createBannerView();
// Request interstitial (will present automatically when autoShowInterstitial is set to true)
admob.requestInterstitialAd();
}
document.addEventListener("deviceready", onDeviceReady, false)
If you don't specify tappxId, no tappx requests will be placed (even if you specify a tappxShare).
Include the following script in your index.html
(just it, no need to copy any file: the plugin is in charge to copy the script when the app is prepared):
<script src="lib/angular-admob/angular-admob.js"></script>
Here is a quick example on how to use Admob plugin with Angular.js based apps, for example Ionic:
var app = angular.module('myApp', ['admobModule']);
app.config(['admobSvcProvider', function (admobSvcProvider) {
// Optionally you can configure the options here:
admobSvcProvider.setOptions({
publisherId: "ca-app-pub-XXXXXXXXXXXXXXXX/BBBBBBBBBB", // Required
interstitialAdId: "ca-app-pub-XXXXXXXXXXXXXXXX/IIIIIIIIII", // Optional
tappxIdiOS: "/XXXXXXXXX/Pub-XXXX-iOS-IIII", // Optional
tappxIdAndroid: "/XXXXXXXXX/Pub-XXXX-Android-AAAA", // Optional
tappxShare: 0.5 // Optional
});
// Optionally configure the events prefix (by default set to 'admob:')
admobSvcProvider.setPrefix('myTag~');
}]);
app.run(['admobSvc', function (admobSvc) {
// Also you could configure the options here (or in any controller):
// admobSvcProvider.setOptions({ ... });
admobSvc.createBannerView();
// You could also call admobSvc.createBannerView(options);
// Handle events:
$rootScope.$on('myTag~' + admobSvc.events.onAdOpened, function onAdOpened(evt, e) {
console.log('adOpened: type of ad:' + e.adType);
});
// The default prefix for events is 'admob:'
// $rootScope.$on('admob:' + admobSvc.events...
}]);
Note: All success callbacks are in the form 'function () {}'
, and all failure callbacks are in the form 'function (err) {}'
where err
is a String explaining the error reason.
Set the options to start displaying ads:
A JSON object whith the following fields:
publisherId
.false
.false
.false
.false
.admob.events.onAdLoaded
event is called). Default true
.admob.events.onAdLoaded
event is called). Default true
.Example (those are also the default options, except for tappxId's which are empty by default):
{
publisherId: "ca-app-pub-XXXXXXXXXXXXXXXX/BBBBBBBBBB",
interstitialAdId: "ca-app-pub-XXXXXXXXXXXXXXXX/IIIIIIIIII",
tappxIdiOS: "/XXXXXXXXX/Pub-XXXX-iOS-IIII",
tappxIdAndroid: "/XXXXXXXXX/Pub-XXXX-Android-AAAA",
tappxShare: 0.5,
adSize: admob.AD_SIZE.SMART_BANNER,
bannerAtTop: false,
overlap: false,
offsetStatusBar: false,
isTesting: false,
adExtras : {},
autoShowBanner: true,
autoShowInterstitial: true
}
Create a new banner view.
Show banner ads.
Hide and destroy banner view.
Request an interstitial ad.
If options.autoShowInterstitial
is set to true, the ad will automatically be displayed. Otherwise you should call showInterstitialAd()
when admob.events.onAdLoaded
event is called.
If you already called requestInterstitialAd()
but the interstitial was never shown, the following calls to requestInterstitialAd()
will result in the ad being inmediately available (the same ad as the one in the first call).
Show an interstitial ad.
This method must be called after admob.events.onAdLoaded
event is called. If there is no ad available it calls fail callback.
Records the purchase status and conversion events.
admob.events.onInAppPurchase
event is called).Records the purchase status and conversion events for a play billing purchase.
admob.events.onInAppPurchase
event is called).AdMobAds Cordova library will use the same events for Android as for iOS (the iOS ones are mapped to the Android ones). See Google Docs for more info.
To listen to any of those events you can use:
document.addEventListener(admob.events.onAdLoaded, function (e) { });
Called when an ad is received.
{ adType : "banner" }
admob.AD_TYPE.BANNER
or admob.AD_TYPE.INTERSTITIAL
Called when an ad request failed.
{ adType : "[string]", error : [integer], reason : "[string]" }
Called when an ad opens an overlay that covers the screen. Please note that onPause
event is raised when an interstitial is shown.
{ adType : "banner" }
admob.AD_TYPE.BANNER
or admob.AD_TYPE.INTERSTITIAL
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.
{ adType : "banner" }
admob.AD_TYPE.BANNER
or admob.AD_TYPE.INTERSTITIAL
Called when an ad leaves the application (e.g., to go to the browser).
{ adType : "banner" }
admob.AD_TYPE.BANNER
or admob.AD_TYPE.INTERSTITIAL
Called when the user clicks the buy button of an in-app purchase ad. You shoud complete the transaction by calling admob.recordResolution(...)
or admob.recordPlayBillingResolution(...)
.
{ adType : "banner" }
admob.AD_TYPE.BANNER
or admob.AD_TYPE.INTERSTITIAL
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 cordova-admob
// now remove the default www content, copy the demo html file to www
rm -rf www/*;
cp plugins/cordova-admob/test/* www/
cordova prepare; cordova run android; cordova run ios;
// or import into Xcode / eclipse
Note that the admob ads are configured inside onDeviceReady()
. This is because only after device ready the AdMob Cordova plugin will be working.
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,
tappxIdiOS: "/XXXXXXXXX/Pub-XXXX-iOS-IIII",
tappxIdAndroid: "/XXXXXXXXX/Pub-XXXX-Android-AAAA",
tappxShare: 0.5
});
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);
You can use this cordova plugin for free. You can contribute to this project in many ways:
You can also support this project by sharing 2% Ad traffic (it's not mandatory: if you are unwilling to share, please fork and remove the donation code) and by donations via paypal
iPhone:
iPad Banner Portrait:
iPad Banner Landscape:
The MIT License
Copyright (c) 2014 AppFeel
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.