一只风里 2025-05-30 19:23 采纳率: 50%
浏览 50
已结题

微信小程序加载激励广告出现无合适的广告问题

目前我用的是Uniapp框架开发小程序,最近有些用户(已知有2个)加载不了我小程序的所有激励广告,但是我自己的微信和别人的微信有可以加载。
还好我针对这个问题,在代码上记录了加载失败的日志传送给后端。

然后我还问了那个用户是不是用Iphone手机,他说不是,用的安卓。我自己手机也是安卓

下面是打印的日志信息:

img

我的小程序前端页面代码是这样的:

首先是initAdver函数,在页面onLoad时候会去调用。,recordLog函数是将广告加载失败的问题记录到数据库里面

//初始化广告信息
            initAdver(){
                console.log("initAdver")
                var that = this;
                this.isLoadAd = false
                console.log("ad为:",getApp().globalData.adverInfo.rewardedVideoAd )
                this.rewardedVideoAd = uni.createRewardedVideoAd({ adUnitId: getApp().globalData.adverInfo.rewardedVideoAd }) 
                this.rewardedVideoAd.onLoad(res => {
                    // 当激励视频被关闭时,默认预载下一条数据,加载完成时仍然触发 `onLoad` 事件
                    that.isLoadAd = true
                    console.log('onLoad event')
                })
                this.rewardedVideoAd.onError((err) => {
                   console.log('onError event', err);
                   recordLog({"module":"广告模块","api":"uni.createRewardedVideoAd","content":"页面调用initAdver,广告加载时候,发生onError","res":JSON.stringify(err) })
                   
                   
                })
                    
                // 用户点击了【关闭广告】按钮
                this.rewardedVideoAd.onClose((res) => {
                    var that = this;
                    if (res && res.isEnded) {
                      // 正常播放结束
                      // 这里应该联网给予用户激励。且这段代码应该做安全保护,详见下文中的“安全注意”
                    console.log("奖励开始发送")
                     this.toReward();
                    } else {
                        // 播放中途退出
                        console.log('播放中途退出')
                    }
                })
            },

showRewardedVideo函数是当用户点击要看广告的时候触发的,recordLog函数是将广告加载失败的问题记录到数据库里面

showRewardedVideo() {
                var that = this;
                //显示广告提示语
                uni.showModal({
                    title:'提示',
                    content:getApp().globalData.adverInfo.tip,
                    success(res){
                        if(res.confirm){
                            if (that.isLoadAd) {
                              that.rewardedVideoAd.show().catch(err=>{
                                  //失败重试
                                  
                                  that.rewardedVideoAd.load()
                                      .then(() => that.rewardedVideoAd.show())
                                      .catch(err => {
                                          uni.showToast({
                                            title:'广告加载失败,请刷新本页面或者联系管理员哦~',
                                            icon:'none'
                                          })
                                        console.log('激励视频 广告显示失败')
                                        recordLog({"module":"广告模块","api":"rewardedVideoAd.show()","content":"that.isLoadAd为true,that.rewardedVideoAd.show().catch触发","res":JSON.stringify(err)})
                                         
                                      })
                              })
                            }else{
                                that.rewardedVideoAd.load()
                                    .then(() => that.rewardedVideoAd.show())
                                    .catch(err => {
                                          uni.showToast({
                                            title:'广告加载失败,请刷新本页面或者联系管理员哦~',
                                            icon:'none'
                                          })
                                        recordLog({"module":"广告模块","api":"rewardedVideoAd.show()","content":"that.isLoadAd为true,that.rewardedVideoAd.show().catch触发","res":JSON.stringify(err)})
                                                                     
                                    })
                            }
                        }    
                    },
                    fail(err){
                        console.log(err)
                    }
                })
            }

各位可以看看什么问题吗,我自己开发的两个小程序都时不时有这个问题,不知道是腾讯本身广告API的问题,还是我自己代码的问题,很苦恼

  • 写回答

4条回答 默认 最新

  • 宝码香车 优质创作者: JavaScript技术领域 2025-05-30 20:23
    关注

    让【宝码香车】来帮你解答,本回答参考DeepSeek编写,并整理提供。

    如果还有疑问可以点击头像关注私信或评论。如果答案让您满意,请采纳、点赞、关注,非常感谢!

    微信小程序激励广告加载问题分析

    根据你提供的信息,用户遇到"无合适的广告"(errCode:1004)的问题,这是一个常见的微信小程序广告加载问题。我来分析可能的原因和解决方案。

    问题原因分析

    1. 广告填充率问题:微信广告平台可能没有足够的广告库存匹配特定用户
    2. 用户画像问题:某些用户的特征不符合当前可投放广告的要求
    3. 网络环境问题:用户网络可能触发了广告平台的某些限制
    4. 设备/地区限制:某些设备或地区可能有广告投放限制
    5. 广告单元配置问题:广告单元ID可能在某些情况下无效

    解决方案

    代码优化建议

    // 修改后的initAdver函数
    initAdver() {
        console.log("initAdver")
        var that = this;
        this.isLoadAd = false
        console.log("ad为:", getApp().globalData.adverInfo.rewardedVideoAd)
        
        // 增加广告单元ID验证
        if (!getApp().globalData.adverInfo.rewardedVideoAd) {
            console.error('广告单元ID未配置')
            return
        }
        
        // 创建广告实例
        this.rewardedVideoAd = uni.createRewardedVideoAd({ 
            adUnitId: getApp().globalData.adverInfo.rewardedVideoAd 
        })
        
        // 增加重试机制
        let retryCount = 0
        const maxRetry = 3
        
        const loadAd = () => {
            this.rewardedVideoAd.load().then(() => {
                that.isLoadAd = true
                console.log('广告加载成功')
            }).catch(err => {
                console.log('广告加载失败', err)
                if (retryCount < maxRetry) {
                    retryCount++
                    console.log(`正在第${retryCount}次重试...`)
                    setTimeout(loadAd, 2000) // 2秒后重试
                } else {
                    recordLog({
                        "module": "广告模块",
                        "api": "uni.createRewardedVideoAd",
                        "content": "广告加载失败,达到最大重试次数",
                        "res": JSON.stringify(err)
                    })
                }
            })
        }
        
        this.rewardedVideoAd.onLoad(res => {
            that.isLoadAd = true
            console.log('onLoad event')
            retryCount = 0 // 重置重试计数器
        })
        
        this.rewardedVideoAd.onError((err) => {
            console.log('onError event', err)
            if (err.errCode === 1004) {
                // 针对无广告的情况特殊处理
                uni.showToast({
                    title: '暂无可用广告,请稍后再试',
                    icon: 'none'
                })
            }
            recordLog({
                "module": "广告模块",
                "api": "uni.createRewardedVideoAd",
                "content": "页面调用initAdver,广告加载时候,发生onError",
                "res": JSON.stringify(err)
            })
            
            // 错误时尝试重新加载
            if (retryCount < maxRetry) {
                retryCount++
                setTimeout(loadAd, 2000)
            }
        })
        
        // 初始加载
        loadAd()
        
        this.rewardedVideoAd.onClose((res) => {
            var that = this;
            if (res && res.isEnded) {
                console.log("奖励开始发送")
                this.toReward()
                // 广告关闭后预加载下一条
                loadAd()
            } else {
                console.log('播放中途退出')
            }
        })
    }
    

    其他建议

    1. 增加用户反馈渠道:让用户可以报告广告加载问题,收集更多信息
    2. 监控广告填充率:记录广告请求成功/失败的比例,分析是否是普遍问题
    3. 多样化广告单元:考虑使用多个广告单元ID,当一个失败时尝试另一个
    4. 优雅降级:当广告不可用时提供其他激励方式或内容

    广告加载失败处理流程

    用户点击广告

    总结

    1. 这个问题主要是微信广告平台的填充率问题,不完全是你代码的问题
    2. 建议增加重试机制和更友好的错误提示
    3. 监控广告加载失败率,如果持续很高可能需要联系微信广告团队
    4. 考虑实现备选方案,当广告不可用时提供其他用户体验

    你的代码整体结构是正确的,主要是需要增加更多的错误处理和重试机制。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 6月15日
  • 已采纳回答 6月7日
  • 创建了问题 5月30日