vuepress v2 增加百度统计
2022年1月11日
vuepress v2 增加百度统计
添加百度统计
.vuepress/config.js
head: [
// 添加百度统计
[
"script",
{},
`
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);
})();
`
]
]
切换页面时手工上报 pv 统计
创建 .vuepress/clientAppEnhance.ts 文件:
import { defineClientAppEnhance } from '@vuepress/client'
export default defineClientAppEnhance(({ app, router, siteData }) => {
/**
* 路由切换事件处理
*/
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();
});