Vue3 接入Google AdSense

vue3引入谷歌广告方法
前言:个人博客 我的博客网站也是通过这样的方法接入,不添加插件

为什么选择谷歌广告

门槛低,作为新生站点,缺的是内容和流量, 国内最大就是百度联盟,但是对这2点都有要求。
相比较之下除了对内容审核是否灰色以外,对流量要求不大

vue3接入

前置条件:已经在Google AdSense 中 添加网站 && 通过审核 && 已经创建广告单元
我这里项目是quasar V2 ssr,主要以项目为主讲解
index.template.html 对标 正常vue项目 index.html

//index.template.html
/* *
在head标签中添加 script
【 **** 】:你的发布商 ID
*/
<script async 
src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=****"
crossorigin="anonymous"
>
</script>
//index.template.html
//在body标签中为window添加函数
<body>
 
  <div id="q-app"></div>

  <script>
    window['addAds'] = function () {
      let chlid = document.getElementsByClassName('adsItem')
      for (let index = 0; index < chlid.length; index++) {
        (adsbygoogle = window.adsbygoogle || []).push({});
      }
    }
  </script>
</body>

组件中应用

onMounted(() => {
  window.addAds();
});
//将广告单元代码添加到盒子中
<template>
  <div class="col adsItem">
      <ins
        class="adsbygoogle"
        style="display: block"
        data-ad-client="**"
        data-ad-slot="**"
        data-ad-format="auto"
        data-full-width-responsive="true"
       ></ins>
   </div>
</template>

注意事项

Google AdSense 中添加域名 ,本地开发时会 广告返回403。只有运行对应谷歌添加域名,才会显示

Google AdSense 广告单元有几种,根据需要添加。效果 个人博客 也是这样的方式添加的

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值