Rewarded ads integration for React Native
Integrate rewarded video ads in your React Native app by initializing the SDK, creating ad units, and setting up event listeners to handle ad lifecycle events.
Read time 5 minutesLast updated a month ago
Prerequisites
- Ensure that you have correctly integrated the React Native plug-in into your app. Integration is outlined here.
- Ensure that you initialize the SDK using LevelPlay Initialization API.
- Find the AdUnitID in LevelPlay dashboard.
Create Rewarded and Register to Events
After you receive the onInitSuccess callback, you can create an ad unit using the relevant Ad Unit ID, as defined in the LevelPlay platform (Initializing the LevelPlay SDK step).
Set the rewarded listener for the rewarded ad unit created, to get informed of ad delivery.
import { LevelPlayRewardedAd, type LevelPlayRewardedAdListener, type LevelPlayAdInfo, type LevelPlayAdError, type LevelPlayReward} from 'unity-levelplay-mediation'const [rewardedAd, setRewardedAd] = useState<LevelPlayRewardedAd>(new LevelPlayRewardedAd('YOUR_AD_UNIT_ID'))useEffect(() => { const listener: LevelPlayRewardedAdListener = { onAdLoaded: (adInfo: LevelPlayAdInfo) => { // Provided when the ad is successfully loaded }, onAdLoadFailed: (error: LevelPlayAdError) => { // Provided when the ad fails to load. Ad Unit information is included }, onAdInfoChanged: (adInfo: LevelPlayAdInfo) => { // Provided when the ad info is updated. Available when another ad has loaded, and includes a higher CPM/Rate }, onAdDisplayed: (adInfo: LevelPlayAdInfo) => { // Provided when the ad is displayed }, onAdDisplayFailed: (error: LevelPlayAdError, adInfo: LevelPlayAdInfo) => { // Provided when the ad fails to be displayed }, onAdClicked: (adInfo: LevelPlayAdInfo) => { // Provided when the user clicks on the ad }, onAdClosed: (adInfo: LevelPlayAdInfo) => { // Provided when the ad is closed }, onAdRewarded: (reward: LevelPlayReward, adInfo: LevelPlayAdInfo) => { // Provided when the ad is rewarded. Ad Unit info and the reward info are included. } }; rewardedAd.setListener(listener);}, [rewardedAd]);
LevelPlay Rewarded Ad Callbacks
onAdLoaded – Provided when the ad is successfully loaded
onAdLoadFailed – Provided when the ad fails to load. Ad Unit information is included
onAdDisplayed – Provided when the ad is displayed. This is equivalent to an impression
onAdDisplayFailed (optional) – Provided when the ad fails to be displayed
onAdRewarded – Provided when the ad is rewarded. Ad Unit info and the reward info are included.
onAdClicked (optional) – Provided when the user clicks on the ad
onAdClosed (optional) – Provided when the ad is closed
onAdInfoChanged (optional) – Provided when the ad info is updated. Available when another ad has loaded, and includes a higher CPM/Rate
Load Rewarded Ad
After you receive the onInitSuccess callback, you are ready to load a rewarded ad. This should be done using the method:
rewardedAd.loadAd();
Show Rewarded Ad
You can show a rewarded ad after you receive onAdLoaded callback, using the showAd APIs.
If you are using placements, share their name as part of the API, as shown below.
// Show ad without placementrewardedAd.showAd();// Show ad with placementrewardedAd.showAd('YOUR_PLACEMENT');
Check ad ready
To avoid show failures, and to make sure the ad could be displayed correctly, we recommend using the following API, before calling the showAd() API.
isAdReady – returns a Promise that resolves to true if the ad was loaded successfully and the ad unit is not capped, or false otherwise.
isPlacementCapped – static method that returns a Promise resolving to true when a valid placement is capped. If the placement is not valid or not capped, it returns false.
// Check that ad is ready and that the placement is not capped (both APIs are async)const isReady = await rewardedAd.isAdReady();const isPlacementCapped = await LevelPlayRewardedAd.isPlacementCapped('YOUR_PLACEMENT');if (isReady && !isPlacementCapped) { await rewardedAd.showAd('YOUR_PLACEMENT');}
After the ad was displayed successfully to the player, you can load another ad, repeating the Load Rewarded Ad step. You do not need to create a new ad entity when loading a single ad at a time.
Dynamic UserId
The Dynamic UserID is a parameter used to verify AdRewarded transactions that you can change throughout the session. You will receive this parameter through the server-to-server ad rewarded callbacks, and you must set it before calling showAd.
- The string value must be comprised of 1-64 alphanumeric characters.
- You will receive a parameter in the callback URL with the reward details.
dynamicUserId
LevelPlay.setDynamicUserId("userId");
Reward the User
The LevelPlay SDK will fire the onAdRewarded each time the user successfully completes a video.
The onAdRewarded and onAdClosed are asynchronous. Make sure to set up your listener to grant rewards even in cases where onAdRewarded is fired after the onAdClosed.
onAdRewarded: (reward: LevelPlayReward, adInfo: LevelPlayAdInfo) => { // Provided when the ad is rewarded. Ad Unit info and the reward info are included}
Remove Rewarded Ad
When you no longer need the rewarded ad object (for example, when the screen that owns it is unmounted), call to release the native ad instance. A removed object can't be loaded or shown again; create a new to load another ad.
remove()LevelPlayRewardedAdawait rewardedAd.remove();
Full Implementation Example of Rewarded Ad
import { LevelPlayRewardedAd, type LevelPlayRewardedAdListener, type LevelPlayAdInfo, type LevelPlayAdError, type LevelPlayReward} from 'unity-levelplay-mediation'const [rewardedAd, setRewardedAd] = useState<LevelPlayRewardedAd>(new LevelPlayRewardedAd('YOUR_AD_UNIT_ID'))const listener: LevelPlayRewardedAdListener = { onAdLoaded: (adInfo: LevelPlayAdInfo) => { // Implement your logic here, for example showing the ad rewardedAd.showAd() }, onAdLoadFailed: (error: LevelPlayAdError) => { // Implement your logic here... }, onAdInfoChanged: (adInfo: LevelPlayAdInfo) => { // Implement your logic here... }, onAdDisplayed: (adInfo: LevelPlayAdInfo) => { // Implement your logic here... }, onAdDisplayFailed: (error: LevelPlayAdError, adInfo: LevelPlayAdInfo) => { // Implement your logic here... }, onAdClicked: (adInfo: LevelPlayAdInfo) => { // Implement your logic here... }, onAdClosed: (adInfo: LevelPlayAdInfo) => { // Implement your logic here... }, onAdRewarded: (reward: LevelPlayReward, adInfo: LevelPlayAdInfo) => { // Implement your logic here... }};useEffect(() => { rewardedAd.setListener(listener) rewardedAd.loadAd()}, []);// Rest of component ...// End of component ...
LevelPlay Mediation Demo App
The Integration Demo application demonstrates how to integrate rewarded Ad Unit APIs in your app.
Verify your integration with our Integration Test Suite.
Next steps
Follow our integration guides to integrate additional Rewarded Ad networks or configure additional ad formats: