Skip to content

Commit

Permalink
feat: allow users to opt out of analytics tracking (#206)
Browse files Browse the repository at this point in the history
* add analytics opt out flag and dont send tracking events if true

* update golden file

* update SendEvent to no-op vs pre-run
  • Loading branch information
k3llymariee authored Apr 24, 2024
1 parent 1670cae commit e782a43
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 4 deletions.
1 change: 1 addition & 0 deletions cmd/cliflags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const (
BaseURIDefault = "https://app.launchdarkly.com"

AccessTokenFlag = "access-token"
AnalyticsOptOut = "analytics-opt-out"
BaseURIFlag = "base-uri"
DataFlag = "data"
EmailsFlag = "emails"
Expand Down
1 change: 1 addition & 0 deletions cmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func NewConfigCmd(analyticsTracker analytics.Tracker) *cobra.Command {
analyticsTracker.SendEvent(
viper.GetString(cliflags.AccessTokenFlag),
viper.GetString(cliflags.BaseURIFlag),
viper.GetBool(cliflags.AnalyticsOptOut),
"CLI Command Run",
analytics.CmdRunEventProperties(cmd, "config"),
)
Expand Down
1 change: 1 addition & 0 deletions cmd/config/testdata/help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ Flags:

Global Flags:
--access-token string LaunchDarkly API token with write-level access
--analytics-opt-out Opt out of analytics tracking
--base-uri string LaunchDarkly base URI (default "https://app.launchdarkly.com")
-o, --output string Command response output format in either JSON or plain text (default "plaintext")
1 change: 1 addition & 0 deletions cmd/environments/environments.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func NewEnvironmentsCmd(
analyticsTracker.SendEvent(
viper.GetString(cliflags.AccessTokenFlag),
viper.GetString(cliflags.BaseURIFlag),
viper.GetBool(cliflags.AnalyticsOptOut),
"CLI Command Run",
analytics.CmdRunEventProperties(cmd, "environments"),
)
Expand Down
1 change: 1 addition & 0 deletions cmd/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func NewFlagsCmd(analyticsTracker analytics.Tracker, client flags.Client) (*cobr
analyticsTracker.SendEvent(
viper.GetString(cliflags.AccessTokenFlag),
viper.GetString(cliflags.BaseURIFlag),
viper.GetBool(cliflags.AnalyticsOptOut),
"CLI Command Run",
analytics.CmdRunEventProperties(cmd, "flags"),
)
Expand Down
1 change: 1 addition & 0 deletions cmd/members/members.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func NewMembersCmd(analyticsTracker analytics.Tracker, client members.Client) (*
analyticsTracker.SendEvent(
viper.GetString(cliflags.AccessTokenFlag),
viper.GetString(cliflags.BaseURIFlag),
viper.GetBool(cliflags.AnalyticsOptOut),
"CLI Command Run",
analytics.CmdRunEventProperties(cmd, "members"),
)
Expand Down
1 change: 1 addition & 0 deletions cmd/projects/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func NewProjectsCmd(analyticsTracker analytics.Tracker, client projects.Client)
analyticsTracker.SendEvent(
viper.GetString(cliflags.AccessTokenFlag),
viper.GetString(cliflags.BaseURIFlag),
viper.GetBool(cliflags.AnalyticsOptOut),
"CLI Command Run",
analytics.CmdRunEventProperties(cmd, "projects"),
)
Expand Down
10 changes: 10 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ func NewRootCommand(
return nil, err
}

cmd.PersistentFlags().Bool(
cliflags.AnalyticsOptOut,
false,
"Opt out of analytics tracking",
)
err = viper.BindPFlag(cliflags.AnalyticsOptOut, cmd.PersistentFlags().Lookup(cliflags.AnalyticsOptOut))
if err != nil {
return nil, err
}

cmd.PersistentFlags().StringP(
cliflags.OutputFlag,
"o",
Expand Down
7 changes: 7 additions & 0 deletions internal/analytics/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Tracker interface {
SendEvent(
accessToken string,
baseURI string,
optOut bool,
eventName string,
properties map[string]interface{},
)
Expand All @@ -29,9 +30,13 @@ type Client struct {
func (c *Client) SendEvent(
accessToken string,
baseURI string,
optOut bool,
eventName string,
properties map[string]interface{},
) {
if optOut {
return
}
input := struct {
Event string `json:"event"`
Properties map[string]interface{} `json:"properties"`
Expand Down Expand Up @@ -87,6 +92,7 @@ type NoopClient struct{}
func (c *NoopClient) SendEvent(
accessToken string,
baseURI string,
optOut bool,
eventName string,
properties map[string]interface{},
) {
Expand All @@ -100,6 +106,7 @@ type MockTracker struct {
func (m *MockTracker) SendEvent(
accessToken string,
baseURI string,
optOut bool,
eventName string,
properties map[string]interface{},
) {
Expand Down
16 changes: 12 additions & 4 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"fmt"
"strconv"

"github.com/mitchellh/go-homedir"

Expand All @@ -12,23 +13,30 @@ const Filename = ".ldcli-config.yml"

// ConfigFile represents the data stored in the config file.
type ConfigFile struct {
AccessToken string `json:"access-token,omitempty" yaml:"access-token,omitempty"`
BaseURI string `json:"base-uri,omitempty" yaml:"base-uri,omitempty"`
AccessToken string `json:"access-token,omitempty" yaml:"access-token,omitempty"`
AnalyticsOptOut bool `json:"analytics-opt-out,omitempty" yaml:"analytics-opt-out,omitempty"`
BaseURI string `json:"base-uri,omitempty" yaml:"base-uri,omitempty"`
}

func NewConfig(rawConfig map[string]interface{}) ConfigFile {
var accessToken string
if rawConfig[cliflags.AccessTokenFlag] != nil {
accessToken = rawConfig[cliflags.AccessTokenFlag].(string)
}
var analyticsOptOut bool
if rawConfig[cliflags.AnalyticsOptOut] != nil {
stringValue := rawConfig[cliflags.AnalyticsOptOut].(string)
analyticsOptOut, _ = strconv.ParseBool(stringValue)
}
var baseURI string
if rawConfig[cliflags.BaseURIFlag] != nil {
baseURI = rawConfig[cliflags.BaseURIFlag].(string)
}

return ConfigFile{
AccessToken: accessToken,
BaseURI: baseURI,
AccessToken: accessToken,
AnalyticsOptOut: analyticsOptOut,
BaseURI: baseURI,
}
}

Expand Down

0 comments on commit e782a43

Please sign in to comment.