开始第一想法就是放在index.html都可以调用,回头一想不对,聪明反被聪明误,vue不会页面刷新了,也就是你的加载百度统计代码也就那一次,统计也就一次,很显然不对
下面是百度给的代码
var
_hmt = _hmt || [];
(
function
() {
//每次执行前,先移除上次插入的代码
document.getElementById(
'baidu_tj'
) && document.getElementById(
'baidu_tj'
).remove();
var
hm = document.createElement(
"script"
);
hm.src =
"https://hm.baidu.com/hm.js?xxxx"
;
hm.id =
"baidu_tj"
var
s = document.getElementsByTagName(
"script"
)[0];
s.parentNode.insertBefore(hm, s);
})();
vue路由的时候每次都要经过
router.beforeEach((to,from ,next)=> {
})
,进入页面每次调用就可以,对思路没错,那代码写好,对了吗?
最后发现统计的还是一个页面的,,,
哪里出问题了,
对,你需要给百度一个说我统计的是这个链接的,代码如下
if (to.path) {
_hmt.push(['_trackPageview', '/#' + to.fullPath]);
}
还有一点不要着急,百度的统计貌似刷新比较慢,测试的时候看看里面有没统计到
完整代码如下
router.beforeEach((to,from ,next)=> {
//处理统计
if(window.location.host.indexOf("lianghui") != -1){
document.getElementById('baidu_tj') && document.getElementById('baidu_tj').remove();
var hm = document.createElement("script");
hm.src = "https://hm.baidu.com/hm.js?你的id";
var s = document.getElementsByTagName("script")[0];
hm.id = "baidu_tj"
s.parentNode.insertBefore(hm, s);
if (to.path) {
_hmt.push(['_trackPageview', '/#' + to.fullPath]);
}
}
next()
})
还有一个重要的一个
var _hmt = _hmt || [];
这个我是写在index.html文件里了
好了就到这里吧,下班了。。。。