# Incentive video advertising

Weixin Mini Program Ad Traffic Master Operating Instructions: Document Address The incentive video advertising component is made up of client-native pictures, text, and video controls, and is the highest level, which is covered by the ordinary components.

Developers can call wx.createRewardedVideoAd to create a motivational video ad component.This method returns a singleton, that is valid only for the current page and is not allowed to be used across pages.

# Advertising Creation

The incentive video advertising component is hidden by default, so it can be created ahead of time to initialize the component in advance.Developers can create an ad instance in the onLoad event callback of the Weixin Mini Program page and repeatedly call the ad instance over the life of the page.

let rewardedVideoAd = null
Page({
  onLoad() {
    if(wx.createRewardedVideoAd){
      rewardedVideoAd = wx.createRewardedVideoAd({ adUnitId: 'xxxx' })
      rewardedVideoAd.onLoad(() => {
        console.log('onLoad event emit')
      })
      rewardedVideoAd.onError((err) => {
        console.log('onError event emit', err)
      })
      rewardedVideoAd.onClose((res) => {
        console.log('onClose event emit', res)
      })
    }
  }
})

To avoid misuse of advertising resources, there is currently a limited number of incentive video ads per user per day, and it is recommended to judge whether the ad is pulled successfully before displaying the ad button.

# Show / hide

The incentive video ad component is hidden by default, after the user actively triggers the ad, the developer needs to call RewardedVideoAd.show () Make a display.

rewardedVideoAd.show() 

Ads are turned off only when the user clicks theTurn off adsbutton on the incentive video ad component.Developers have no control over the hidden nature of incentive video ad components.

# Successes and failures of advertising campaigns

The incentive video advertising component automatically pulls ads and updates them.An ad is pulled once after the component is created, and the user clicksto close the adto pull the next ad.

If the pull is successful, the callback registered with RewardedVideoAd.onLoad () will execute, RewardedVideoAd.show () The Promise returned will also be a resolvedPromise.Both callbacks have no parameter passing.

rewardedVideoAd.onLoad(() => {
  console.log('激励视频 广告加载成功')
})

rewardedVideoAd.show()
.then(() => console.log('激励视频 广告显示'))

If the pull fails, rewardedVideoAd.onError ()]((RewardedVideoAd.onError)) A registered callback function will be executed, and the parameter of the callback function is an object that contains error information. Common Exceptions Error Reference Documentation

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

The Promise returned by RewardedVideoAd.show () will also be a rejected Promise.

rewardedVideoAd.show()
.catch(err => console.log(err))

# Pull failed, pull again.

If an auto-pull of a component fails, the show () that is called later will be rejected.You can then call RewardedVideoAd.load () to manually repull the ad.

rewardedVideoAd.show()
.catch(() => {
    rewardedVideoAd.load()
    .then(() => rewardedVideoAd.show())
    .catch(err => {
      console.log('激励视频 广告显示失败')
    })
})

If the automatic pull of the component is successful, calling the load () method will return a resolvedPromise directly, without pulling the ad.

rewardedVideoAd.load()
.then(() => rewardedVideoAd.show())

# Listen to users turn off ads

Ads are turned off only when the user clicks theTurn off adsbutton on the incentive video ad component.This event can be monitored via RewardedVideoAd.onClose () .

RewardedVideoAd.onClose () The callback function passes a parameter res,res.isEndedDescribe the state of the ad when it is closed.

attribute type Introductions
isEnded boolean Whether the video is closed when the user watches it completely, true indicates that the user closes the video after the video is played, and false indicates that the user closes the video during the video playback.

The developer needs to decide whether the video is finished or not according tores.isEnded.

rewardedVideoAd.onClose(res => {
    // 用户点击了【关闭广告】按钮
    if (res && res.isEnded) {
      // 正常播放结束,可以下发游戏奖励
    } else {
      // 播放中途退出,不下发游戏奖励
    }
})

# Note

Multiple calls RewardedVideoAd.onLoad()RewardedVideoAd.onError()RewardedVideoAd.onClose() Other methods of listening to an ad event generate multiple event callbacks. It is recommended to listen to the ad once after the ad is created, or to cancel the original listening event before relistening.