You can show rewarded ads to users in exchange for additional value on your site, such as access to premium content, pages without ads, or coupon codes.
On this page:
- Before you start
- Instructions
- Examples
- Rewarded ad function reference
- Test your rewarded ad unit
- Run a report on your rewarded-ads
Before you start
Before you implement rewarded ad units on your page:
- Decide what will trigger your rewarded ad and what reward you'd like to offer your users for watching the rewarded ad. For example, you could trigger a rewarded ad when users visit a recipe page, and the reward for watching the ad could be coupon codes for the ingredients.
- Make sure you review the Policies for ad units that offer rewards.
Instructions
Create a rewarded ad unit
- Sign in to your AdSense account.
- Click Ads.
- Click By ad unit.
- Click Rewarded ads.
- Give your rewarded ad unit a name.
- (Optional) Complete the reward settings for this ad unit. If you choose to use the reward settings, you can include a callback in your code for AdSense to return these values back to you when the user closes the rewarded ad. For details, review the implement the rewarded ad code on your site section below.
- Ad reward type: Enter the name of the reward type the user will receive. Examples: premium content, coupons, etc.
- Ad reward amount: Enter the number of reward items the user will receive. Must be a whole number.
- Click Create.
- In the "Add the rewarded ad HTML code" dialog, click Copy.
- Click Done.
Implement the rewarded ad code on your site
- Paste the rewarded ad code between the
<body>and</body>tags of your page where you want the rewarded ad to appear.Code example 1
Here's an example showing the rewarded ad code pasted between the
<body>and</body>tags.<!doctype html>
<html>
<head>
<title>Rewarded ad unit page</title>
</head>
<body>
<script async src="https://pagead2.googlesyndication.com/pagead/js/
adsbygoogle.js?client=ca-pub-123456" crossorigin="anonymous"></script>
<script>
slotVars = {
google_ad_loaded_callback: {{enter your callback function}},
google_ad_slot: 123456789,
google_ad_format: "rewarded",
};
adsbygoogle = window.adsbygoogle || [];
adsbygoogle.push({params: slotVars});
</script>
</body>
</html> - Implement a JavaScript callback function to pass in a
google_ad_loaded_callbackparameter. This callback function will be called when the rewarded ad is loaded.Code example 2
Here's an example of the rewarded ad code with a
loadRewardedAdcallback function added:<script async src="https://pagead2.googlesyndication.com/pagead/js/
adsbygoogle.js?client=ca-pub-123456" crossorigin="anonymous"></script>
<script>
slotVars = {
google_ad_loaded_callback: loadRewardedAd, // your JavaScript
callback function
google_ad_slot: 1234567890, // replace with your ad slot ID
google_ad_format: "rewarded",
};
adsbygoogle = window.adsbygoogle || [];
adsbygoogle.push({params: slotVars});
</script>Explanation:
google_ad_loaded_callback: The callback function you create to handle the loading of the rewarded ad. The function receives therewardedAdobject as input, allowing you to handle ad-related events.googld_ad_slot: The unique ad slot ID of the rewarded ad unit you created in AdSense.google_ad_format: Indicates that this is the rewarded ad format.
- Implement a call to the
show()function to display the rewarded ad. You need to pass in a JavaScript callback function as a parameter to handle the reward after the ad is closed. When the ad is viewed, the callback function retrieves the reward.Code example 3
Here's an example that shows using the
loadRewardedAdcallback function to display the loaded ad and handle the reward once the ad is viewed and closed by the user.function loadRewardedAd(rewardedAd) {
// handle case where ad fails to load.
if (rewardedAd == null){
alert("No rewarded ad loaded");
return;
}
if(confirm('show reward ads')){
rewardedAd.show(result => {
const closeStatus = result.status;
const rewardPayload = result.reward;
if(closeStatus == 'viewed') {
// Grant reward to the user (e.g., update in-app currency)
alert("Reward viewed");
} else {
alert("Ad closed without reward");
}
});
}
}Explanation:
- The
loadRewardedAdfunction receives therewardedAdobject when the ad is loaded. - If the ad is not loaded successfully,
loadRewardedAdhandles the no ad case and returns. - If the ad is loaded successfully and the ad is ready to be shown,
loadRewardedAdcallsshow()to display the ad. rewardedAd.show()displays the ad and takes a callback function as an argument to handle the result after the ad is closed.- The callback function receives
AdClosedResultas the parameter which contains the close status and reward payload.
- The
Examples
To use these examples, copy and paste the HTML code, and replace the publisher code and ad slot ID before testing.
Example 1: Reward button appears when ad is loaded
This code demonstrates how to reward users for watching an ad after clicking on a button. The initial button will only be shown after an ad has successfully loaded.
<html>
<body>
<!-- replace with your pub code -->
<script async
src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-123456" crossorigin="anonymous">
</script>
<button id="rewarded-button" hidden>Get Reward!</button>
<script>
function loadRewardedAd(rewardedAd) {
if (rewardedAd != null) {
var button = document.getElementById("rewarded-button");
button.hidden = false;
button.onclick = function() {
rewardedAd.show(result => {
if (result.status == 'viewed') {
console.log('Reward was granted successfully:',
result.reward);
} else {
console.log('Reward was not granted');
}
});
button.hidden = true;
};
}
}
window.adsbygoogle = window.adsbygoogle || [];
slotVars = {
google_ad_loaded_callback: loadRewardedAd,
google_ad_format: "rewarded",
google_ad_slot: 234567890, // replace with your ad slot ID
};
window.adsbygoogle.push({params: slotVars});
</script>
</body>
</html>
Example 2: Reward button is enabled when ad is loaded
This code demonstrates how to reward users for watching an ad after clicking on a button. The initial button will remain disabled until an ad has been successfully loaded.
<html>
<body>
<!-- replace with your pub code -->
<script async
src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-123456" crossorigin="anonymous">
</script>
<button id="rewarded-button" disable"true">Get Reward!</button>
<script>
function loadRewardedAd(rewardedAd) {
if (rewardedAd != null) {
var button = document.getElementById("rewarded-button");
button.disabled = false;
button.onclick = function() {
rewardedAd.show(result => {
if (result.status == 'viewed') {
console.log('Reward was granted successfully:',
result.reward);
} else {
console.log('Reward was not granted');
}
});
button.disabled = true;
};
}
}
window.adsbygoogle = window.adsbygoogle || [];
slotVars = {
google_ad_loaded_callback: loadRewardedAd,
google_ad_format: "rewarded",
google_ad_slot: 234567890, // replace with your ad slot ID
};
window.adsbygoogle.push({params: slotVars});
</script>
</body>
</html>
Rewarded ad function reference
The following table describes the rewarded ad functions.
| Function | Parameters | Description | ||||
|---|---|---|---|---|---|---|
show |
adClosedCallback (callback function)
The callback function that receives a
|
This method is called to make ads visible to the user. The callback function receives a result object containing the status of the ad closure and the reward payload you specified in your rewarded ad unit settings in AdSense. |
||||
disposeAd |
None | This method is called to dispose of unused rewardedAd objects. |
Test your rewarded ad unit
You can use the adtest parameter to test that your rewarded ad unit is working correctly. When the adtest parameter has a value of "on", Google treats the ad request as a test. You don't generate any revenue and Google doesn't count the ad impressions or track the clickthrough results, which means you can test your implementation.
Here's an example of the rewarded ad code with the adtest parameter set to "on":
<script async src="https://pagead2.googlesyndication.com/pagead/js/
adsbygoogle.js?client=ca-pub-123456" crossorigin="anonymous"
data-adtest="on"></script>
<script>
slotVars = {
google_ad_loaded_callback: loadRewardedAd, // your JavaScript callback
google_ad_slot: 1234567890, // replace with your ad slot ID
google_ad_format: "rewarded",
};
adsbygoogle = window.adsbygoogle || [];
adsbygoogle.push({params: slotVars});
</script>adtest parameter
(String) Specifies whether this is a test implementation. Possible values are "on" or "off". If set to "on", impressions and clicks won't be recorded in your account. This parameter must be used during testing. Defaults to "off" if not set.
Values:
- off – (Default) All ads generate revenue normally. This is the setting that should be used for production.
- on – Ensures that no ads log impressions or clicks. This setting is for use during testing.
Run a report on your rewarded ads
To track the performance of your rewarded ad units:
- Visit the Reports page in AdSense.
- Create a custom report, and add the "Ad format" and "Placement method" breakdowns.
In the report, your rewarded ads units are shown with the ad format "Rewarded" and the placement method "Manual".