vue H5项目加入百度统计代码实现
YuanTouo 2020-07-23 vue百度统计功能
首先登录百度统计百度统计官网
百度统计官网:百度统计?
左侧网站列表,在右侧点击新增网站 一次获得xxx(代码里面使用)
// 百度统计
var _hmt = _hmt || [];
window._hmt = _hmt; // 修改为window 全局变量
(function () {
var hm = document.createElement("script");
//xxx 为自己申请
hm.src = "https://hm.baidu.com/hm.js?xxx";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
// 使用路由守卫将接下来将要进入的路由参数
// push进入全局的window _hmt
router.beforeEach((to, from, next) => {
if (window._hmt) {
if (to.path) {
window._hmt.push(['_trackPageview', '/#' + to.fullPath])
}
}
next()
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
等待时间访问,查看百度统计数据变化就OK!!!
切记不能再index.html里面加入一下代码
否则只能统计到首页的访问流量
<script>
var _hmt = _hmt || [];
window._hmt = _hmt; // 修改为window 全局变量
(function () {
var hm = document.createElement("script");
//xxx 为自己申请
hm.src = "https://hm.baidu.com/hm.js?xxx";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script>
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
😍 😍 😍
就很棒!!!