feat:架构代码优化
This commit is contained in:
@@ -3,12 +3,7 @@
|
||||
* 提供分享和下载打印事件的上报功能
|
||||
*/
|
||||
|
||||
// 生成唯一 UUID
|
||||
function generateUUID(): string {
|
||||
const timestamp = Date.now();
|
||||
const random = Math.floor(Math.random() * 1000000);
|
||||
return `${timestamp}-${random}`;
|
||||
}
|
||||
import { getAppUUID } from './uuid';
|
||||
|
||||
// 格式化时间为 yyyy-MM-dd HH:mm
|
||||
function formatTime(date: Date = new Date()): string {
|
||||
@@ -65,29 +60,24 @@ function getEventCount(eventName: string): number {
|
||||
}
|
||||
|
||||
/**
|
||||
* 埋点追踪器
|
||||
* 埋点追踪器(单例模式)
|
||||
*/
|
||||
class Tracker {
|
||||
private uuid: string;
|
||||
private static instance: Tracker | null = null;
|
||||
private openLog: boolean;
|
||||
|
||||
constructor() {
|
||||
// 初始化时获取或生成 UUID(持久化存储)
|
||||
this.uuid = this.getOrCreateUUID();
|
||||
private constructor() {
|
||||
this.openLog = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取或创建 UUID
|
||||
* 获取 Tracker 单例实例
|
||||
*/
|
||||
private getOrCreateUUID(): string {
|
||||
const uuidKey = 'tracker_uuid';
|
||||
let storedUUID = wx.getStorageSync(uuidKey);
|
||||
if (!storedUUID) {
|
||||
storedUUID = generateUUID();
|
||||
wx.setStorageSync(uuidKey, storedUUID);
|
||||
public static getInstance(): Tracker {
|
||||
if (!Tracker.instance) {
|
||||
Tracker.instance = new Tracker();
|
||||
}
|
||||
return storedUUID;
|
||||
return Tracker.instance;
|
||||
}
|
||||
|
||||
printLog(tip: string, message?: string | object): void {
|
||||
@@ -104,20 +94,34 @@ class Tracker {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成公共埋点属性
|
||||
* @param eventName 事件名称
|
||||
* @param pageName 页面名称
|
||||
* @param extraParams 额外的参数对象
|
||||
* @returns 包含公共属性和额外参数的埋点参数对象
|
||||
*/
|
||||
private getCommonEventParams(
|
||||
eventName: string,
|
||||
pageName: string,
|
||||
extraParams?: Record<string, any>,
|
||||
): Record<string, any> {
|
||||
return {
|
||||
count: getEventCount(eventName),
|
||||
date_time: formatTime(),
|
||||
uuid: getAppUUID(),
|
||||
page_name: pageName,
|
||||
...extraParams,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 上报分享点击事件
|
||||
* @param pageName 页面名称
|
||||
*/
|
||||
reportShare(pageName: string): void {
|
||||
try {
|
||||
const time = formatTime();
|
||||
const count = getEventCount('share_click');
|
||||
const params = {
|
||||
count,
|
||||
time,
|
||||
uuid: this.uuid,
|
||||
page_name: pageName,
|
||||
};
|
||||
const params = this.getCommonEventParams('share_click', pageName);
|
||||
|
||||
this.printLog('分享事件', params);
|
||||
wx.reportEvent('share_click', params);
|
||||
@@ -129,17 +133,13 @@ class Tracker {
|
||||
/**
|
||||
* 上报下载打印事件
|
||||
* @param pageName 页面名称
|
||||
* @param mode 模式(可选)
|
||||
*/
|
||||
reportDownload(pageName: string): void {
|
||||
reportDownload(pageName: string, mode?: string): void {
|
||||
try {
|
||||
const time = formatTime();
|
||||
const count = getEventCount('download');
|
||||
const params = {
|
||||
count,
|
||||
time,
|
||||
uuid: this.uuid,
|
||||
page_name: pageName,
|
||||
};
|
||||
const params = this.getCommonEventParams('download', pageName, {
|
||||
...(mode && { mode }),
|
||||
});
|
||||
|
||||
this.printLog('下载事件', params);
|
||||
wx.reportEvent('download', params);
|
||||
@@ -149,7 +149,7 @@ class Tracker {
|
||||
}
|
||||
}
|
||||
|
||||
// 创建并导出 tracker 实例
|
||||
const tracker = new Tracker();
|
||||
// 导出 Tracker 单例实例
|
||||
const tracker = Tracker.getInstance();
|
||||
|
||||
export default tracker;
|
||||
|
||||
Reference in New Issue
Block a user