html加上百度统计,vue单页面应用加入百度统计
版權聲明:本文為CSDN博主「鐘文輝」的原創文章,遵循CC 4.0 by-sa版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/qq_39753974/article/details/80322643
在單頁面中,要是只加在head中的話那是沒用的,因為只能運行一次。
在百度中復制代碼
將代碼放在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函數中使用
// 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();
}
檢測是否安裝成功
打開控制臺查看網絡請求
每次路由變化就會發出一個叫hm.gif的請求
兩個小時候再進網頁就會有統計數據
總結
以上是生活随笔為你收集整理的html加上百度统计,vue单页面应用加入百度统计的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 作家笔下的数学与数学家
- 下一篇: java dumpheap_java程序