How do you use Vungle?

I’m trying to make a game where a user can gain coins by collecting them when playing the games or by watching video ads. I only want the user to be rewarded if they watch the whole video or click on the ad.

Are there samples of code for this? So far I could only find code samples for displaying the ads, but not to check to see if the user watched the whole video.

Here is code I’m using in a game.  The idea here is that when you get an adEnd event, the user watched the whole video or clicked through the ad.  In my case I add 500 the player’s energy total and save my settings.  Then update the onscreen text is that screen has the appropriate text object.

local function vungleListener(event)     -- video ad not yet downloaded and available     if event.type == "adStart" and event.isError then         print("event.type", event.type, system.getInfo("platformName"))     elseif event.type == "adEnd" then         print("ad ended")         myData.settings.energy = myData.settings.energy + 500         utility.saveTable(myData.settings, "settings.json")         if myData.energyText then             myData.energyText:setText(tostring(myData.settings.energy))         end     else         print( "Received event:")     end     return true end ads.init("vungle", vendors.vungleID, vungleListener)

Thank you for that code, but whenever I use it event.isError is true.

How am I supposed to implement Vungle into Storyboard (or Composer)? Maybe proper implementation would solve some of my problems? Where do you put ads.init?

Hi tyrotech123, 

I’ve just created a walk-through guide and a sample app to show a simple integration. Check it out and let me know if it helps, or if anything is unclear. I’ll be tweaking it a bit more this week and would appreciate any feedback :slight_smile:

Jordyn

Developer Programs Engineer

Vungle

If you have an ad listener do you still put the ads.init in the main.lua file? Do you need to put the ad listener function in main.lua as well? (I’m using storyboard, so all of my actual code is in different files)

You’ll need to register the ad listener before you call ads.init. You want ads.init to be called as early in the app as possible, so that an ad has time to cache before you call ads.show.  The adListener and init don’t necessarily need to be in main, but I believe they should be in the same file.

Do you know whether I should put the ads.init in the createScene function or the enterScene function?

I put my ad listeners and such in main.lua.  You can’t guarantee that your storyboard scenes will be in memory when a listener callback fires.

I didn’t include everything you need since you asked for how to detect when the ad was completed.  You still need to include the Vungle plugin in your build.settings.  You have to require it in main.lua before you call the init call as well.  Then you will need to call ads.show() with the proper parameters to show the ad.

Rob

I don’t know what I’m doing wrong, but it’s still not working. I have storyboard.purgeOnSceneChange = true

Could that be messing some stuff up?

Possibly? It’s hard to tell without looking at the project. If you’d like, you can send it to me and I’ll see what I can find. Just add @Jordyn somewhere in the email body.

tech-support@vungle.com 

Okay, I sent it

Thanks for sending that in, looks like it was a very common issue we see- incorrect App ID.

When you calls.init, make sure you are passing in the App ID (not the Reporting API ID). You can find your App ID in red on your app’s page of the Vungle dashboard. 

This is a common mix-up, and we are working on a new dashboard design which will make these more clear.

Thank you! Now it works! Is there a way I can force Vungle to load/cache a new ad after one is already seen?

That will happen automatically, you don’t need to do anything :slight_smile:

It doesn’t look like it. Every time I try to see an ad, it gives me the same one (some Vegas slots app).

Also, this code is not working for me. For each condition I want it to change the text of an object, but nothing ever happens. I have all the ad code in one file (the scene in which I am displaying ads). The ad is showing, but it doesn’t seem like the ad listener is working

local function adListener(event) if (event.type == "adStart" and event.isError) then cointext.text="error" print("event.type", event.type, system.getInfo("platformName")) elseif event.type == "adEnd" then dodgestuff.totalcoins = dodgestuff.totalcoins+20 saver() --Saves the coins coins print(coins) cointext.text=dodgestuff.totalcoins --should update the coin text else cointext.text = "else was called..." end return true end ads.init( "vungle", vungleID, adListener )

You might want to put a print at the very top of your listener function to verify what values you’re getting.

Also make sure you don’t have another function called “adListener” too.

Rob

For some reason it randomly started to work now.

Can I used wasCallToActionClicked so the user has to interact with the ad for coins? How would I implement this?

elseif event.type == "adEnd" and event.wasCallToActionClicked then         dodgestuff.totalcoins = dodgestuff.totalcoins+20     saver() --Saves the coins coins     print(coins)     cointext.text=dodgestuff.totalcoins --should update the coin text

When you get an adEnd event, there will be a member variable set called wasCallToActionClicked which is either true or false.  Just check against that condition.

Rob

Here is code I’m using in a game.  The idea here is that when you get an adEnd event, the user watched the whole video or clicked through the ad.  In my case I add 500 the player’s energy total and save my settings.  Then update the onscreen text is that screen has the appropriate text object.

local function vungleListener(event)     -- video ad not yet downloaded and available     if event.type == "adStart" and event.isError then         print("event.type", event.type, system.getInfo("platformName"))     elseif event.type == "adEnd" then         print("ad ended")         myData.settings.energy = myData.settings.energy + 500         utility.saveTable(myData.settings, "settings.json")         if myData.energyText then             myData.energyText:setText(tostring(myData.settings.energy))         end     else         print( "Received event:")     end     return true end ads.init("vungle", vendors.vungleID, vungleListener)