cordova-plugin-ads
v2.0.5
Published
Cordova plugin to add ads into an app. Use javascript to request ads by AdMob.
Downloads
10
Maintainers
Readme
Cordova-Plugin-Ads
jump to: PLUGIN USAGE | Cordova | ADD ADS | More | cozycode.ca | Open Source License
Updated 2023 for Cordova with tracking consent:
To include ads without any tracking, see: cordova-plugin-ads-donottrack
Cordova plugin to add ads (by Google AdMob) into an app. Use JavaScript to include ads in your app.
If you notice any issues, submit here: github issues
Have a good one!
PLUGIN USAGE
Add these calls inside of your cordova JavaScript to show ads:
banner ads
adMob.banner(bannerId,adMob.ad_sizes.RESIZE,adMob.ad_positions.TOP)
adMob.removeBanner()
full screen ads
adMob.interstitial(interstitialId)
adMob.showInterstitial()
rewarded video ads
adMob.rewarded(rewardedId)
adMob.showRewarded()
rewarded full screen video ads (new, and better supported than rewarded ads)
adMob.rewardedInterstitial(rewardedInterstitialId)
adMob.showRewardedInterstitial()
The plugin creates an adMob
object that can request to load new ads.
Note: to fund the development of the plugin, the plugin requests 2% of ads for the plugin developer. It seems there are a lot of Cordova ad plugins that request this lol, it can there even if not said.
CREATE AN APP WITH CORDOVA
How to create a Cordova app:
npm install -g cordova
cordova create directory_name com.your_name_or_company.your_app_name
Cordova is an open source project maintained by Apache that lets you make apps written in HTML, css, and JavaScript. You can create cross-platform apps to publish on the Google Play Store for Android, and the Appstore for iOS, MacOS, and Apple devices.
ADD ADS
- Add the plugin
cordova plugin add cordova-plugin-ads
- Test that ads are working using test ads. Use the id
"test"
to load test ads from the plugin.
var test_ad_id = "test";
async function runAllOfTheAds(){
await adMob.banner(test_ad_id).then(function(){
alert("loaded banner ads");
}).catch(function(err){
alert("unable to load ads: "+JSON.stringify(err));
});
await adMob.interstitial(test_ad_id).then(function(){
alert("loaded interstitial ads");;
return adMob.showInterstitial();
}).then(function(){
alert("showed interstitial ads");
}).catch(function(err){
alert("unable to load ads: "+JSON.stringify(err));
});
await adMob.rewarded(test_ad_id).then(function(){
console.log("loaded rewarded ads");
return adMob.showRewarded();
}).then(function(reward){
alert("showed rewarded ads"+JSON.stringify(reward));
}).catch(function(err){
alert("unable to load rewarded ads: "+JSON.stringify(err));
});
await adMob.rewardedInterstitial(test_ad_id).then(function(){
alert("loaded rewarded ads");
return adMob.showRewardedInterstitial();
}).then(function(reward){
alert("showed rewarded ads"+JSON.stringify(reward));
}).catch(function(err){
alert("unable to load rewarded ads: "+JSON.stringify(err));
});
}
function onDeviceReady() {
runAllOfTheAds();
}
- In your Google AdMob account, create ad units for your apps - note that the ads may return LOAD_AD_ERRORS errors (especially iOS) until the app has been approved, or while during testing
var admob_ids = {
'android' : {
'banner': "ca-app-pub-4029587076166791/6431168058",
'interstitial': "ca-app-pub-4029587076166791/1370413062",
'rewarded': "ca-app-pub-4029587076166791/9712771663",
'rewardedInterstitial': "ca-app-pub-4029587076166791/3530506691"
}, 'ios' : {
'banner': "ca-app-pub-4029587076166791/6694891931",
'interstitial': "ca-app-pub-4029587076166791/2436352227",
'rewarded': "ca-app-pub-4029587076166791/5286441495",
'rewardedInterstitial': "ca-app-pub-4029587076166791/2300620853"
}
};
adMob.banner(admob_ids).then(function(){
console.log("loaded banner ad");
}).catch(function(err){
console.log("unable to load banner ad: "+JSON.stringify(err));
});
adMob.interstitial(admob_ids).then(function(){
console.log("loaded interstitial ads");;
return adMob.showInterstitial();
}).then(function(){
console.log("showed interstitial ads");
}).catch(function(err){
console.log("unable to load interstitial ads: "+JSON.stringify(err));
});
adMob.rewarded(admob_ids).then(function(){
console.log("loaded rewarded ads");
return adMob.showRewarded();
}).then(function(reward){
console.log("showed rewarded ads"+JSON.stringify(reward));
}).catch(function(err){
console.log("unable to load rewarded ads: "+JSON.stringify(err));
});
adMob.rewardedInterstitial(admob_ids).then(function(){
console.log("loaded rewarded ads");
return adMob.showRewardedInterstitial();
}).then(function(reward){
console.log("showed rewarded ads"+JSON.stringify(reward));
}).catch(function(err){
console.log("unable to load rewarded ads: "+JSON.stringify(err));
});
More on the plugin:
- wait for
deviceready
to fire before calling any functions - errors can be automatically retried after a timeout or ignored
adMob
uses a promise-based API to load ads- since the API returns a promise, it is asynchronous... call it and continue with other actions, or await for it to finish from within an async function to wait for the call to finish
More from cozycode.ca
- make apps with JavaScript
- how to make an in-app purchases with Cordova
- a testing app for in-app purchases
- a demo app for in-app purchases
Open Source License
MIT Licensed (MIT)
Copyright © 2023 cozycode.ca
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.