@@ -28,18 +28,19 @@ import (
28
28
"github.com/optimizely/go-sdk/pkg/entities"
29
29
"github.com/optimizely/go-sdk/pkg/event"
30
30
"github.com/optimizely/go-sdk/pkg/logging"
31
+ "github.com/optimizely/go-sdk/pkg/notification"
31
32
"github.com/optimizely/go-sdk/pkg/utils"
32
33
)
33
34
34
35
var logger = logging .GetLogger ("Client" )
35
36
36
37
// OptimizelyClient is the entry point to the Optimizely SDK
37
38
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
43
44
}
44
45
45
46
// 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
332
333
}
333
334
334
335
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
+
336
343
return nil
337
344
}
338
345
@@ -430,6 +437,44 @@ func (o *OptimizelyClient) getExperimentDecision(experimentKey string, userConte
430
437
return decisionContext , experimentDecision , err
431
438
}
432
439
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
+
433
478
// GetProjectConfig returns the current ProjectConfig or nil if the instance is not valid.
434
479
func (o * OptimizelyClient ) GetProjectConfig () (projectConfig pkg.ProjectConfig , err error ) {
435
480
0 commit comments