@@ -3,13 +3,14 @@ package sensorsabtest
33import (
44 "errors"
55 "fmt"
6- "github.com/sensorsdata/abtesting-sdk-go/beans"
7- "github.com/sensorsdata/abtesting-sdk-go/utils"
8- "github.com/sensorsdata/abtesting-sdk-go/utils/lru"
96 "reflect"
107 "strconv"
118 "sync"
129 "time"
10+
11+ "github.com/sensorsdata/abtesting-sdk-go/beans"
12+ "github.com/sensorsdata/abtesting-sdk-go/utils"
13+ "github.com/sensorsdata/abtesting-sdk-go/utils/lru"
1314)
1415
1516// 用户的试验缓存
@@ -40,7 +41,7 @@ func loadExperimentFromNetwork(sensors *SensorsABTest, distinctId string, isLogi
4041 experiment := filterExperiment (requestParam , experiments )
4142 if experiment .AbtestExperimentId != "" {
4243 if isTrack {
43- trackABTestEvent (distinctId , isLoginId , experiment , sensors , nil )
44+ trackABTestEvent (distinctId , isLoginId , experiment , sensors , nil , requestParam . CustomIDs )
4445 }
4546 // 回调试验变量给客户
4647 experiment .DistinctId = distinctId
@@ -57,7 +58,7 @@ func loadExperimentFromNetwork(sensors *SensorsABTest, distinctId string, isLogi
5758
5859func loadExperimentFromCache (sensors * SensorsABTest , distinctId string , isLoginId bool , requestParam beans.RequestParam , isTrack bool ) (error , beans.Experiment ) {
5960 var experiment beans.Experiment
60- experiments , ok := loadExperimentCache (distinctId )
61+ experiments , ok := loadExperimentCache (distinctId , isLoginId , requestParam . CustomIDs )
6162 if experiments != nil || ok {
6263 experiment = filterExperiment (requestParam , experiments .([]beans.Experiment ))
6364 }
@@ -72,13 +73,13 @@ func loadExperimentFromCache(sensors *SensorsABTest, distinctId string, isLoginI
7273 }
7374
7475 // 缓存试验
75- saveExperiment2Cache (distinctId , experiments , sensors .config .ExperimentCacheTime )
76+ saveExperiment2Cache (distinctId , isLoginId , requestParam . CustomIDs , experiments , sensors .config .ExperimentCacheTime )
7677 // 筛选试验
7778 experiment = filterExperiment (requestParam , experiments )
7879 }
7980
8081 if isTrack && experiment .AbtestExperimentId != "" {
81- trackABTestEvent (distinctId , isLoginId , experiment , sensors , nil )
82+ trackABTestEvent (distinctId , isLoginId , experiment , sensors , nil , requestParam . CustomIDs )
8283 }
8384 experiment .DistinctId = distinctId
8485 experiment .IsLoginId = isLoginId
@@ -115,7 +116,7 @@ func filterExperiment(requestParam beans.RequestParam, experiments []beans.Exper
115116 }
116117}
117118
118- func trackABTestEvent (distinctId string , isLoginId bool , experiment beans.Experiment , sensors * SensorsABTest , properties map [string ]interface {}) {
119+ func trackABTestEvent (distinctId string , isLoginId bool , experiment beans.Experiment , sensors * SensorsABTest , properties map [string ]interface {}, customIDs map [ string ] interface {} ) {
119120 if sensors .config .SensorsAnalytics .C == nil {
120121 return
121122 }
@@ -126,12 +127,12 @@ func trackABTestEvent(distinctId string, isLoginId bool, experiment beans.Experi
126127 }
127128
128129 // 如果在缓存中,则不触发 $ABTestTrigger 事件
129- _ , ok := loadEventFromCache (getId ( distinctId , experiment ) )
130+ _ , ok := loadEventFromCache (distinctId , customIDs , experiment )
130131 if ok {
131132 return
132133 }
133134
134- saveEvent2Cache (distinctId , experiment , sensors )
135+ saveEvent2Cache (distinctId , customIDs , experiment , sensors )
135136 if properties == nil {
136137 properties = map [string ]interface {}{
137138 "$abtest_experiment_id" : experiment .AbtestExperimentId ,
@@ -168,41 +169,44 @@ func initCache(config beans.ABTestConfig) {
168169}
169170
170171// 从缓存读取 $ABTestTrigger
171- func loadEventFromCache (idExperiment string ) (interface {}, bool ) {
172+ func loadEventFromCache (distinctId string , customIDs map [ string ] interface {}, experiment beans. Experiment ) (interface {}, bool ) {
172173 eventsLock .Lock ()
173174 defer eventsLock .Unlock ()
174- return eventsCache .Get (idExperiment )
175+ idEvent := getEventKey (distinctId , customIDs , experiment )
176+ return eventsCache .Get (idEvent )
175177}
176178
177179// 保存 $ABTestTrigger 到缓存中
178- func saveEvent2Cache (distinctId string , experiment beans.Experiment , sensors * SensorsABTest ) {
180+ func saveEvent2Cache (distinctId string , customIDs map [ string ] interface {}, experiment beans.Experiment , sensors * SensorsABTest ) {
179181 // 缓存 $ABTestTrigger 事件
180182 if sensors .config .EnableEventCache {
181183 eventsLock .Lock ()
182184 defer eventsLock .Unlock ()
183- idExperiment := getId (distinctId , experiment )
184- eventsCache .Add (idExperiment , experiment )
185+ idEvent := getEventKey (distinctId , customIDs , experiment )
186+ eventsCache .Add (idEvent , experiment )
185187 // 进行清理缓存
186- removeCache (idExperiment , func (id string ) {
188+ removeCache (idEvent , func (id string ) {
187189 eventsCache .Remove (id )
188190 }, sensors .config .EventCacheTime )
189191 }
190192}
191193
192194// 从缓存读取试验
193- func loadExperimentCache (distinctId string ) (interface {}, bool ) {
195+ func loadExperimentCache (distinctId string , isLoginId bool , customIds map [ string ] interface {} ) (interface {}, bool ) {
194196 experimentLock .Lock ()
195197 defer experimentLock .Unlock ()
196- return experimentCache .Get (distinctId )
198+ idKey := getExperimentKey (distinctId , customIds , isLoginId )
199+ return experimentCache .Get (idKey )
197200}
198201
199202// 保存试验到缓存
200- func saveExperiment2Cache (distinctId string , experiment []beans.Experiment , timeout time.Duration ) {
203+ func saveExperiment2Cache (distinctId string , isLoginId bool , customIds map [ string ] interface {}, experiments []beans.Experiment , timeout time.Duration ) {
201204 experimentLock .Lock ()
202205 defer experimentLock .Unlock ()
203- experimentCache .Add (distinctId , experiment )
206+ idKey := getExperimentKey (distinctId , customIds , isLoginId )
207+ experimentCache .Add (idKey , experiments )
204208 // 进行清理缓存
205- removeCache (distinctId , func (id string ) {
209+ removeCache (idKey , func (id string ) {
206210 experimentCache .Remove (id )
207211 }, timeout )
208212}
@@ -238,7 +242,13 @@ func buildRequestParam(distinctId string, isLoginId bool, requestParam beans.Req
238242
239243 params ["abtest_lib_version" ] = SDK_VERSION
240244 params ["platform" ] = LIB_NAME
241- params ["properties" ] = requestParam .Properties
245+ if requestParam .Properties != nil && len (requestParam .Properties ) > 0 {
246+ params ["custom_properties" ] = requestParam .Properties
247+ }
248+ if requestParam .CustomIDs != nil && len (requestParam .CustomIDs ) > 0 {
249+ params ["custom_ids" ] = requestParam .CustomIDs
250+ }
251+
242252 return params
243253}
244254
@@ -259,6 +269,10 @@ func removeCache(idExperiment string, removeCache func(id string), timeout time.
259269}
260270
261271// 拼接缓存唯一标识
262- func getId (distinctId string , experiment beans.Experiment ) string {
263- return distinctId + "$" + experiment .AbtestExperimentId
272+ func getEventKey (distinctId string , customIds map [string ]interface {}, experiment beans.Experiment ) string {
273+ return distinctId + "$" + utils .MapToJson (customIds ) + "$" + experiment .AbtestExperimentId
274+ }
275+
276+ func getExperimentKey (distinctId string , customIds map [string ]interface {}, isLoginId bool ) string {
277+ return distinctId + "$" + utils .MapToJson (customIds ) + "$" + strconv .FormatBool (isLoginId )
264278}
0 commit comments