前端也可以通过设置“监控”观察用户,在遇到一些无法复现、奇奇怪怪的问题,或者单个用户所在的环境与大部分用户的环境不一样时产生的问题,例如 该用户对浏览器设置了不允许cookie 等;这些问题通过前端监控记录下来,让开发者模拟出问题环境、操作步骤,能更好的解决问题。

进入正题


Vue2:

需要使用到Vuex,因为要将记录的数据存放到Vuex中
store.js:

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    monitoring:[],
  },
  mutations: {
    setMonitoring(state,data){
      state.monitoring.push({
        ...data,
        nowPage:window.location.href,
        steps:state.monitoring.length+1,
        time:`${new Date().getFullYear()}-${new Date().getMonth()+1}-${new Date().getDate()}-${new Date().getHours()}:${new Date().getMinutes()}:${new Date().getSeconds()}`
      })
    }
  },
  actions: {
  },
  modules: {
  }
})

在src目录下新建monitoring文件夹(名字可以自定义),monitoring文件夹下创建一个index.js文件

在Vue中搭建前端监控日志-LMLPHP
monitoring中的index.js

import store from "@/store";//引入Vuex的store
//获取当前浏览器的Storage大小
function get_cache_size(t){
    t = t == undefined ? "l" : t;
    var obj = "";
    if(t==='l'){
        if(!window.localStorage) {
            return "当前不支持localStorage存储"
        }else{
            obj = window.localStorage;
        }
    }else{
        if(!window.sessionStorage) {
            return "当前不支持sessionStorage存储"
        }else{
            obj = window.sessionStorage;
        }
    }
    var size = 0;
    if(obj!==""){
        for(let item in obj) {
            if(obj.hasOwnProperty(item)) {
                size += obj.getItem(item).length;
            }
        }
    }
    return size
}
//存储当前Storage大小
store.commit("setMonitoring",{
    title:`当前Location大小:${get_cache_size('l')};当前Session大小:${get_cache_size('s')}`
})
store.commit("setMonitoring",{
    title:`浏览器cookie设置:${navigator.cookieEnabled==true?'开启':'禁用'};当前Cookie:${document.cookie || "暂无"}`
})
//监听页面隐藏或者打开
window.addEventListener("visibilitychange",()=>{
    if(document.hidden){
        store.commit("setMonitoring",{
            title:"离开/隐藏页面"
        })
    }else{
        store.commit("setMonitoring",{
            title:"进入页面"
        })
    }
})
//监听鼠标点击事件
window.addEventListener('mouseup',(event)=>{
    store.commit("setMonitoring",{
        title:"用户点击",
        Element:event.path[0].outerHTML
    })
})
//监听报错信息
window.addEventListener("error",(err)=>{
    store.commit("setMonitoring",{
        title:"报错",
        errorMsg:err.message
    })
})
//在页面刷新、关闭前发送记录的数据,fetch能保证浏览器结束进程前发送请求
window.addEventListener('beforeunload',()=>{
    let arr=JSON.stringify(store.state.monitoring);
    fetch('http://192.168.10.170:8081/sendData', {
      method: 'POST',
      headers:{
        'Accept': 'application/json',
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      params:arr,
      keepalive: true
    });
  })

main.js:

import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import "@/monitoring";//引入文件

Vue.config.productionTip = false

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')

router/index.js

import Vue from 'vue'
import VueRouter from 'vue-router'
import store from "@/store";

Vue.use(VueRouter)

const routes = [
  {
    path:"/",
    redirect:"/test"
  }
]

const router = new VueRouter({
  routes
})

router.afterEach((to,from)=>{
  store.commit('setMonitoring',{
    title:"切换页面",
    formPage:from.path,
    toPage:to.path,
  })
})

export default router

在axios拦截器中记录

import axios from "axios"
import store from "@/store"

axios.interceptors.request.use((config)=>{
    store.commit("setMonitoring",{
        title:"发请求",
        data:JSON.stringify(config)
    })
    return config
})

axios.interceptors.response.use((config)=>{
    store.commit("setMonitoring",{
        title:"接收请求",
        data:JSON.stringify(config)
    })
    return config
})

最后看看效果:
点击TEST文本会发送请求
在Vue中搭建前端监控日志-LMLPHP
在Vue中搭建前端监控日志-LMLPHP

09-06 11:47