-
Notifications
You must be signed in to change notification settings - Fork 67
/
analytics.go
88 lines (73 loc) 路 1.56 KB
/
analytics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package analytics
import (
"os"
"github.com/denisbrodbeck/machineid"
"github.com/kubeshop/tracetest/cli/config"
segment "github.com/segmentio/analytics-go/v3"
)
var (
SecretKey = ""
client segment.Client
mid string
)
func ClientID() string {
return mid
}
func Init(conf config.Config) {
if !conf.AnalyticsEnabled || os.Getenv("TRACETEST_DEV") != "" {
// non-empty TRACETEST_DEV variable means it's running by a dev,
// and we should totally ignore analytics
return
}
client, _ = segment.NewWithConfig(SecretKey, segment.Config{
BatchSize: 1,
})
id, err := machineid.ProtectedID("tracetest")
if err == nil {
// only use id if available.
mid = id
} // ignore errors and continue with an empty ID if neccesary
client.Enqueue(segment.Identify{
UserId: mid,
Traits: segment.NewTraits().
Set("source", "cli").
Set("clientID", mid).
Set("env", config.Env).
Set("appVersion", config.Version),
Context: &segment.Context{
Direct: true,
},
})
}
func Track(name, category string, props map[string]string) error {
if client == nil {
return nil
}
p := segment.NewProperties().
Set("source", "cli").
Set("clientID", mid).
Set("env", config.Env).
Set("appVersion", config.Version).
Set("category", category)
for k, v := range props {
p = p.Set(k, v)
}
err := client.Enqueue(segment.Track{
Event: name,
UserId: mid,
Properties: p,
Context: &segment.Context{
Direct: true,
},
})
return err
}
func Close() {
if client == nil {
return
}
err := client.Close()
if err != nil {
panic(err)
}
}