mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-09-22 13:39:35 +00:00
add telemetry to cli
This commit is contained in:
64
cli/packages/telemetry/telemetry.go
Normal file
64
cli/packages/telemetry/telemetry.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user