feat:添加埋点,createPage 重构
This commit is contained in:
@@ -0,0 +1,155 @@
|
||||
/**
|
||||
* 埋点服务
|
||||
* 提供分享和下载打印事件的上报功能
|
||||
*/
|
||||
|
||||
// 生成唯一 UUID
|
||||
function generateUUID(): string {
|
||||
const timestamp = Date.now();
|
||||
const random = Math.floor(Math.random() * 1000000);
|
||||
return `${timestamp}-${random}`;
|
||||
}
|
||||
|
||||
// 格式化时间为 yyyy-MM-dd HH:mm
|
||||
function formatTime(date: Date = new Date()): string {
|
||||
const year = date.getFullYear();
|
||||
const month = String(date.getMonth() + 1).padStart(2, '0');
|
||||
const day = String(date.getDate()).padStart(2, '0');
|
||||
const hours = String(date.getHours()).padStart(2, '0');
|
||||
const minutes = String(date.getMinutes()).padStart(2, '0');
|
||||
return `${year}-${month}-${day} ${hours}:${minutes}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取今天的日期字符串(yyyy-MM-dd)
|
||||
*/
|
||||
function getTodayString(): string {
|
||||
const date = new Date();
|
||||
const year = date.getFullYear();
|
||||
const month = String(date.getMonth() + 1).padStart(2, '0');
|
||||
const day = String(date.getDate()).padStart(2, '0');
|
||||
return `${year}-${month}-${day}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取事件计数(当天该事件上报的次数)
|
||||
* 使用本地存储持久化,每天重置计数
|
||||
*/
|
||||
function getEventCount(eventName: string): number {
|
||||
try {
|
||||
const storageKey = `tracker_count_${eventName}`;
|
||||
const dateKey = `tracker_date_${eventName}`;
|
||||
const today = getTodayString();
|
||||
|
||||
// 获取上次记录的日期
|
||||
const lastDate = wx.getStorageSync(dateKey);
|
||||
|
||||
let currentCount = 0;
|
||||
// 如果是今天,获取上次的计数;否则重置为0
|
||||
if (lastDate === today) {
|
||||
currentCount = wx.getStorageSync(storageKey) || 0;
|
||||
}
|
||||
|
||||
// 递增计数
|
||||
const newCount = currentCount + 1;
|
||||
|
||||
// 保存计数和日期
|
||||
wx.setStorageSync(storageKey, newCount);
|
||||
wx.setStorageSync(dateKey, today);
|
||||
|
||||
return newCount;
|
||||
} catch (error) {
|
||||
console.error('获取事件计数失败:', error);
|
||||
return 1; // 如果存储失败,返回 1
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 埋点追踪器
|
||||
*/
|
||||
class Tracker {
|
||||
private uuid: string;
|
||||
private openLog: boolean;
|
||||
|
||||
constructor() {
|
||||
// 初始化时获取或生成 UUID(持久化存储)
|
||||
this.uuid = this.getOrCreateUUID();
|
||||
this.openLog = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取或创建 UUID
|
||||
*/
|
||||
private getOrCreateUUID(): string {
|
||||
const uuidKey = 'tracker_uuid';
|
||||
let storedUUID = wx.getStorageSync(uuidKey);
|
||||
if (!storedUUID) {
|
||||
storedUUID = generateUUID();
|
||||
wx.setStorageSync(uuidKey, storedUUID);
|
||||
}
|
||||
return storedUUID;
|
||||
}
|
||||
|
||||
printLog(tip: string, message?: string | object): void {
|
||||
if (this.openLog) {
|
||||
if (message) {
|
||||
if (typeof message === 'object') {
|
||||
console.log(tip, ':', JSON.stringify(message, null, 2));
|
||||
} else {
|
||||
console.log(tip, ':', message as string);
|
||||
}
|
||||
} else {
|
||||
console.log(tip);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 上报分享点击事件
|
||||
* @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,
|
||||
};
|
||||
|
||||
this.printLog('分享事件', params);
|
||||
wx.reportEvent('share_click', params);
|
||||
} catch (error) {
|
||||
console.error('上报分享事件失败:', error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 上报下载打印事件
|
||||
* @param pageName 页面名称
|
||||
*/
|
||||
reportDownload(pageName: string): void {
|
||||
try {
|
||||
const time = formatTime();
|
||||
const count = getEventCount('download');
|
||||
const params = {
|
||||
count,
|
||||
time,
|
||||
uuid: this.uuid,
|
||||
page_name: pageName,
|
||||
};
|
||||
|
||||
this.printLog('下载事件', params);
|
||||
wx.reportEvent('download', params);
|
||||
} catch (error) {
|
||||
console.error('上报下载事件失败:', error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 创建并导出 tracker 实例
|
||||
const tracker = new Tracker();
|
||||
|
||||
export default tracker;
|
||||
Reference in New Issue
Block a user