Ad management
You can generate revenue from placing ad units in your games. To do this:
- Study the features and recommendations for placing ads.
- Set up ad calls using the SDK.
- Enable monetization in the Developer Console.
Configure ad calls
Interstitial ad
An ad block that completely covers the app and is shown between a user's request for some information (for example, when moving to the next game level) and receiving it.
To call an ad, use the ysdk.adv.show_fullscreen_adv() method.
ysdk.adv.show_fullscreen_adv(callbacks: table)
callbacks: table — Optional callback functions. They are configured individually for each ad unit.
-
on_close— called when the ad closes, after an error, or after an ad failed to open due to too frequent calls. It's used with thewasShownargument (booleantype), the value of which indicates whether the ad was shown or not. -
on_open— called when the ad is opened successfully. -
on_error— called when an error occurs. The error object is passed to the callback function. -
on_offline— called when the network connection is lost (switching to offline mode).
Example
1ysdk.adv.show_fullscreen_adv({
2 on_close = function (self, was_shown)
3 -- An action on ad closing.
4 end,
5 on_error = function (self, error)
6 -- An action in case of an error.
7 end
8})
Rewarded videos
A video ad block for which the user receives a reward, such as in-game currency, for watching.
To call an ad, use the ysdk.adv.show_rewarded_video() method.
ysdk.adv.show_rewarded_video(callbacks: table)
callbacks: table — optional callback functions. They are configured individually for each ad unit.
-
on_close— called when the video ad closes. -
on_open— called when the video ad is shown on the screen. -
on_error— called when an error occurs. The error object is passed to the callback function. -
on_rewarded— called when a video ad impression is counted. This function should specify a reward for viewing the ad.
Example
1ysdk.adv.show_rewarded_video({
2 on_open = function (self)
3 print("Video ad open")
4 end,
5 on_close = function (self)
6 print("Rewarded!")
7 end,
8 on_rewarded = function (self)
9 print("Video ad closed.")
10 end,
11 on_error = function (self, error)
12 print("Error while open video ad: " .. error)
13 end
14})
Sticky banner
An ad unit that is displayed during the game.
For information on enabling the sticky banner in the Developer Console, see the main article.
By default, the sticky banner appears when the game starts and is displayed throughout the entire session. To configure when the banner is shown:
- Under Sticky banners, enable the Use the API to display a sticky-banner option.
- Set up banner display using the following methods:
ysdk.adv.get_banner_adv_status()— shows the status of the banner.ysdk.adv.show_banner_adv()— calls the banner.ysdk.adv.hide_banner_adv()— hides the banner.
The method ysdk.adv.get_banner_adv_status() returns a value sticky_adv_is_showing: boolean. If sticky_adv_is_showing = false, the method
ysdk.adv.get_banner_adv_status() may return an optional field reason with possible reasons:
ADV_IS_NOT_CONNECTED— banners are not enabled.UNKNOWN— error displaying ads on the Yandex side.
Example
1ysdk.adv.get_banner_adv_status(
2 function (self, sticky_adv_is_showing, reason)
3 if sticky_adv_is_showing then
4 -- Advertisement is shown.
5 elseif reason then
6 -- Advertisement is not shown.
7 print(reason)
8 else
9 ysdk.adv.show_banner_adv()
10 end
11 end
12)