banner广告
<ad unitId="你的广告id"></ad>
==============================
卡片广告
<ad unit-id="你的广告id" type="card"></ad>
==============================
盒子广告
let appbox = qq.createAppBox({
adUnitId: '你的盒子广告id'
})
appbox.load().then(()=>{
appbox.show()
})
==============================
激励视频
var videoAd = null;
page({
onLoad:function(){
if (wx.createRewardedVideoAd) {
// 加载激励视频广告
videoAd = wx.createRewardedVideoAd({
adUnitId: '你的 adUnitId'
})
//捕捉错误
videoAd.onError(err => {
// 进行适当的提示
console.log('videoAd onError', res)
})
// 监听关闭
videoAd.onClose((status) => {
if (status && status.isEnded || status === undefined) {
// 正常播放结束,下发奖励
// continue you code
} else {
// 播放中途退出,进行提示
}
})
}
}
})
展示位置:
if (videoAd) {
videoAd.show().catch(err => {
// 失败重试
videoAd.load()
.then(() => videoAd.show())
})
}
===================================
插屏广告
let interstitialAd = null
Page({
onLoad() {
if(wx.createInterstitialAd){
//1.创建广告实例
interstitialAd = wx.createInterstitialAd({ adUnitId: 'xxxx' })
//3.监听广告,广告显示出来以后你要做的操作
interstitialAd.onLoad(() => {
console.log('onLoad event emit')
})
//其实可以不用管这个onError,它的作用是如果广告拉取失败,就提示你
interstitialAd.onError((err) => {
console.log('onError event emit', err)
})
//广告关闭时,触发。
interstitialAd.onClose((res) => {
console.log('onClose event emit', res)
})
}
},
onshow:function(){
//2.让广告显示出来
interstitialAd.show().catch((err) => {
console.error(err)
})
}
================================================
激励视频广告:
<view class='btn-view' bindtap="openVideo">
<view class='btn'>打开激励视频</view>
</view>
let videoAd = null
Page({
onLoad() {
// 在页面onLoad回调事件中创建激励视频广告实例
if (wx.createRewardedVideoAd) {
videoAd = wx.createRewardedVideoAd({
adUnitId: 'adunit-xxxxx'
})
videoAd.onLoad(() => {})
videoAd.onError((err) => {})
videoAd.onClose((res) => {})
}
},
openVideo: function(e) {
if (videoAd) {
videoAd.show().catch(() => {
// 失败重试
videoAd.load()
.then(() => videoAd.show())
.catch(err => {
console.log('激励视频 广告显示失败')
})
})
}
}
})
==================
插屏广告
<view>
<view class='btn-view' >
<view class='btn'>我没有点击效果哦</view>
</view>
<view class='btn-view'>
<view class='btn'>我不能点击哦</view>
</view>
<view class='btn-view' bindtap="openAd">
<view class='btn'>点击哦,有广告</view>
</view>
<view class='btn-view'>
<view class='btn'>我不能点击哦</view>
</view>
</view>
// 在页面中定义插屏广告
let interstitialAd = null
Page({
/**
* 生命周期函数--监听页面加载
*/
onLoad: function(options) {
// 在页面onLoad回调事件中创建插屏广告实例
if (wx.createInterstitialAd) {
interstitialAd = wx.createInterstitialAd({
adUnitId: 'adunit-xxxxxx'
})
interstitialAd.onLoad(() => {})
interstitialAd.onError((err) => {})
interstitialAd.onClose(() => {})
}
},
openAd: function(e) {
// 在适合的场景显示插屏广告
if (interstitialAd) {
interstitialAd.show().catch((err) => {
console.error(err)
})
}
}
})在这里插入代码片
===============、
打开视频广告
<ad unit-id="adunit-xxxxxx" ad-type="video" ad-theme="white"></ad>
打开前贴视频
<view>带广告</view>
<video src="http://wxsnsdy.tc.qq.com/105/20210/snsdyvideodownload?filekey=30280201010421301f0201690402534804102ca905ce620b1241b726bc41dcff44e00204012882540400&bizid=1023&hy=SH&fileparam=302c020101042530230204136ffd93020457e3c4ff02024ef202031e8d7f02030f42400204045a320a0201000400"
unit-id="adunit-xxxxxx"></video>
<view style="margin-top:100rpx;">同样链接,不带广告</view>
<video src="http://wxsnsdy.tc.qq.com/105/20210/snsdyvideodownload?filekey=30280201010421301f0201690402534804102ca905ce620b1241b726bc41dcff44e00204012882540400&bizid=1023&hy=SH&fileparam=302c020101042530230204136ffd93020457e3c4ff02024ef202031e8d7f02030f42400204045a320a0201000400" ></video>
打开格子视频
<ad unit-id="adunit-xxxxxx" ad-type="grid" grid-opacity="0.8" grid-count="5" ad-theme="white"></ad>
==========================================
<button class='payBtn01' bindtap='bindAddCredits'>看广告领取</button>
//按钮触发加载广告
bindAddCredits() {
var that = this
console.log('打开激励视频');
//
if (videoAd) {
//videoAd是自己声明的变量他是在全局外面声明的被onload里面定义了如果有广告就进去下面
// videoAd.show().catch(err => {
// // 如果显示show失败了就catch重试
// videoAd.load()
// .then(() => videoAd.show())
// })
videoAd.show()
.catch(() => {
videoAd.load()
.then(() => videoAd.show())
.catch(err => {
console.log(err)
console.log('激励视频 广告显示失败')
})
})
}
},
if (wx.createRewardedVideoAd) {
// 加载激励视频广告
videoAd = wx.createRewardedVideoAd({
adUnitId: 'adunit-76c516264a93e1a2'
})
//捕捉错误
videoAd.onError(err => {
console.log(err)
// 进行适当的提示
})
// 监听关闭
videoAd.onClose((status) => {
if (status && status.isEnded || status === undefined) {
// 正常播放结束,下发奖励
// continue you code
// wx.navigateTo({
// url: '../getGift/getGift?code=' + that.data.vid + '&barCode=' + that.data.typeCode
// })
that.gift()
} else {
// 播放中途退出,进行提示
}
})
}
================================================
首先在.js文件page上方定义videoAd,
var videoAd = null;
1
在js页面里
//加载激励广告
adGet: function () {
if (qq.createRewardedVideoAd) {
// 加载激励视频广告
videoAd = qq.createRewardedVideoAd({
adUnitId: 'egffhghfhfgfag7'//你的广告key
})
//捕捉错误
videoAd.onError(err => {
// 进行适当的提示
})
// 监听关闭
videoAd.onClose((status) => {
if (status && status.isEnded || status === undefined) {
// 正常播放结束,下发奖励
// continue you code
} else {
// 播放中途退出,进行提示
}
})
}
},
有的教程说需要加入到onload事件函数中,我试了试好像不用,在后面直接调用就可以了。
然后就是创建点击事件函数了。
//激励广告展示,函数名称是随意的,和前面对应就行了。
openVideoAd() {
console.log('打开激励视频');
qq.showToast({
title: '广告完成后跳转至空间',
icon: 'none',
duration: 2000
});
this.adGet();//这个地方就是调用了广告函数,然后直接展示
// 在合适的位置打开广告
if (videoAd) {
videoAd.show().catch(err => {
// 失败重试
videoAd.load()
.then(() => videoAd.show())
})
}
},
现在我们只需要在wxml的按钮上添加点击事件就可以实现了。
<button class="goodbutton" hover-class="hover" bindtap="openVideoAd">广告展示</button>
=====================================
//视频广告
let videoAd = null;
//视频广告拉取状态
let videoAdPushStatus = false;
Page({
data: {
//积分总数
creditsAmountSum: 100
},
onLoad(options) {
let that = this;
that.videoAdShowSetting();
},
onShow() {
},
videoAdShowSetting: function() {
var that = this;
if (wx.createRewardedVideoAd) {
videoAd = wx.createRewardedVideoAd({
adUnitId: '自己申请的广告ID'
})
videoAd.onLoad(() => {
//设置广告拉取成功
videoAdPushStatus = true;
});
videoAd.onClose((status) => {
if (status && status.isEnded || status === undefined) {
//正常播放结束,可以下发奖励
that.addUserCredits();
} else {
//不下发奖励
wx.showModal({
content: '广告未播放完成,无法获取积分',
showCancel: false
})
}
});
videoAd.onError(() => {
that.showToast('获取积分异常,请稍后重试');
});
}
},
//显示广告
bindAddCredits: function() {
let that = this;
that.showVideoAd();
},
//视频广告
showVideoAd: function() {
let that = this;
videoAd.load()
.then(() => {
//重置视频广告拉取状态
videoAdPushStatus = false;
videoAd.show();
})
.catch(err => {
that.showToast('加载异常,请稍后重试...');
});
},
addUserCredits: function() {
let that = this;
that.setData({
creditsAmountSum: 10,
});
that.showSuccessToast('已获取积分+10');
},
showSuccessToast(title) {
wx.showToast({
title: title,
icon: 'success',
duration: 3000
})
},
showToast(title) {
wx.showToast({
title: title,
icon: 'none',
duration: 2000
})
}
});