"use strict"; const common_vendor = require("../common/vendor.js"); class PostHogTracker { constructor(config) { this.apiHost = config.apiHost || "https://us.i.posthog.com"; this.apiKey = config.apiKey; this.distinctId = null; } // 初始化并获取用户唯一标识 init() { const storedId = common_vendor.index.getStorageSync("posthog_distinct_id"); if (storedId) { this.distinctId = storedId; } else { this.distinctId = this._generateUUID(); common_vendor.index.setStorageSync("posthog_distinct_id", this.distinctId); } return this; } // 设置用户 ID(登录后调用) identify(userId, userProperties = {}) { this.distinctId = userId; common_vendor.index.setStorageSync("posthog_distinct_id", userId); this._capture("$identify", { $set: userProperties }); } // 捕获事件 capture(eventName, properties = {}) { this._capture(eventName, properties); } // 页面浏览事件 pageView(pagePath, pageTitle) { this._capture("$pageview", { $current_url: pagePath, $title: pageTitle }); } // 内部发送方法 _capture(event, properties = {}) { const payload = { api_key: this.apiKey, event, distinct_id: this.distinctId, timestamp: (/* @__PURE__ */ new Date()).toISOString(), properties: { ...properties, $lib: "uniapp-miniprogram", $lib_version: "1.0.0", ...this._getDefaultProperties() } }; common_vendor.index.request({ url: `${this.apiHost}/i/v0/e/`, method: "POST", header: { "Content-Type": "application/json" }, data: payload, success: (res) => { common_vendor.index.__f__("log", "at utils/posthog.js:71", "PostHog event captured:", event); }, fail: (err) => { common_vendor.index.__f__("error", "at utils/posthog.js:74", "PostHog capture failed:", err); this._saveFailedEvent(payload); } }); } // 批量发送事件(性能优化) captureBatch(events) { const payload = events.map((e) => ({ api_key: this.apiKey, event: e.event, distinct_id: this.distinctId, timestamp: e.timestamp || (/* @__PURE__ */ new Date()).toISOString(), properties: { ...e.properties, ...this._getDefaultProperties() } })); common_vendor.index.request({ url: `${this.apiHost}/batch/`, method: "POST", header: { "Content-Type": "application/json" }, data: payload, success: (res) => { common_vendor.index.__f__("log", "at utils/posthog.js:102", "PostHog batch captured:", events.length, "events"); } }); } // 获取默认属性 _getDefaultProperties() { const systemInfo = common_vendor.index.getSystemInfoSync(); return { $os: systemInfo.platform, $os_version: systemInfo.system, $device: systemInfo.model, $screen_height: systemInfo.screenHeight, $screen_width: systemInfo.screenWidth, mp_platform: "uniapp" }; } // 生成 UUID _generateUUID() { return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => { const r = Math.random() * 16 | 0; const v = c === "x" ? r : r & 3 | 8; return v.toString(16); }); } // 保存失败事件(离线支持) _saveFailedEvent(payload) { const failedEvents = common_vendor.index.getStorageSync("posthog_failed_events") || []; failedEvents.push(payload); common_vendor.index.setStorageSync("posthog_failed_events", failedEvents); } // 重试发送失败的事件 retryFailedEvents() { const failedEvents = common_vendor.index.getStorageSync("posthog_failed_events") || []; if (failedEvents.length > 0) { this.captureBatch(failedEvents); common_vendor.index.removeStorageSync("posthog_failed_events"); } } } const posthog = new PostHogTracker({ apiHost: "https://us.i.posthog.com", // 或 eu.i.posthog.com apiKey: "YOUR_PROJECT_API_KEY" // 在 PostHog 项目设置中获取 }).init(); exports.posthog = posthog; //# sourceMappingURL=../../.sourcemap/mp-weixin/utils/posthog.js.map