-
Notifications
You must be signed in to change notification settings - Fork 1
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
feat: track help cmd #245
feat: track help cmd #245
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,12 +31,15 @@ func NewConfigCmd(analyticsTracker analytics.Tracker) *cobra.Command { | |
Short: "View and modify specific configuration values", | ||
Use: "config", | ||
PreRun: func(cmd *cobra.Command, args []string) { | ||
analyticsTracker.SendCommandRunEvent( | ||
viper.GetString(cliflags.AccessTokenFlag), | ||
viper.GetString(cliflags.BaseURIFlag), | ||
viper.GetBool(cliflags.AnalyticsOptOut), | ||
cmdAnalytics.CmdRunEventProperties(cmd, "config"), | ||
) | ||
// only track event if there are flags | ||
if len(os.Args[1:]) > 1 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if there's a better way to do this. Since we define |
||
analyticsTracker.SendCommandRunEvent( | ||
viper.GetString(cliflags.AccessTokenFlag), | ||
viper.GetString(cliflags.BaseURIFlag), | ||
viper.GetBool(cliflags.AnalyticsOptOut), | ||
cmdAnalytics.CmdRunEventProperties(cmd, "config", nil), | ||
) | ||
} | ||
}, | ||
} | ||
|
||
|
@@ -53,6 +56,19 @@ func NewConfigCmd(analyticsTracker analytics.Tracker) *cobra.Command { | |
sb.WriteString(fmt.Sprintf("- `%s`: %s\n", s, cliflags.AllFlagsHelp()[s])) | ||
} | ||
cmd.Long += sb.String() | ||
|
||
analyticsTracker.SendCommandRunEvent( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is similar to what we do in the root command. |
||
viper.GetString(cliflags.AccessTokenFlag), | ||
viper.GetString(cliflags.BaseURIFlag), | ||
viper.GetBool(cliflags.AnalyticsOptOut), | ||
cmdAnalytics.CmdRunEventProperties(cmd, | ||
"config", | ||
map[string]interface{}{ | ||
"action": "help", | ||
}, | ||
), | ||
) | ||
|
||
helpFun(cmd, args) | ||
}) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ import ( | |
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
|
||
cmdAnalytics "ldcli/cmd/analytics" | ||
"ldcli/cmd/cliflags" | ||
configcmd "ldcli/cmd/config" | ||
resourcecmd "ldcli/cmd/resources" | ||
|
@@ -66,6 +67,24 @@ func NewRootCommand( | |
SilenceUsage: true, | ||
} | ||
|
||
hf := cmd.HelpFunc() | ||
cmd.SetHelpFunc(func(c *cobra.Command, args []string) { | ||
// get the resource for the tracking event, not the action | ||
resourceCommand := getResourceCommand(c) | ||
analyticsTracker.SendCommandRunEvent( | ||
viper.GetString(cliflags.AccessTokenFlag), | ||
viper.GetString(cliflags.BaseURIFlag), | ||
viper.GetBool(cliflags.AnalyticsOptOut), | ||
cmdAnalytics.CmdRunEventProperties(c, | ||
resourceCommand.Name(), | ||
map[string]interface{}{ | ||
"action": "help", | ||
}, | ||
), | ||
) | ||
hf(c, args) | ||
}) | ||
|
||
if useConfigFile { | ||
setFlagsFromConfig() | ||
} | ||
|
@@ -174,3 +193,14 @@ func setFlagsFromConfig() { | |
viper.SetConfigFile(config.GetConfigFile()) | ||
_ = viper.ReadInConfig() | ||
} | ||
|
||
// getResourceCommand returns the command for a resource or an action's parent resource. | ||
// ldcli projects // returns projects command | ||
// ldcli projects list // returns projects command | ||
func getResourceCommand(c *cobra.Command) *cobra.Command { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need this since a user could call help in various ways. |
||
if !c.HasParent() || c.Parent() == c.Root() { | ||
return c | ||
} | ||
|
||
return getResourceCommand(c.Parent()) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,11 +47,12 @@ type Tracker interface { | |
} | ||
|
||
type Client struct { | ||
ID string | ||
HTTPClient *http.Client | ||
Version string | ||
sentRunEvent bool | ||
wg sync.WaitGroup | ||
ID string | ||
HTTPClient *http.Client | ||
Version string | ||
sentHelpEvent bool | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to keep state here to know if we already tracked a help event. |
||
sentRunEvent bool | ||
wg sync.WaitGroup | ||
} | ||
|
||
// SendEvent makes an async request to track the given event with properties. | ||
|
@@ -127,6 +128,10 @@ func (c *Client) SendCommandRunEvent( | |
) | ||
if !optOut { | ||
c.sentRunEvent = true | ||
action, ok := properties["action"] | ||
if ok && action == "help" { | ||
c.sentHelpEvent = true | ||
} | ||
} | ||
} | ||
|
||
|
@@ -137,6 +142,10 @@ func (c *Client) SendCommandCompletedEvent( | |
outcome string, | ||
) { | ||
if c.sentRunEvent { | ||
if c.sentHelpEvent { | ||
outcome = HELP | ||
} | ||
|
||
c.sendEvent( | ||
accessToken, | ||
baseURI, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add overrides so we can pass in an optional "action: help". We could make this functional options if we want to use it for more than the one-off case this introduces.