Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

WeChat small program ads and in-screen ads


May 18, 2021 WeChat Mini Program Development Document


Table of contents


Plug-in ads

The on-screen ad component is composed of the client's native picture, text, and video controls, with the highest level and overlays on the normal component.

Developers can call wx.createInterstitialAd to create an in-screen ad component. Each time the method is called, a completely new instance is returned that is valid only for the current page and is not allowed across pages.

Ad creation

The on-screen ad component is hidden by default, so it can be created in advance to initialize the component in advance. Developers can create an ad instance in the onLoad event callback for a small program page and repeatedly call the ad instance over the life of the page.

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

Show/Hide

The on-screen ad component is hidden by default, and developers need to call InterstitialAd.show() to display it. If the ad pull fails or triggers a frequency limit, the InterstitialAd.show() method returns a rejected Promise that allows the developer to listen for error messages on his own.

interstitialAd.show().catch((err) => {
  console.error(err)
})

Users can actively turn off on-screen ads. Developers have no control over the hidden in-screen ad components.

Ads pull success and failure

The on-screen ad component automatically pulls ads and updates them. Once the component is created, an ad is pulled, and when the user closes the ad, the ad is pulled down.

If the pull is successful, the callback function registered with InterstitialAd.onLoad() is executed and the callback function has no arguments to pass.

interstitialAd.onLoad(() => {
  console.log('插屏 广告加载成功')
})

If the pull fails, the callback function registered with InterstitialAd.onError() executes, and the argument to the callback function is an object that contains error messages. Common exception errors refer to the documentation

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

Listen to users turn off ads

If the ad is turned off, a callback function registered with InterstitialAd.onClose() is executed and the callback function has no arguments to pass.

interstitialAd.onClose(res => {
    console.log('插屏 广告关闭')
})

Precautions

Multiple calls to InterstitialAd.onLoad(), InterstitialAd.onError(), InterstitialAd.onClose() and other methods to listen to ad events will result in multiple event callbacks, suggesting that you listen once after you create an ad, or cancel the original listening event before re-listening.

If you quickly switch pages during on-screen ad display, you may see on-screen ads appear on non-call pages, and if necessary, plug-in ads appear when the page switch is complete.