()
| 16 | } |
| 17 | |
| 18 | async initialize(): Promise<AnalyticsSettings> { |
| 19 | try { |
| 20 | // Try to load from localStorage first |
| 21 | const stored = localStorage.getItem(ANALYTICS_STORAGE_KEY); |
| 22 | if (stored) { |
| 23 | this.settings = JSON.parse(stored); |
| 24 | } else { |
| 25 | // Initialize with default settings |
| 26 | this.settings = { |
| 27 | enabled: true, |
| 28 | hasConsented: true, |
| 29 | }; |
| 30 | } |
| 31 | |
| 32 | // Generate anonymous user ID if not exists |
| 33 | if (this.settings && !this.settings.userId) { |
| 34 | this.settings.userId = this.generateAnonymousId(); |
| 35 | await this.saveSettings(); |
| 36 | } |
| 37 | |
| 38 | // Generate session ID |
| 39 | if (this.settings) { |
| 40 | this.settings.sessionId = this.generateSessionId(); |
| 41 | } |
| 42 | |
| 43 | return this.settings || { |
| 44 | enabled: true, |
| 45 | hasConsented: true, |
| 46 | }; |
| 47 | } catch (error) { |
| 48 | console.error('Failed to initialize consent manager:', error); |
| 49 | // Return default settings on error |
| 50 | return { |
| 51 | enabled: true, |
| 52 | hasConsented: true, |
| 53 | }; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | async grantConsent(): Promise<void> { |
| 58 | if (!this.settings) { |
no test coverage detected