vue單頁面應用加入百度統計


版權聲明:本文為CSDN博主「鍾文輝」的原創文章,遵循CC 4.0 by-sa版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/qq_39753974/article/details/80322643

在單頁面中,要是只加在head中的話那是沒用的,因為只能運行一次。

在百度中復制代碼

將代碼放在index.html的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);
        })();
    </script>

  

在路由的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的請求

兩個小時候再進網頁就會有統計數據

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM