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

前言

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

流量指标

这里参考我们之前的文章前端应用监控前端部分方案设计,在这里面我们详细介绍了我们的流量是怎么计算的,我们这里主要介绍我们是如何实现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的采集,便于统计我们系统的流量。

相关推荐
jinxinyuuuus几秒前
vsGPU:硬件参数的数据仓库设计、ETL流程与前端OLAP分析
前端·数据仓库·etl
Xの哲學14 分钟前
Linux NAT 深度剖析: 从设计哲学到实现细节
linux·服务器·网络·架构·边缘计算
Allen正心正念202518 分钟前
AWS专家Greg Coquillo提出的8层Agentic AI架构分析
人工智能·架构·aws
小信丶30 分钟前
解决 pnpm dev 报错:系统禁止运行脚本的问题
前端·vue.js·windows·npm
૮・ﻌ・37 分钟前
Vue3:组合式API、Vue3.3新特性、Pinia
前端·javascript·vue3
前端不太难37 分钟前
RN + TypeScript 项目越写越乱?如何规范架构?
前端·javascript·typescript
神算大模型APi--天枢64638 分钟前
全栈自主可控:国产算力平台重塑大模型后端开发与部署生态
大数据·前端·人工智能·架构·硬件架构
苏打水com38 分钟前
第十五篇:Day43-45 前端性能优化进阶——从“可用”到“极致”(对标职场“高并发场景优化”需求)
前端·css·vue·html·js
@大迁世界1 小时前
08.CSS if() 函数
前端·css
Moment1 小时前
小米不仅造车,还造模型?309B参数全开源,深度思考完胜DeepSeek 🐒🐒🐒
前端·人工智能·后端