打造自己的前端监控---前端流量监控

前言

我们前面已经对前端错误、前端接口实现了监控采集,今天我们来实现前端流量监控。

流量指标

这里参考我们之前的文章前端应用监控前端部分方案设计,在这里面我们详细介绍了我们的流量是怎么计算的,我们这里主要介绍我们是如何实现PV的,也就是一个系统的页面的访问量采集和统计。

具体实现

  • 监听pushState、replaceState、hashchange

  • 自定义事件

typescript 复制代码
/**
 * @description 用来监听页面变化
 * @author 
 */
export class PageObserver {
  private bt1 = null;
  private bt2 = null;
  private bt3 = null;

  constructor() {
    history.pushState = this.replaceAndPush('pushState');
    history.replaceState = this.replaceAndPush('replaceState');
  }

  private replaceAndPush(type: string) {
    let orig = history[type];
    return function () {
      let rv = orig.apply(this, arguments);
      let e;
      if (!!_global.ActiveXObject || 'ActiveXObject' in window) {
        e = document.createEvent(type.toLocaleLowerCase());
      } else {
        e = new Event(type.toLocaleLowerCase());
      }
      e['arguments'] = arguments;
      _global.dispatchEvent(e);
      return rv;
    }
  }

  public pageChangeObserve(options) {
    on(_global, EVENTTYPES.POPSTATE, event => {
      this.bt1 && clearTimeout(this.bt1);
      this.bt1 = setTimeout(() => {
        PageObserver.popStateHandle(event, options)
      }, 300);
    }, true);

    on(_global, EVENTTYPES.PUSHSTATE, event => {
      this.bt2 && clearTimeout(this.bt2);
      this.bt2 = setTimeout(() => {
        PageObserver.pushStateHandle(event, options)
      }, 300);
    }, true);

    on(_global, EVENTTYPES.LOAD, event => {
      this.bt3 && clearTimeout(this.bt3);
      let time: string | number;
      if (performance.timing && performance.timing.domLoading && performance.timing.domComplete) {
        time = performance.timing.domComplete - performance.timing.domLoading;
      } else {
        time = 1000;
      }

      this.bt3 = setTimeout(() => {
        PageObserver.loadHandle(event, options);
      }, time);
    }, true);
    on(document, EVENTTYPES.VISIBILITYCHAGE, () => {
      PageObserver.visibilityHandle(options);
    }, true);
  }

  private static pushStateHandle(event: Event, opts: IParams) {
    creatPageLog(event, opts);
  }

  private static popStateHandle(event: Event, opts: IParams) {
    creatPageLog(event, opts);
  }

  private static loadHandle(event: Event, opts: IParams) {
    creatPageLog(event, opts);
  }

  private static visibilityHandle(opts: IParams) {
    if (document.visibilityState !== 'visible') {
      let apiArr = getLocalStorage(ARRAYNAME.APIARR) || '[]';
      let logArr = getLocalStorage(ARRAYNAME.LOGARR) || '[]';
      let pageArr = getLocalStorage(ARRAYNAME.PAGEARR) || '[]';
      if (JSON.parse(apiArr).length > 0) {
        let obj = [...JSON.parse(apiArr)];
        apiInfoReport(opts.apiContextReportUrl, {
          apiContexts: obj,
          sendTime: getTimeStamp()
        },opts)
        setLocalStorage(ARRAYNAME.APIARR, '[]');
      }
      if (JSON.parse(logArr).length > 0) {
        let obj = [...JSON.parse(logArr)];
        apiInfoReport(opts.logContextReportUrl, {
          logContexts: obj,
          sendTime: getTimeStamp()
        }, opts)
        setLocalStorage(ARRAYNAME.LOGARR, '[]');
      }
      if (JSON.parse(pageArr).length > 0) {
        let obj = [...JSON.parse(pageArr)];
        apiInfoReport(opts.pageContextReportUrl, {
          pageContexts: obj,
          sendTime: getTimeStamp()
        }, opts)
        setLocalStorage(ARRAYNAME.PAGEARR, '[]');
      }

    }
  }
}

总结

经过测试不管是hash模式还是history模式以及reload我们都能够正确采集页面地址信息,结合我们之前的sessionId和唯一的userId,实现我们对我们网站的PV的采集,便于统计我们系统的流量。

相关推荐
jingling5559 分钟前
Git 常用命令指南:从入门到高效开发
前端·javascript·git·前端框架
索西引擎11 分钟前
【前端】网站favicon图标制作
前端
程序员海军17 分钟前
告别低质量Prompt!:字节跳动PromptPilot深度测评
前端·后端·aigc
华洛19 分钟前
关于可以控制大模型提升任意产品的排名这件事📈
前端·github·产品经理
Yanc20 分钟前
翻了vue源码 终于解决了这个在SFC中使用tsx的bug
前端·vue.js
nujnewnehc24 分钟前
失业落伍前端, 尝试了一个月 ai 协助编程的真实感受
前端·ai编程·github copilot
大熊学员26 分钟前
HTML 媒体元素概述
前端·html·媒体
好好好明天会更好29 分钟前
那些关于$event在vue中不得不说的事
前端·vue.js
默默地离开37 分钟前
CSS定位全解析:从static到sticky的5种position属性详解(第五回)
前端·css
JosieBook40 分钟前
【web应用】前后端分离项目基本框架组成:Vue + Spring Boot 最佳实践指南
前端·vue.js·spring boot