Under the Products section, click BytePlus Data Intelligence.
On the Project List page, for the project you want to integrate the SDK with, under the Actions column, click Details.
On the Social Media Details pop-up window, copy the App Key.
Install the SDK
go get github.com/volcengine/datatester-go-sdk@v1.0.4
Using the SDK
The following is a code example of using the Go SDK.
package main
import (
"github.com/volcengine/datatester-go-sdk/client"
"github.com/volcengine/datatester-go-sdk/config"
)
func main() {
// Find your app key by clicking "details" on the project list page on the BytePlus console
abClient := client.NewClient("${app_key}")
/*
client.NewClient("2b47a1f318d78fd718548153901addde",
config.WithMetaHost(config.MetaHostSG), // Change it to "https://datarangers.com"
config.WithTrackHost(config.TrackHostSG), // Change it to "https://mcs.tobsnssdk.com"
config.WithWorkerNumOnce(20)) // The number of worker threads. 20 by default. Only supports one-time configuration
config.WithFetchInterval(60 * time.Second), // meta update interval, N * time.Second is recommended
config.WithAnonymousConfig(true, true), // anonymous user event reporting configuration(enable/disable, saas/onpremise)
config.WithLogger(log.NewLogrusAdapt(logrus.New()))) // customize the log interface
*/
// attributes: User properties. Only used for allocating the traffic
attributes := map[string]interface{}{
}
// decisionId: The local user identifier. Not used for event tracking. You need to replace it with the actual user ID
// trackId(uuid): The user identifier for event tracking. You need to replace it with the actual user ID
value, err := abClient.Activate("${experiment_key}", "decisionId", "trackId", true, attributes)
if err != nil {
return
}
if value.(bool) {
} else {
}
}
API reference
Interfaces with WithImpression automatically track events. Meanwhile, make sure you fill in the trackId field in the activate interface if you want to track events.
AbClient
A class for traffic allocation during initialization.
Ensure that incoming users’ information about version ID remains unchanged. If you want to constantly store the information of incoming users, you must implement it by yourself
SetCohortHandler
Set a cohort handler used to obtain experiment/feature cohort ids
An array object of detailed information about a feature’s variant that a user joins. “nil” when a user/device is not in this feature or the feature is disabled. The following is an example:
The detailed information about a feature’s variant that a user joins. “nil” when a user/device is not in this feature or the feature is disabled. The following is an example:
In order to better use the sdk, some suggestions are provided.
UserAbInfoHandler
Maintain user historical decision information; If you need to use the function of ‘freezing experiment‘ or ‘Traffic changes will not affect exposed users‘, you can customize the implementation class processing, and use it in when instantiating AbClient(NewClientWithUserAbInfo).
Suggestion
It is recommended to use redis to cache decision information.
The following is an example:
client.NewClientWithUserAbInfo("appKey", NewRedisUserAbInfoHandler())
type RedisAbInfoHandler struct{}
func (u *RedisAbInfoHandler) Query(decisionID string) string {
// need to implement it yourself
return redis.get(decisionID);
}
func (u *RedisAbInfoHandler) CreateOrUpdate(decisionID, experiment2Variant string) bool {
// need to implement it yourself
return redis.set(decisionID, experiment2Variant);
}
func (u *RedisAbInfoHandler) NeedPersistData() bool {
// return true if customize this interface
return true
}
func NewRedisUserAbInfoHandler() *RedisAbInfoHandler {
return &RedisAbInfoHandler{}
}
Cohort Handler
Handler to obtain cohort ids if your experiment / feature uses cohorts as traffic filter. You can either use the built-in cohort handler listed below, or implement your own handler
DataTester GO SDK
Limitation
Prerequisite
Obtain your project’s App Key:
Install the SDK
Using the SDK
The following is a code example of using the Go SDK.
API reference
AbClient
A class for traffic allocation during initialization.
Parameter description
NewClientWithUserAbInfo
A class for traffic allocation during initialization.
Parameter description
SetCohortHandler
Set a cohort handler used to obtain experiment/feature cohort ids
Parameter description
Activate
Obtain a specific experiment version’s configuration after traffic allocation and automatically track the exposed events.
Parameter description
Returned value
Parameters of the version ID that a user enters or the default value when a user/device is not in this version.
getExperimentVariantName
Obtain the version’s name of an experiment that a user enters.
Parameter description
Returned value
Name of the version ID that a user enters or “” when a user/device is not in this version.
getExperimentConfigs
Obtain detailed information about the version that a user enters.
Parameter description
Returned value
The detailed information about the version ID that a user enters or “nil” when a user/device is not in this version. The following is an example:
getAllExperimentConfigs
Obtain detailed information about all the versions in all experiments.
Parameter description
Returned value
The detailed information about version IDs that a user enters or “nil” when a user/device is not in this version. The following is an example:
getFeatureConfigs
Obtain detailed information about a feature that a user joins.
Parameter description
Returned value
An array object of detailed information about a feature’s variant that a user joins. “nil” when a user/device is not in this feature or the feature is disabled. The following is an example:
getAllFeatureConfigs
Obtain detailed information about all features that a user joins.
Parameter description
Returned value
The detailed information about all feature variants that a user joins. The following is an example:
getExperimentVariantNameWithImpression
Obtain the version’s name of the experiment that a user enters.
Parameter description
Returned value
Name of the version ID that a user enters or “” when a user/device is not in this version.
getExperimentConfigsWithImpression
Obtain detailed information about the version that a user enters.
Parameter description
Returned value
The detailed information about the version ID that a user enters or “nil” when a user/device is not in this version. The following is an example:
getAllExperimentConfigsWithImpression
Obtain detailed information about all the versions in all experiments.
Parameter description
Returned value
The detailed information about version IDs that a user enters or “nil” when a user/device is not in this version. The following is an example:
getFeatureConfigsWithImpression
Obtain detailed information about a feature that a user joins.
Parameter description
Returned value
The detailed information about a feature’s variant that a user joins. “nil” when a user/device is not in this feature or the feature is disabled. The following is an example:
Others
In order to better use the sdk, some suggestions are provided.
UserAbInfoHandler
Suggestion
The following is an example:
Cohort Handler
for more detail in cdp api, see https://github.com/volcengine/cdp-openapi-sdk-go
you can customize your own cohort handler by implementing
cohort.Clientinterfacehere shows a simple example, obtain cohort from redis
Anonymously tracking
DataTester GO SDK
版本需求
准备工作
获取应用的App Key(即SDK使用的token):
安装SDK
代码示例
接口描述
AbClient
初始化ABTest分流类
参数
NewClientWithUserAbInfo
初始化ABTest分流类,传入自定义的userAbInfoHandler
参数
SetCohortHandler
设置用户分群handler
参数
Activate
获取特定key的分流结果,并上报曝光事件
参数
返回值
该函数返回命中版本的参数值,未命中时返回默认值
getExperimentVariantName
获取用户命中的特定实验的变体名称
参数
返回值
该函数返回用户命中的特定实验的变体名称
getExperimentConfigs
获取用户命中的特定实验的变体详情
参数
返回值
该函数返回用户命中某个实验的变体详情,通常仅能命中一个变体
getAllExperimentConfigs
获取用户命中的所有实验的变体详情
参数
返回值
该函数返回用户命中所有实验的变体详情,通常命中多个变体
getFeatureConfigs
获取用户命中的特定feature的变体详情
参数
返回值
该函数返回用户命中某个feature的变体详情,通常仅能命中一个变体
getAllFeatureConfigs
获取用户命中的所有feature的变体详情
参数
返回值
该函数返回用户命中所有feature的变体详情,通常命中多个变体
getExperimentVariantNameWithImpression
同接口“getExperimentVariantName”
getExperimentConfigsWithImpression
同接口“getExperimentConfigs”
getAllExperimentConfigsWithImpression
同接口“getAllExperimentConfigs”
getFeatureConfigsWithImpression
同接口“getFeatureConfigs”
其他
UserAbInfoHandler
用户信息处理接口,冻结实验、进组不出组场景下使用
使用Redis缓存示例(仅供参考)
Cohort Handler
cdp api的使用可以参考 https://github.com/volcengine/cdp-openapi-sdk-go
可以通过实现
cohort.Client接口来自定义cohort handler的实现通过redis获取分群的示例(仅供参考)
匿名上报