-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtracker.go
255 lines (218 loc) · 5.97 KB
/
tracker.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
255
package main
import (
"github.com/dukex/mixpanel"
pc "github.com/padloc/padlock-cloud/padlockcloud"
"github.com/satori/go.uuid"
"net/http"
"time"
)
func sourceFromRef(ref string) string {
switch ref {
case "app-1":
return "Cloud View - Manage Account"
case "app-2":
return "Cloud View - Trialing"
case "app-3":
return "Cloud View - Readonly"
case "app-4":
return "Dialog - Trial Ending"
case "app-5":
return "Dialog - Readonly"
case "pair":
return "Device Paired"
default:
return ref
}
}
type TrackingEvent struct {
TrackingID string `json:"trackingID"`
Name string `json:"event"`
Properties map[string]interface{} `json:"props"`
request *http.Request
authToken *pc.AuthToken
}
type Tracker interface {
Track(event *TrackingEvent) error
DeleteProfile(acc *Account) error
UpdateProfile(acc *Account, props map[string]interface{}) error
UnsubscribeProfile(tid string) error
}
type mixpanelTracker struct {
mixpanel mixpanel.Mixpanel
storage pc.Storage
}
func NewMixpanelTracker(token string, storage pc.Storage) Tracker {
return &mixpanelTracker{
storage: storage,
mixpanel: mixpanel.New(token, ""),
}
}
func (t *mixpanelTracker) Track(event *TrackingEvent) error {
var ip string
if event.request != nil {
ip = pc.IPFromRequest(event.request)
}
a := event.authToken
originalTrackingID := event.TrackingID
if event.TrackingID == "" {
event.TrackingID = uuid.NewV4().String()
}
if event.Properties == nil {
event.Properties = make(map[string]interface{})
}
var acc *Account
if a != nil {
_acc := &Account{Email: a.Email}
if err := t.storage.Get(_acc); err == nil {
acc = _acc
}
}
if acc != nil {
if acc.TrackingID == "" {
acc.TrackingID = event.TrackingID
t.storage.Put(acc)
} else {
event.TrackingID = acc.TrackingID
if originalTrackingID != "" && originalTrackingID != acc.TrackingID {
if err := t.mixpanel.Update(originalTrackingID, &mixpanel.Update{
IP: ip,
Operation: "$set_once",
Properties: map[string]interface{}{
"Converted To": acc.TrackingID,
},
}); err != nil {
return err
}
}
}
}
props := event.Properties
props["Authenticated"] = a != nil
var device *pc.Device
if a != nil {
device = a.Device
} else if event.request != nil {
device = pc.DeviceFromRequest(event.request)
}
if device != nil {
props["Platform"] = device.Platform
props["Device UUID"] = device.UUID
props["Device Manufacturer"] = device.Manufacturer
props["Device Model"] = device.Model
props["OS Version"] = device.OSVersion
props["Device Name"] = device.HostName
props["App Version"] = device.AppVersion
}
// Make sure email address isn't tracked in events
delete(props, "Email")
delete(props, "$email")
if err := t.mixpanel.Track(event.TrackingID, event.Name, &mixpanel.Event{
IP: ip,
Properties: props,
}); err != nil {
return err
}
// If the user is logged in, update/create profile
if acc != nil {
updateOnce := map[string]interface{}{
"$email": acc.Email,
"Created Padlock Cloud Account": acc.Created.UTC().Format(time.RFC3339),
"$created": props["First Launch"],
"First App Launch": props["First Launch"],
"First Platform": props["Platform"],
}
if err := t.mixpanel.Update(event.TrackingID, &mixpanel.Update{
IP: ip,
Operation: "$set_once",
Properties: updateOnce,
}); err != nil {
return err
}
nDevices := 0
platforms := make([]string, 0)
versions := make([]string, 0)
pMap := make(map[string]bool)
vMap := make(map[string]bool)
for _, token := range a.Account().AuthTokens {
if token.Type == "api" && !token.Expired() {
nDevices = nDevices + 1
}
if token.Device != nil && token.Device.Platform != "" && !pMap[token.Device.Platform] {
platforms = append(platforms, token.Device.Platform)
pMap[token.Device.Platform] = true
}
if token.Device != nil && token.Device.AppVersion != "" && !vMap[token.Device.AppVersion] {
versions = append(versions, token.Device.AppVersion)
vMap[token.Device.AppVersion] = true
}
}
if err := t.UpdateProfile(acc, map[string]interface{}{
"Last Sync": props["Last Sync"],
"Last Rated": props["Last Rated"],
"Rated Version": props["Rated Version"],
"Rating": props["Rating"],
"Last Reviewed": props["Last Reviewed"],
"Paired Devices": nDevices,
"Platforms": platforms,
"Versions": versions,
}); err != nil {
return err
}
}
return nil
}
func (t *mixpanelTracker) UpdateProfile(acc *Account, props map[string]interface{}) error {
if acc.TrackingID == "" {
acc.TrackingID = uuid.NewV4().String()
t.storage.Put(acc)
}
subStatus, _ := acc.SubscriptionStatus()
update := map[string]interface{}{
"Last Updated": time.Now().UTC().Format(time.RFC3339),
"Plan": acc.SubscriptionPlan(),
"Subscription Status": subStatus,
}
if props != nil {
for k, v := range props {
update[k] = v
}
}
return t.mixpanel.Update(acc.TrackingID, &mixpanel.Update{
IP: "0",
Operation: "$set",
Timestamp: mixpanel.IgnoreTime,
Properties: update,
})
}
func (t *mixpanelTracker) DeleteProfile(acc *Account) error {
if acc.TrackingID == "" {
return nil
}
if err := t.Track(&TrackingEvent{
TrackingID: acc.TrackingID,
Name: "Delete Account",
}); err != nil {
return err
}
return t.mixpanel.Update(acc.TrackingID, &mixpanel.Update{
Operation: "$set",
Properties: map[string]interface{}{
"Account Deleted": time.Now().UTC().Format(time.RFC3339),
"$email": "",
},
})
}
func (t *mixpanelTracker) UnsubscribeProfile(tid string) error {
if err := t.Track(&TrackingEvent{
TrackingID: tid,
Name: "Unsubscribe",
}); err != nil {
return err
}
return t.mixpanel.Update(tid, &mixpanel.Update{
Operation: "$set",
Properties: map[string]interface{}{
"$unsubscribed": time.Now().UTC().Format(time.RFC3339),
},
})
}