微信小程序> 微信小程序添加插屏广告并设置显示频率(一天一次)

微信小程序添加插屏广告并设置显示频率(一天一次)

浏览量:890 时间: 来源:wantingtr

微信小程序今年新上线了插屏广告,设置和在代码库中接入都非常方便。详细可见微信小程序官方文档。

大体的流程就是在小程序后台新建广告位,获取到广告位的adUnitId并嵌入到源代码,因为插屏广告的单页面性,在页面的onload处添加即可。

let interstitialAd = null;if (wx.createInterstitialAd) {    interstitialAd = wx.createInterstitialAd({        adUnitId: 'adunit-ID'    })}if(interstitialAd) {    interstitialAd.show().catch((err) => {        console.error(err);    })}

嵌入广告非常简单,毕竟微信已经将所有的接口写好了,开发者仅需调用即可。
主要的功能点是设置一个插屏广告一天只显示一次,而微信并没有提供这方面的api,秉持前端能完成的就不要麻烦后端,便想到直接使用缓存存储当期日期,用户打开页面的时候获取上次缓存的日期查看是否相同即可。

//这里使用的是mpVue框架,写在mounted里。let nowday = new Date().getFullYear().toString() + (new Date().getMonth() + 1).toString() + new Date().getDate().toString() ;//获取上次打开页面时间try {    let lastDay = wx.getStorageSync('day');    if(lastDay) {        console.log('lastday', lastDay);        console.log('nowday', nowday)        if(lastDay == nowday) {            this.flag = false;        } else {            this.flag = true;        }      }} catch (e) {    //用户首次打开    this.flag = true;    console.error(e);    console.log('true no storage', this.flag)}if(interstitialAd && this.flag) {    interstitialAd.show().catch((err) => {        console.error(err);    })}interstitialAd.onLoad(() => {    try {        wx.setStorageSync('day', nowday);        console.log('存储时间', nowday);        } catch (e) {         console.log('err', err)    }})
微信小程序

版权声明

即速应用倡导尊重与保护知识产权。如发现本站文章存在版权问题,烦请提供版权疑问、身份证明、版权证明、联系方式等发邮件至197452366@qq.com ,我们将及时处理。本站文章仅作分享交流用途,作者观点不等同于即速应用观点。用户与作者的任何交易与本站无关,请知悉。

产品经理

手机 : 13312967497

擅长 : 小程序流量变现

扫码领取礼包

热门模板

  • 头条
  • 搜狐
  • 微博
  • 百家
  • 一点资讯
  • 知乎