forked from sensorsdata/sa-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
track.go
217 lines (187 loc) · 4.87 KB
/
track.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
/*
* Created by dengshiwei on 2022/06/06.
* Copyright 2015-2022 Sensors Data Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sensorsanalytics
import (
"fmt"
"github.com/sensorsdata/sa-sdk-go/structs"
"github.com/sensorsdata/sa-sdk-go/utils"
"math/rand"
"os"
"runtime"
"time"
)
const (
SDK_VERSION = "2.1.2"
LIB_NAME = "Golang"
)
func TrackEvent(sa *SensorsAnalytics, etype, event, distinctId, originId string, properties map[string]interface{}, isLoginId bool) error {
eventTime := utils.NowMs()
if properties == nil {
properties = map[string]interface{}{}
}
if et := extractUserTime(properties); et > 0 {
eventTime = et
}
rand.Seed(time.Now().UnixNano())
data := structs.EventData{
Type: etype,
TrackID: rand.Int(),
Time: eventTime,
DistinctId: distinctId,
Properties: properties,
LibProperties: getLibProperties(),
}
project := getProject(data.Properties, sa.ProjectName)
if project != "" {
data.Project = project
}
if etype == TRACK || etype == TRACK_SIGNUP {
data.Event = event
properties["$lib"] = LIB_NAME
properties["$lib_version"] = SDK_VERSION
}
if etype == TRACK_SIGNUP {
data.OriginId = originId
}
if sa.TimeFree {
data.TimeFree = true
}
if isLoginId {
properties["$is_login_id"] = true
}
err := data.NormalizeData()
if err != nil {
return err
}
return sa.C.Send(data)
}
func ItemTrack(sa *SensorsAnalytics, trackType string, itemType string, itemId string, properties map[string]interface{}) error {
eventTime := utils.NowMs()
if et := extractUserTime(properties); et > 0 {
eventTime = et
}
libProperties := getLibProperties()
var nproperties map[string]interface{}
// merge properties
if properties == nil {
nproperties = make(map[string]interface{})
} else {
nproperties = utils.DeepCopy(properties)
}
rand.Seed(time.Now().UnixNano())
itemData := structs.Item{
Type: trackType,
ItemId: itemId,
TrackID: rand.Int(),
Time: eventTime,
ItemType: itemType,
Properties: nproperties,
LibProperties: libProperties,
}
project := getProject(itemData.Properties, sa.ProjectName)
if project != "" {
itemData.Project = project
}
err := itemData.NormalizeItem()
if err != nil {
return err
}
return sa.C.ItemSend(itemData)
}
func TrackEventID3(sa *SensorsAnalytics, identity Identity, etype, event string, properties map[string]interface{}) error {
eventTime := utils.NowMs()
if properties == nil {
properties = map[string]interface{}{}
}
if et := extractUserTime(properties); et > 0 {
eventTime = et
}
rand.Seed(time.Now().UnixNano())
data := structs.EventData{
Type: etype,
TrackID: rand.Int(),
Time: eventTime,
Identities: identity.Identities,
Properties: properties,
LibProperties: getLibProperties(),
}
err := data.CheckIdentities()
if err != nil {
return err
}
// 添加 distinct_id
var distinctId string
idValue := identity.Identities[LOGIN_ID]
if len(idValue) <= 0 {
for k, v := range identity.Identities {
distinctId = k + "+" + v
}
} else {
distinctId = idValue
}
data.DistinctId = distinctId
project := getProject(data.Properties, sa.ProjectName)
if project != "" {
data.Project = project
}
if etype == TRACK || etype == BIND || etype == UNBIND {
data.Event = event
properties["$lib"] = LIB_NAME
properties["$lib_version"] = SDK_VERSION
}
if sa.TimeFree {
data.TimeFree = true
}
err = data.NormalizeData()
if err != nil {
return err
}
return sa.C.Send(data)
}
func getLibProperties() structs.LibProperties {
lp := structs.LibProperties{}
lp.Lib = LIB_NAME
lp.LibVersion = SDK_VERSION
lp.LibMethod = "code"
if pc, file, line, ok := runtime.Caller(3); ok { //3 means sdk's caller
f := runtime.FuncForPC(pc)
lp.LibDetail = fmt.Sprintf("##%s##%s##%d", f.Name(), file, line)
}
return lp
}
func extractUserTime(p map[string]interface{}) int64 {
if t, ok := p["$time"]; ok {
v, ok := t.(int64)
delete(p, "$time")
if !ok {
fmt.Fprintln(os.Stderr, "It's not ok for type string")
return 0
}
return v
}
return 0
}
func getProject(properties map[string]interface{}, defaultProject string) string {
if properties != nil && properties["$project"] != nil {
project, ok := properties["$project"].(string)
delete(properties, "$project")
if ok && project != "" {
return project
}
}
return defaultProject
}