/
thinkingdata.go
291 lines (248 loc) · 9.01 KB
/
thinkingdata.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package wildfiredata
import (
"errors"
"sync"
)
const (
Track = "track"
TrackUpdate = "track_update"
TrackOverwrite = "track_overwrite"
UserSet = "user_set"
UserUnset = "user_unset"
UserSetOnce = "user_setOnce"
UserAdd = "user_add"
UserAppend = "user_append"
UserUniqAppend = "user_uniq_append"
UserDel = "user_del"
SdkVersion = "2.0.1"
LibName = "Golang"
)
type Data struct {
IsComplex bool `json:"-"` // properties are nested or not
AccountId string `json:"#account_id,omitempty"`
DistinctId string `json:"#distinct_id,omitempty"`
Type string `json:"#type"`
Time string `json:"#time"`
EventName string `json:"#event_name,omitempty"`
EventId string `json:"#event_id,omitempty"`
FirstCheckId string `json:"#first_check_id,omitempty"`
Ip string `json:"#ip,omitempty"`
UUID string `json:"#uuid,omitempty"`
AppId string `json:"#app_id,omitempty"`
Properties map[string]interface{} `json:"properties"`
}
// TDConsumer define operation interface
type TDConsumer interface {
Add(d Data) error
Flush() error
Close() error
IsStringent() bool // check data or not.
}
type TDAnalytics struct {
consumer TDConsumer
superProperties map[string]interface{}
mutex *sync.RWMutex
dynamicSuperProperties func() map[string]interface{}
}
// New init SDK
func New(c TDConsumer) TDAnalytics {
tdLogInfo("init SDK success")
return TDAnalytics{
consumer: c,
superProperties: make(map[string]interface{}),
mutex: new(sync.RWMutex),
}
}
// GetSuperProperties get common properties
func (ta *TDAnalytics) GetSuperProperties() map[string]interface{} {
result := make(map[string]interface{})
ta.mutex.Lock()
mergeProperties(result, ta.superProperties)
ta.mutex.Unlock()
return result
}
// SetSuperProperties set common properties
func (ta *TDAnalytics) SetSuperProperties(superProperties map[string]interface{}) {
ta.mutex.Lock()
mergeProperties(ta.superProperties, superProperties)
ta.mutex.Unlock()
}
// ClearSuperProperties clear common properties
func (ta *TDAnalytics) ClearSuperProperties() {
ta.mutex.Lock()
ta.superProperties = make(map[string]interface{})
ta.mutex.Unlock()
}
// SetDynamicSuperProperties set common properties dynamically.
// not recommend to add the operation which with a lot of computation
func (ta *TDAnalytics) SetDynamicSuperProperties(action func() map[string]interface{}) {
ta.mutex.Lock()
ta.dynamicSuperProperties = action
ta.mutex.Unlock()
}
// GetDynamicSuperProperties dynamic common properties
func (ta *TDAnalytics) GetDynamicSuperProperties() map[string]interface{} {
result := make(map[string]interface{})
ta.mutex.RLock()
if ta.dynamicSuperProperties != nil {
mergeProperties(result, ta.dynamicSuperProperties())
}
ta.mutex.RUnlock()
return result
}
// Track report ordinary event
func (ta *TDAnalytics) Track(accountId, distinctId, eventName string, properties map[string]interface{}) error {
return ta.track(accountId, distinctId, Track, eventName, "", properties)
}
// TrackFirst report first event
func (ta *TDAnalytics) TrackFirst(accountId, distinctId, eventName, firstCheckId string, properties map[string]interface{}) error {
if len(firstCheckId) == 0 {
msg := "the 'firstCheckId' must be provided"
tdLogInfo(msg)
return errors.New(msg)
}
p := make(map[string]interface{})
mergeProperties(p, properties)
p["#first_check_id"] = firstCheckId
return ta.track(accountId, distinctId, Track, eventName, "", p)
}
// TrackUpdate report updatable event
func (ta *TDAnalytics) TrackUpdate(accountId, distinctId, eventName, eventId string, properties map[string]interface{}) error {
return ta.track(accountId, distinctId, TrackUpdate, eventName, eventId, properties)
}
// TrackOverwrite report overridable event
func (ta *TDAnalytics) TrackOverwrite(accountId, distinctId, eventName, eventId string, properties map[string]interface{}) error {
return ta.track(accountId, distinctId, TrackOverwrite, eventName, eventId, properties)
}
func (ta *TDAnalytics) track(accountId, distinctId, dataType, eventName, eventId string, properties map[string]interface{}) error {
defer func() {
if r := recover(); r != nil {
tdLogError("%+v\ndata: %+v", r, properties)
}
}()
if len(eventName) == 0 {
msg := "the event name must be provided"
tdLogError(msg)
return errors.New(msg)
}
// eventId not be null unless eventType is equal Track.
if len(eventId) == 0 && dataType != Track {
msg := "the event id must be provided"
tdLogError(msg)
return errors.New(msg)
}
p := ta.GetSuperProperties()
dynamicSuperProperties := ta.GetDynamicSuperProperties()
mergeProperties(p, dynamicSuperProperties)
// preset properties has the highest priority
p["#lib"] = LibName
p["#lib_version"] = SdkVersion
// custom properties
mergeProperties(p, properties)
return ta.add(accountId, distinctId, dataType, eventName, eventId, p)
}
// UserSet set user properties. would overwrite existing names.
func (ta *TDAnalytics) UserSet(accountId string, distinctId string, properties map[string]interface{}) error {
return ta.user(accountId, distinctId, UserSet, properties)
}
// UserUnset clear the user properties of users.
func (ta *TDAnalytics) UserUnset(accountId string, distinctId string, s []string) error {
if len(s) == 0 {
msg := "invalid params for UserUnset: properties is nil"
tdLogInfo(msg)
return errors.New(msg)
}
prop := make(map[string]interface{})
for _, v := range s {
prop[v] = 0
}
return ta.user(accountId, distinctId, UserUnset, prop)
}
// UserSetOnce set user properties, If such property had been set before, this message would be neglected.
func (ta *TDAnalytics) UserSetOnce(accountId string, distinctId string, properties map[string]interface{}) error {
return ta.user(accountId, distinctId, UserSetOnce, properties)
}
// UserAdd to accumulate operations against the property.
func (ta *TDAnalytics) UserAdd(accountId string, distinctId string, properties map[string]interface{}) error {
return ta.user(accountId, distinctId, UserAdd, properties)
}
// UserAppend to add user properties of array type.
func (ta *TDAnalytics) UserAppend(accountId string, distinctId string, properties map[string]interface{}) error {
return ta.user(accountId, distinctId, UserAppend, properties)
}
// UserUniqAppend append user properties to array type by unique.
func (ta *TDAnalytics) UserUniqAppend(accountId string, distinctId string, properties map[string]interface{}) error {
return ta.user(accountId, distinctId, UserUniqAppend, properties)
}
// UserDelete delete a user, This operation cannot be undone.
func (ta *TDAnalytics) UserDelete(accountId string, distinctId string) error {
return ta.user(accountId, distinctId, UserDel, nil)
}
func (ta *TDAnalytics) user(accountId, distinctId, dataType string, properties map[string]interface{}) error {
defer func() {
if r := recover(); r != nil {
tdLogError("%+v\ndata: %+v", r, properties)
}
}()
if properties == nil && dataType != UserDel {
msg := "invalid params for " + dataType + ": properties is nil"
tdLogError(msg)
return errors.New(msg)
}
p := make(map[string]interface{})
mergeProperties(p, properties)
return ta.add(accountId, distinctId, dataType, "", "", p)
}
// Flush report data immediately.
func (ta *TDAnalytics) Flush() error {
return ta.consumer.Flush()
}
// Close and exit sdk
func (ta *TDAnalytics) Close() error {
err := ta.consumer.Close()
tdLogInfo("SDK close")
return err
}
func (ta *TDAnalytics) add(accountId, distinctId, dataType, eventName, eventId string, properties map[string]interface{}) error {
if len(accountId) == 0 && len(distinctId) == 0 {
msg := "invalid parameters: account_id and distinct_id cannot be empty at the same time"
tdLogError(msg)
return errors.New(msg)
}
// get "#ip" value in properties, empty string will be return when not found.
ip := extractStringProperty(properties, "#ip")
// get "#app_id" value in properties, empty string will be return when not found.
appId := extractStringProperty(properties, "#app_id")
// get "#time" value in properties, empty string will be return when not found.
eventTime := extractTime(properties)
firstCheckId := extractStringProperty(properties, "#first_check_id")
// get "#uuid" value in properties, empty string will be return when not found.
uuid := extractStringProperty(properties, "#uuid")
if len(uuid) == 0 {
uuid = generateUUID()
}
data := Data{
AccountId: accountId,
DistinctId: distinctId,
Type: dataType,
Time: eventTime,
EventName: eventName,
EventId: eventId,
FirstCheckId: firstCheckId,
Ip: ip,
UUID: uuid,
Properties: properties,
}
if len(appId) > 0 {
data.AppId = appId
}
err := formatProperties(&data, ta)
if err != nil {
return err
}
return ta.consumer.Add(data)
}
// Deprecated: please use TDConsumer
type Consumer interface {
TDConsumer
}