Difference between revisions of "Ads"

From GiderosMobile
 
(5 intermediate revisions by 2 users not shown)
Line 12: Line 12:
 
=== Examples ===
 
=== Examples ===
 
'''Admob'''
 
'''Admob'''
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
Ads = Core.class(Sprite) -- you need to add "Ads" and "Gaming" to your project Plugins
 
Ads = Core.class(Sprite) -- you need to add "Ads" and "Gaming" to your project Plugins
  
 
function Ads:init()
 
function Ads:init()
  -- ads
+
-- ads
  self.ADMOB_APP_ID = "ca-app-pub-3940256099942544~3347511713" -- google test id
+
self.ADMOB_APP_ID = "ca-app-pub-3940256099942544~3347511713" -- google test id
  self.ADMOB_UNIT_ID = "ca-app-pub-3940256099942544/6300978111" -- google test id
+
self.ADMOB_UNIT_ID = "ca-app-pub-3940256099942544/6300978111" -- google test id
  
  -- admob (setup for simple banner adverts), note: only visible on phone/tablet
+
-- admob (setup for simple banner adverts), note: only visible on phone/tablet
  if application:getDeviceInfo() == "Android" then
+
if application:getDeviceInfo() == "Android" then
      require 'ads' --create real object for on device
+
require 'ads' --create real object for on device
      self.ADMOB = Ads.new('admob')
+
self.ADMOB = Ads.new('admob')
      self.ADMOB:setKey(self.ADMOB_APP_ID)
+
self.ADMOB:setKey(self.ADMOB_APP_ID)
      self.ADMOB:addEventListener(Event.AD_RECEIVED, function() -- show ad
+
self.ADMOB:addEventListener(Event.AD_RECEIVED, function() -- show ad
        self.ADMOB:showAd('banner')
+
self.ADMOB:showAd('banner')
        self.ADMOB:setAlignment('center', 'bottom')
+
self.ADMOB:setAlignment('center', 'bottom')
      end)
+
end)
      self.ADMOB:addEventListener(Event.AD_FAILED, function(e) end)
+
self.ADMOB:addEventListener(Event.AD_FAILED, function(e) end)
      self.ADMOB:addEventListener(Event.AD_ACTION_BEGIN, function() end)
+
self.ADMOB:addEventListener(Event.AD_ACTION_BEGIN, function() end)
      self.ADMOB:addEventListener(Event.AD_ACTION_END, function() end)
+
self.ADMOB:addEventListener(Event.AD_ACTION_END, function() end)
      self.ADMOB:addEventListener(Event.AD_DISMISSED, function() end)
+
self.ADMOB:addEventListener(Event.AD_DISMISSED, function() end)
  else
 
      self.ADMOB = {} -- create fake object for testing in windows player
 
      self.ADMOB.loadAd = function() end
 
      self.ADMOB.showAd = function() end
 
  end
 
  
  -- LISTENERS
+
self.ADMOB:loadAd("banner", self.ADMOB_UNIT_ID) -- load ad
  self:addEventListener("enterBegin", self.onTransitionInBegin, self)
+
else
  self:addEventListener("enterEnd", self.onTransitionInEnd, self)
+
self.ADMOB = {} -- create fake object for testing in windows player
  self:addEventListener("exitBegin", self.onTransitionOutBegin, self)
+
self.ADMOB.loadAd = function() end
  self:addEventListener("exitEnd", self.onTransitionOutEnd, self)
+
self.ADMOB.showAd = function() end
 +
end
 +
 
 +
-- LISTENERS
 +
self:addEventListener("enterBegin", self.onTransitionInBegin, self)
 +
self:addEventListener("enterEnd", self.onTransitionInEnd, self)
 +
self:addEventListener("exitBegin", self.onTransitionOutBegin, self)
 +
self:addEventListener("exitEnd", self.onTransitionOutEnd, self)
 
end
 
end
  
Line 49: Line 51:
 
function Ads:onTransitionInBegin()
 
function Ads:onTransitionInBegin()
 
end
 
end
 
 
function Ads:onTransitionInEnd()
 
function Ads:onTransitionInEnd()
  self.ADMOB:loadAd("banner", self.ADMOB_UNIT_ID)
 
 
end
 
end
 
 
function Ads:onTransitionOutBegin()
 
function Ads:onTransitionOutBegin()
 
end
 
end
 
 
function Ads:onTransitionOutEnd()
 
function Ads:onTransitionOutEnd()
 
end
 
end
</source>
+
</syntaxhighlight>
  
  
 
'''Creating fallbacks with Ads frameworks'''
 
'''Creating fallbacks with Ads frameworks'''
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
--require plugin
 
--require plugin
 
require "ads"
 
require "ads"
Line 91: Line 89:
 
--start displaying amazon ads
 
--start displaying amazon ads
 
amazon:showAd("auto")
 
amazon:showAd("auto")
</source>
+
</syntaxhighlight>
  
 
{|-
 
{|-
Line 97: Line 95:
 
=== Methods ===
 
=== Methods ===
 
[[Ads.new]] ''initializes new ad framework''<br/><!--GIDEROSMTD:Ads.new(adframework) initializes a new ad framework-->
 
[[Ads.new]] ''initializes new ad framework''<br/><!--GIDEROSMTD:Ads.new(adframework) initializes a new ad framework-->
 +
[[Ads:checkConsent]] ''triggers consent flow if needed''<br/><!-- GIDEROSMTD:Ads:checkConsent(parameters) triggers consent flow if need -->
 
[[Ads:enableTesting]] ''enables testing ads''<br/><!--GIDEROSMTD:Ads:enableTesting() enables testing ads-->
 
[[Ads:enableTesting]] ''enables testing ads''<br/><!--GIDEROSMTD:Ads:enableTesting() enables testing ads-->
 
[[Ads:get]] ''gets property value of the ad''<br/><!--GIDEROSMTD:Ads:get(property) gets the property value of the ad-->
 
[[Ads:get]] ''gets property value of the ad''<br/><!--GIDEROSMTD:Ads:get(property) gets the property value of the ad-->
Line 105: Line 104:
 
[[Ads:getY]] ''gets y position of the ad''<br/><!--GIDEROSMTD:Ads:getY() gets the y position of the ad-->
 
[[Ads:getY]] ''gets y position of the ad''<br/><!--GIDEROSMTD:Ads:getY() gets the y position of the ad-->
 
[[Ads:hideAd]] ''hides the ad''<br/><!-- GIDEROSMTD:Ads:hideAd() hides the ad-->
 
[[Ads:hideAd]] ''hides the ad''<br/><!-- GIDEROSMTD:Ads:hideAd() hides the ad-->
 +
[[Ads:loadAd]] ''loads the ad''<br/><!-- GIDEROSMTD:Ads:loadAd() loads an ad-->
 
[[Ads:set]] ''sets property value of the ad''<br/><!--GIDEROSMTD:Ads:set(property,value) sets the property value of the ad-->
 
[[Ads:set]] ''sets property value of the ad''<br/><!--GIDEROSMTD:Ads:set(property,value) sets the property value of the ad-->
 
[[Ads:setAlignment]] ''sets alignment of the ad''<br/><!--GIDEROSMTD:Ads:setAlignment(horizontal,vertical) sets the alignment of the ad-->
 
[[Ads:setAlignment]] ''sets alignment of the ad''<br/><!--GIDEROSMTD:Ads:setAlignment(horizontal,vertical) sets the alignment of the ad-->
Line 114: Line 114:
  
 
| style="width: 50%; vertical-align:top;"|
 
| style="width: 50%; vertical-align:top;"|
 +
 
=== Events ===
 
=== Events ===
 
[[Event.AD_ACTION_BEGIN]]<br/><!--GIDEROSEVT:Event.AD_ACTION_BEGIN adActionBegin-->
 
[[Event.AD_ACTION_BEGIN]]<br/><!--GIDEROSEVT:Event.AD_ACTION_BEGIN adActionBegin-->
 
[[Event.AD_ACTION_END]]<br/><!--GIDEROSEVT:Event.AD_ACTION_END adActionEnd-->
 
[[Event.AD_ACTION_END]]<br/><!--GIDEROSEVT:Event.AD_ACTION_END adActionEnd-->
 +
[[Event.AD_CONSENT]]<br/><!--GIDEROSEVT:Event.AD_CONSENTD adConsent-->
 
[[Event.AD_DISMISSED]]<br/><!--GIDEROSEVT:Event.AD_DISMISSED adDismissed-->
 
[[Event.AD_DISMISSED]]<br/><!--GIDEROSEVT:Event.AD_DISMISSED adDismissed-->
 
[[Event.AD_ERROR]]<br/><!--GIDEROSEVT:Event.AD_ERROR adError-->
 
[[Event.AD_ERROR]]<br/><!--GIDEROSEVT:Event.AD_ERROR adError-->
 
[[Event.AD_FAILED]]<br/><!--GIDEROSEVT:Event.AD_FAILED adFailed-->
 
[[Event.AD_FAILED]]<br/><!--GIDEROSEVT:Event.AD_FAILED adFailed-->
 +
[[Event.ADS_READY]]<br/><!--GIDEROSEVT:Event.ADS_READY adsReady-->
 
[[Event.AD_RECEIVED]]<br/><!--GIDEROSEVT:Event.AD_RECEIVED adReceived-->
 
[[Event.AD_RECEIVED]]<br/><!--GIDEROSEVT:Event.AD_RECEIVED adReceived-->
[[Event.AD_REWARDED]]<br/><!--GIDEROSEVT:Event.AD_RECEIVED adReceived-->
+
[[Event.AD_REWARDED]]<br/><!--GIDEROSEVT:Event.AD_REWARDED adRewarded-->
  
 
=== Constants ===
 
=== Constants ===

Latest revision as of 00:26, 29 August 2023

Supported platforms: Platform android.pngPlatform ios.pngPlatform html5.pngPlatform winrt.png
Available since: Gideros 2011.6
Inherits from: EventDispatcher

Description

The idea is to provide a common Ads interface for most of available ad frameworks. A common ads interface allows users to switch and fall back between ad frameworks at runtime.

More information available in [1](Ads interface guide).

Examples

Admob

Ads = Core.class(Sprite) -- you need to add "Ads" and "Gaming" to your project Plugins

function Ads:init()
	-- ads
	self.ADMOB_APP_ID = "ca-app-pub-3940256099942544~3347511713" -- google test id
	self.ADMOB_UNIT_ID = "ca-app-pub-3940256099942544/6300978111" -- google test id

	-- admob (setup for simple banner adverts), note: only visible on phone/tablet
	if application:getDeviceInfo() == "Android" then
		require 'ads' --create real object for on device
		self.ADMOB = Ads.new('admob')
		self.ADMOB:setKey(self.ADMOB_APP_ID)
		self.ADMOB:addEventListener(Event.AD_RECEIVED, function() -- show ad
			self.ADMOB:showAd('banner')
			self.ADMOB:setAlignment('center', 'bottom')
		end)
		self.ADMOB:addEventListener(Event.AD_FAILED, function(e) end)
		self.ADMOB:addEventListener(Event.AD_ACTION_BEGIN, function() end)
		self.ADMOB:addEventListener(Event.AD_ACTION_END, function() end)
		self.ADMOB:addEventListener(Event.AD_DISMISSED, function() end)

		self.ADMOB:loadAd("banner", self.ADMOB_UNIT_ID) -- load ad
	else
		self.ADMOB = {} -- create fake object for testing in windows player
		self.ADMOB.loadAd = function() end
		self.ADMOB.showAd = function() end
	end

	-- LISTENERS
	self:addEventListener("enterBegin", self.onTransitionInBegin, self)
	self:addEventListener("enterEnd", self.onTransitionInEnd, self)
	self:addEventListener("exitBegin", self.onTransitionOutBegin, self)
	self:addEventListener("exitEnd", self.onTransitionOutEnd, self)
end

-- EVENT LISTENERS
function Ads:onTransitionInBegin()
end
function Ads:onTransitionInEnd()
end
function Ads:onTransitionOutBegin()
end
function Ads:onTransitionOutEnd()
end


Creating fallbacks with Ads frameworks

--require plugin
require "ads"

--initialize amazon
amazon = Ads.new("amazon")
amazon:setKey("amazon-key")

--initialize admob
admob = Ads.new("admob")
admob:setKey("admob-key")

--if amazon fails
--show admob
amazon:addEventListener(Event.AD_FAILED, function(e)
	print("amazon AD_FAILED", e.error)
	admob:showAd("auto")
end)

--if admob fails
--show amazon
admob:addEventListener(Event.AD_FAILED, function(e)
	print("admob AD_FAILED", e.error)
	amazon:showAd("auto")
end)

--start displaying amazon ads
amazon:showAd("auto")

Methods

Ads.new initializes new ad framework
Ads:checkConsent triggers consent flow if needed
Ads:enableTesting enables testing ads
Ads:get gets property value of the ad
Ads:getHeight gets height of the ad
Ads:getPosition gets x and y position of the ad
Ads:getWidth gets width of the ad
Ads:getX gets x position of the ad
Ads:getY gets y position of the ad
Ads:hideAd hides the ad
Ads:loadAd loads the ad
Ads:set sets property value of the ad
Ads:setAlignment sets alignment of the ad
Ads:setKey sets keys for the framework
Ads:setPosition sets position of the ad
Ads:setX sets x position of the ad
Ads:setY sets y position of the ad
Ads:showAd displays the ad

Events

Event.AD_ACTION_BEGIN
Event.AD_ACTION_END
Event.AD_CONSENT
Event.AD_DISMISSED
Event.AD_ERROR
Event.AD_FAILED
Event.ADS_READY
Event.AD_RECEIVED
Event.AD_REWARDED

Constants