forked from sensorsdata/sa-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensorsanalytics.go
203 lines (168 loc) · 6.55 KB
/
sensorsanalytics.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
/*
* Created by dengshiwei on 2020/01/06.
* Copyright 2015-2020 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 (
"errors"
"github.com/sensorsdata/sa-sdk-go/consumers"
"github.com/sensorsdata/sa-sdk-go/utils"
)
const (
TRACK = "track"
TRACK_SIGNUP = "track_signup"
PROFILE_SET = "profile_set"
PROFILE_SET_ONCE = "profile_set_once"
PROFILE_INCREMENT = "profile_increment"
PROFILE_APPEND = "profile_append"
PROFILE_UNSET = "profile_unset"
PROFILE_DELETE = "profile_delete"
ITEM_SET = "item_set"
ITEM_DELETE = "item_delete"
MAX_ID_LEN = 255
)
// 静态公共属性
var superProperties map[string]interface{}
type SensorsAnalytics struct {
C consumers.Consumer
ProjectName string
TimeFree bool
}
func InitSensorsAnalytics(c consumers.Consumer, projectName string, timeFree bool) SensorsAnalytics {
return SensorsAnalytics{C: c, ProjectName: projectName, TimeFree: timeFree}
}
func (sa *SensorsAnalytics) Flush() {
sa.C.Flush()
}
func (sa *SensorsAnalytics) Close() {
sa.C.Close()
}
func (sa *SensorsAnalytics) Track(distinctId, event string, properties map[string]interface{}, isLoginId bool) error {
var nproperties map[string]interface{}
// merge properties
if properties == nil {
nproperties = make(map[string]interface{})
} else {
nproperties = utils.DeepCopy(properties)
}
// merge super properties
if superProperties != nil {
utils.MergeSuperProperty(superProperties, nproperties)
}
return TrackEvent(sa, TRACK, event, distinctId, "", nproperties, isLoginId)
}
func (sa *SensorsAnalytics) TrackSignup(distinctId, originId string) error {
// check originId and merge properties
if originId == "" {
return errors.New("property [original_id] must not be empty")
}
if len(originId) > MAX_ID_LEN {
return errors.New("the max length of property [original_id] is 255")
}
properties := make(map[string]interface{})
// merge super properties
if superProperties != nil {
utils.MergeSuperProperty(superProperties, properties)
}
return TrackEvent(sa, TRACK_SIGNUP, "$SignUp", distinctId, originId, properties, false)
}
func (sa *SensorsAnalytics) ProfileSet(distinctId string, properties map[string]interface{}, isLoginId bool) error {
var nproperties map[string]interface{}
if properties == nil {
return errors.New("property should not be nil")
} else {
nproperties = utils.DeepCopy(properties)
}
return TrackEvent(sa, PROFILE_SET, "", distinctId, "", nproperties, isLoginId)
}
func (sa *SensorsAnalytics) ProfileSetOnce(distinctId string, properties map[string]interface{}, isLoginId bool) error {
var nproperties map[string]interface{}
if properties == nil {
return errors.New("property should not be nil")
} else {
nproperties = utils.DeepCopy(properties)
}
return TrackEvent(sa, PROFILE_SET_ONCE, "", distinctId, "", nproperties, isLoginId)
}
func (sa *SensorsAnalytics) ProfileIncrement(distinctId string, properties map[string]interface{}, isLoginId bool) error {
var nproperties map[string]interface{}
if properties == nil {
return errors.New("property should not be nil")
} else {
nproperties = utils.DeepCopy(properties)
}
return TrackEvent(sa, PROFILE_INCREMENT, "", distinctId, "", nproperties, isLoginId)
}
func (sa *SensorsAnalytics) ProfileAppend(distinctId string, properties map[string]interface{}, isLoginId bool) error {
var nproperties map[string]interface{}
if properties == nil {
return errors.New("property should not be nil")
} else {
nproperties = utils.DeepCopy(properties)
}
return TrackEvent(sa, PROFILE_APPEND, "", distinctId, "", nproperties, isLoginId)
}
func (sa *SensorsAnalytics) ProfileUnset(distinctId string, properties map[string]interface{}, isLoginId bool) error {
var nproperties map[string]interface{}
if properties == nil {
return errors.New("property should not be nil")
} else {
nproperties = utils.DeepCopy(properties)
}
return TrackEvent(sa, PROFILE_UNSET, "", distinctId, "", nproperties, isLoginId)
}
func (sa *SensorsAnalytics) ProfileDelete(distinctId string, isLoginId bool) error {
nproperties := make(map[string]interface{})
return TrackEvent(sa, PROFILE_DELETE, "", distinctId, "", nproperties, isLoginId)
}
func (sa *SensorsAnalytics) ItemSet(itemType string, itemId string, properties map[string]interface{}) error {
return ItemTrack(sa, ITEM_SET, itemType, itemId, properties)
}
func (sa *SensorsAnalytics) ItemDelete(itemType string, itemId string) error {
return ItemTrack(sa, ITEM_DELETE, itemType, itemId, nil)
}
func (sa *SensorsAnalytics) ItemDelete3(itemType string, itemId string, properties map[string]interface{}) error {
return ItemTrack(sa, ITEM_DELETE, itemType, itemId, properties)
}
// RegisterSuperProperties 注册公共属性
func (sa *SensorsAnalytics) RegisterSuperProperties(superProperty map[string]interface{}) {
if superProperties == nil {
superProperties = make(map[string]interface{})
}
utils.MergeSuperProperty(superProperty, superProperties)
}
// ClearSuperProperties 清除公共属性
func (sa *SensorsAnalytics) ClearSuperProperties() {
superProperties = make(map[string]interface{})
}
// UnregisterSuperProperty 清除指定 key 的公共属性
func (sa *SensorsAnalytics) UnregisterSuperProperty(key string) {
delete(superProperties, key)
}
func InitDefaultConsumer(url string, timeout int) (*consumers.DefaultConsumer, error) {
return consumers.InitDefaultConsumer(url, timeout)
}
func InitBatchConsumer(url string, max, timeout int) (*consumers.BatchConsumer, error) {
return consumers.InitBatchConsumer(url, max, timeout)
}
func InitLoggingConsumer(filename string, hourRotate bool) (*consumers.LoggingConsumer, error) {
return consumers.InitLoggingConsumer(filename, hourRotate)
}
func InitConcurrentLoggingConsumer(filename string, hourRotate bool) (*consumers.ConcurrentLoggingConsumer, error) {
return consumers.InitConcurrentLoggingConsumer(filename, hourRotate)
}
func InitDebugConsumer(url string, writeData bool, timeout int) (*consumers.DebugConsumer, error) {
return consumers.InitDebugConsumer(url, writeData, timeout)
}