collect.ts 1.72 KB
/*
 * @Author: yangming yangming@didihu.com.cn
 * @Date: 2023-11-28 17:05:34
 * @LastEditors: yangming yangming@didihu.com.cn
 * @LastEditTime: 2023-12-15 16:36:17
 * @FilePath: \glsx-tracker-javascript\src\modules\collect.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import type { DefaultInfoDescriptor, EventType, ReportInfoDescriptor } from '../types/collect'
import utils from '../utils/helper'
import user from './user'
class Collect {
  /**
   * 采集用户信息数据
   * @param  info 接收的数据
   */
  getUserData() {}
  /**
   * 采集默认信息数据
   * @param  info 接收的数据
   */
  getDefaultData(): DefaultInfoDescriptor {
    return {
      $page_title: document.title,
      $page_url: location.href,
      $page_path: location.pathname
    }
  }
  /**
   * 收集点击事件数据
   * 判断需要监听哪些dom元素
   */
  getClickEvent<T extends object>(para: T) {
    this.send(para, 'clickEvent')
  }
  /**
   * 收集浏览事件数据
   */
  getViewEvent<T extends object>(para: T) {
    this.send(para, 'viewEvent')
  }
  /**
   * 收集停留事件数据
   */
  getStayEvent<T extends object>(para: T) {
    this.send(para, 'stayEvent')
  }

  /**
   * 发送埋点数据
   */
  send<T>(para: T, type: EventType) {
    const reportInfo: ReportInfoDescriptor = {
      properties: Object.assign(this.getDefaultData(), para),
      eventName: type,
      anonymous_id: '888',
      send_time: utils.dateFormat(Date.now())
    }
    if (user.user_login_id) {
      reportInfo.user_login_id = user.user_login_id
    }
    console.log(reportInfo)
  }
}
export default new Collect()