UNPKG

native-update

Version:

Foundation package for building a comprehensive update system for Capacitor apps. Provides architecture and interfaces but requires backend implementation.

137 lines 3.86 kB
import { Logger } from './logger'; export class Analytics { constructor() { this.providers = []; this.properties = {}; this.logger = Logger.getInstance(); } static getInstance() { if (!Analytics.instance) { Analytics.instance = new Analytics(); } return Analytics.instance; } /** * Add an analytics provider */ addProvider(provider) { this.providers.push(provider); // Set existing user ID and properties if (this.userId) { provider.setUserId(this.userId); } if (Object.keys(this.properties).length > 0) { provider.setProperties(this.properties); } } /** * Track an update event */ async trackEvent(event) { this.logger.debug('Analytics event', event); const promises = this.providers.map((provider) => provider .trackEvent(event) .catch((error) => this.logger.error('Analytics provider error', error))); await Promise.all(promises); } /** * Track an error */ async trackError(error, context) { this.logger.error('Analytics error', { error, context }); const promises = this.providers.map((provider) => provider .trackError(error, context) .catch((err) => this.logger.error('Analytics provider error', err))); await Promise.all(promises); } /** * Set user ID for analytics */ setUserId(userId) { this.userId = userId; this.providers.forEach((provider) => provider.setUserId(userId)); } /** * Set global properties */ setProperties(properties) { this.properties = Object.assign(Object.assign({}, this.properties), properties); this.providers.forEach((provider) => provider.setProperties(this.properties)); } /** * Track update check */ async trackUpdateCheck(available, version) { const startTime = Date.now(); await this.trackEvent({ type: 'check', version, success: true, metadata: { updateAvailable: available }, duration: Date.now() - startTime, }); } /** * Track download progress */ async trackDownload(version, success, error) { await this.trackEvent({ type: 'download', version, success, error, }); } /** * Track installation */ async trackInstall(version, success, error) { await this.trackEvent({ type: 'install', version, success, error, }); } /** * Track rollback */ async trackRollback(fromVersion, toVersion, reason) { await this.trackEvent({ type: 'rollback', success: true, metadata: { fromVersion, toVersion, reason, }, }); } } /** * Console analytics provider for development */ export class ConsoleAnalyticsProvider { constructor() { this.properties = {}; } async trackEvent(event) { console.log('[Analytics]', event.type, Object.assign(Object.assign({}, event), { userId: this.userId, properties: this.properties })); } async trackError(error, context) { console.error('[Analytics Error]', { error: error.message, stack: error.stack, context, userId: this.userId, properties: this.properties, }); } setUserId(userId) { this.userId = userId; } setProperties(properties) { this.properties = Object.assign(Object.assign({}, this.properties), properties); } } //# sourceMappingURL=analytics.js.map