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

前言

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

流量指标

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

相关推荐
LaughingZhu6 小时前
Product Hunt 每日热榜 | 2026-05-21
前端·人工智能·经验分享·chatgpt·html
怕浪猫6 小时前
Electron 开发实战(一):从零入门核心基础与环境搭建
前端·electron·ai编程
小鹏linux7 小时前
Ubuntu 22.04 部署开源免费具有精美现代web页面的Casdoor账号管理系统
linux·前端·ubuntu·开源·堡垒机
前端若水8 小时前
会话管理:创建、切换、删除对话历史
前端·人工智能·python·react.js
Bigger8 小时前
mini-cc:一个轻量级 AI 编程助手的诞生
前端·ai编程·claude
涵涵(互关)8 小时前
Naive-ui树型选择器只显示根节点
前端·ui·vue
BY组态8 小时前
Ricon组态系统最佳实践:从零开始构建物联网监控平台
前端·物联网·iot·web组态·组态
BY组态8 小时前
Ricon组态系统vs传统组态软件:为什么选择新一代Web组态平台
前端·物联网·iot·web组态·组态
SoaringHeart8 小时前
Flutter进阶:OverlayEntry 插入图层管理器 NOverlayZIndexManager
前端·flutter
放下华子我只抽RuiKe58 小时前
React 从入门到生产(四):自定义 Hook
前端·javascript·人工智能·深度学习·react.js·自然语言处理·前端框架