Wrong adlistener is fired (AppLovin)

Hi,

I made a new topic two days ago because something wasn’t correct with the ads on my app. I pretty much solved that issue and decided to open a new topic so people with the same problem can find it as well.

This is the problem:

I’ve got an app that will show ads on 2 screens , either when you die and you want to continue playing you can watch an ad or if you don’t have enough in game credits , you can watch an ad to get a free item.

I’ve got two different adListeners setup for that, this is how both adListeners are written:

  • This is the one for when you die (it is initialized in a later stage of the scene)

    function functions.applovinAdListener( event ) if ( event.phase == “init” ) then – Successful initialization print( event.isError ) – Load an AppLovin ad applovin.load() local isAdLoaded = applovin.isLoaded() print(“Ad loaded:” … isAdLoaded ) elseif ( event.phase == “loaded” ) then – The ad was successfully loaded print( event.type ) elseif ( event.phase == “failed” ) then – The ad failed to load print( event.type ) print( event.isError ) print( event.response ) applovin.load() elseif ( event.phase == “displayed” or event.phase == “playbackBegan” ) then – The ad was displayed/played print( event.type ) applovin.load() elseif ( event.phase == “hidden” ) then – The ad was closed/hidden print( event.type ) elseif ( event.phase == “clicked” ) then – The ad was clicked/tapped print( event.type ) end end

This is the one that you use when you are low on credits: 

 function functions.applovinShopAdListener( event ) if ( event.phase == "init" ) then -- Successful initialization print( event.isError ) -- Load an AppLovin ad applovin.load() local isAdLoaded = applovin.isLoaded() print( isAdLoaded ) elseif ( event.phase == "loaded" ) then -- The ad was successfully loaded print( event.type ) elseif ( event.phase == "failed" ) then -- The ad failed to load print( event.type ) print( event.isError ) print( event.response ) applovin.load() elseif ( event.phase == "displayed" or event.phase == "playbackBegan" ) then -- The ad was displayed/played print( event.type ) applovin.load() elseif ( event.phase == "hidden" ) then -- The ad was closed/hidden print( event.type ) print("Ad hidden") settings.adAvailable = settings.adAvailable + 1 loadsave.saveTable(settings, "circleGameSettings.json") functions.freeRandomBall() elseif ( event.phase == "clicked" ) then -- The ad was clicked/tapped print( event.type ) print("Clicked") functions.freeRandomBall() end end applovin.init(functions.applovinShopAdListener, { sdkKey="MyKey" })

By debugging on my device, I found that when I launch an ad for credits and after that play the game and die and try to watch an ad to get an extra life, the wrong eventlistener is fired. I’ve read the tutorial on appLovin multiple times and went through the docs but I just can’t find the answer for this…

Is there anyone who knows how to fix this? I know that some ad networks give multiple keys for multiple ads, but that is not the case for applovin. Applovin provides one key/application.

Kind regards

Bram

You can only have one listener per ad provider. You need to make decisions in the listener based on either data returned in the event table or by setting a flag that identifies the current ad type you’re trying to show and use it for your logic in the single listener.

Rob

Hi Rob,

Thanks, I’ll try changing up my code :slight_smile:

Is this mentioned in the docs, because I’m pretty sure that I didn’t see it.

Thanks for the help tho :smiley:

Ad plugins and their listeners are usually done in main.lua. While we don’t encourage globals, its certainly a place where the concept of globals is useful.

There are several techniques of getting data into scenes that don’t actually use global data.

  1. Use composer’s setVariable/getVariable methods. Basically these store key-value pairs in the Composer object.

  2. Use a data module. See: http://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

This data module can also have functions with it too.

3.If you’re player for instance is an object in it’s own module, have it have methods for processing rewards. 

  1. Instead of local functions in your scene you can use scene objects:

– game.lua

function scene:rewardPlayer( rewardType, rewardAmount)     -- code to give the player their award end

Now at this point rewardPlayer() is part of the scene object. In another module or the adlistner in main.lua you can:

local currentSceneName = composer.getSceneName( "current" ) local currentSceen = composer.getScene( currentSceneName ) if currentScene.rewardPlayer then     currentScene:rewardPlayer( "coins", 100 ) end

Hi Rob,

One more question, I used the scene objects. But where should I initialize the ads? I tried doing it directly in main.lua, but that didn’t work and I tried to do it in the lua files in which I want to display my ads. But I’m getting: “ERROR: applovin.show() you must call applovin.init() before making any other applovin.* Api calls”.

call .init() in main.lua. It needs to be called before you call composer.gotoScene() to go to your first scene. .show() gets called in the scene at the appropriate time.

Rob

Hi Rob

This is the code in my main.lua file, still getting the error.

function applovinAdListener( event ) if ( event.phase == "init" ) then -- Successful initialization print( event.isError ) -- Load an AppLovin ad applovin.load() local isAdLoaded = applovin.isLoaded() print("Ad loaded:" .. isAdLoaded ) elseif ( event.phase == "loaded" ) then -- The ad was successfully loaded print( event.type ) elseif ( event.phase == "failed" ) then -- The ad failed to load print( event.type ) print( event.isError ) print( event.response ) applovin.load() elseif ( event.phase == "displayed" or event.phase == "playbackBegan" ) then -- The ad was displayed/played print( event.type ) applovin.load() elseif ( event.phase == "hidden" ) then -- The ad was closed/hidden print( event.type ) if composer.getSceneName("current") == shop then functions.freeRandomBall() else composer.gotoScene("levelRetry", "fade", 500) end elseif ( event.phase == "clicked" ) then -- The ad was clicked/tapped print( event.type ) end end applovin.init(applovinShopAdListener, { sdkKey="MyKey" })

Where is composer.gotoScene() relative to this code?

Wow, I’m sorry. I thought that I copied that as well, here is the code with the composer part:

function applovinAdListener( event ) if ( event.phase == "init" ) then -- Successful initialization print( event.isError ) -- Load an AppLovin ad applovin.load() local isAdLoaded = applovin.isLoaded() print("Ad loaded:" .. isAdLoaded ) elseif ( event.phase == "loaded" ) then -- The ad was successfully loaded print( event.type ) elseif ( event.phase == "failed" ) then -- The ad failed to load print( event.type ) print( event.isError ) print( event.response ) applovin.load() elseif ( event.phase == "displayed" or event.phase == "playbackBegan" ) then -- The ad was displayed/played print( event.type ) applovin.load() elseif ( event.phase == "hidden" ) then -- The ad was closed/hidden print( event.type ) if composer.getSceneName("current") == shop then functions.freeRandomBall() else composer.gotoScene("levelRetry", "fade", 500) end elseif ( event.phase == "clicked" ) then -- The ad was clicked/tapped print( event.type ) end end applovin.init(applovinShopAdListener, { sdkKey="MyKey" }) composer.gotoScene( "menu", "fade", 500 )

You’re function is called:    applovinAdListener

You’re trying to call:            applovinShopAdListener

in the init call!

Damn, that still was from the second afListener. Sorry for all the trouble, but thanks!

You can only have one listener per ad provider. You need to make decisions in the listener based on either data returned in the event table or by setting a flag that identifies the current ad type you’re trying to show and use it for your logic in the single listener.

Rob

Hi Rob,

Thanks, I’ll try changing up my code :slight_smile:

Is this mentioned in the docs, because I’m pretty sure that I didn’t see it.

Thanks for the help tho :smiley:

Ad plugins and their listeners are usually done in main.lua. While we don’t encourage globals, its certainly a place where the concept of globals is useful.

There are several techniques of getting data into scenes that don’t actually use global data.

  1. Use composer’s setVariable/getVariable methods. Basically these store key-value pairs in the Composer object.

  2. Use a data module. See: http://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

This data module can also have functions with it too.

3.If you’re player for instance is an object in it’s own module, have it have methods for processing rewards. 

  1. Instead of local functions in your scene you can use scene objects:

– game.lua

function scene:rewardPlayer( rewardType, rewardAmount)     -- code to give the player their award end

Now at this point rewardPlayer() is part of the scene object. In another module or the adlistner in main.lua you can:

local currentSceneName = composer.getSceneName( "current" ) local currentSceen = composer.getScene( currentSceneName ) if currentScene.rewardPlayer then     currentScene:rewardPlayer( "coins", 100 ) end

Hi Rob,

One more question, I used the scene objects. But where should I initialize the ads? I tried doing it directly in main.lua, but that didn’t work and I tried to do it in the lua files in which I want to display my ads. But I’m getting: “ERROR: applovin.show() you must call applovin.init() before making any other applovin.* Api calls”.

call .init() in main.lua. It needs to be called before you call composer.gotoScene() to go to your first scene. .show() gets called in the scene at the appropriate time.

Rob

Hi Rob

This is the code in my main.lua file, still getting the error.

function applovinAdListener( event ) if ( event.phase == "init" ) then -- Successful initialization print( event.isError ) -- Load an AppLovin ad applovin.load() local isAdLoaded = applovin.isLoaded() print("Ad loaded:" .. isAdLoaded ) elseif ( event.phase == "loaded" ) then -- The ad was successfully loaded print( event.type ) elseif ( event.phase == "failed" ) then -- The ad failed to load print( event.type ) print( event.isError ) print( event.response ) applovin.load() elseif ( event.phase == "displayed" or event.phase == "playbackBegan" ) then -- The ad was displayed/played print( event.type ) applovin.load() elseif ( event.phase == "hidden" ) then -- The ad was closed/hidden print( event.type ) if composer.getSceneName("current") == shop then functions.freeRandomBall() else composer.gotoScene("levelRetry", "fade", 500) end elseif ( event.phase == "clicked" ) then -- The ad was clicked/tapped print( event.type ) end end applovin.init(applovinShopAdListener, { sdkKey="MyKey" })

Where is composer.gotoScene() relative to this code?

Wow, I’m sorry. I thought that I copied that as well, here is the code with the composer part:

function applovinAdListener( event ) if ( event.phase == "init" ) then -- Successful initialization print( event.isError ) -- Load an AppLovin ad applovin.load() local isAdLoaded = applovin.isLoaded() print("Ad loaded:" .. isAdLoaded ) elseif ( event.phase == "loaded" ) then -- The ad was successfully loaded print( event.type ) elseif ( event.phase == "failed" ) then -- The ad failed to load print( event.type ) print( event.isError ) print( event.response ) applovin.load() elseif ( event.phase == "displayed" or event.phase == "playbackBegan" ) then -- The ad was displayed/played print( event.type ) applovin.load() elseif ( event.phase == "hidden" ) then -- The ad was closed/hidden print( event.type ) if composer.getSceneName("current") == shop then functions.freeRandomBall() else composer.gotoScene("levelRetry", "fade", 500) end elseif ( event.phase == "clicked" ) then -- The ad was clicked/tapped print( event.type ) end end applovin.init(applovinShopAdListener, { sdkKey="MyKey" }) composer.gotoScene( "menu", "fade", 500 )

You’re function is called:    applovinAdListener

You’re trying to call:            applovinShopAdListener

in the init call!