This function depends on the services provided by the Petal Ads SDK, which may collect users' personal data. Inform users that their personal data will be collected and guide them to read the corresponding privacy policy before they use this function in your quick game.
This document describes only the API definition. For details about the API usage sequence and scenarios, please refer to Advertising and Integrating AGD Pro.
Add the following configuration to the features attribute in the manifest.json file:
{"name": "service.ad"} Add the following configuration to the script element on the API call page:
import ad from '@service.ad'
or
var ad = require("@service.ad") | API | Description |
|---|---|
| Obtains the service provider information, for example, the English brand name of the vendor. | |
| Rewarded video ad APIs | |
| Creates a rewarded video ad. | |
| Manually obtains ads for refreshing the ad content. | |
| Displays the rewarded video ad. The rewarded video ad is hidden by default, and calling show() will make it visible. | |
| Sets the callback for successful ad loading. | |
| Removes the listener for successful rewarded video ad loading. | |
| Listens for rewarded video ad closing events. A rewarded video ad can be closed only when the user actively closes it. | |
| Removes the listener for rewarded video ad closing events. | |
| Listens for rewarded video ad loading error events. | |
| Removes the listener for rewarded video ad loading error events. | |
| rewardedVideoAd.setTagForChildProtection(number childProtection) | Sets whether to process ad requests according to the COPPA. |
| rewardedVideoAd.setTagForUnderAgeOfPromise(number underAgeOfPromiseStr) | Sets whether to process ad requests in a manner appropriate for users below the age of consent. |
| rewardedVideoAd.setAdContentClassification(string adContentClassification) | Sets the maximum ad content rating. |
| Sets whether to request non-personalized ads. | |
| Destroys a rewarded video ad. | |
| Banner ad APIs | |
| Creates a banner ad. If a banner ad has been created before, the API returns the existing one. | |
| Loads and displays a banner ad. | |
| Hides a banner ad. | |
| Listens for banner ad error events. | |
| Removes the listener for banner ad error events. | |
| Listens for banner ad loading events. | |
| Removes the listener for banner ad loading events. | |
| Listens for banner ad closing events. | |
| Removes the listener for banner ad closing events. | |
| Destroys a banner ad. | |
| Interstitial ad APIs | |
| Creates an interstitial ad. | |
| Loads an interstitial ad. | |
| Displays an interstitial ad. | |
| Listens for interstitial ad loading events. | |
| Removes the listener for interstitial ad loading events. | |
| Listens for interstitial ad error events. | |
| Removes the listener for interstitial ad error events. | |
| Listens for interstitial ad closing events. | |
| Removes the listener for interstitial ad closing events. | |
| Listens for interstitial ad click events. | |
| Removes the listener for interstitial ad click events. | |
| Destroys an interstitial ad. | |
Obtains the service provider information, for example, the English brand name of the vendor.
A character string, which is the service provider code (for example, the English brand name of the vendor), is returned. If the service does not exist, an empty string is returned. If an empty string is returned, do not call other APIs of the advertising service. Otherwise, an exception will occur.
ad.getProvider();
Creates a rewarded video ad. If an ad with the same adUnitId has already been created, the existing instance will be reused. After creation, the ad assets will be loaded. This API returns a singleton instance that is valid only within the current page and must not be used across pages.
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| adUnitId | string | Yes | ID of a rewarded video ad unit. |
let rewardedVideoAd = ad.createRewardedVideoAd({
adUnitId: 'xxx'
})Manually obtains ads for refreshing the ad content. If the ad is successfully obtained, resolved Promise is called back. If the ad fails to be obtained, onError is called back.
If requests are sent too frequently, onError may be triggered.
rewardedVideoAd.onLoad(() => {
console.log('ad loaded.')
})
rewardedVideoAd.load();| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| callback | function | Yes | Function triggered after ad loading is complete. |
rewardedVideoAd.onLoad(() => {
console.log('ad loaded.')
})rewardedVideoAd.offLoad(() => {
console.log('ad off loaded.')
})Listens for rewarded video ad closing events. A rewarded video ad can be closed only when the user actively closes it.
| Parameter | Type | Description |
|---|---|---|
| callback | function | Function triggered after an ad is closed. The callback parameter is {isEnded: xxx}. |
| Parameter | Type | Description |
|---|---|---|
| isEnded | boolean | Indicates whether a rewarded video ad is closed after being completely watched.
|
rewardedVideoAd.onClose((res) => {
console.log('ad onClose: ' + res.isEnded)
})| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| callback | function | Yes | Function triggered when an error occurs during ad loading. The callback data is an object containing the result code and message. |
| Parameter | Type | Description |
|---|---|---|
| errCode | number | Result code. |
| errMsg | string | Error message. |
rewardedVideoAd.onError((e) => {
console.error('load ad error:' + JSON.stringify(e));
const errCode = e.errCode
const errMsg = e.errMsg
})| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| childProtection | number | Yes | Indicates whether to process ad requests according to the COPPA.
|
rewardedVideoAd.setTagForChildProtection(1)
Sets whether to process ad requests in a manner appropriate for users below the age of consent.
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| underAgeOfPromiseStr | number | Yes | Indicates whether to process ad requests in a manner appropriate for users below the age of consent.
|
rewardedVideoAd.setTagForUnderAgeOfPromise(1)
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| adContentClassification | string | Yes | Ad content rating.
|
rewardedVideoAd.setAdContentClassification('W')| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| personalizedAd | number | Yes | Indicates whether to request non-personalized ads.
|
rewardedVideoAd.setNonPersonalizedAd(1)
Creates a banner ad. If a banner ad has been created before, the API returns the existing one.
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| adUnitId | string | Yes | Banner ad unit ID. |
| style | object | Yes | Banner ad style. For details, please refer to Parameters in style. |
| adIntervals | number | No | Interval for automatically refreshing an ad, in seconds. The value ranges from 30 to 120. The recommended value is 60. If this parameter is not passed, banner ads will not be automatically refreshed. This parameter is valid only when the API is called for the first time. If this parameter is not set for the first time, the setting is invalid in subsequent calls. The setting is valid only when the ad is re-created after the bannerAd.destroy() API is called to destroy the ad. |
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| top | number | Yes | Vertical coordinate of the upper left corner of the banner ad. |
| left | number | Yes | Horizontal coordinate of the upper left corner of the banner ad. |
| width | number | Yes | Width of the banner ad, in dp. |
| height | number | Yes | Height of the banner ad, in dp. |
The following table describes the restrictions on the width and height of the ad.
| Width | Height | Description |
|---|---|---|
| 360 | 57 | Regular banner ads, applicable to 1080 x 170 px ad assets. |
| 360 | 144 | Large banner ads, applicable to 1080 x 432 px ad assets. |
| 160 | 600 | Not supported in the Chinese mainland. |
| 300 | 250 | Not supported in the Chinese mainland. |
| 320 | 100 | Not supported in the Chinese mainland. |
| 320 | 50 | Not supported in the Chinese mainland. |
| 468 | 60 | Not supported in the Chinese mainland. |
| 728 | 90 | Not supported in the Chinese mainland. |
| -1 | -2 | The ad width and height automatically adapt to the screen size. |
Banner ad bannerAd. You can use bannerAd.style to obtain the actual width and height of an ad. The returns are described as follows.
| Parameter | Type | Description |
|---|---|---|
| top | number | Vertical coordinate of the upper left corner of the banner ad. |
| left | number | Horizontal coordinate of the upper left corner of the banner ad. |
| width | number | Width of the banner ad, in dp. |
| height | number | Height of the banner ad, in dp. |
| realWidth | number | Actual width of the banner ad, in px. |
| realHeight | number | Actual height of the banner ad, in px. |
let bannerAd = ad.createBannerAd({
adUnitId: 'testw6vs28auh3',
style:{
top:20,
left:20,
height:57,
width:360
}
})| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| errMsg | string | Yes | Error message. |
| errCode | number | Yes | Result code. |
bannerAd.onError((err)=>{
console.log(err)
})Creates an interstitial ad. If an interstitial ad has been created before, the API returns the existing one.
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| adUnitId | string | Yes | Interstitial ad unit ID. |
let interstitialAd = ad.createInterstitialAd({
adUnitId: 'xxxx'
})Obtains the interstitial ad data. On success, the onLoad callback is triggered; on failure, the onError callback is triggered.
interstitialAd.load();
interstitialAd.show().then(
() => { console.log('Success') },
() => { console.log('Failure') }
)interstitialAd.onLoad(function (data) {
console.log('onLoad data ' + JSON.stringify(data));
});interstitialAd.onError((err)=>{
console.log(err)
})interstitialAd.onClose(()=>{
console.log('interstitialAd closed');
})interstitialAd.onClick(()=>{
console.log('interstitialAd clicked');
})A result code is the error information obtained through the onError method.
| Result Code | Description |
|---|---|
| 200 | Universal error. |
| 202 | Invalid parameter. |
| 1001 | The ad request is invalid due to specific reasons. For example, the ad unit ID is not set or the banner ad dimensions are invalid. |
| 1002 | The banner ad has expired or the banner ad task has been removed. |
| 1003 | Internal error. |
| 1004 | The ad request succeeded, but the server did not return any available ad assets. |
| 1100 | The ad is being requested and cannot be requested again. |
| 1102 | The API version is not supported by Ads Kit. |
| 1104 | The ad request failed due to a network connection error. |
| 2000 | The HMS Core (APK) version is outdated, which does not support the setting of AppInfo. |