Cordova / PhoneGap Plugin for Adivery.
This Cordova / PhoneGap plugin enables displaying mobile Ads with single line of javascript code. Designed for the use in HTML5-based cross-platform hybrid games and other applications.
Platforms supported:
- Amazon-FireOS, via Android SDK (part of Google Play service)
- Android, via Android SDK (part of Google Play service)
- iOS
- Windows Phone
Ad Types:
- Banner
- Interstitial
- Reward Video
- Native Ads
- Native Ads Advanced
- AppOpen Ads
To quickly test this plugin and see ads on your devices use commands bellow.
# install cordova CLI
[sudo] npm install cordova -g
# install a small utility to run all the commands for you
[sudo] npm install plugin-verify -g
# Demo: run adivery demo with sample index.html
plugin-verify adivery-cordova-plugin
# create a demo project
cordova create test1 com.adivery.example Test1
cd test1
cordova platform add android
# now add the plugin, cordova CLI will handle dependency automatically
cordova plugin add adivery-cordova-plugin
# now remove the default www content, copy the demo html file to www
rm -r www/*;
cp plugins/adivery-cordova-plugin/test/* www/;
# now build and run the demo in your device or emulator
cordova prepare;
cordova run android;
# or import into eclipse
- If use with Cordova CLI:
cordova plugin add adivery-cordova-plugin
- If use with PhoneGap Build:
<plugin name="adivery-cordova" source="npm"></plugin>
Notice:
- If build locally using
adivery-cordova
, to avoid build error, you need install some extras in Android SDK manager (typeandroid sdk
to launch it):
Show Mobile Ad with single line of javascript code.
Create new application, in Adivery portal, then write it in your javascript code and replace AppID with the ID generated for your application in Adivery panel.
Adivery.initialize('AppID');
To create banner ads in your application use code bellow.
// it will display small banner at top center, using the default options
Adivery.createBanner(zoneId, Adivery.AD_POSITION.TOP_CENTER, Adivery.AD_SIZE.BANNER_SMALL);
To prepare Interstitial ads on your application run code bellow.
// prepare and load interstitial ad resource in background, e.g. at begining of game level
Adivery.prepareInterstitialAd(zoneId);
- :alert: Adivery will automatically request next ad when current ad is displayed and there is no need to call
prepareInterstitialAd
again.
To get events related to Interstitial ads use code bellow.
document.addEventListener('onInterstitialAdLoaded', function(event) {
if (typeof event.originalEvent !== 'undefined') event = event.originalEvent;
var data = eevent.detail || event.data || event;
console.log("Adivery InterstitialAd Loaded", data.placementId);
});
To prepare Rewarded ads on your application use code bellow.
// prepare and load Rewarded ad resource in background, e.g. at begining of game level
Adivery.prepareRewardedAd(zoneId);
- :alert: Adivery will automatically request next ad when current ad is displayed and there is no need to call
prepareRewardedAd
again.
To get events related to rewarded ads use code bellow.
document.addEventListener('onRewardedAdClosed', function(event) {
if (typeof event.originalEvent !== 'undefined') event = event.originalEvent;
var data = eevent.detail || event.data || event;
console.log("Adivery RewardedAd closed, isRewared:", data.isRewarded);
});
To prepare AppOpen ads on your application run code bellow.
// prepare and load interstitial ad resource in background, e.g. at begining of game level
Adivery.prepareAppOpenAd(zoneId);
- :alert: Adivery will automatically request next ad when current ad is displayed and there is no need to call
prepareInterstitialAd
again.
To get events related to Interstitial ads use code bellow.
document.addEventListener('onAppOpenAdLoaded', function(event) {
if (typeof event.originalEvent !== 'undefined') event = event.originalEvent;
var data = eevent.detail || event.data || event;
console.log("Adivery AppOpen Loaded", data.placementId);
});
To display ads just call showAd
or showAppOpenAd
depending on if you want to display interstitial, rewareded ads or AppOpen ads.
Adivery.showAd('ZoneId');
Adivery.showAppOpenAd('ZoneId');
To get a Native ad use requestNativeAd
function to load a native ad.
Adivery.requestNativeAd('ZoneId`);
To get loaded ad and begin displaying it, use this code.
document.addEventListener('nativeAdLoaded', function(event) {
if (typeof event.originalEvent !== 'undefined') event = event.originalEvent;
var data = eevent.detail || event.data || event;
console.log(data['headline']);
console.log(data['description']);
console.log(data['advertiser']);
console.log(data['callToAction']);
console.log(data['icon']);
console.log(data['image']);
});
To record Impression of native ad in your application use this code.
Please have in mind to call this code only and only if Ad is displayed to the user.
Adivery.recordNativeAdImpression();
Finally to record click of native ads, use this code.
Please have in mine, calling this function will navigate user to market or web browser depending on what ad is dispaying, so call it when user actually clicks on CTA button.
Adivery.recordNativeAdClick();
Methods:
// initialize plugin
initialize(AppID);
setLoggingEnabled(logEnabled);
// use banner
createBanner(zoneId, position, size);
createBannerAtXY(zoneId, x, y, size);
removeBanner();
showBanner();
hideBanner();
// use native
requestNativeAd(zoneId);
recordNativeAdImpression();
recordNativeAdClick();
// use interstitial/rewarded
prepareInterstitialAd(zoneId);
prepareRewardedAd(zoneId);
showAd(zoneId);
// use app open ads
prepareAppOpenAd(zondId);
showAppOpenAd(zoneId);
Events:
// onAdLoaded
// onAdRewarded
// onShowFailed
document.addEventListener('onBannerAdClicked', function(e){
// handle the event
// no data will be passed to this event
});
document.addEventListener('onBannerAdLoaded', function(e){
// handle the event
// no data will be passed to this event
});
document.addEventListener('onBannerAdShown', function(e){
// handle the event
// no data will be passed to this event
});
document.addEventListener('nativeAdClicked', function(e){
// handle the event
// no data will be passed to this event
});
document.addEventListener('nativeAdLoadFailed', function(e){
// handle the event
if (typeof e.originalEvent !== 'undefined') e = e.originalEvent;
var data = e.detail || e.data || e;
consoleLog("onAdLoaded. Type: " + data.message);
});
document.addEventListener('nativeAdLoaded', function(e){
// handle the event
if (typeof e.originalEvent !== 'undefined') e = e.originalEvent;
var data = e.detail || e.data || e;
console.log("native ad loaded", data['headline']);
console.log("native ad loaded", data['advertiser']);
console.log("native ad loaded", data['description']);
console.log("native ad loaded", data['callToAction']);
console.log("native ad loaded", data['icon']);
console.log("native ad loaded", data['image']);
});
document.addEventListener('nativeAdShown', function(e){
// handle the event
// no data will be passed to this event
});
document.addEventListener('onAppOpenAdClicked', function(e){
// handle the event
if (typeof e.originalEvent !== 'undefined') e = e.originalEvent;
var data = e.detail || e.data || e;
consoleLog("onAdLoaded. Type: " + data.placement);
});
document.addEventListener('onAppOpenAdClosed', function(e){
// handle the event
if (typeof e.originalEvent !== 'undefined') e = e.originalEvent;
var data = e.detail || e.data || e;
consoleLog("onAdLoaded. Type: " + data.placement);
});
document.addEventListener('onAppOpenAdLoaded', function(e){
// handle the event
if (typeof e.originalEvent !== 'undefined') e = e.originalEvent;
var data = e.detail || e.data || e;
consoleLog("onAdLoaded. Type: " + data.placement);
});
document.addEventListener('onAppOpenAdShown', function(e){
// handle the event
if (typeof e.originalEvent !== 'undefined') e = e.originalEvent;
var data = e.detail || e.data || e;
consoleLog("onAdLoaded. Type: " + data.placement);
});
document.addEventListener('onInterstitialAdClicked', function(e){
// handle the event
if (typeof e.originalEvent !== 'undefined') e = e.originalEvent;
var data = e.detail || e.data || e;
consoleLog("onAdLoaded. Type: " + data.placement);
});
document.addEventListener('onInterstitialAdClosed', function(e){
// handle the event
if (typeof e.originalEvent !== 'undefined') e = e.originalEvent;
var data = e.detail || e.data || e;
consoleLog("onAdLoaded. Type: " + data.placement);
});
document.addEventListener('onInterstitialAdLoaded', function(e){
// handle the event
if (typeof e.originalEvent !== 'undefined') e = e.originalEvent;
var data = e.detail || e.data || e;
consoleLog("onAdLoaded. Type: " + data.placement);
});
document.addEventListener('onInterstitialAdShown', function(e){
// handle the event
if (typeof e.originalEvent !== 'undefined') e = e.originalEvent;
var data = e.detail || e.data || e;
consoleLog("onAdLoaded. Type: " + data.placement);
});
document.addEventListener('onRewardedAdClicked', function(e){
// handle the event
if (typeof e.originalEvent !== 'undefined') e = e.originalEvent;
var data = e.detail || e.data || e;
consoleLog("onAdLoaded. Type: " + data.placement);
});
document.addEventListener('onRewardedAdClosed', function(e){
// handle the event
if (typeof e.originalEvent !== 'undefined') e = e.originalEvent;
var data = e.detail || e.data || e;
consoleLog("onAdLoaded. Type: " + data.placement);
consoleLog("onAdClosed, reward: " + data.isRewarded);
});
document.addEventListener('onRewardedAdLoaded', function(e){
// handle the event
if (typeof e.originalEvent !== 'undefined') e = e.originalEvent;
var data = e.detail || e.data || e;
consoleLog("onAdLoaded. Type: " + data.placement);
});
document.addEventListener('onRewardedAdShown', function(e){
// handle the event
if (typeof e.originalEvent !== 'undefined') e = e.originalEvent;
var data = e.detail || e.data || e;
consoleLog("onAdLoaded. Type: " + data.placement);
});
document.addEventListener('onError', function(e){
// handle the event
if (typeof e.originalEvent !== 'undefined') e = e.originalEvent;
var data = e.detail || e.data || e;
consoleLog("onAdLoaded. Type: " + data.placement);
consoleLog("onAdLoaded. Type: " + data.message);
});
You can use the plugin for free.
This project is created and maintained by Milad Mohammadi and then updated to latest fearures by Adivery Team.
More Cordova/PhoneGap plugins by Milad Mohammadi, find them in plugin registry, or find them in npm.
More about Adivery