Rewarded ads
Rewarded ads are a popular fullscreen ad format where users receive incentives for viewing ads.
Ad impressions are opt-in: for example, users can initiate them to get game bonuses or extra lives.
Strong user motivation makes this ad format the most popular and profitable adoption in free apps.
Appearance
This guide covers the process of integrating rewarded ads into Android apps using the Jetpack Compose extension. Besides code samples and instructions, it contains recommendations and links to additional resources.
Prerequisite
- Follow the SDK integration steps described under Quick start.
- First, you need to initialize the advertising SDK.
- Make sure you have the latest Yandex Mobile Ads SDK version. If you're using mediation, update to the most recent single build version.
To use Jetpack Compose, add the following dependency to build.gradle.kts:
dependencies {
implementation("com.yandex.android:mobileads:8.0.0-beta.1")
implementation("com.yandex.android:mobileads-compose:8.0.0-beta.1")
// Compose BOM (minimum 2024.01.00)
implementation(platform("androidx.compose:compose-bom:2025.03.00"))
}
Implementation
Key steps for integrating rewarded ads:
- Use
rememberRewardedAdLoader()to create an ad loader. - Load the ad using the
loadAd()suspend function and handle theRewardedAdLoadResultresult. - Register a
RewardedAdEventListenerto listen for ad event callbacks. - Display the
RewardedAdobject. - Reward the user for viewing the ad.
Features of rewarded ad integration
-
All calls to Yandex Mobile Ads SDK methods must be made from the main thread.
-
If a
RewardedAdLoadResult.Failureerror occurs, don't try to load a new ad again. If you have to, limit the number of ad loading retries to avoid unsuccessful requests and connection issues. -
Maintain a strong reference to the ad throughout the lifetime of the screen where the ad interaction is taking place.
-
When the composable leaves the tree,
cancelLoading()is called automatically. You don't need to release the loader's resources explicitly.
Loading the ad
To load rewarded ads, use rememberRewardedAdLoader(). Ads are loaded via the loadAd() suspend function, which returns RewardedAdLoadResult.
To load an ad, you need the ad placement ID obtained in the Boost interface (adUnitId).
You can expand the ad request parameters using AdRequestConfiguration.Builder(). To do this, pass information about the user's interests, page context, location, and other additional data in the request. Context can greatly improve the ad quality. To learn more, see Ad targeting.
Example of loading a rewarded ad
import com.yandex.mobile.ads.common.AdRequestConfiguration
import com.yandex.mobile.ads.compose.rememberRewardedAdLoader
import com.yandex.mobile.ads.rewarded.RewardedAdLoadResult
@Composable
fun MyScreen(activity: Activity) {
var rewardedAd by remember { mutableStateOf<RewardedAd?>(null) }
val loader = rememberRewardedAdLoader()
LaunchedEffect(Unit) {
val adRequestConfiguration = AdRequestConfiguration.Builder("your-ad-unit-id").build()
when (val result = loader.loadAd(adRequestConfiguration)) {
is RewardedAdLoadResult.Success -> rewardedAd = result.ad
is RewardedAdLoadResult.Failure -> {
// Ad failed to load with AdRequestError.
// Attempting to load a new ad from here is strongly discouraged.
}
}
}
}
Displaying ads
Rewarded ads are an incentivized ad format that allows users to earn rewards by viewing ads. These rewards may include extra lives or the ability to advance to the next level in a game. The reward format is determined by the app itself.
To track a rewarded ad's lifecycle and reward events, set a RewardedAdEventListener for the RewardedAd class object.
The reward is passed to the onRewarded callback directly when the show() method is called:
@Composable
fun MyScreen(activity: Activity) {
var rewardedAd by remember { mutableStateOf<RewardedAd?>(null) }
val loader = rememberRewardedAdLoader()
LaunchedEffect(Unit) {
val adRequestConfiguration = AdRequestConfiguration.Builder("your-ad-unit-id").build()
when (val result = loader.loadAd(adRequestConfiguration)) {
is RewardedAdLoadResult.Success -> rewardedAd = result.ad
is RewardedAdLoadResult.Failure -> {
// Ad failed to load with AdRequestError.
}
}
}
LaunchedEffect(rewardedAd) {
rewardedAd?.apply {
setAdEventListener(object : RewardedAdEventListener {
override fun onAdShown() {
// Called when ad is shown.
}
override fun onAdFailedToShow(adError: AdError) {
// Called when an RewardedAd failed to show.
// Now you can preload the next rewarded ad.
loadRewardedAd()
}
override fun onAdDismissed() {
// Called when ad is dismissed.
// Now you can preload the next rewarded ad.
loadRewardedAd()
}
override fun onAdClicked() {
// Called when a click is recorded for an ad.
}
override fun onAdImpression(impressionData: ImpressionData?) {
// Called when an impression is recorded for an ad.
}
override fun onRewarded(reward: Reward) {
// Called when the user can be rewarded.
}
})
show(activity)
}
}
}
Testing rewarded ad integration
Using demo ad units for ad testing
Use test ads to check your rewarded ad integration and the app itself. To make sure that test ads are returned for each ad request, you can use a special demo ad placement ID.
Demo adUnitId: demo-rewarded-yandex.
Warning
Before publishing your app in the store, make sure to replace the demo placement ID with the real ID you obtained in the Boost interface.
For the list of all available demo ad placement IDs, see Demo ad units for testing.
Testing ad integration
You can check if your rewarded ads are integrated correctly using the SDK's built-in analyzer. A detailed report with the test results will appear in the log.
To view the report, search for the keyword “YandexAds” in Logcat, a tool for debugging Android apps.
adb logcat -v brief '*:S YandexAds'
If the integration is successful, the following message is returned:
adb logcat -v brief '*:S YandexAds'
mobileads$ adb logcat -v brief '*:S YandexAds'
I/YandexAds(13719): [Integration] Ad type rewarded was integrated successfully
If there are any rewarded ad integration issues, you'll get a detailed issue report and troubleshooting recommendations.
Additional resources
-
Link to GitHub.