From 595c5d8a0f17069d85309396acc7ecb573fd6521 Mon Sep 17 00:00:00 2001 From: Rafael Araujo Lehmkuhl Date: Fri, 22 Nov 2024 13:26:42 -0300 Subject: [PATCH] event-tracking: Disable if not in production or user opted-out --- src/libs/external-telemetry/event-tracking.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/libs/external-telemetry/event-tracking.ts b/src/libs/external-telemetry/event-tracking.ts index 9ef30ecd8..884081496 100644 --- a/src/libs/external-telemetry/event-tracking.ts +++ b/src/libs/external-telemetry/event-tracking.ts @@ -2,6 +2,8 @@ import ky from 'ky' import localforage from 'localforage' import posthog from 'posthog-js' +const cockpitTelemetryEnabledKey = 'cockpit-enable-usage-statistics-telemetry' + type EventPayload = { /** * The name of the event @@ -21,6 +23,7 @@ type EventPayload = { * */ class EventTracker { + private static enableEventTracking = false static postHogApiUrl = 'https://us.i.posthog.com' static postHogApiKey = 'phc_SfqVeZcpYHmhUn9NRizThxFxiI9fKqvjRjmBDB8ToRs' static posthog: ReturnType | undefined = undefined @@ -30,6 +33,16 @@ class EventTracker { * Initialize the event tracking system */ constructor() { + // Only track usage statistics if the user has not opted out and the app is not in development mode + const isRunningInProduction = import.meta.env.PROD + const userHasExternalTelemetryEnabled = window.localStorage.getItem(cockpitTelemetryEnabledKey) === 'true' + EventTracker.enableEventTracking = isRunningInProduction && userHasExternalTelemetryEnabled + + if (!EventTracker.enableEventTracking) { + console.info('Event tracking is disabled. Not initializing event tracker.') + return + } + if (!EventTracker.posthog) { EventTracker.posthog = posthog.init(EventTracker.postHogApiKey, { api_host: EventTracker.postHogApiUrl, @@ -56,6 +69,8 @@ class EventTracker { * @param {Record} eventProperties - The properties of the event */ async capture(eventName: string, eventProperties?: Record): Promise { + if (!EventTracker.enableEventTracking) return + const eventId = `${eventName}-${Date.now()}` const eventPayload: EventPayload = { eventName,