add telemetry to cli

This commit is contained in:
Maidul Islam
2023-05-19 00:16:26 -04:00
parent 460bdbb91c
commit e4953398df
13 changed files with 121 additions and 1 deletions

View File

@@ -0,0 +1,64 @@
package telemetry
import (
"os"
"github.com/Infisical/infisical-merge/packages/util"
"github.com/denisbrodbeck/machineid"
"github.com/posthog/posthog-go"
)
type Telemetry struct {
isEnabled bool
posthogClient posthog.Client
}
func NewTelemetry(telemetryIsEnabled bool) *Telemetry {
client, _ := posthog.NewWithConfig(
os.Getenv("POSTHOG_API_KEY_FOR_CLI"),
posthog.Config{},
)
return &Telemetry{isEnabled: telemetryIsEnabled, posthogClient: client}
}
func (t *Telemetry) CaptureEvent(eventName string, properties posthog.Properties) {
userIdentity, err := t.GetDistinctId()
if err != nil {
return
}
if t.isEnabled {
t.posthogClient.Enqueue(posthog.Capture{
DistinctId: userIdentity,
Event: eventName,
Properties: properties,
})
}
defer t.posthogClient.Close()
}
func (t *Telemetry) GetDistinctId() (string, error) {
var distinctId string
var outputErr error
machineId, err := machineid.ID()
if err != nil {
outputErr = err
}
userDetails, err := util.GetCurrentLoggedInUserDetails()
if err != nil {
outputErr = err
}
if userDetails.IsUserLoggedIn && userDetails.UserCredentials.Email != "" {
distinctId = userDetails.UserCredentials.Email
} else if machineId != "" {
distinctId = "anonymous_cli" + machineId
} else {
distinctId = ""
}
return distinctId, outputErr
}