AdMob helper module

Hi guys,

Here is the AdMob module I made based on kam187’s work. It is similar in use to my InMobi helper module if you tried it.

This allows for easy switching between AdMob in InMobi in your game:
[lua]require “admob”
require “inmobi”

require “settings”

– assuming “isFreeVersion” and “isAdMob” are defined in the “settings” module

if settings.isFreeVersion then
if settings.isAdMob then
admob.showAd(true)
else
inmobi.showAd(1, 0, 0)
end
end[/lua]

The setup is simple. Just create a file named “admob.lua”:

[lua]module(…, package.seeall)

local dummyAd
local isAdVisible = false
local isAdOnTop = false
local dummyTimer
local isAndroid = “Android” == system.getInfo(“platformName”)

local CW = display.contentWidth
local CH = display.contentHeight
local SOX = display.screenOriginX
local SOY = display.screenOriginY

local function round(n)
return math.floor(n + 0.5)
end

local function adToFront()
if dummyAd then
dummyAd:toFront()
end
end

local function showAd_Android(event)
– Is the url a remote call?
if string.find(event.url, “android_ad.html”, 1, false) then
return true
else
system.openURL( string.gsub(event.url, “Corona:”, “”) )
– Refresh ad
hideAd()
showAd(isAdOnTop)
return true
end
end

local function showAd_Apple(event)
– Is the url a remote call?
if string.find(event.url, “http://”, 1, false) == 1 then
– Is it a call to the admob server?
if string.find(event.url, “c.admob.com”, 1, false) ~= nil then
– an actual click on an ad, so open in Safari
system.openURL(event.url)
– Refresh ad
hideAd()
showAd(isAdOnTop)
end
else
return true
end
end

function showAd(onTop)
if isAdVisible then
if (isAdOnTop == onTop) then
return
else
hideAd()
end
end
isAdOnTop = onTop == true
hideAd()

local adfile = “apple_ad.html”
local sizeX = 320
local sizeY = 48
local posX = (CW - sizeX) / 2
– round the Y position to remove the 1px gap
– between the bottom of the ad the bottom of the screen
local posY = isAdOnTop and SOY or round(CH - SOY - sizeY)

if isAndroid then
adfile = “android_ad.html”
native.showWebPopup(posX, posY, sizeX, sizeY, adfile,
{
baseUrl = system.ResourceDirectory,
hasBackground = false,
urlRequest = showAd_Android,
autoCancel = false
}
)

elseif system.getInfo(“environment”) == “simulator” then
dummyAd = display.newRect(posX, posY, sizeX, sizeY)
dummyTimer = timer.performWithDelay(1000, adToFront, 0)
else
native.showWebPopup(posX, posY, sizeX, sizeY, adfile,
{
baseUrl = system.ResourceDirectory,
hasBackground = false,
urlRequest = showAd_Apple,
autoCancel = false
}
)
end
isAdVisible = true
end

function hideAd()
native.cancelWebPopup()
if dummyTimer then
timer.cancel(dummyTimer)
dummyTimer = nil
end
if dummyAd then
dummyAd:removeSelf()
dummyAd = nil
end
isAdVisible = false
end[/lua]

and an HTML file named “android_ad.html”:

br\> "http://www.w3.org/TR/html4/loose.dtd"\>  
  
  
  
<meta name="viewport" content="width=320; user-scalable=0;">  
<meta http-equiv="Refresh" content="30">

<title>ad</title>
  
  
  
<script type="text/javascript"><br> var admob_vars = {<br> pubid: 'YOUR ID HERE!!!', // publisher id<br> bgcolor: '356FA8', // background color (hex)<br> text: 'FFFFFF', // font-color (hex)<br> test: false // test mode, set to false to receive live ads<br> };<br> </script>  
<script type="text/javascript" src="http://mmv.admob.com/static/iphone/iadmob.js"></script>  
<script type="text/javascript"><br> _admob.gotourl = function (V, W) {<br> document.location = "Corona:" + V;<br> return true;<br> };<br> </script>  
  
[/html]  

NOTE: I haven’t tested this on the iOS, but it will probably work there too.

“apple_ad.html” code:

[code]
br> “http://www.w3.org/TR/html4/loose.dtd”>

ad

[/html]
[/code] [import]uid: 52103 topic_id: 16514 reply_id: 316514[/import]

any trouble with Apple approval process when implementing either InMobi or AdMob ? [import]uid: 95911 topic_id: 16514 reply_id: 65446[/import]

@rbhfst I haven’t yet ported my app to iOS, so can’t comment. AdMob performance for me was very poor. I wrote to AdMob guys and they suggested switching to AdSense instead, since they have some fill rate problems with AdMob (duh!). So I did and there _is_ a difference. With AdMob ads only showed up from time to time here in Russia and they were not relevant at all. Now I see ads every single time, some relevant (i.e. in Russian), some not, but they always show up. Also CPC is higher.

And it’s very easy to make the code above work with AdSense too, just replace the script tags with the ones from AdSense and that’s it.

Another nice thing about AdSense ads that you don’t have this annoying ad centering issue. You know, when the AdMob ad shows up it quickly jumps 2 pixels to the side and back. Maybe it’s not a big deal - Blast Monkeys have it too, but it always bothered me. With AdSense it’s gone :slight_smile:

So to wrap it up, AdSense is a clear winner here.

InMobi 320x48 ads won’t show up on Android at all (or at least didn’t - don’t know, maybe they fixed this). People also complained about performance.

AdMob has fill rate problems and shows a lot of (jumping:) irrelevant ads in some countries.

AdSense - no problems so far. But I’ll let you know if I find something. [import]uid: 52103 topic_id: 16514 reply_id: 65450[/import]

@vitalyx the code says

require “inmobi”

require “settings”

but where is inmobi.lua and settings.lua

By the way, only what i need is Admob on my free app. Many thanks in advance. [import]uid: 13454 topic_id: 16514 reply_id: 68750[/import]

I’m also interested in finding the settings.lua file… Are you referring to the one located here?

Thanks,
Clay [import]uid: 62528 topic_id: 16514 reply_id: 70250[/import]

Okay, so I’ve worked around the settings module, but am getting the following Runtime error when trying to run the code:

admob.lua:20: attempt to call method ‘toFront’ (a nil value)

Any help would be greatly appreciated.

Thanks,
Clay [import]uid: 62528 topic_id: 16514 reply_id: 70253[/import]

Ah, figured it out! I just needed to call admob.hideAd() before clean()

Thanks! [import]uid: 62528 topic_id: 16514 reply_id: 70255[/import]

Yikes! Sorry to inundate this thread with questions…

I’m now having the following issue: The dummy ad on the simulator seems to work perfectly, but on my device, the ad seems to try to load for a second before disappearing.

Any idea what would cause this behavior?

  • Clay [import]uid: 62528 topic_id: 16514 reply_id: 70263[/import]

Hmm… I don’t remember having this problem. Can’t have a look at it at the moment, but I’d check if the ad disappears or the webpopup itself closes. [import]uid: 52103 topic_id: 16514 reply_id: 70316[/import]

mine too, it loads for a second then disappears, i don’t understand what is wrong. Any help would be appreciated. thx [import]uid: 89663 topic_id: 16514 reply_id: 71215[/import]

good work vitalyx! [import]uid: 70114 topic_id: 16514 reply_id: 71388[/import]

Okay I was able to get the ad to load (just had to give the admob account some time to activate apparently) and have submitted my first game with ads! Hurrah!

One question about integrating ads into a different game: Is there any way to preload an ad behind a scene, and show it at a certain time?

For example, I just want to show ads on the “You Lose” screen, but if I just try to load the ad when the player loses, they will have clicked continue by the time actually appears. I would like to be able to load the ad at the start but have it either sit behind the game scene or have an alpha of 0 until the user gets to the proper place.

Is this possible?

Thanks,
Clay [import]uid: 62528 topic_id: 16514 reply_id: 71995[/import]

@clay
i don’t think you can hide, move, or set the zOrder on webpopup after its been loaded. i tried this for awhile but could not figure anything out.

@vitalyx

i used this for adsense, but i am unsure if all adsense ads use this tag

if string.find(event.url, “googleadservices.com”, 1, false) == nil then
vitalyx what tags did you use? [import]uid: 89663 topic_id: 16514 reply_id: 72004[/import]

@ezraanderson1979

Sorry, I still can’t recover from my SSD crash. I didn’t have the most recent backup, so I’ve lost a lot of SVG art and some code, including last changes to the ad module :frowning: [import]uid: 52103 topic_id: 16514 reply_id: 73197[/import]

Sorry, a little off-topic:

???, ???, ? ??? ??? ??? ??? ???
[import]uid: 13989 topic_id: 16514 reply_id: 93337[/import]

Hey Vitalyx,

I recently started a website that sells my full games’ source code to other developers to modify / build upon. All of the code/ templates being sold on my site are full games that I have released to the app store.

One of my games, Ack!, utilizes your ad module to show ads during gameplay. I was wondering if you would be okay with me selling my source code to this game as-is or if I should remove the ad module from the source before putting it up for sale.

The reason I am asking is because you’ve graciously posted your code for free, but have not included a “license” at the beginning of the code such as the one at the top of the “Director” module.

Please note that I would not be selling your ad module individually; it would simply be included as a part of the code package as I built “Ack!” using your ad module to show ads. I can either remove the module from the game or I could credit you on the ad module code file.

Either way I wanted to get your permission first. I would love it if you could reach out to me via email at Clay (at) ClayKohut (dot) com and give me the “yay or nay”.

Thanks a lot,
Clay [import]uid: 62528 topic_id: 16514 reply_id: 102777[/import]

@Fox Sorry for my late reply, was busy porting my webOS app to the Blackberry Playbook.

Unfortunately, no, not really. I guess, in-app purchase should be the way to go. For me there are too many variables with the ads to keep in mind and less control. I know some people are successful with mobile ads, but to each his own.

@clay Thanks for asking. I am OK with that. As long as you link to this page so that people know this module is free I’m fine. [import]uid: 52103 topic_id: 16514 reply_id: 102874[/import]

Thanks a lot, Vitalyx! I will certainly link to this page and credit you for the module, both from the module’s code file and from the product page on my website.

Thanks again – you rock!

  • Clay [import]uid: 62528 topic_id: 16514 reply_id: 102916[/import]