Skip to content
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 51 additions & 6 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,19 @@ import (
"github.com/optimizely/go-sdk/pkg/entities"
"github.com/optimizely/go-sdk/pkg/event"
"github.com/optimizely/go-sdk/pkg/logging"
"github.com/optimizely/go-sdk/pkg/notification"
"github.com/optimizely/go-sdk/pkg/utils"
)

var logger = logging.GetLogger("Client")

// OptimizelyClient is the entry point to the Optimizely SDK
type OptimizelyClient struct {
ConfigManager pkg.ProjectConfigManager
DecisionService decision.Service
EventProcessor event.Processor

executionCtx utils.ExecutionCtx
ConfigManager pkg.ProjectConfigManager
DecisionService decision.Service
EventProcessor event.Processor
NotificationCenter notification.Center
executionCtx utils.ExecutionCtx
}

// Activate returns the key of the variation the user is bucketed into and queues up an impression event to be sent to
Expand Down Expand Up @@ -332,7 +333,13 @@ func (o *OptimizelyClient) Track(eventKey string, userContext entities.UserConte
}

userEvent := event.CreateConversionUserEvent(projectConfig, configEvent, userContext, eventTags)
o.EventProcessor.ProcessEvent(userEvent)
if o.EventProcessor.ProcessEvent(userEvent) && o.NotificationCenter != nil {
trackNotification := notification.TrackNotification{EventKey: eventKey, UserContext: userContext, EventTags: eventTags, ConversionEvent: *userEvent.Conversion}
if err = o.NotificationCenter.Send(notification.Track, trackNotification); err != nil {
logger.Warning("Problem with sending notification")
}
}

return nil
}

Expand Down Expand Up @@ -430,6 +437,44 @@ func (o *OptimizelyClient) getExperimentDecision(experimentKey string, userConte
return decisionContext, experimentDecision, err
}

// OnTrack registers a handler for Track notifications
func (o *OptimizelyClient) OnTrack(callback func(eventKey string, userContext entities.UserContext, eventTags map[string]interface{}, conversionEvent event.ConversionEvent)) (int, error) {
if o.NotificationCenter == nil {
return 0, fmt.Errorf("no notification center found")
}

handler := func(payload interface{}) {
success := false
if trackNotification, ok := payload.(notification.TrackNotification); ok {
if conversionEvent, ok := trackNotification.ConversionEvent.(event.ConversionEvent); ok {
success = true
callback(trackNotification.EventKey, trackNotification.UserContext, trackNotification.EventTags, conversionEvent)
}
}
if !success {
logger.Warning(fmt.Sprintf("Unable to convert notification payload %v into TrackNotification", payload))
}
}
id, err := o.NotificationCenter.AddHandler(notification.Track, handler)
if err != nil {
logger.Warning("Problem with adding notification handler")
return 0, err
}
return id, nil
}

// RemoveOnTrack removes handler for Track notification with given id
func (o *OptimizelyClient) RemoveOnTrack(id int) error {
if o.NotificationCenter == nil {
return fmt.Errorf("no notification center found")
}
if err := o.NotificationCenter.RemoveHandler(id, notification.Track); err != nil {
logger.Warning("Problem with removing notification handler")
return err
}
return nil
}

// GetProjectConfig returns the current ProjectConfig or nil if the instance is not valid.
func (o *OptimizelyClient) GetProjectConfig() (projectConfig pkg.ProjectConfig, err error) {

Expand Down
Loading