generated from datumforge/go-template
-
Notifications
You must be signed in to change notification settings - Fork 5
/
event.go
183 lines (161 loc) · 5.11 KB
/
event.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package posthog
import (
"time"
"github.com/posthog/posthog-go"
"github.com/datumforge/datum/pkg/analytics/machine"
)
// Config is the configuration for PostHog
type Config struct {
// Enabled is a flag to enable or disable PostHog
Enabled bool `json:"enabled" koanf:"enabled" default:"false"`
// APIKey is the PostHog API Key
APIKey string `json:"apiKey" koanf:"apiKey"`
// Host is the PostHog API Host
Host string `json:"host" koanf:"host" default:"https://app.posthog.com"`
}
type PostHog struct {
client posthog.Client
Identifier string
}
// Init returns a pointer to a PostHog object
func (c *Config) Init() *PostHog {
if !c.Enabled || c.APIKey == "" || c.Host == "" || !machine.Available() {
return nil
}
client, _ := posthog.NewWithConfig(c.APIKey, posthog.Config{
Endpoint: c.Host,
BatchSize: 1,
Logger: new(noopLogger),
})
if client != nil {
return &PostHog{
client: client,
Identifier: machine.ID(),
}
}
return nil
}
// Event is used to send an event to PostHog
func (p *PostHog) Event(eventName string, properties posthog.Properties) {
_ = p.client.Enqueue(posthog.Capture{
DistinctId: p.Identifier,
Event: eventName,
Timestamp: time.Now(),
Properties: properties,
})
}
// UserEvent captures user properties
func (p *PostHog) UserEvent(userID, eventName string, properties posthog.Properties) {
_ = p.client.Enqueue(posthog.Capture{
DistinctId: userID,
Event: eventName,
Timestamp: time.Now(),
Properties: properties,
})
}
// AssociateUser function is used to associate a user with an organization in PostHog
func (p *PostHog) AssociateUser(userID string, organizationID string) {
_ = p.client.Enqueue(posthog.GroupIdentify{
DistinctId: organizationID,
Type: "user",
Key: userID,
Timestamp: time.Now(),
Properties: map[string]interface{}{
"$set": map[string]interface{}{
"user": userID,
"organization": organizationID,
},
},
})
}
// OrganizationEvent creates an event associated with the organization, where the eventName can be passed in generically and associated with the org ID if provided
func (p *PostHog) OrganizationEvent(organizationID, userID, eventName string, properties posthog.Properties) {
_ = p.client.Enqueue(posthog.Capture{
DistinctId: userID,
Event: eventName,
Timestamp: time.Now(),
Properties: properties,
Groups: posthog.NewGroups().
Set("organization", organizationID),
})
}
// GroupEvent creates an event associated with the group, where the eventName can be passed in generically and associated with the group ID if provided
func (p *PostHog) GroupEvent(groupID, userID, eventName string, properties posthog.Properties) {
_ = p.client.Enqueue(posthog.Capture{
DistinctId: userID,
Event: eventName,
Timestamp: time.Now(),
Properties: properties,
Groups: posthog.NewGroups().
Set("group", groupID),
})
}
// NewOrganization uses the NewGroups reference to create a new organization in the organization groups category, and also sets attributes for the organization
func (p *PostHog) NewOrganization(organizationID, userID string, properties posthog.Properties) {
// this event is creating the organization and associating it with our internal organization ID
_ = p.client.Enqueue(posthog.Capture{
DistinctId: userID,
Event: "organization_created",
Timestamp: time.Now(),
Groups: posthog.NewGroups().
Set("organization", organizationID),
})
// this is attempting to set
_ = p.client.Enqueue(posthog.GroupIdentify{
Type: "organization",
Key: organizationID,
Timestamp: time.Now(),
Properties: properties,
})
}
// Properties sets generic properties
func (p *PostHog) Properties(id, obj string, properties posthog.Properties) {
_ = p.client.Enqueue(posthog.GroupIdentify{
Type: obj,
Key: id,
Timestamp: time.Now(),
Properties: properties,
})
}
// OrganizationProperties sets org properties
func (p *PostHog) OrganizationProperties(organizationID string, properties posthog.Properties) {
_ = p.client.Enqueue(posthog.GroupIdentify{
Type: "organization",
Key: organizationID,
Timestamp: time.Now(),
Properties: properties,
})
}
// UserProperties is to expand the properties of the user in the user group
func (p *PostHog) UserProperties(userID string, properties posthog.Properties) {
_ = p.client.Enqueue(posthog.GroupIdentify{
Type: "user",
Key: userID,
Timestamp: time.Now(),
Properties: properties,
})
}
// NewUser maps the userID to the user group
func (p *PostHog) NewUser(userID string, properties posthog.Properties) {
_ = p.client.Enqueue(posthog.Capture{
DistinctId: userID,
Event: "user_created",
Timestamp: time.Now(),
Groups: posthog.NewGroups().
Set("user", userID),
})
}
// NewGroup maps the groupID to the group group
func (p *PostHog) NewGroup(groupID string, properties posthog.Properties) {
_ = p.client.Enqueue(posthog.Capture{
DistinctId: groupID,
Event: "group_created",
Timestamp: time.Now(),
Groups: posthog.NewGroups().
Set("group", groupID),
})
}
// Cleanup cleans up the cleanup
func (p *PostHog) Cleanup() {
_ = p.client.Close()
}