Universal wrapper for ADS on different platforms
MIT License
Ads Wrapper allows:
Supported services:
Ads Wrapper also allows you to run multiple networks at the same time. For example Admob and Unity Ads.
You can find more information on the networks
You can use it in your own project by adding this project as a Defold library dependency. Open your game.project
file and in the dependencies field add a link to the ZIP file of a specific release.
First you need to make a require for ads_wrapper.ads_wrapper
and ads_wrapper.events
.
local ads_wrapper = require("ads_wrapper.ads_wrapper")
local events = require("ads_wrapper.events")
Next, you need to register the networks. More details about networks can be found here.
-- Getting our module with networks
local test = require("ads_wrapper.ads_networks.test")
-- Register the first network
local test_1_id = ads_wrapper.register_network(test.network1, {param = "test_param 1"})
-- Register the second network
local test_2_id = ads_wrapper.register_network(test.network2, {param = "test_param 2"})
Next, you need to configure the mediators you need. There are two types: Video and Banner. Video mediator refers to the functions associated with interstitials and rewarded videos. Banner mediator refers to the functions associated with banners. You need to set up only the mediator that you need. For example, Poki does not support banners.
-- Setup video mediator
ads_wrapper.setup_video({{id = test_2_id, count = 2}, {id = test_1_id, count = 2}}, 4)
-- Setup banner mediator
ads_wrapper.setup_banner({{id = test_1_id, count = 1}})
You can read more about mediators here.
Next, you need to initialize ads wrapper.
ads_wrapper.init(true, true, function(response)
if response.result == events.SUCCESS then
pprint("Ads wrapper is initialized", response)
else
pprint("Something bad happened", response)
end
end)
In the first two parameters, you can specify the need to initialize the network.
First - initilize_video
, Second - initilize_banner
. This may take time depending on the service.
Further, they can be initialized separately, if not done immediately.
When an advertisement is called, they will be initialized automatically
if it has not been done before.
Almost all functions have a callback
parameter.
callback
- function which takes one response
parameter.
The response
can be of two types: success
and error
.
Response is a table:
events.SUCCESS
or events.ERROR
init_video_networks
is called again.show_interstitial
or show_rewarded
.Also, the response may contain additional information from a specific network.
Module ads_wrapper.events
contains useful variables:
events.SUCCESS
hash("SUCCESS")
events.ERROR
hash("ERROR")
The response table may have the code. All of them are contained in the ads_wrapper.events
module, like events.C_SKIPPED
. There are types of codes:
events.C_SKIPPED
hash("C_SKIPPED") - if the rewarded advertisement was skippedevents.C_ERROR_UNKNOWN
hash("C_ERROR_UNKNOWN") - an unknown error type is occuredevents.C_ERROR_AD_BLOCK
hash("C_ERROR_AD_BLOCK") - an error is related to the adblockevents.C_ERROR_NO_CONNECTION
hash("C_ERROR_NO_CONNECTION") - no internet connectionMediators are configured with two functions: ads_wrapper.setup_video
and ads_wrapper.setup_banner
.
The function sets the order which creates the queue.
Options:
{id = network_id, count = 2}
.
ads_wrapper.register_network
.1
Examples:
Single network
All other parameters are not needed for one network.
ads_wrapper.setup_video({{id = test_id_1}})
Queue: test_1->test_1->test_1->test_1->test_1->
Multiple networks with repeat_cut
parameter
ads_wrapper.setup_video({{id = test_id_1, count = 2}, {id = test_id_2, count = 2}, {id = test_id_1, count = 1}}, 3)
Queue:
1: test_1->test_1->test_2->test_2->test_1->
2: test_2->test_2->test_1->
3: test_2->test_2->test_1->
Part of the order is cut off after the first cycle.
Multiple networks without repeat_cut
parameter
repeat_cut
automatically becomes 3
.
ads_wrapper.setup_video({{id = test_id_1, count = 1}, {id = test_id_2, count = 2}})
Queue:
1: test_1->test_2->test_2->
2: test_1->test_2->test_2->
3: test_1->test_2->test_2->
In many functions there is a queue of calls. Before calling show_intertitial
ads_wrapper
will check if the ad has been loaded, and if not, it will load it first. If there is an error somewhere, the queue will break.
ads_wrapper.register_network(network, params)
Registers network. Returns id.
Parameters
network
table required network moduleparams
table required network options. They are different for every network. They can be found in the networks section.Return
ads_wrapper.setup_video(order, repeat_count)
Setups interstitial and reward mediator. More info here.
Parameters
{id = network_id, count = 2}
.ads_wrapper.setup_banner(order, repeat_count, _banner_auto_hide)
Setups banner mediator. More info here.
Parameters
{id = network_id, count = 2}
.hide_banner
was called after show_banner
, but the banner did not have time to load. Default: false
ads_wrapper.init(initilize_video, initilize_banner, callback)
Initializes ads_wrapper. Callback consists responses
field.
Queue: check_connection->request_idfa->init
Parameters
ads_wrapper.init_video_networks(callback)
Initialize video networks.
Queue: check_connection->request_idfa->init
Parameters
ads_wrapper.init_banner_networks(callback)
Initialize banner networks.
Queue: check_connection->request_idfa->init
Parameters
ads_wrapper.load_rewarded(callback)
Loads rewarded ads for next network.
Queue: check_connection->request_idfa->init->load_rewarded
Parameters
Return
ads_wrapper.show_rewarded(callback)
Shows rewarded ads for next network. Callback contain a special code
field with events.C_SKIPPED if the user skipped the ad.
Queue: check_connection->request_idfa->init->load_rewarded->show_rewarded
Parameters
Return
ads_wrapper.load_interstitial(callback)
Loads interstitial ads for next network.
Queue: check_connection->request_idfa->init->load_interstitial
Parameters
Return
ads_wrapper.show_interstitial(callback)
Shows interstitial ads for next network.
Queue: check_connection->request_idfa->init->load_interstitial->show_interstitial
Parameters
Return
ads_wrapper.load_banner(callback)
Loads banner for for next network.
Queue: check_connection->request_idfa->init->load_banner
Parameters
Return
ads_wrapper.show_banner(callback)
Shows setup banner for next network. Hides the previous banner if it was displayed.
Queue: check_connection->request_idfa->init->load_banner->show_banner
Parameters
Return
ads_wrapper.hide_banner(callback)
Hides setup banner for current network.
Parameters
ads_wrapper.unload_banner(callback)
Unloads banner for current networks.
Parameters
ads_wrapper.is_banner_setup()
Check if the banner mediator is set up
Return
ads_wrapper.is_video_setup()
Check if the interstitial and rewarded video mediator is set up
Return
ads_wrapper.is_initialized()
Check if ads wrapper is initiailzed
Return
ads_wrapper.is_internet_connected()
Checks for internet connection
Return
ads_wrapper.is_interstitial_loaded(check_current)
Check if the interstitial video is loaded.
Default checks the next
network in mediator.
Parameters
false
Return
ads_wrapper.is_rewarded_loaded(check_current)
Check if the rewarded video is loaded.
Default checks the next
network in mediator.
Parameters
false
Return
ads_wrapper.is_banner_loaded(check_current)
Check if the banner is loaded.
Default checks the next
network in mediator.
Parameters
false
Return
ads_wrapper.get_current_network(check_banner)
Returns the current network pointed to by mediator Default returns for the video mediator
Parameters
false
Return
ads_wrapper.get_next_network(check_banner)
Returns the next network pointed to by mediator Default returns for the video mediator
Parameters
false
Return
ads_wrapper.clear_networks()
Remove all registered networks
ads_wrapper.cancel(id)
ancel execution of the next step in the queue. For example, if you cancel show_interstitial while the ad is loading, then it will not be shown.
Parameters
Constants are used to set network parameters
This module can be used in network settings.
local platform = require("ads_wrapper.platform")
Constants:
platform.is_same(pl_value)
Parameters
heck if the platform is correct
Return
platform.get()
Returns current hash platform
Return
You can use existing networks or create your own. More information can be found at the links:
You can create your own network. It is best to look at already made networks.
You can include output to the console of all operations that are done when calling functions with the queue.
local events = require("ads_wrapper.events")
local queue = require("ads_wrapper.queue")
queue.set_verbose_mode(events.V_ALL)
Constants:
Made by PotatoJam.
For example used:
MIT license.