Skip to content

Commit

Permalink
add posthog telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
zeucapua committed Oct 3, 2024
1 parent 34aed66 commit d3684bb
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
19 changes: 15 additions & 4 deletions cmd/offboard/offboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/spf13/cobra"

"github.com/open-sauced/pizza-cli/v2/pkg/config"
"github.com/open-sauced/pizza-cli/v2/pkg/constants"
"github.com/open-sauced/pizza-cli/v2/pkg/utils"
)

type Options struct {
Expand All @@ -21,6 +23,9 @@ type Options struct {

// from global config
ttyDisabled bool

// telemetry for capturing CLI events via PostHog
telemetry *utils.PosthogCliClient
}

const offboardLongDesc string = `[WIP] Removes a user from the \".sauced.yaml\" config and \"CODEOWNERS\" files.
Expand All @@ -44,13 +49,15 @@ func NewConfigCommand() *cobra.Command {
RunE: func(cmd *cobra.Command, _ []string) error {
opts.ttyDisabled, _ = cmd.Flags().GetBool("tty-disable")
opts.configPath, _ = cmd.Flags().GetString("config")
disableTelem, _ := cmd.Flags().GetBool(constants.FlagNameTelemetry)

opts.telemetry = utils.NewPosthogCliClient(!disableTelem)

opts.path, _ = cmd.Flags().GetString("path")
err := run(opts)
if err != nil {
return err
}
return nil
_ = opts.telemetry.Done()

return err
},
}

Expand All @@ -62,6 +69,7 @@ func run(opts *Options) error {
spec, _, err := config.LoadConfig(opts.configPath)

if err != nil {
_ = opts.telemetry.CaptureFailedOffboard()
return fmt.Errorf("error loading config: %v", err)
}

Expand Down Expand Up @@ -89,13 +97,16 @@ func run(opts *Options) error {

err = generateConfigFile(opts.configPath, attributions)
if err != nil {
_ = opts.telemetry.CaptureFailedOffboard()
return fmt.Errorf("error generating config file: %v", err)
}

err = generateOwnersFile(opts.path, offboardingNames)
if err != nil {
_ = opts.telemetry.CaptureFailedOffboard()
return fmt.Errorf("error generating owners file: %v", err)
}

_ = opts.telemetry.CaptureOffboard()
return nil
}
22 changes: 22 additions & 0 deletions pkg/utils/posthog.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,28 @@ func (p *PosthogCliClient) CaptureFailedConfigGenerate() error {
return nil
}

func (p *PosthogCliClient) CaptureOffboard() error {
if p.activated {
return p.client.Enqueue(posthog.Capture{
DistinctId: p.uniqueID,
Event: "pizza_cli_offboard",
})
}

return nil
}

func (p *PosthogCliClient) CaptureFailedOffboard() error {
if p.activated {
return p.client.Enqueue(posthog.Capture{
DistinctId: p.uniqueID,
Event: "pizza_cli_failed_to_offboard",
})
}

return nil
}

// CaptureInsights gathers telemetry on successful Insights command runs
func (p *PosthogCliClient) CaptureInsights() error {
if p.activated {
Expand Down

0 comments on commit d3684bb

Please sign in to comment.