-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrack_sdk.go
171 lines (148 loc) · 4.01 KB
/
track_sdk.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
/*
* 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 (
"encoding/json"
"math/rand"
"time"
"github.com/oggyunao/sensorsanalytics/structs"
"github.com/oggyunao/sensorsanalytics/utils"
)
func TrackEvent2(sa *SensorsAnalytics, etype, event, distinctId, originId string, properties map[string]interface{}, isLoginId bool) ([]byte, 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.Int31(),
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 nil, err
}
return json.Marshal(data)
}
func ItemTrack2(sa *SensorsAnalytics, trackType string, itemType string, itemId string, properties map[string]interface{}) ([]byte, 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 nil, err
}
return json.Marshal(itemData)
}
func TrackEventID32(sa *SensorsAnalytics, identity Identity, etype, event string, properties map[string]interface{}) ([]byte, 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.Int31(),
Time: eventTime,
Identities: identity.Identities,
Properties: properties,
LibProperties: getLibProperties(),
}
err := data.CheckIdentities()
if err != nil {
return nil, 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 nil, err
}
return json.Marshal(data)
}