0.1.0 • Published 4 years ago

cthun v0.1.0

Weekly downloads
87
License
ISC
Repository
github
Last release
4 years ago

克苏恩(Cthun)

项目说明

该项目是用于前端监控的埋点系统,旨在实现一个支持无埋点,手工埋点,声明埋点为一体的埋点方案,并且提供一定自由扩展能力,可以进行一些定制化埋点需求,后期考虑支持小程序平台,该项目为非商用项目,如使用该项目开发商业项目,本人不对其使用过程中产生的任何问题付责。

安装

npm install cthun --save

引入方法

es moduel引入

import * as Cthun from 'cthun'

commonjs引入

const Cthun = require('cthun')

script引入

<script src='./cthun.min.js' />

使用方法

参数类型说明

interface IMonitorOptions {
  // appId
  appId: string;
  // 是否持久化
  persistence: boolean;
  // 是否压缩
  gzip: boolean; 
  // 收集器
  error?: boolean;
  uncaught?: boolean;
  action?: boolean;
  pv?: boolean;
  performance?: boolean;
}

type TInfos = IMsgInfo | IPerformanceInfo | IEnvInfo | IErrorInfo | IActionInfo | IPvInfo;

基本使用方法

let launcher = new Cthun.MonitorLauncher(IMonitorOptions);
let receptacle =  Cthun.Receptacle.getInstance();
receptacle.push(TInfos)

launcher.start();

声明埋点

<input id="input1" type="text" action-data='{"events":["click","input"]}'/>

进阶用法

自定义上报策略

let launcher = new Cthun.MonitorLauncher(IMonitorOptions);

class CustomStrategy extends Cthun.AbstarctStrategy {
  consume(params: IUploadParams): Promise<any> {
    // TODO: 这里写上报策略消费逻辑
  }
}

launcher.subscribe({
  api: "http://localhost:8080/api",
  // 传AbstarctStrategy子类或者子类的数组,将依次尝试数组中提供的策略
  strategys: [new CustomStrategy({
    breakerOptions: {
      thresholdForOpen: '5/60',
      idleTimeForOpen: 5 * 60,
      thresholdForHalfOpen: '1/60'
    }
  })],
  // 本地化策略,传入对象必须实现localForage相关接口
  store: localforage 
});

自定义收集器

class CustomCollector extends Cthun.AbstractCollector {
  start () {
    // TODO: 这里写收集器开始的逻辑
  }

  stop () {
    // TODO: 这里写收集器结束的结束
  }
}

Cthun.collectors.register("custom", CustomCollector);

收集和发送handler

  // 收集前执行的策略,全局需要添加自定义参数可以在这里实现
  Cthun.hanlders.beforeCollect = function (args) {
    console.log("beforeCollect", args[0]);
    args[0] = {
      ...args[0],
      aa: 123
    }
    return args;
  }

  // 发送前执行的策略(可以添加压缩策略)
  Cthun.hanlders.beforeConsume = function (args) {
    console.log("beforeConsume", args[0], "origin length", args[0].length);
    args[0] = pako.gzip(encodeURIComponent(args[0]), { to: 'string' })
    console.log("beforeConsume", args[0], "gzip length", args[0].length);
    return args;
  }