Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reenable metrics prompt on fresh install #2608

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 50 additions & 5 deletions pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import (

"github.com/ava-labs/avalanche-cli/pkg/application"
"github.com/ava-labs/avalanche-cli/pkg/constants"

"github.com/ava-labs/avalanche-cli/pkg/utils"
"github.com/ava-labs/avalanche-cli/pkg/ux"
"github.com/ava-labs/avalanchego/utils/logging"

"github.com/posthog/posthog-go"
"github.com/spf13/cobra"
Expand All @@ -40,14 +41,21 @@ func GetCLIVersion() string {
return string(content)
}

func userDoesNotOpted(app *application.Avalanche) bool {
return !app.Conf.ConfigFileExists() || !app.Conf.ConfigValueIsSet(constants.ConfigMetricsEnabledKey)
}

func userIsOptedIn(app *application.Avalanche) bool {
if app.Conf.ConfigFileExists() && app.Conf.GetConfigBoolValue(constants.ConfigMetricsEnabledKey) {
return true
}
return false
return app.Conf.ConfigFileExists() && app.Conf.GetConfigBoolValue(constants.ConfigMetricsEnabledKey)
}

func HandleTracking(cmd *cobra.Command, commandPath string, app *application.Avalanche, flags map[string]string) {
if userDoesNotOpted(app) {
if err := handleUserMetricsPreference(app); err != nil {
ux.Logger.PrintToUser(logging.Red.Wrap("failure setting metrics preference: %s"), err)
return
}
}
if !userIsOptedIn(app) {
return
}
Expand Down Expand Up @@ -105,3 +113,40 @@ func trackMetrics(app *application.Avalanche, commandPath string, flags map[stri
Properties: telemetryProperties,
})
}

func saveMetricsConfig(app *application.Avalanche, metricsEnabled bool) error {
return app.Conf.SetConfigValue(constants.ConfigMetricsEnabledKey, metricsEnabled)
}

func handleUserMetricsPreference(app *application.Avalanche) error {
if utils.IsE2E() {
return saveMetricsConfig(app, false)
}
ux.Logger.PrintToUser(
"\nAvalanche-CLI (the \"software\") may collect statistical data on how the software is used on an anonymous " +
"basis for purposes of product improvement. This data will not (i) include any passwords, scripts, or data " +
"files, (ii) be associated with any particular user or entity, or (iii) include any personally identifiable " +
"information or be used to identify individuals or entities using the software. You can disable such data " +
"collection with `avalanche config metrics disable` command, which will result in no data being collected; " +
"by using the software without so disabling such data collection you expressly consent to the collection of " +
"such data. You can also read our privacy statement <https://www.avalabs.org/privacy-policy> to learn more. \n",
)
ux.Logger.PrintToUser(
"You can disable data collection with `avalanche config metrics disable` command. " +
"You can also read our privacy statement <https://www.avalabs.org/privacy-policy> to learn more.\n",
)
txt := "Press [Enter] to opt-in, or opt out by choosing 'No'"
yes, err := app.Prompt.CaptureYesNo(txt)
if err != nil {
return err
}
if !yes {
ux.Logger.PrintToUser("Avalanche CLI usage metrics will not be collected")
} else {
ux.Logger.PrintToUser("Thank you for opting in Avalanche CLI usage metrics collection")
}
if err = saveMetricsConfig(app, yes); err != nil {
return err
}
return nil
}
Loading