From 10d01888665963831ce4d0343ee6d78a45ab0372 Mon Sep 17 00:00:00 2001
From: Juraj Majerik <juro.majerik@gmail.com>
Date: Tue, 5 Dec 2023 17:35:36 +0100
Subject: [PATCH] chore(feature flag copy): add event tracking (#19092)

---
 frontend/src/lib/utils/eventUsageLogic.ts             | 8 ++++++++
 frontend/src/scenes/feature-flags/featureFlagLogic.ts | 7 ++++---
 2 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/frontend/src/lib/utils/eventUsageLogic.ts b/frontend/src/lib/utils/eventUsageLogic.ts
index 03a177c28908e..15401da60b193 100644
--- a/frontend/src/lib/utils/eventUsageLogic.ts
+++ b/frontend/src/lib/utils/eventUsageLogic.ts
@@ -443,6 +443,8 @@ export const eventUsageLogic = kea<eventUsageLogicType>([
         reportAutocaptureToggled: (autocapture_opt_out: boolean) => ({ autocapture_opt_out }),
         reportAutocaptureExceptionsToggled: (autocapture_opt_in: boolean) => ({ autocapture_opt_in }),
         reportFailedToCreateFeatureFlagWithCohort: (code: string, detail: string) => ({ code, detail }),
+        reportFeatureFlagCopySuccess: true,
+        reportFeatureFlagCopyFailure: (error) => ({ error }),
         reportInviteMembersButtonClicked: true,
         reportDashboardLoadingTime: (loadingMilliseconds: number, dashboardId: number) => ({
             loadingMilliseconds,
@@ -1048,6 +1050,12 @@ export const eventUsageLogic = kea<eventUsageLogicType>([
         reportFailedToCreateFeatureFlagWithCohort: ({ detail, code }) => {
             posthog.capture('failed to create feature flag with cohort', { detail, code })
         },
+        reportFeatureFlagCopySuccess: () => {
+            posthog.capture('feature flag copied')
+        },
+        reportFeatureFlagCopyFailure: ({ error }) => {
+            posthog.capture('feature flag copy failure', { error })
+        },
         reportInviteMembersButtonClicked: () => {
             posthog.capture('invite members button clicked')
         },
diff --git a/frontend/src/scenes/feature-flags/featureFlagLogic.ts b/frontend/src/scenes/feature-flags/featureFlagLogic.ts
index 742eff1b44cb8..c7542d156b1bd 100644
--- a/frontend/src/scenes/feature-flags/featureFlagLogic.ts
+++ b/frontend/src/scenes/feature-flags/featureFlagLogic.ts
@@ -795,10 +795,11 @@ export const featureFlagLogic = kea<featureFlagLogicType>([
                     ? 'updated'
                     : 'copied'
                 lemonToast.success(`Feature flag ${operation} successfully!`)
+                eventUsageLogic.actions.reportFeatureFlagCopySuccess()
             } else {
-                lemonToast.error(
-                    `Error while saving feature flag: ${JSON.stringify(featureFlagCopy?.failed) || featureFlagCopy}`
-                )
+                const errorMessage = JSON.stringify(featureFlagCopy?.failed) || featureFlagCopy
+                lemonToast.error(`Error while saving feature flag: ${errorMessage}`)
+                eventUsageLogic.actions.reportFeatureFlagCopyFailure(errorMessage)
             }
 
             actions.loadProjectsWithCurrentFlag()