IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    Nuxt 项目中如何引入百度统计?

    子舒的博客发表于 2022-01-17 00:00:00
    love 0
    在部署一个网站之后,我们最关心的事情无异于是访问量,以及对它的分析。国内的百度统计是一个不错的选择,基本的功能都是免费的。 只需要在 head 中引入一串 javascript 代码即可。 <!-- <script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?xxxxxxxxxxxxxxxxxxx"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> --> 上面是百度提供的统计代码,需要放在 <head></head>中。 但是在 nuxt 中,没有传统的 <head></head> 。所以要对他进行一些处理。 1. 首先在根目录下 /plugins 新建一个文件 baidu.js // /plugins/baidu.js export default ({app: {router}, store}) => { /* 每次路由变更时进行 pv 统计 */ router.afterEach((to, from) => { /* 告诉增加一个 PV */ try { window._hmt = window._hmt || [] window._hmt.push(['_trackPageview', to.fullPath]) } catch (e) { } }) } 2. 配置 nuxt.config.js 文件 在 plugins中: plugins: [ { src: '~/plugins/baidu' } ], 在 head中: head: { // ... link: [ // ... ], script: [ { src: 'https://hm.baidu.com/hm.js?xxxxxxxxxxxxxxxxxxx' } ] }, 在 script 中写入百度统计提供的 url 即可,按照对应的字符。


沪ICP备19023445号-2号
友情链接