设置通知侦听器
May 11, 2023About 1 min
设置通知侦听器
本主题介绍如何设置和删除云眼特性标帜(Feature Flag)AB实验 Go SDK 的通知侦听器。
通知侦听器触发您在 SDK 中触发某些操作时定义的回调函数。
最常见的用例是将所有特性标帜(Feature Flag)决策的流发送到分析提供商或内部数据仓库,以将其与您拥有的有关用户的其他数据联接。
通知侦听器类型
有关通知侦听器类型和用例的更多信息,请参阅通知侦听器。
有关代码示例,请参阅以下部分。
设置每种类型的通知侦听器
下面的示例代码演示如何设置和删除每种类型的通知侦听器。
Go
// Create default Client
eyeofcloudFactory := &client.EyeofcloudFactory{
SDKKey: "[SDK_KEY_HERE]",
}
eyeofcloudClient, _ := eyeofcloudFactory.Client()
// SET UP DECISION NOTIFICATION LISTENER
onDecision := func(notification notification.DecisionNotification) {
// Add a DECISION Notification Listener for type FLAG
if string(notification.Type) == "flag" {
// Access information about feature, for example, key and enabled status
fmt.Print(notification.DecisionInfo["flagKey"])
fmt.Print(notification.DecisionInfo["enabled"])
fmt.Print(notification.DecisionInfo["decisionEventDispatched"])
}
}
notificationID, _ := eyeofcloudClient.DecisionService.OnDecision(onDecision)
// REMOVE DECISION NOTIFICATION LISTENER
eyeofcloudClient.DecisionService.RemoveOnDecision(notificationID)
// SET UP LOG EVENT NOTIFICATION LISTENER
onLogEvent := func(eventNotification event.LogEvent) {
// process the logEvent object here (send to analytics provider, audit/inspect data)
}
notificationID, _ = eyeofcloudClient.EventProcessor.OnEventDispatch(onLogEvent)
// REMOVE LOG EVENT NOTIFICATION LISTENER
eyeofcloudClient.EventProcessor.RemoveOnEventDispatch(notificationID)
// SET UP EYEOFCLOUD CONFIG NOTIFICATION LISTENER
// listen to EYEOFCLOUD_CONFIG_UPDATE to get updated data
onConfigUpdate := func(notification notification.ProjectConfigUpdateNotification) {
}
notificationID, _ = eyeofcloudClient.ConfigManager.OnProjectConfigUpdate(onConfigUpdate)
// REMOVE EYEOFCLOUD CONFIG NOTIFICATION LISTENER
eyeofcloudClient.ConfigManager.RemoveOnProjectConfigUpdate(notificationID)
// SET UP TRACK LISTENER
onTrack := func(
eventKey string, userContext entities.UserContext, eventTags map[string]interface{}, conversionEvent event.ConversionEvent
)
{
// process the event here (send to analytics provider, audit/inspect data)
}
notificationID, _ = eyeofcloudClient.OnTrack(onTrack)
// REMOVE TRACK LISTENER eyeofcloudClient.RemoveOnTrack(notificationID)