html加上百度统计,vue单页面应用加入百度统计
版權(quán)聲明:本文為CSDN博主「鐘文輝」的原創(chuàng)文章,遵循CC 4.0 by-sa版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/qq_39753974/article/details/80322643
在單頁面中,要是只加在head中的話那是沒用的,因?yàn)橹荒苓\(yùn)行一次。
在百度中復(fù)制代碼
將代碼放在index.html的head中
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "https://hm.baidu.com/hm.js?你的代碼";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
在路由的beforeEach函數(shù)中使用
// router.js
let router= new Router({
mode: 'history',
base: process.env.BASE_URL,
routes: [
{
path: '*',
name: 'error_404',
component: error_404
}
]
});
router.beforeEach((to, from, next) => {
if (to.path) {
_hmt.push(['_trackPageview', '/#' + to.fullPath]);
}
next();
}
檢測是否安裝成功
打開控制臺查看網(wǎng)絡(luò)請求
每次路由變化就會發(fā)出一個叫hm.gif的請求
兩個小時候再進(jìn)網(wǎng)頁就會有統(tǒng)計數(shù)據(jù)
總結(jié)
以上是生活随笔為你收集整理的html加上百度统计,vue单页面应用加入百度统计的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 作家笔下的数学与数学家
- 下一篇: 万张图片,流畅体验——记一次 Vue 列