目录

Ads Kit

This function depends on the services provide by the Petal Ads SDK. The SDK may collect personal data of users. Before using this function in a quick app, inform the users that personal data will be collected and guide them to read the privacy statement.

This document describes only API definitions. For details about how and when to call these APIs, please refer to Integrating Ads Kit.

API Declaration

Add the following configuration to the features attribute in the manifest.json file:

{"name": "service.ad"}

Module Import

Add the following configuration to the script element on the page where the API will be called:

import ad from '@service.ad'

Or

var ad = require("@service.ad")

Application Scope

Item

Description

Applicable devices

Mobile phones and tablets

Applicable regions

Global

Definition

API

Description

ad.getProvider()

Obtains the service provider, for example, the English brand name of the vendor.

Consent APIs

ad.requestConsentUpdate(object)

Requests user consent updates.

ad.setUnderAgeOfPromise(boolean underAgeOfPromise)

Sets the tag for users under the age of consent.

ad.setConsentStatus(number consentStatus)

Sets consent status.

ad.setTagForChildProtection(number childProtection)

Sets whether to process ad requests according to the Children's Online Privacy Protection Act (COPPA).

ad.setTagForUnderAgeOfPromise(number underAgeOfPromiseStr)

Sets whether to process ad requests as directed to users under the age of consent.

ad.setAdContentClassification(string adContentClassification)

Sets the maximum ad content rating.

ad.setNonPersonalizedAd(number personalizedAd)

Sets whether to request non-personalized ads.

Native Ads APIs

ad.createNativeAd(object)

Creates a native ad. If a native ad has been created before, the API returns the created ad.

nativeAd.load()

Obtains ad data.

nativeAd.reportAdShow()

Reports ad impressions. The data is reported for each ad only once. The adId field is in the ad data obtained by using the load method.

nativeAd.reportAdClick()

Reports ad taps. The data is reported for each ad only once. The adId field is in the ad data obtained by using the load method.

nativeAd.onLoad(function callback)

Sets the callback function for successful ad loading.

nativeAd.offLoad(function callback)

Removes listening to successful native ad loading.

nativeAd.onError(function callback)

Listens to native ad error events.

nativeAd.offError(function callback)

Removes listening to native ad loading errors.

nativeAd.setTagForChildProtection(number childProtection)

Sets whether to process ad requests according to the COPPA.

nativeAd.setTagForUnderAgeOfPromise(number underAgeOfPromiseStr)

Sets whether to process ad requests as directed to users under the age of consent.

nativeAd.setAdContentClassification(string adContentClassification)

Sets the maximum ad content rating.

nativeAd.setNonPersonalizedAd(number personalizedAd)

Sets whether to request non-personalized ads.

nativeAd.getDownloadProgress(OBJECT)

Obtains the download progress.

nativeAd.getAppStatus(OBJECT)

Obtains the download status.

nativeAd.onStatusChanged(function)

Listens to download status changes.

nativeAd.offStatusChanged()

Cancels listening to download status changes.

nativeAd.onDownloadProgress(function)

Listens to download progress changes.

nativeAd.offDownloadProgress()

Cancels listening to download progress changes.

nativeAd.destroy()

Destroys a native ad.

Rewarded Ads APIs

ad.createRewardedVideoAd(object)

Creates a rewarded ad.

rewardedVideoAd.load()

Manually obtains ads for updating.

rewardedVideoAd.show()

Displays ads. The rewarded ad component is hidden by default.

rewardedVideoAd.onLoad(function callback)

Sets the callback function for successful ad loading.

rewardedVideoAd.offLoad(function callback)

Removes listening to successful loading of a rewarded ad.

rewardedVideoAd.onClose(function callback)

Listens to rewarded ad closing events. A rewarded ad can be closed only by a user.

rewardedVideoAd.offClose(function callback)

Removes listening to rewarded ad closing events.

rewardedVideoAd.onError(function callback)

Listens to rewarded ad loading error events.

rewardedVideoAd.offError(function callback)

Removes listening to rewarded 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 as directed to users under the age of consent.

rewardedVideoAd.setAdContentClassification(string adContentClassification)

Sets the maximum ad content rating.

rewardedVideoAd.setNonPersonalizedAd(number personalizedAd)

Sets whether to request non-personalized ads.

rewardedVideoAd.destroy()

Destroys a rewarded ad.

Banner Ads APIs

ad.createBannerAd(OBJECT)

Creates a banner ad. If a banner ad has been created before, the API returns the created ad.

bannerAd.show()

Loads and displays a banner ad.

bannerAd.hide()

Hides a banner ad.

bannerAd.onError(function callback)

Listens to banner ad error events.

bannerAd.offError()

Removes listening to banner ad error events.

bannerAd.onLoad()

Listens to banner ad loading events.

bannerAd.offLoad()

Removes listening to banner ad loading events.

bannerAd.onClose()

Listens to banner ad closing events.

bannerAd.offClose()

Removes listening to banner ad closing events.

bannerAd.destroy()

Destroys a banner ad.

Interstitial Ad APIs

ad.createInterstitialAd(OBJECT)

Creates an interstitial ad.

interstitialAd.load()

Loads an interstitial ad.

interstitialAd.show()

Displays an interstitial ad.

interstitialAd.onLoad(function callback)

Listens to interstitial ad loading events.

interstitialAd.offLoad()

Removes listening to interstitial ad loading events.

interstitialAd.onError(function callback)

Listens to interstitial ad error events.

interstitialAd.offError()

Removes listening to interstitial ad error events.

interstitialAd.onClose(function callback)

Listens to interstitial ad closing events.

interstitialAd.offClose()

Removes listening to interstitial ad closing events.

interstitialAd.onClick(function callback)

Listens to interstitial ad tap events.

interstitialAd.offClick()

Removes listening to interstitial ad tap events.

interstitialAd.destroy()

Destroys an interstitial ad.

[h2]ad.getProvider() (1075+)

Description

Obtains the service provider, for example, the English brand name of the vendor.

Returns

Character string, which is the code of the service provider (for example, the English brand name of the vendor), is returned. If the service does not exist, an empty string is returned. If so, do not call other APIs of the service. Otherwise, an exception will occur.

Sample code

ad.getProvider();

[h2]ad.requestConsentUpdate(object) (1077+)

Description

Requests user consent updates.

Parameters

Parameter

Type

Mandatory

Description

success

function

No

Called upon a success in requesting user consent updates.

fail

function

No

Called upon a failure in requesting user consent updates.

complete

function

No

Callback upon completion.

Parameters in the success callback

Parameter

Type

Description

consentStatus

number

Consent status.

  • 0: The user has agreed to receive personalized and non-personalized ads.
  • 1: The user has agreed to receive only non-personalized ads.
  • 2: The user neither agrees nor declines to receive personalized or non-personalized ads.

isNeedConsent

boolean

Indicates whether user consent is required.

  • false: Personalized ads can be requested from the Petal Ads SDK.
  • true: The user is in the EEA or another sensitive region, and user consent must be obtained.

AdProviderList

array

Ad technology provider information list. For details about the AdProvider structure, please refer to AdProvider parameters.

AdProvider parameters

Parameter

Description

id

ID of an ad technology provider.

name

Name of an ad technology provider.

serviceArea

Service area of an ad technology provider.

privacyPolicyUrl

URL of the privacy policy of an ad technology provider.

Sample code

ad.requestConsentUpdate({
  success: function(data) {
    console.log('requestConsentUpdate success data ' + JSON.stringify(data))
  },
  fail: function(data) {
    console.log('requestConsentUpdate fail data ' + JSON.stringify(data))
  },
  complete: function() {},
})

[h2]ad.setUnderAgeOfPromise(boolean underAgeOfPromise) (1077+)

Description

Sets the tag for users under the age of consent.

Parameters

Parameter

Type

Mandatory

Description

underAgeOfPromise

boolean

Yes

  • true: Process ad requests as directed to users under the age of consent.
  • false: Do not process ad requests as directed to users under the age of consent.

Sample code

ad.setUnderAgeOfPromise(true)

[h2]ad.setConsentStatus(number consentStatus) (1077+)

Description

Sets consent status.

Parameters

Parameter

Type

Mandatory

Description

consentStatus

number

Yes

User consent.

  • 0: The user has agreed to receive personalized and non-personalized ads.
  • 1: The user has agreed to receive only non-personalized ads.
  • 2: The user neither agrees nor declines to receive personalized or non-personalized ads.

Sample code

ad.setConsentStatus(1)

[h2]ad.setTagForChildProtection(number childProtection) (1078+)

Description

Sets whether to process ad requests according to the COPPA.

Parameters

Parameter

Type

Mandatory

Description

childProtection

number

Yes

Indicates whether to process ad requests according to the COPPA.

  • -1: You do not want to specify whether your ad content needs to be COPPA-compliant.
  • 0: You do not want your ad content to be COPPA-compliant.
  • 1: You want your ad content to be COPPA-compliant (no ad will be returned for this ad request).

Sample code

ad.setTagForChildProtection(1)

[h2]ad.setTagForUnderAgeOfPromise(number underAgeOfPromiseStr) (1078+)

Description

Sets whether to process ad requests as directed to users under the age of consent.

Parameters

Parameter

Type

Mandatory

Description

underAgeOfPromiseStr

number

Yes

Setting directed to users under the age of consent.

  • -1: You have not specified whether the ad content should meet the ad standard for users under the age of consent.
  • 0: You do not want the ad content to meet the ad standard for users under the age of consent.
  • 1: You want the ad content to meet the ad standard for users under the age of consent.

Sample code

ad.setTagForUnderAgeOfPromise(1)

[h2]ad.setAdContentClassification(string adContentClassification) (1078+)

Description

Sets the maximum ad content rating.

Parameters

Parameter

Type

Mandatory

Description

adContentClassification

string

Yes

Ad content rating.

  • W: content suitable for widespread audiences
  • PI: content suitable for audiences with parental guidance
  • J: content suitable for junior and older audiences
  • A: content suitable only for adults

Sample code

ad.setAdContentClassification('W')

[h2]ad.setNonPersonalizedAd(number personalizedAd) (1078+)

Description

Sets whether to request non-personalized ads.

Parameters

Parameter

Type

Mandatory

Description

personalizedAd

number

Yes

Indicates whether to request non-personalized ads.

  • 0: personalized and non-personalized ads
  • 1: non-personalized ads

Sample code

ad.setNonPersonalizedAd(1)

[h2]ad.createNativeAd(object) (1075+)

Description

Creates a native ad. If a native ad has been created before, the API returns the created ad.

Parameters

Parameter

Type

Mandatory

Description

adUnitId

string

Yes

Native ad unit ID.

Returns

Native ad.

Sample code

let nativeAd = ad.createNativeAd({ 
   adUnitId: 'xxx',
})

[h2]nativeAd.load() (1075+)

Description

Obtains ad data. If data obtaining is successful, onLoad is called back. If it fails, onError is called back.

Currently, Huawei returns one ad data record each time it calls the API.

[h2]nativeAd.reportAdShow() (1075+)

Description

Reports ad impressions. The data is reported for each ad only once. The adId field is in the ad data obtained by using the load method.

Parameters

Parameter

Type

Mandatory

Description

adId

string

Yes

Ad ID, which is returned by the onload callback.

Sample code

nativeAd.reportAdShow({
    adId: "xxx"
})

[h2]nativeAd.reportAdClick() (1075+)

Description

Reports ad taps. The data is reported for each ad only once. The adId field is in the ad data obtained by using the load method.

Parameters

Parameter

Type

Mandatory

Description

adId

string

Yes

Ad ID, which is returned by the onload callback.

[h2]nativeAd.onLoad(function callback) (1075+)

Description

Sets the callback function for successful ad loading.

Parameters

Parameter

Type

Mandatory

Description

callback

function

Yes

Function that is triggered after an ad is loaded. The callback data is the ad list object {adList: xxx}.

Parameter

Type

Description

adList

array

Ad details. For details, please refer to Ad item description.

Ad item description

Parameter

Type

Description

adId

string

Ad ID, which is used to report impressions and taps.

title

string

Ad title.

appName(1106+)

string

App name.

appVersion(1106+)

string

App version number.

appPermissionUrl(1106+)

string

App permission link.

appPrivacyUrl(1106+)

string

App privacy policy link.

appCompany(1106+)

string

App developer name.

desc

string

Ad description.

source (1077+)

string

Ad source.

icon

string

Icon of a promoted app.

imgUrlList

array

List of image URLs.

videoUrlList

array

List of video URLs.

videoRatio (1078+)

array

Ad video aspect ratio.

logoUrl

string

Ad tag image.

NOTE:

Currently, Huawei returns only an empty string "".

clickBtnTxt

string

Text on the tapped button.

creativeType

number

Ad type.

  • 1: text
  • 2: large image
  • 3: large image and text
  • 4: GIF image
  • 6: video and text
  • 7: small image and text
  • 8: three small images and text
  • 9: video
  • 10: icon (text)
  • 101: text with a download button
  • 102: large image with a download button
  • 103: large image and text with a download button
  • 104: GIF image with a download button
  • 106: video and text with a download button
  • 107: small image and text with a download button
  • 108: three small images and text with a download button
  • 0 or 110: icon (text) with a download button
NOTE:

The options are different from those of vendor alliance.

interactionType

number

Interaction type after an ad is tapped.

  • 0: none
  • 1: browsing
  • 2: download
  • 3: browser opening (for download screen ads)
  • 4: app home screen opening
  • 5: app details screen opening
NOTE:

Currently, the return value is fixed at 0.

Sample code

nativeAd.onLoad((data) => {
 console.info('ad data loaded: ' + JSON.stringify(data))
 this.adItemData = data.adList[0]
})

[h2]nativeAd.offLoad(function callback) (1075+)

Description

Removes listening to successful native ad loading.

Parameters

Parameter

Type

Mandatory

Description

callback

function

No

Callback object to be removed.

NOTE:

Currently, Huawei does not support the removal of a callback object. This parameter can be left empty to remove all callback objects.

[h2]nativeAd.onError(function callback) (1075+)

Description

Listens to native ad error events.

Parameters

Parameter

Type

Mandatory

Description

callback

function

Yes

Function that is triggered when an error occurs during ad loading. The callback data is an object containing the error code and error information.

callback parameters

Parameter

Type

Description

errCode

number

Result code.

errMsg

string

Error information.

Result codes

Result Code

Description

1001

Invalid parameter.

1002

Invalid ad unit.

1003

Internal error.

1004

No suitable ad found.

1100

Related APIs are called too frequently.

1102

An unsupported method is called.

1104

Network error.

2000

Unknown error.

Sample code

nativeAd.onError((e) => {
  console.error('load ad error:' + JSON.stringify(e));
  const errCode = e.errCode
  const errMsg = e.errMsg
})

[h2]nativeAd.offError(function callback) (1075+)

Description

Removes listening to native ad loading errors.

Parameters

Parameter

Type

Mandatory

Description

callback

function

No

Callback object to be removed.

NOTE:

Currently, Huawei does not support the removal of a callback object. This parameter can be left empty to remove all callback objects.

[h2]nativeAd.setTagForChildProtection(number childProtection) (1077+)

Description

Sets whether to process ad requests according to the COPPA. You are advised to use the ad.setTagForChildProtection(number childProtection) API instead.

Parameters

Parameter

Type

Mandatory

Description

childProtection

number

Yes

Indicates whether to process ad requests according to the COPPA.

  • -1: You do not want to specify whether your ad content needs to be COPPA-compliant.
  • 0: You do not want your ad content to be COPPA-compliant.
  • 1: You want your ad content to be COPPA-compliant (no ad will be returned for this ad request).

Sample code

nativeAd.setTagForChildProtection(1)

[h2]nativeAd.setTagForUnderAgeOfPromise(number underAgeOfPromiseStr) (1077+)

Description

Sets whether to process ad requests as directed to users under the age of consent. You are advised to use the ad.setTagForUnderAgeOfPromise(number underAgeOfPromiseStr) API instead.

Parameters

Parameter

Type

Mandatory

Description

underAgeOfPromiseStr

number

Yes

Setting directed to users under the age of consent.

  • -1: You have not specified whether the ad content should meet the ad standard for users under the age of consent.
  • 0: You do not want the ad content to meet the ad standard for users under the age of consent.
  • 1: You want the ad content to meet the ad standard for users under the age of consent.

Sample code

nativeAd.setTagForUnderAgeOfPromise(1)

[h2]nativeAd.setAdContentClassification(string adContentClassification) (1077+)

Description

Sets the maximum ad content rating. You are advised to use the ad.setAdContentClassification(string adContentClassification) API instead.

Parameters

Parameter

Type

Mandatory

Description

adContentClassification

string

Yes

Ad content rating.

  • W: content suitable for widespread audiences
  • PI: content suitable for audiences with parental guidance
  • J: content suitable for junior and older audiences
  • A: content suitable only for adults

Sample code

nativeAd.setAdContentClassification('W')

[h2]nativeAd.setNonPersonalizedAd(number personalizedAd) (1077+)

Description

Sets whether to request non-personalized ads. You are advised to use the ad.setNonPersonalizedAd(number personalizedAd) API instead.

Parameters

Parameter

Type

Mandatory

Description

personalizedAd

number

Yes

Indicates whether to request non-personalized ads.

  • 0: personalized and non-personalized ads
  • 1: non-personalized ads

Sample code

nativeAd.setNonPersonalizedAd(1)

[h2]nativeAd.getDownloadProgress(OBJECT) (1078+)

Description

Obtains the download progress.

Parameters

Parameter

Type

Mandatory

Description

adId

string

Yes

Ad ID, which is returned by the onload callback.

Returns

App download progress. The value is of the number type and ranges from 0 to 100.

Sample code

const progress = nativeAd.getDownloadProgress({
    adId: "xxx"
})
console.log('getDownloadProgress progress = ' + progress);

[h2]nativeAd.getAppStatus(OBJECT) (1078+)

Description

Obtains the download status.

Parameters

Parameter

Type

Mandatory

Description

adId

string

Yes

Ad ID, which is returned by the onload callback.

Returns

App download status. The value is of the string type. The options are described as follows.

Value

Type

Description

DOWNLOAD

string

App download has not started. This is the initial app status.

DOWNLOADING

string

App download is in progress.

DOWNLOADFAILED

string

Download fails.

WAITING

string

App download is in waiting state.

PAUSE

string

App download is paused.

INSTALL

string

App installation is in waiting state.

INSTALLING

string

The app is being installed.

INSTALLED

string

App installation is complete. The app can be launched.

Sample code

const status = nativeAd.getAppStatus({
    adId: "xxx"
})
console.log('getAppStatus status = ' + status );

[h2]nativeAd.onStatusChanged(function) (1078+)

Description

Listens to download status changes.

This method must be called in nativeAd.onLoad. Otherwise, the download status cannot be listened.

Parameters

Parameter

Type

Description

status

string

App download status.

uniqueId

string

Unique ID of an app.

Sample code

nativeAd.onStatusChanged((data) => {
    console.log('onStatusChanged data = ', data)
})

[h2]nativeAd.offStatusChanged() (1078+)

Description

Cancels listening to download status changes.

Sample code

nativeAd.offStatusChanged()

[h2]nativeAd.onDownloadProgress(function) (1078+)

Description

Listens to download progress changes.

This method must be called in nativeAd.onLoad. Otherwise, the download progress cannot be listened.

Parameters

Parameter

Type

Description

progress

number

Download progress. The value ranges from 0 to 100.

uniqueId

string

Unique ID of an app.

Sample code

nativeAd.onDownloadProgress((data) => {
    console.log('onDownloadProgress data = ', data)
})

[h2]nativeAd.offDownloadProgress() (1078+)

Description

Cancels listening to download progress changes.

Sample code

nativeAd.offDownloadProgress()

[h2]nativeAd.destroy() (1075+)

Description

Destroys a native ad.

[h2]ad.createRewardedVideoAd(object) (1075+)

Description

Creates a rewarded ad. If an ad has been created for the passed adUnitId, the existing object is reused. Then ad assets will be loaded. A singleton is returned, which is valid only for the current screen.

Parameters

Parameter

Type

Mandatory

Description

adUnitId

string

Yes

ID of a rewarded ad unit.

Returns

Rewarded ad.

Sample code

let rewardedVideoAd = ad.createRewardedVideoAd({
    adUnitId: 'xxx'
})

[h2]rewardedVideoAd.load() (1075+)

Description

Manually obtains an ad to refresh. 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 called back.

[h2]rewardedVideoAd.show() (1075+)

Description

Displays ads. The rewarded ad is hidden by default. onError is called back for a call failure.

[h2]rewardedVideoAd.onLoad(function callback) (1075+)

Description

Sets the callback function for successful ad loading.

Parameters

Parameter

Type

Mandatory

Description

callback

function

Yes

Function that is triggered after an ad is loaded.

Sample code

rewardedVideoAd.onLoad(() => {
  console.log('ad loaded.')
})

[h2]rewardedVideoAd.offLoad(function callback) (1075+)

Description

Removes listening to successful loading of a rewarded ad.

Parameters

Parameter

Type

Mandatory

Description

callback

function

No

Callback object to be removed.

Note: Huawei does not support the removal of a callback object currently. This parameter can be left empty to remove all callback objects.

Sample code

rewardedVideoAd.offLoad(() => {
  console.log('ad off loaded.')
})

[h2]rewardedVideoAd.onClose(function callback) (1075+)

Description

Listens to rewarded ad closing events. A rewarded ad can be closed only by a user.

Parameters

Parameter

Type

Description

callback

function

Function that is triggered after an ad is closed. The callback parameter is {isEnded: xxx}.

Parameter

Type

Description

isEnded

boolean

Indicates whether a rewarded ad is closed after a user watches it completely.

  • true
  • false

Sample code

rewardedVideoAd.onClose((res) => {
  console.log('ad onClose: ' + res.isEnded)
})

[h2]rewardedVideoAd.offClose(function callback) (1075+)

Description

Removes listening to rewarded ad closing events.

Parameters

Parameter

Type

Mandatory

Description

callback

function

No

Callback object to be removed.

NOTE:

Currently, Huawei does not support the removal of a callback object. This parameter can be left empty to remove all callback objects.

[h2]rewardedVideoAd.onError(function callback) (1075+)

Description

Listens to rewarded ad loading error events.

Parameters

Parameter

Type

Mandatory

Description

callback

function

Yes

Function that is triggered when an error occurs during ad loading. The callback data is an object containing the error code and error information.

Callback parameters

Parameter

Type

Description

errCode

number

Error code.

errMsg

string

Error information.

Sample code

rewardedVideoAd.onError((e) => {
  console.error('load ad error:' + JSON.stringify(e));
  const errCode = e.errCode
  const errMsg = e.errMsg
})

[h2]rewardedVideoAd.offError(function callback) (1075+)

Description

Removes listening to rewarded ad loading error events.

Parameters

Parameter

Type

Mandatory

Description

callback

function

No

Callback object to be removed.

Note: Huawei does not support the removal of a callback object currently. This parameter can be left empty to remove all callback objects.

[h2]rewardedVideoAd.setTagForChildProtection(number childProtection) (1077+)

Description

Sets whether to process ad requests according to the COPPA.

Parameters

Parameter

Type

Mandatory

Description

childProtection

number

Yes

Indicates whether to process ad requests according to the COPPA.

  • -1: You do not want to specify whether your ad content needs to be COPPA-compliant.
  • 0: You do not want your ad content to be COPPA-compliant.
  • 1: You want your ad content to be COPPA-compliant (no ad will be returned for this ad request).

Sample code

rewardedVideoAd.setTagForChildProtection(1)

[h2]rewardedVideoAd.setTagForUnderAgeOfPromise(number underAgeOfPromiseStr) (1077+)

Description

Sets whether to process ad requests as directed to users under the age of consent.

Parameters

Parameter

Type

Mandatory

Description

underAgeOfPromiseStr

number

Yes

Setting directed to users under the age of consent.

  • -1: You have not specified whether the ad content should meet the ad standard for users under the age of consent.
  • 0: You do not want the ad content to meet the ad standard for users under the age of consent.
  • 1: You want the ad content to meet the ad standard for users under the age of consent.

Sample code

rewardedVideoAd.setTagForUnderAgeOfPromise(1)

[h2]rewardedVideoAd.setAdContentClassification(string adContentClassification) (1077+)

Description

Sets the maximum ad content rating.

Parameters

Parameter

Type

Mandatory

Description

adContentClassification

string

Yes

Ad content rating.

  • W: content suitable for widespread audiences
  • PI: content suitable for audiences with parental guidance
  • J: content suitable for junior and older audiences
  • A: content suitable only for adults

Sample code

rewardedVideoAd.setAdContentClassification('W')

[h2]rewardedVideoAd.setNonPersonalizedAd(number personalizedAd) (1077+)

Description

Sets whether to request non-personalized ads.

Parameters

Parameter

Type

Mandatory

Description

personalizedAd

number

Yes

Indicates whether to request personalized ads.

  • 0: yes
  • 1: no

Sample code

rewardedVideoAd.setNonPersonalizedAd(1)

[h2]rewardedVideoAd.destroy() (1075+)

Description

Destroys a rewarded ad.

[h2]ad.createBannerAd(OBJECT) (1078+)

Description

Creates a banner ad. If a banner ad has been created before, the API returns the created ad.

Parameters

Parameter

Type

Mandatory

Description

adUnitId

string

Yes

Banner ad unit ID.

style

object

Yes

Style of the banner ad. For details, please refer to style parameters.

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, unless the banner ad is destroyed using bannerAd.destroy() and created again.

style parameters

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

Common 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.

Returns

Banner ad. You can use bannerAd.style to obtain the actual width and height of an ad. The response parameters 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.

Sample code

let bannerAd = ad.createBannerAd({
    adUnitId: 'testw6vs28auh3',
    style:{
        top:20,
        left:20,
        height:57,
        width:360
    }
})

[h2]bannerAd.show() (1078+)

Description

Loads and displays a banner ad. When an error occurs, onError is called back. When the loading is successful, onLoad is called back.

[h2]bannerAd.hide() (1078+)

Description

Hides a banner ad.

[h2]bannerAd.onError(function callback) (1078+)

Description

Listens to banner ad error events.

Returns

Object type.

Parameter

Type

Mandatory

Description

errMsg

string

Yes

Error information.

errCode

number

Yes

Error code.

Sample code

bannerAd.onError((err)=>{
    console.log(err)
})

[h2]bannerAd.offError() (1078+)

Description

Removes listening to banner ad error events.

[h2]bannerAd.onLoad() (1078+)

Description

Listens to banner ad loading events.

[h2]bannerAd.offLoad() (1078+)

Description

Removes listening to banner ad loading events.

[h2]bannerAd.onClose() (1078+)

Description

Listens to banner ad closing events.

[h2]bannerAd.offClose() (1078+)

Description

Removes listening to banner ad closing events.

[h2]bannerAd.onResize() (1078+)

Description

Listens to banner ad size change events. (This function is not implemented currently.)

[h2]bannerAd.offResize() (1078+)

Description

Removes listening to banner ad size change events. (This function is not implemented currently.)

[h2]bannerAd.destroy() (1078+)

Description

Destroys a banner ad.

[h2]ad.createInterstitialAd(OBJECT) (1078+)

Description

Creates an interstitial ad. If an interstitial ad has been created before, the API returns the created ad.

Parameters

Parameter

Type

Mandatory

Description

adUnitId

string

Yes

Interstitial ad slot ID.

Sample code

let interstitialAd = ad.createInterstitialAd({
    adUnitId: 'xxxx'
})

[h2]interstitialAd.load() (1078+)

Description

Obtains the interstitial ad data. If data is obtained, onLoad is called back. If data is not obtained, onError is called back.

Sample code

interstitialAd.load();

[h2]interstitialAd.show() (1078+)

Description

Displays an interstitial ad.

Returns

Promise, which indicates the operation result.

Sample code

interstitialAd.show().then(
    () => { console.log('Success') },
    () => { console.log('Failure') }
)

[h2]interstitialAd.onLoad(function callback) (1078+)

Description

Sets the callback function for successful interstitial ad loading.

Sample code

interstitialAd.onLoad(function (data) {
    console.log('onLoad data ' + JSON.stringify(data));
});

[h2]interstitialAd.offLoad() (1078+)

Description

Removes listening to interstitial ad loading events.

Sample code

interstitialAd.offLoad();

[h2]interstitialAd.onError(function callback) (1078+)

Description

Listens to interstitial ad error events.

Returns

Object type.

Parameter

Type

Description

errMsg

string

Error information.

errCode

number

Error code.

Sample code

interstitialAd.onError((err)=>{
    console.log(err)
})

[h2]interstitialAd.offError() (1078+)

Description

Removes listening to interstitial ad error events.

Sample code

interstitialAd.offError();

[h2]interstitialAd.onClose(function callback) (1078+)

Description

Listens to interstitial ad closing events.

Sample code

interstitialAd.onClose(()=>{
    console.log('interstitialAd closed');
})

[h2]interstitialAd.offClose() (1078+)

Description

Removes listening to interstitial ad closing events.

Sample code

interstitialAd.offClose();

[h2]interstitialAd.onClick(function callback) (1078+)

Description

Listens to interstitial ad tap events.

Sample code

interstitialAd.onClick(()=>{
    console.log('interstitialAd clicked');
})

[h2]interstitialAd.offClick() (1078+)

Description

Removes listening to interstitial ad tap events.

Sample code

interstitialAd.offClick();

[h2]interstitialAd.destroy() (1078+)

Description

Destroys an interstitial ad.

Sample code

interstitialAd.destroy()

Error Codes

An error code is the error information obtained through onError.

Error Code

Description

200

General error.

202

Invalid parameter.

1000

Call failed due to a backend error.

1001

The ad request is invalid due to specific causes, such as not setting the ad unit ID or invalid banner ad size.

1002

The banner ad has expired or the banner ad task has been removed.

1003

Internal error.

1004

The ad request is sent successfully, but the server returns a response indicating no available ad assets.

1005

The ad component is being reviewed.

1006

The ad component is rejected.

1007

The ad function is disabled.

1008

The ad function of the ad slot is disabled.

1100

The ad is being requested and cannot be requested again.

1101

The ad is not displayed for a long time after being loaded. The ad information has expired.

1102

The API version is not supported by the HMS Core Ads SDK.

1103

Failed to monitor the environment. For example, the app has no permission.

1104

The ad request fails due to a network connection error.

1105

Failed to load the ad.

1106

Failed to display the ad.

2000

The HMS Core (APK) version does not support the setting of AppInfo.

Precautions

Before submitting an ad for review, perform self-check on the ad based on the following rules:

  1. Do not set a timer to cyclically request ads.
  2. Do not repeatedly request ads after a failure.

    [Incorrect] Request the ad again in onError after the failure. If onError is called back each time, the request-fail-request cycle starts.

    [Recommended] Do not send a request again regardless of whether the request succeeds or fails. The service can have one more try only for rewarded ads.

  3. An ad cannot be displayed repeatedly for one request. After being displayed, it will need to be reloaded in real time for the next request.

    [Incorrect] Display the same global ad on multiple pages.

    [Recommended] The ad object on each page is independent. You need to use the createXX method to create ads and register a callback.

  4. For a pre-loaded ad, such as rewarded and interstitial ads, the interval between calling the load and show APIs cannot be too long. If the interval exceeds one hour, request the ad again. Otherwise, the ad display will not be counted as valid for settlement.
  5. You are advised to pre-load the rewarded ad before calling the show API. If the loading fails, the ad entry is not displayed. Otherwise, the video data may not be completely loaded when users enter the ad entry, and they may view a blank screen.

    [Recommended] Add a delay of several seconds or minutes before calling the show API in the onLoad callback.

    The delay cannot exceed one hour. Otherwise, the ad is regarded as invalid and will not be charged.

  6. Ads should not be placed on screens that are not content-based.
  7. An ad must have a close button, especially for native ads with custom layout.
  8. Ad assets must be displayed without being compressed. Ads can only be resized proportionally, with aspect ratio unchanged.
  9. Do not set the ad background, which is the area without ad assets, as a clickable area. Users should only be allowed to go to the landing page when they click ad assets, including the images, title, and buttons.
  10. Each time a native ad is tapped, the reportAdClick API is called to report a click event.

    [Incorrect] When a native ad is clicked for multiple times, only one click event is reported.

  11. Each time a native ad is displayed, an impression event needs to be reported.

    [Incorrect] Only one impression event is reported when the native ad is displayed for multiple times. For example, when an ad is displayed for the first time, the reportAdShow API is called to report the ad. When a user switches from another page to the ad page, the ad is still displayed, but no impression event is reported.

    [Recommended] When a user switches from another page to the ad page and the native ad is still displayed, the impression event needs to be reported again. Generally, this logic is processed in the page lifecycle function onShow.

  12. When a user taps the native ad image, the reportAdClick API is called to jump to the landing page.
  13. The native ad must contain the following information: ad source, ad title, ad ID, close button, and view or installation button.
  14. Rewarded and interstitial ads need to be pre-loaded. Other types of ads are loaded upon request success.

    [Incorrect] The native ad is requested but not displayed.

  15. Ads can be requested and displayed only after user consent to the privacy statement is obtained.
  16. You are advised to preferentially select test ad unit IDs beginning with test.

    Ad Format

    Test Ad Unit ID

    Display Form

    Dimensions

    Promotion Type

    Splash

    testq6zq98hecj

    Image

    1080 x 1620

    Web page

    q6zq98hecj

    Image

    1920 x 1080

    Web page

    n7p4mh57xs

    Image

    1080 x 1620

    App engagement

    q1h4mvd5a3

    Image

    1080 x 1620

    App download

    testd7c5cewoj6

    Video

    720 x 1280

    Web page

    Native

    testu7m3hc4gvm

    Large image

    1080 x 607

    Web page

    i7ik1b3mgg

    Large image

    1080 x 607

    App engagement

    a5sz2rxpaj

    Large image

    1080 x 607

    App download

    u7m3hc4gvm

    Large image

    1080 x 607

    App download

    r6w14o0hqz

    Three images

    225 x 150

    App download

    testb65czjivt9

    Small image

    225 x 150

    App download

    b65czjivt9

    Small image

    225 x 150

    App download

    testy63txaom86

    Video

    640 x 360

    App download

    y63txaom86

    Video

    640 x 360

    App download

    j79gukslrn

    Image

    160*160

    App download

    Rewarded

    u2k89ub7vq

    Video

    640 x 360

    Web page

    e7hm5vx799

    Video

    640 x 360

    App download

    testx9dtjwj8hp

    Video

    720 x 1080

    App download

    Banner

    x0kvs12iu6

    Image

    1080 x 170

    App download

    testw6vs28auh3

    Image

    1080 x 170

    App download

    Interstitial

    testb4znbuh3n2

    Video

    720 x 1080

    Web page

    t3u4pks711

    Video

    640 x 360

    App download

    l7s7x17w20

    Video

    1280 x 720

    App download

    g2tz5d4wkv

    Video

    720 x 1280

    App download

FAQs

Version Change History

Version

Date

Description

1070

2020-05-19

Released the first version.