Skip to content

Commit f5b8ffe

Browse files
msohailhussainMichael Ng
authored andcommitted
feat(onTrack): Added onTrack callback (#198)
1 parent 1709e4c commit f5b8ffe

File tree

5 files changed

+458
-12
lines changed

5 files changed

+458
-12
lines changed

pkg/client/client.go

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,19 @@ import (
2828
"github.com/optimizely/go-sdk/pkg/entities"
2929
"github.com/optimizely/go-sdk/pkg/event"
3030
"github.com/optimizely/go-sdk/pkg/logging"
31+
"github.com/optimizely/go-sdk/pkg/notification"
3132
"github.com/optimizely/go-sdk/pkg/utils"
3233
)
3334

3435
var logger = logging.GetLogger("Client")
3536

3637
// OptimizelyClient is the entry point to the Optimizely SDK
3738
type OptimizelyClient struct {
38-
ConfigManager pkg.ProjectConfigManager
39-
DecisionService decision.Service
40-
EventProcessor event.Processor
41-
42-
executionCtx utils.ExecutionCtx
39+
ConfigManager pkg.ProjectConfigManager
40+
DecisionService decision.Service
41+
EventProcessor event.Processor
42+
notificationCenter notification.Center
43+
executionCtx utils.ExecutionCtx
4344
}
4445

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

334335
userEvent := event.CreateConversionUserEvent(projectConfig, configEvent, userContext, eventTags)
335-
o.EventProcessor.ProcessEvent(userEvent)
336+
if o.EventProcessor.ProcessEvent(userEvent) && o.notificationCenter != nil {
337+
trackNotification := notification.TrackNotification{EventKey: eventKey, UserContext: userContext, EventTags: eventTags, ConversionEvent: *userEvent.Conversion}
338+
if err = o.notificationCenter.Send(notification.Track, trackNotification); err != nil {
339+
logger.Warning("Problem with sending notification")
340+
}
341+
}
342+
336343
return nil
337344
}
338345

@@ -430,6 +437,44 @@ func (o *OptimizelyClient) getExperimentDecision(experimentKey string, userConte
430437
return decisionContext, experimentDecision, err
431438
}
432439

440+
// OnTrack registers a handler for Track notifications
441+
func (o *OptimizelyClient) OnTrack(callback func(eventKey string, userContext entities.UserContext, eventTags map[string]interface{}, conversionEvent event.ConversionEvent)) (int, error) {
442+
if o.notificationCenter == nil {
443+
return 0, fmt.Errorf("no notification center found")
444+
}
445+
446+
handler := func(payload interface{}) {
447+
success := false
448+
if trackNotification, ok := payload.(notification.TrackNotification); ok {
449+
if conversionEvent, ok := trackNotification.ConversionEvent.(event.ConversionEvent); ok {
450+
success = true
451+
callback(trackNotification.EventKey, trackNotification.UserContext, trackNotification.EventTags, conversionEvent)
452+
}
453+
}
454+
if !success {
455+
logger.Warning(fmt.Sprintf("Unable to convert notification payload %v into TrackNotification", payload))
456+
}
457+
}
458+
id, err := o.notificationCenter.AddHandler(notification.Track, handler)
459+
if err != nil {
460+
logger.Warning("Problem with adding notification handler")
461+
return 0, err
462+
}
463+
return id, nil
464+
}
465+
466+
// RemoveOnTrack removes handler for Track notification with given id
467+
func (o *OptimizelyClient) RemoveOnTrack(id int) error {
468+
if o.notificationCenter == nil {
469+
return fmt.Errorf("no notification center found")
470+
}
471+
if err := o.notificationCenter.RemoveHandler(id, notification.Track); err != nil {
472+
logger.Warning("Problem with removing notification handler")
473+
return err
474+
}
475+
return nil
476+
}
477+
433478
// GetProjectConfig returns the current ProjectConfig or nil if the instance is not valid.
434479
func (o *OptimizelyClient) GetProjectConfig() (projectConfig pkg.ProjectConfig, err error) {
435480

0 commit comments

Comments
 (0)