0

Corona SDK AdMob Template – Banner Ads And Interstitial Ads In Mobile Apps

Corona SDK Google Admob Ads

It is increasingly getting tough to make the user pay for an app. While there are 9 ways to make money from apps , making money from ads is the easiest of all.

Implementing ads with Corona SDK is the easiest of all. In less than 5 minutes , you can start showing ads using Corona SDK plugins.

There are two major ad plugins supported by Corona SDK for ads. Google Admob and Apple iAds are both supported by Corona SDK Free version.

You can see below apps using these functionality and built on top of Corona SDK below. The apps display both banner and interstitial aps.

Google Play Store

https://play.google.com/store/apps/developer?id=Smart%20Games%20Fun%20Lab

Amazon Play Store

http://www.amazon.com/s/ref=bl_sr_mobile-apps?_encoding=UTF8&field-brandtextbin=Smart%20Games%20Fun%20Lab&node=2350149011

In this tutorial , we are going to look at implementing Google Admob. While Apple iAds is also a great platform for making money from ads , Google Admob is the best because of higher fill rates and easy setup and approvals.

As said earlier banner ads and interstitial ads are the most prominent ad types for apps.

Banner Ads : Banner ads are the ads that you see at the top of the app or at the bottom of the app. There are usually good as they mostly don’t disturb the user experience. The usual pay out for these types of ads is slightly less. Most of the ad networks auto refreshes the ads ( called ad cycling) for a fixed duration like 30 seconds or 45 seconds either set by you or by your ad network provider.

So, even if your user is on same screen playing something , the banner refreshes itself and shows new ad increasing the click though rate.

Interstitial Ads : Interstitial Ads are ads that cover the whole screen of your app. You must be really carefully while displaying these ads without compromising the user user experience. Too many interstitial ads often irritate the user and makes him/her uninstall your app looking one user. So, the cost of showing too many interstitial ads is loosing users. However , these pay high rates are the advertiser is occupying your whole screen. Again , most of the networks pay you only when the user clicks the ad.

The best time to show this type of ad is when the user finishes a level or lost a game or won a game. Never try to show the interstitial ads when the user is seriously playing or really looking for something.

You can read more about Banner Ads and Interstitial Ads here.

Make sure you have an account with Admob and setup a banner ad unit id and interstitial ad unit id before you proceed.

Corona SDK Admob Signup

Corona SDK Admob Signup

Lets see how to implement Banner Ads using Google Admob v2. Also , the question often asked about Google Admob is if Admob ads can be used in iOS Apps – Yes, you can use admob ads without any issues on iOS Apps.

Step 1 – Add below lines of code to build.settings file

android =

    {
        versionCode = "10",
        usesPermissions =
        {
            "android.permission.INTERNET",
        },
    },
	
	plugins =
    {
        ["plugin.google.play.services"] =
        {
            publisherId = "com.coronalabs"
        },
    },


This block of code is for the app to get Internet access to pull the ads. Also , it mentions that we are using Google Admob Plugin for ads. This will help in compiling the code and building the app package on Corona server.

android.permission.INTERNET -> This is for requesting internet access for the app

 

Step 2 – Download this file and keep it in the same level main.lua exits and replace the ad ids with your ad ids.

File Name wpmaGlobalAdmob.lua ->   Download here

Open the file and lets see what it has.

 

local bannerAppID = "ca-app-pub-bannerAppAdID"  -- Admob Banner App ID
local interstitialAppID = "ca-app-pub-interstitialAppAdID"  -- Android Admob Interstitial

Replace the above values in quotes with your admob ad unit ids.

 

function adListener( event )
      -- The 'event' table includes:
    -- event.name: string value of "adsRequest"
    -- event.response: message from the ad provider about the status of this request
    -- event.phase: string value of "loaded", "shown", or "refresh"
    -- event.type: string value of "banner" or "interstitial"
    -- event.isError: boolean true or false

    if (targetStore == 'nook') then
        return false 
    end
        
    local msg = event.response
    -- Quick debug message regarding the response from the library
    print( "Message from the ads library: ", msg )

    if (event.type == "banner") then
        if ( event.isError ) then
            print( "Error, no ad received", msg )
        else
            print( "Got an Ad!" )
        end
    elseif (event.type == "interstitial") then
        if ( event.isError ) then
            print( "Error while calling interstitialAdListener ... Try another Ad from another provider" )
        -- attempt to fetch another ad
        elseif ( event.phase == "loaded" ) then
        -- an ad was preloaded
            interstitialFetchFlag = 0
            print("interstitial loaded and show can be called to show ad")
        elseif ( event.phase == "shown" ) then
        -- the ad was viewed and closed
            print("interstitial-closed shown")
        -- composer.gotoScene( "loopScene1", options)
        end
    end

end

This is the adlistener function that gets called every time you call Admob function. This works the same as normal Event Listeners.

The code sees if the request is made for a Nook device . If the device is a Nook device , the function returns false as Nook/BN(Barnes And Nobles doesn’t allow ads on devices.

Then the code checks if the call is made for a banner ad or an interstitial ad. Remember , not always the admob call returns an Ad. There are several occasions Admob may not be able to deliver you an ad either because of non availability of the ad for that given time/location/user or for some other issues.

So, if it is a banner ad request and returns no ad , it prints the same on your console. If you get an ad , the same message will be written to your console. This will help you understand whats going on.

If you use Admob code on an app for first time , wait atleast 30 seconds to 60 seconds before you start seeing an ad. Don’t panic if you dont see an ad. Wait for a minute and then you can start seeing ads on device.

Note : Corona Simulator cannot show ads. The testing must be done on a real device.

Coming back , if the call is for an interstitial ad , you have to use a function called ads.load to load the ad and ads.show to show the ad.

Donot worry about all these. This is only for your explanation of how the code works.

 

function initAdmobBannerAd()
    if (targetStore == 'nook') then 
        return false 
    end
    ads.init( adProvider, bannerAppID, adListener)
end

wpmaGlobalAdmob.initAdmobBannerAd = initAdmobBannerAd

For Banner Ads , you need to initiate the Banner Ad one time when the app starts. The above chuck of code initiate the Admob ad. This function will not show an ads. It will only initiate the ad pulling process.

function showAdmobBannerAd(position)
    if (targetStore == 'nook') then 
        return false 
    end

    if (position == nil) then
        position = "top"
    elseif (position ~= "top" and position ~= "bottom") then
        position = "top"
    end

    if (position == "top") then
      ads.show( "banner", { x=0, y=0, appId=bannerAppID })
    elseif (position == "bottom") then
      print( "Bottom Ad Called" )
      ads.show( "banner", { x=0, y=100000, appId=bannerAppID })
    else
      print( "Something wrong with Banner Ad Position Logic ... Shouldn't come to this statement" )
    end
    
end
wpmaGlobalAdmob.showAdmobBannerAd = showAdmobBannerAd

This is for showing the Banner Ad. The showAdMobBannerAd function here displays the actual banner ad in Corona SDK Apps.

function hideAdmobBannerAd()
    if (targetStore == 'nook') then 
        return false 
    end
    ads.hide()
end
wpmaGlobalAdmob.hideAdmobBannerAd = hideAdmobBannerAd

This block is for hiding the Admob Banner Ad in Corona.

function initAdmobInterstitialAd()
    if (targetStore == 'nook') then 
        return false 
    end
    ads.init( adProvider, interstitialAppID, adListener)
end

wpmaGlobalAdmob.initAdmobInterstitialAd = initAdmobInterstitialAd

Similar to initiating the Banner Ad , you need to initiate the Interstitial Admob Ads also. Above function takes care of it using Corona.

function loadAdmobInterstitialAd()
    if (targetStore == 'nook') then 
        return false 
    end
    if (interstitialFetchFlag == 1) then
        ads.load( "interstitial", { appId=interstitialAppID } )
    else
        print( "Fetch Flag is 0 ... Ad must be ready to show" )
    end
    
end
wpmaGlobalAdmob.loadAdmobInterstitialAd = loadAdmobInterstitialAd

As discussed earlier , you need to load the Interstitial Ad before playing the ad. Calling the above function will load the Ad.

function showAdmobInterstitialAd()
    if (targetStore == 'nook') then 
        return false 
    end
    --if (ads.isLoaded( "interstitial", { appId=interstitialAppID } )) then
    if (interstitialFetchFlag == 0) then
      interstitialFetchFlag = 1
      ads.show( "interstitial", { appId=interstitialAppID } )
    else
      loadAdmobInterstitialAd() -- Load Admob Interstitial Ad and get ready for next call
      print( "ads.isLoaded" .. "returned FALSE and so can't display Interstitial Ad" )
    end
end
wpmaGlobalAdmob.showAdmobInterstitialAd = showAdmobInterstitialAd

This function showAdmobInterstitialAd displays the interstitial ad.

Note : Whenever an interstitial ad is displayed or a banner or interstitial ad is clicked , your app scene’s hide functionality gets called.

For interstitial ads , if the user closes the ad , the app automatically resumes itself by calling Corona SDK show method.

If you dont want to go much into technical details in Step 2 , you can ignore.

In Step 2 , all you have to do download the file , keep it in same folder where main.lua exists and replace the banner and interstitial Ad Ids with your AD Unit Ids from Google Admob Console.

 

Step 3 – Call below functions to show/hide ads

Add below 3 lines of code in your main.lua

local wpmaGlobalAdmob = require("wpmaGlobalAdmob")

wpmaGlobalAdmob.initAdmobBannerAd()
wpmaGlobalAdmob.initAdmobInterstitialAd()

This initiate the ads but dont yet display the ads.

Now pick the modules where ever you want to call Admob functions and add below line at the beginning of those files.

local wpmaGlobalAdmob = require("wpmaGlobalAdmob")

Now , the real Ads display fun. Call below functions in the code to show/hide ads as described below.

wpmaGlobalAdmob.showAdmobBannerAd() -> This will display the banner ad in your app. By defualt , it displays ad at the top

wpmaGlobalAdmob.showAdmobBannerAd(“top”) -> This will display the banner ad in your app at the top.

wpmaGlobalAdmob.showAdmobBannerAd(“bottom”) -> This will display the banner ad in your app. By defualt , it displays ad at the bottom of the screen

Note : You cannot display banner ads at any other places on screen. It can be shown either at the top or bottom.

wpmaGlobalAdmob.hideAdmobBannerAd() -> This will remove the banner ad from your app.

The next time you want to show it , you can call the show function directly with out calling the initiate function again.

wpmaGlobalAdmob.loadAdmobInterstitialAd() -> Call this to load the interstitial ad . This yet will not display the ad.

wpmaGlobalAdmob.showAdmobInterstitialAd() -> Call this function to show the Interstitial Admob ad

You can see some apps using these functionality and built on top of Corona SDK below. The apps display both banner and interstitial aps.

Google Play Store

https://play.google.com/store/apps/developer?id=Smart%20Games%20Fun%20Lab

Amazon Play Store

http://www.amazon.com/s/ref=bl_sr_mobile-apps?_encoding=UTF8&field-brandtextbin=Smart%20Games%20Fun%20Lab&node=2350149011

 

Please comment here if you have used Corona to build your apps and are making revenue with Google Admob or iAds. If you have a better and easy way to show ads , please let us know here. Also , please let us know for any issues with the code.