forked from dukex/mixpanel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmixpanel.go
254 lines (197 loc) · 5.52 KB
/
mixpanel.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package mixpanel
import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
)
var IgnoreTime *time.Time = &time.Time{}
type MixpanelError struct {
URL string
Err error
}
func (err *MixpanelError) Cause() error {
return err.Err
}
func (err *MixpanelError) Error() string {
return "mixpanel: " + err.Err.Error()
}
type ErrTrackFailed struct {
Body string
Resp *http.Response
}
func (err *ErrTrackFailed) Error() string {
return fmt.Sprintf("Mixpanel did not return 1 when tracking: %s", err.Body)
}
// The Mixapanel struct store the mixpanel endpoint and the project token
type Mixpanel interface {
// Create a mixpanel event
Track(distinctId, eventName string, e *Event) error
// Set properties for a mixpanel user.
Update(distinctId string, u *Update) error
// Unset properties for a mixpanel user.
Unset(distinctId string, u *Unset) error
Alias(distinctId, newId string) error
}
// The Mixapanel struct store the mixpanel endpoint and the project token
type mixpanel struct {
Client *http.Client
Token string
ApiURL string
}
// A mixpanel event
type Event struct {
// IP-address of the user. Leave empty to use autodetect, or set to "0" to
// not specify an ip-address.
IP string
// Timestamp. Set to nil to use the current time.
Timestamp *time.Time
// Custom properties. At least one must be specified.
Properties map[string]interface{}
}
// An update of a user in mixpanel
type Update struct {
// IP-address of the user. Leave empty to use autodetect, or set to "0" to
// not specify an ip-address at all.
IP string
// Timestamp. Set to nil to use the current time, or IgnoreTime to not use a
// timestamp.
Timestamp *time.Time
// Update operation such as "$set", "$update" etc.
Operation string
// Custom properties. At least one must be specified.
Properties map[string]interface{}
}
// An update of a user in mixpanel with unset operation
type Unset struct {
// IP-address of the user. Leave empty to use autodetect, or set to "0" to
// not specify an ip-address at all.
IP string
// Timestamp. Set to nil to use the current time, or IgnoreTime to not use a
// timestamp.
Timestamp *time.Time
// Update operation "$unset" only
Operation string
// Custom properties. At least one must be specified.
Properties []string
}
// Track create a events to current distinct id
func (m *mixpanel) Alias(distinctId, newId string) error {
props := map[string]interface{}{
"token": m.Token,
"distinct_id": distinctId,
"alias": newId,
}
params := map[string]interface{}{
"event": "$create_alias",
"properties": props,
}
return m.send("track", params, false)
}
// Track create a events to current distinct id
func (m *mixpanel) Track(distinctId, eventName string, e *Event) error {
props := map[string]interface{}{
"token": m.Token,
"distinct_id": distinctId,
}
if e.IP != "" {
props["ip"] = e.IP
}
if e.Timestamp != nil {
props["time"] = e.Timestamp.Unix()
}
for key, value := range e.Properties {
props[key] = value
}
params := map[string]interface{}{
"event": eventName,
"properties": props,
}
autoGeolocate := e.IP == ""
return m.send("track", params, autoGeolocate)
}
// Updates a user in mixpanel. See
// https://mixpanel.com/help/reference/http#people-analytics-updates
func (m *mixpanel) Update(distinctId string, u *Update) error {
params := map[string]interface{}{
"$token": m.Token,
"$distinct_id": distinctId,
}
if u.IP != "" {
params["$ip"] = u.IP
}
if u.Timestamp == IgnoreTime {
params["$ignore_time"] = true
} else if u.Timestamp != nil {
params["$time"] = u.Timestamp.Unix()
}
params[u.Operation] = u.Properties
autoGeolocate := u.IP == ""
return m.send("engage", params, autoGeolocate)
}
// Unset properties for a user in mixpanel. See
// https://mixpanel.com/help/reference/http#people-analytics-updates
func (m *mixpanel) Unset(distinctId string, u *Unset) error {
params := map[string]interface{}{
"$token": m.Token,
"$distinct_id": distinctId,
}
if u.IP != "" {
params["$ip"] = u.IP
}
if u.Timestamp == IgnoreTime {
params["$ignore_time"] = true
} else if u.Timestamp != nil {
params["$time"] = u.Timestamp.Unix()
}
params[u.Operation] = u.Properties
return m.send("engage", params, false)
}
func (m *mixpanel) to64(data []byte) string {
return base64.StdEncoding.EncodeToString(data)
}
func (m *mixpanel) send(eventType string, params interface{}, autoGeolocate bool) error {
data, err := json.Marshal(params)
if err != nil {
return err
}
url := m.ApiURL + "/" + eventType + "?data=" + m.to64(data)
if autoGeolocate {
url += "&ip=1"
}
wrapErr := func(err error) error {
return &MixpanelError{URL: url, Err: err}
}
resp, err := m.Client.Get(url)
if err != nil {
return wrapErr(err)
}
defer resp.Body.Close()
body, bodyErr := ioutil.ReadAll(resp.Body)
if bodyErr != nil {
return wrapErr(bodyErr)
}
if strBody := string(body); strBody != "1" && strBody != "1\n" {
return wrapErr(&ErrTrackFailed{Body: strBody, Resp: resp})
}
return nil
}
// New returns the client instance. If apiURL is blank, the default will be used
// ("https://api.mixpanel.com").
func New(token, apiURL string) Mixpanel {
return NewFromClient(http.DefaultClient, token, apiURL)
}
// Creates a client instance using the specified client instance. This is useful
// when using a proxy.
func NewFromClient(c *http.Client, token, apiURL string) Mixpanel {
if apiURL == "" {
apiURL = "https://api.mixpanel.com"
}
return &mixpanel{
Client: c,
Token: token,
ApiURL: apiURL,
}
}