Skip to content

Commit

Permalink
Merge b3a34ed into 1709e4c
Browse files Browse the repository at this point in the history
  • Loading branch information
msohailhussain committed Dec 6, 2019
2 parents 1709e4c + b3a34ed commit 18e8248
Show file tree
Hide file tree
Showing 5 changed files with 458 additions and 12 deletions.
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

0 comments on commit 18e8248

Please sign in to comment.