微信小程序怎么添加插屏广告并设置显示频率
发表于:2024-10-01 作者:千家信息网编辑
千家信息网最后更新 2024年10月01日,本篇内容主要讲解"微信小程序怎么添加插屏广告并设置显示频率",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"微信小程序怎么添加插屏广告并设置显示频率"吧!插屏
千家信息网最后更新 2024年10月01日微信小程序怎么添加插屏广告并设置显示频率
本篇内容主要讲解"微信小程序怎么添加插屏广告并设置显示频率",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"微信小程序怎么添加插屏广告并设置显示频率"吧!
插屏广告
用户触发小程序中的特定场景时,插屏广告将自动向用户展现,用户可以随时关闭插屏广告。广告触发场景由流量主自定义,广告按曝光计费(CPM)。
微信小程序今年新上线了插屏广告,设置和在代码库中接入都非常方便。详细可见微信小程序官方文档。
大体的流程就是在小程序后台新建广告位,获取到广告位的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) }})
PS:下面看下一天只显示一次的弹出广告
仿京东官网顶部的广告关闭,效果为第一次进入官网会出现广告,然后点击关闭,刷新网页不会再显示广告,但是当清除localStorage存入的数据,刷新网页会再显示广告。
html代码
css代码
.header{ width:100%; height:80px; background:#000;}.header-a{ width:1190px; margin:0 auto; position:relative; background:url("images/1.jpg") no-repeat;}.header-a a{ width:100%; height:80px; display:block;}.close{ cursor:pointer; color:#fff; position:absolute; top:5px; right:5px; background:rgb(129, 117, 117); width: 20px; text-align: center; line-height: 20px;}
js代码
//localStorage方法function popAd(){ //判断localStorage里有没有isClose if(localStorage.getItem("isClose")){ $(".header").hide(); }else{ $(".header").show(); } //点击关闭隐藏图片存取数据 $(".close").click(function(){ $(".header").fadeOut(1000); localStorage.setItem("isClose", "1"); }) } popAd();
chrome console清本地缓存localStorage.clear()
批量清:
localStorage.clear()
单独清除某个记录的缓存,如:
localStorage.clear("popup_info") 或 localStorage.removeItem("popup_info");
到此,相信大家对"微信小程序怎么添加插屏广告并设置显示频率"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
广告
插屏
程序
用户
代码
缓存
页面
频率
内容
场景
数据
方法
日期
时间
网页
存储
学习
实用
更深
相同
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
fc网络技术
卫星软件开发平均价格
世界互联网根服务器分布
中医药科技与互联网健康
网络安全法的简称
教育系统网络安全演练
战舰少女r服务器边上的标志锚
期刊数据库按什么分组
住宅物业管理服务器导则
互联网科技学校学什么
软件开发 详细设计阶段
开区服务器怎么样
西安住宿软件开发
小型家庭服务器
全国网络安全先进单位
互联网信息科技公司有哪些
公共数据库好还是meta好
二甲医院网络安全等级保护
关于网络安全的手抄
物联网服务器学什么
服务器多少核
苏州美居客网络技术有限公司
北京腾升网络技术有限公司
软件开发的职业发展趋势和要求
国内常用数据库是
tsc244如何连接数据库
c 图像保存在数据库
华为服务器和小型机哪个好
河源通讯软件开发平均价格
内容管理系统数据库设置