-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathposthog.go
More file actions
44 lines (36 loc) · 1.01 KB
/
posthog.go
File metadata and controls
44 lines (36 loc) · 1.01 KB
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
package metrics
import (
"crypto/md5"
"encoding/hex"
"github.com/SwissDataScienceCenter/renku-gateway/internal/config"
"github.com/posthog/posthog-go"
)
type PosthogMetricsClient struct {
posthogClient posthog.Client
}
func (p *PosthogMetricsClient) anonymizeUser(userId string) string {
hash := md5.Sum([]byte(userId))
return hex.EncodeToString(hash[:])
}
func (p *PosthogMetricsClient) UserLoggedIn(userId string) error {
return p.posthogClient.Enqueue(posthog.Capture{DistinctId: p.anonymizeUser(userId), Event: "user_logged_in"})
}
func (p *PosthogMetricsClient) Close() {
p.posthogClient.Close()
}
func NewPosthogClient(c config.PosthogConfig) (*PosthogMetricsClient, error) {
if !c.Enabled {
return nil, nil
}
client, err := posthog.NewWithConfig(
string(c.ApiKey),
posthog.Config{
Endpoint: c.Host,
DefaultEventProperties: posthog.Properties{"environment": c.Environment},
},
)
if err != nil {
return nil, err
}
return &PosthogMetricsClient{posthogClient: client}, nil
}