This repository has been archived by the owner on Mar 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
mixpanel.go
220 lines (187 loc) · 5.47 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
package mixpanel
import (
"encoding/base64"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
const (
apiBaseURL = "http://api.mixpanel.com"
library = "timehop/go-mixpanel"
)
// actionSource describes where the call is originating from.
// This determines whether or not the location properties on a profile should be updated.
type actionSource int
const (
// sourceUser flags the action as having originated with the user in question.
sourceUser actionSource = iota
// sourceScript means that the action originated in a backend script.
// The IP should not be tracked in this case.
sourceScript
)
// Mixpanel is a client to talk to the API
type Mixpanel struct {
Token string
BaseUrl string
}
// Properties are key=value pairs that decorate an event or a profile.
type Properties map[string]interface{}
// Operation is an action performed on a user profile.
// Typically this is $set or $unset, but others are available.
type Operation struct {
Name string
Values Properties
}
// NewMixpanel returns a configured client.
func NewMixpanel(token string) *Mixpanel {
return &Mixpanel{
Token: token,
BaseUrl: apiBaseURL,
}
}
// Track sends event data with optional metadata.
func (m *Mixpanel) Track(distinctID string, event string, props Properties) error {
if distinctID != "" {
props["distinct_id"] = distinctID
}
props["token"] = m.Token
props["mp_lib"] = library
data := map[string]interface{}{"event": event, "properties": props}
return m.makeRequestWithData("GET", "track", data, sourceUser)
}
// Engage updates profile data.
// This will update the IP and related data on the profile.
// If you don't have the IP address of the user, then use the UpdateProperties method instead,
// otherwise the user's location will be set to wherever the script was run from.
func (m *Mixpanel) Engage(distinctID string, props Properties, op *Operation) error {
return m.engage(distinctID, props, op, sourceUser)
}
// EngageAsScript calls the engage endpoint, but doesn't set IP, city, country, on the profile.
func (m *Mixpanel) EngageAsScript(distinctID string, props Properties, op *Operation) error {
return m.engage(distinctID, props, op, sourceScript)
}
func (m *Mixpanel) engage(distinctID string, props Properties, op *Operation, as actionSource) error {
if distinctID != "" {
props["$distinct_id"] = distinctID
}
props["$token"] = m.Token
props["mp_lib"] = library
if op.Name == "$unset" {
keys := []interface{}{}
for key := range op.Values {
keys = append(keys, key)
}
props[op.Name] = keys
} else {
props[op.Name] = op.Values
}
return m.makeRequestWithData("GET", "engage", props, as)
}
// TrackingPixel returns a url that, when clicked, will track the given data and then redirect to provided url.
func (m *Mixpanel) TrackingPixel(distinctID, event string, props Properties) (string, error) {
if distinctID != "" {
props["$distinct_id"] = distinctID
}
props["$token"] = m.Token
props["mp_lib"] = library
data := map[string]interface{}{"event": event, "properties": props}
json, err := json.Marshal(data)
if err != nil {
return "", err
}
params := map[string]string{
"data": base64.StdEncoding.EncodeToString(json),
"img": "1",
}
query := url.Values{}
for k, v := range params {
query[k] = []string{v}
}
return fmt.Sprintf("%s/%s?%s", m.BaseUrl, "track", query.Encode()), nil
}
// RedirectURL returns a url that, when clicked, will track the given data and then redirect to provided url.
func (m *Mixpanel) RedirectURL(distinctID, event, uri string, props Properties) (string, error) {
if distinctID != "" {
props["$distinct_id"] = distinctID
}
props["$token"] = m.Token
props["mp_lib"] = library
data := map[string]interface{}{"event": event, "properties": props}
b, err := json.Marshal(data)
if err != nil {
return "", err
}
params := map[string]string{
"data": base64.StdEncoding.EncodeToString(b),
"redirect": uri,
}
query := url.Values{}
for k, v := range params {
query[k] = []string{v}
}
return fmt.Sprintf("%s/%s?%s", m.BaseUrl, "track", query.Encode()), nil
}
func (m *Mixpanel) makeRequest(method string, endpoint string, paramMap map[string]string) error {
var (
err error
req *http.Request
r io.Reader
)
if endpoint == "" {
return fmt.Errorf("endpoint missing")
}
endpoint = fmt.Sprintf("%s/%s", m.BaseUrl, endpoint)
if paramMap == nil {
paramMap = map[string]string{}
}
params := url.Values{}
for k, v := range paramMap {
params[k] = []string{v}
}
switch method {
case "GET":
enc := params.Encode()
if enc != "" {
endpoint = endpoint + "?" + enc
}
case "POST":
r = strings.NewReader(params.Encode())
default:
return fmt.Errorf("method not supported: %v", method)
}
req, err = http.NewRequest(method, endpoint, r)
if err != nil {
return err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
// The API documentation states that success will be reported with either "1" or "1\n".
if strings.Trim(string(b), "\n") != "1" {
return fmt.Errorf("request failed - %s", b)
}
return nil
}
func (m *Mixpanel) makeRequestWithData(method string, endpoint string, data Properties, as actionSource) error {
b, err := json.Marshal(data)
if err != nil {
return err
}
params := map[string]string{
"data": base64.StdEncoding.EncodeToString(b),
}
if as == sourceScript {
params["ip"] = "0"
}
return m.makeRequest(method, endpoint, params)
}