This page demonstrates how RRM:E rewarded ads work and documents how to implement them.
A publisher can configure rewarded ads for manual invocation using an initialized swg.js
instance. To use this feature:
configurationId
and then displays it.
{
"name": "Rewarded Ad",
"configurationId": "8c9f1d2b-4f73-4e9d-bdfd-332d19367258"
}
After creating a configuration, the Publisher Center provides a configurationId
for each rewarded ad configuration. These IDs are used in subsequent JavaScript API calls.
[
{
"name": "Rewarded Ad",
"configurationId": "8c9f1d2b-4f73-4e9d-bdfd-332d19367258"
}
]
Note: Array Not Required
This array of configuration objects is for example purposes only. Publishers must use the configurationId
to invoke a specific rewarded ad, but are not required to use an array as in these examples.
To configure rewarded ads, swg.js
must first be initialized. These examples demonstrate using the library in manual
mode, but the APIs are also available in automatic mode.
To invoke a rewarded ad prompt, the publisher must use the configurationId
from the Publisher Center. Use the subscriptions.getAvailableInterventions()
method from the initialized swg.js
library to fetch the ad configuration.
const publisherConfiguration = {
name: 'Rewarded Ad',
configurationId: '8c9f1d2b-4f73-4e9d-bdfd-332d19367258',
};
const availableInterventions = await subscriptions.getAvailableInterventions();
const prompt = availableInterventions.find(({configurationId}) => {
return configurationId === publisherConfiguration.configurationId;
});
To display the rewarded ad prompt, use the returned value from subscriptions.getAvailableInterventions()
and invoke the show
method:
prompt?.show({
isClosable: true,
onResult: (result) => {
// Handle user engagement results here.
// For example, store the engagement and return true to confirm.
console.log(result);
return true;
}
});
The onResult
callback includes the user engagement result. The configurationId
matches the ID provided to the publisher from Google in the Publisher Center.
The onResult
callback conforms to the documented type in SwG GitHub.
This complete example accomplishes the following:
swg.js
library in manual mode.8c9f1d2b-4f73-4e9d-bdfd-332d19367258
configurationId to request a rewarded ad to display. PromptPersistence()
library.PromptPersistence()
is an example implementation.
In a production environment, a publisher would use the rewarded ad response to send data to the publisher's customer management system or equivalent.
<!-- manual swg.js initialization -->
<script async
subscriptions-control="manual"
type="application/javascript"
src="https://news.google.com/swg/js/v1/swg.js">
</script>
<!-- configuring swg.js to invoke and handle rewarded ads -->
<script type="module">
// Example library for storing engagement data
import {PromptPersistence} from './prompt-persistence.js';
const promptCache = new PromptPersistence();
const promptConfigurations = [
{
name: 'Rewarded Ad',
configurationId: '8c9f1d2b-4f73-4e9d-bdfd-332d19367258',
},
];
const buttonContainer = document.querySelector('#prompts');
(self.SWG = self.SWG || []).push(async (subscriptions) => {
subscriptions.configure({paySwgVersion: '2'});
subscriptions.init('CAowqfCKCw');
// Configure the event manager to log all events to the console
const eventManager = await subscriptions.getEventManager();
eventManager.registerEventListener(console.log);
const availableInterventions =
await subscriptions.getAvailableInterventions();
// For debugging, view all available interventions in the browser console
console.log({availableInterventions});
const availableInterventionConfigurationIds = availableInterventions.map(
(availableIntervention) => availableIntervention.configurationId
);
for (const promptConfiguration of promptConfigurations) {
const buttonEnabledState = availableInterventionConfigurationIds.includes(
promptConfiguration.configurationId
);
createButtonForPrompt(
availableInterventions,
promptConfiguration,
buttonEnabledState,
buttonContainer
);
}
});
// The following helper functions are not required but help to provide a clear syntax in the above example.
// Helper function for returning a specific rewarded ad (if available) from all interventions
async function getPrompt(availableInterventions, specifiedConfigurationId) {
return availableInterventions.find(({configurationId}) => {
return configurationId === specifiedConfigurationId;
});
}
// Launch a given rewarded ad
async function launchSpecificPrompt(prompt) {
prompt?.show({
isClosable: true,
onResult: (result) => {
console.log(result);
promptCache.record(result); // Record the engagement result
},
});
}
// Helper function for creating a button to launch a rewarded ad
async function createButtonForPrompt(
availableInterventions,
promptConfiguration,
buttonEnabledState,
container
) {
const button = document.createElement('button');
const prompt = await getPrompt(
availableInterventions,
promptConfiguration.configurationId
);
if (buttonEnabledState == true) {
button.onclick = () => {
launchSpecificPrompt(prompt);
};
} else {
button.setAttribute('disabled', 'true');
}
button.textContent = `${buttonEnabledState == false ? '✅' : ''} ${promptConfiguration.name}`;
container.appendChild(button);
}
</script>