mp-lens
Version:
微信小程序分析工具 (Unused Code, Dependencies, Visualization)
59 lines • 2.49 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.telemetry = void 0;
const config_1 = require("./config");
const posthog_1 = require("./posthog");
const user_1 = require("./user");
class TelemetryService {
constructor() {
this.enabled = true; // Default to true, will be properly set in init
this.userId = '';
this.initialized = false;
}
init(options) {
this.enabled = (0, config_1.isTelemetryEnabled)(options.telemetry);
if (this.enabled) {
this.userId = (0, user_1.getOrCreateUserId)();
}
this.initialized = true;
}
capture(event) {
if (!this.enabled)
return;
// Prepare the properties for PostHog.
// This should include specific fields from CommandEvent or ErrorEvent,
// and the original event.properties, but NOT event name, userId, or timestamp.
const postHogProperties = {
...(event.properties || {}), // Start with original properties
};
// Add type-specific properties from the event, excluding 'event' and 'properties' keys
// as 'event' is the event name (top-level for PostHog)
// and 'properties' is already spread.
for (const key in event) {
if (key !== 'event' && key !== 'properties') {
postHogProperties[key] = event[key];
}
}
// The PostHog library will add its own timestamp.
// If we need to record the exact client-side generation time for our own analysis
// within properties, we can add it here. The debug output indicates a `timestamp`
// *within* properties, so we will retain that for now, but use the newly generated one.
postHogProperties.timestamp = Date.now();
// The userId is passed as distinctId, but the debug output also shows it in properties.
// We will also retain this for now.
postHogProperties.userId = this.userId;
try {
(0, posthog_1.sendToPostHog)({
event: event.event, // This is the event name like 'command' or 'error'
distinctId: this.userId,
properties: postHogProperties,
});
}
catch (e) {
// Log the error instead of failing silently
console.error('Telemetry capture error:', e);
}
}
}
exports.telemetry = new TelemetryService();
//# sourceMappingURL=service.js.map