Rewarded Ads - cleveradssolutions/CAS-Flutter GitHub Wiki
Rewarded ad units enable users to play games, take surveys, or watch videos to earn in-app rewards, such as coins, extra lives, or points.
This guide explains how to integrate rewarded video ads into an Android app.
flowchart TD
A[isAutoloadEnabled] -->|auto| L((load))
L -->|success| R([onAdLoaded])
L -->|fail| F([onAdFailedToLoad])
F -->|delay| A
R --> RL[isLoaded]
S((show)) --> RL
RL -->|success| SR([onAdShowed])
RL -->|fail| SF([onAdFailedToShow])
SR --> I([onAdImpression])
SR --> C([onAdClicked])
I --> UR([onUserEarnedReward])
UR --> D([onAdDismissed])
D --> A
SF --> A
Ad instance can be initialized along with the widget state before initState
.
import 'package:clever_ads_solutions/clever_ads_solutions.dart';
class _HomeScreenState extends State<HomeScreen> {
final _rewarded = CASRewarded.create(_casId);
@override
void initState() {
...
}
The SDK provides the capability to create and precache multiple ad instances, enabling uninterrupted sequential ad display. CAS SDK will load mediation ads in order for each created instance.
The com.cleveradssolutions.sdk.screen.ScreenAdContentCallback
handles events related to displaying your CASRewarded
. Callbacks are called on the main thread. Before showing ad, make sure to setContentCallback()
.
final ScreenAdContentCallback _adContentCallback = ScreenAdContentCallback(
onAdLoaded: (ad) {
// (Optional) Called when the ad content has been successfully loaded.
},
onAdFailedToLoad: (ad, error) {
// (Optional) Called when the ad content fails to load.
},
onAdShowed: (ad) {
// (Optional) Called when the ad content is successfully shown.
},
onAdFailedToShow: (ad, error) {
// (Optional) Called when the ad content fails to show.
},
onAdClicked: (ad) {
// (Optional) Called when the ad content is clicked by the user.
},
onAdDismissed: (ad) {
// (Optional) Called when the ad content is dismissed.
},
);
Note
- Read more about
AdContentInfo
structure in Impression Level Data. - Attempting to load a new ad from the
onAdFailedToLoad()
method is strongly discouraged. Limit ad load retries to avoid continuous failed ad requests in situations such as limited network connectivity. - When an error occurs during ad impression, executed the
onAdFailedToShow
only. In this case theonAdDismissed
will not be executed, since the impression is not considered successful.
The next step is to fill out the load()
method and handle the ad load callbacks.
@override
void initState() {
_rewarded.contentCallback = _adContentCallback;
_rewarded.load();
}
You can use ad load calls to build up a cache of preloaded ads before you intend to show them, so that ads can be shown with zero latency when needed. Since ads expire after an hour, you should clear this cache and reload with new ads every hour.
If enabled, the ad will automatically load new content when the current ad is dismissed or completed. Additionally, it will automatically retry loading the ad if an error occurs during the loading process.
@override
void initState() {
_rewarded.contentCallback = _adContentCallback;
_rewarded.setAutoloadEnabled(true);
}
By default autoload disabled.
When you show a rewarded ad, you will use an OnRewardEarnedListener
object to handle reward for the user.
_rewarded.show(OnRewardEarnedListener(onUserEarnedReward: (ad) {
// Called when a user earns a reward from the ad.
// TODO: Reward the user
}));
Controls whether interstitial ads are shown as a fallback when a rewarded video ad has no available fill. Interstitial ads do not require the user to watch the entire ad to completion. However, the OnRewardEarnedListener
will still be triggered as if the user completed the rewarded video.
This option is enabled by default. You can disable extra fill by following line:
_rewarded.setExtraFillInterstitialAdEnabled(false);
Indicates if the application’s audio is muted. Affects initial mute state for all ads.
Use this method only if your application has its own volume controls.
CAS.settings.setMutedAdSounds(true);
It is important to destroy()
loaded but not displayed ads.
@override
void dispose() {
_rewarded.destroy();
super.dispose();
}
🔗 Next
Targeting options