@@ -2,6 +2,8 @@ import ky from 'ky'
22import localforage from 'localforage'
33import posthog from 'posthog-js'
44
5+ const cockpitTelemetryEnabledKey = 'cockpit-enable-usage-statistics-telemetry'
6+
57type EventPayload = {
68 /**
79 * The name of the event
@@ -21,6 +23,7 @@ type EventPayload = {
2123 *
2224 */
2325class EventTracker {
26+ private static enableEventTracking = false
2427 static postHogApiUrl = 'https://us.i.posthog.com'
2528 static postHogApiKey = 'phc_SfqVeZcpYHmhUn9NRizThxFxiI9fKqvjRjmBDB8ToRs'
2629 static posthog : ReturnType < typeof posthog . init > | undefined = undefined
@@ -30,6 +33,16 @@ class EventTracker {
3033 * Initialize the event tracking system
3134 */
3235 constructor ( ) {
36+ // Only track usage statistics if the user has not opted out and the app is not in development mode
37+ const isRunningInProduction = import . meta. env . PROD
38+ const userHasExternalTelemetryEnabled = window . localStorage . getItem ( cockpitTelemetryEnabledKey ) === 'true'
39+ EventTracker . enableEventTracking = isRunningInProduction && userHasExternalTelemetryEnabled
40+
41+ if ( ! EventTracker . enableEventTracking ) {
42+ console . info ( 'Event tracking is disabled. Not initializing event tracker.' )
43+ return
44+ }
45+
3346 if ( ! EventTracker . posthog ) {
3447 EventTracker . posthog = posthog . init ( EventTracker . postHogApiKey , {
3548 api_host : EventTracker . postHogApiUrl ,
@@ -56,6 +69,8 @@ class EventTracker {
5669 * @param {Record<string, unknown> } eventProperties - The properties of the event
5770 */
5871 async capture ( eventName : string , eventProperties ?: Record < string , unknown > ) : Promise < void > {
72+ if ( ! EventTracker . enableEventTracking ) return
73+
5974 const eventId = `${ eventName } -${ Date . now ( ) } `
6075 const eventPayload : EventPayload = {
6176 eventName,
0 commit comments