给博客添加百度统计

更新时间: 2021-06-22 19:35:38

emmmm。。。这个也是别的小朋友都有的功能。。我。。也要

  1. 首先注册百度统计

  2. 复制一下代码(不做过多解释,反正百度统计上有傻瓜式教程)

  3. .vuepress/confog.js文件加上header:

const sidebar = require("./utils/getSidebarData")

module.exports = {
    head: [
        // 添加百度统计
        [
            "script",
            {},
            `var _hmt = _hmt || [];
            (function() {
            var hm = document.createElement("script");
            hm.src = "https://hm.baidu.com/hm.js?6371333d39195dbe958ef14c0a722ac4";
            var s = document.getElementsByTagName("script")[0]; 
            s.parentNode.insertBefore(hm, s);
            })();`
        ]
      ],
    title: '刀刀的知识积累',
    。。。。
    。。。。
    //.....
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

4.因为我们是单页面的跳转页面的时候不会刷新页面,所以要在跳转页面的时候主动推送一下:

// .vuepress/enhanceApp.js

export default ({ router }) => {
    /**
     * 路由切换事件处理
     */
    router.beforeEach((to, from, next) => {
      console.log("切换路由", to.fullPath, from.fullPath);
  
      //触发百度的pv统计
      if (typeof _hmt != "undefined") {
        if (to.path) {
          _hmt.push(["_trackPageview", to.fullPath]);
          console.log("上报百度统计", to.fullPath);
        }
      }
  
      // continue
      next();
    });
  };
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

好了好了~到这就可以自己去百度统计检查了,失败的肯定是你代码都复制错了