美文网首页
vue-router 百度监测与终端跳转

vue-router 百度监测与终端跳转

作者: squidbrother | 来源:发表于2020-07-20 16:18 被阅读0次
概述

使用百度监测,发布打包后
将监测代码,放入</head>内

<script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?5***********************2ewq3s2q";
      var s = document.getElementsByTagName("script")[0]; 
      s.parentNode.insertBefore(hm, s);
    })();
</script>
终端判断
function myCheckIsPcFn(){
  var sUserAgent = navigator.userAgent.toLowerCase();
  var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
  var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
  var bIsMidp = sUserAgent.match(/midp/i) == "midp";
  var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
  var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
  var bIsAndroid = sUserAgent.match(/android/i) == "android";
  var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
  var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";
  if (!(bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) ){
      return true
  }else{
      return false;
  }
}
代码
//路由守卫
router.beforeEach(function(to,from,next){
  //判断终端
  function rStr(){
    return Math.random().toString().substring(2,10);
  }
  if(to.path){
    // _hmt.push(['_trackPageview', '/#' + to.fullPath]);
    if(typeof _hmt == 'undefined'){
      console.log('没有监测库');
    }else{
      _hmt.push(['_trackPageview', '/#' + to.fullPath]);
      console.log('存在监测库');
    };
  }
  //判断终端跳转
  if(!myCheckIsPcFn()){
    next();
  }else{
    window.location.href = "https://www.xxx.com/pc/index.html?timestamp="+rStr();
  };
})

相关文章

网友评论

      本文标题:vue-router 百度监测与终端跳转

      本文链接:https://www.haomeiwen.com/subject/qxrykktx.html