@@ -44,6 +44,7 @@ open class OptimizelyManager: NSObject {
44
44
}
45
45
46
46
private let reInitLock = Dispatch . DispatchSemaphore ( value: 1 )
47
+ private let eventImitterQueue = DispatchQueue ( label: " OptimizelyEventImitterQueue " )
47
48
48
49
// MARK: - Public interfaces
49
50
@@ -238,30 +239,11 @@ open class OptimizelyManager: NSObject {
238
239
239
240
let variation = try getVariation ( experimentKey: experimentKey, userId: userId, attributes: attributes)
240
241
241
- // TODO: fix to throw errors
242
- guard let body = BatchEventBuilder . createImpressionEvent ( config: config,
243
- experiment: experiment,
244
- varionation: variation,
245
- userId: userId,
246
- attributes: attributes) else
247
- {
248
- throw OptimizelyError . eventBuildFailure ( DispatchEvent . activateEventKey)
249
- }
250
-
251
- let event = EventForDispatch ( body: body)
252
- // because we are batching events, we cannot guarantee that the completion handler will be
253
- // called. So, for now, we are queuing and calling onActivate. Maybe we should mention that
254
- // onActivate only means the event has been queued and not necessarily sent.
255
- eventDispatcher. dispatchEvent ( event: event) { result in
256
- switch result {
257
- case . failure:
258
- break
259
- case . success( _) :
260
- break
261
- }
262
- }
242
+ sendImpressionEvent ( experiment: experiment,
243
+ variation: variation,
244
+ userId: userId,
245
+ attributes: attributes)
263
246
264
- self . notificationCenter. sendNotifications ( type: NotificationType . Activate. rawValue, args: [ experiment, userId, attributes, variation, [ " url " : event. url as Any , " body " : event. body as Any ] ] )
265
247
266
248
return variation. key
267
249
}
@@ -412,31 +394,8 @@ open class OptimizelyManager: NSObject {
412
394
sourceInfo [ Constants . ExperimentDecisionInfoKeys. experiment] = experiment. key
413
395
sourceInfo [ Constants . ExperimentDecisionInfoKeys. variation] = variation. key
414
396
decisionInfo [ Constants . DecisionInfoKeys. sourceInfo] = sourceInfo
415
-
416
- // TODO: fix to throw errors
417
- guard let body = BatchEventBuilder . createImpressionEvent ( config: config,
418
- experiment: experiment,
419
- varionation: variation,
420
- userId: userId,
421
- attributes: attributes) else
422
- {
423
- throw OptimizelyError . eventBuildFailure ( DispatchEvent . activateEventKey)
424
- }
425
-
426
- let event = EventForDispatch ( body: body)
427
-
428
- // because we are batching events, we cannot guarantee that the completion handler will be
429
- // called. So, for now, we are queuing and calling onActivate. Maybe we should mention that
430
- // onActivate only means the event has been queued and not necessarily sent.
431
- eventDispatcher. dispatchEvent ( event: event) { result in
432
- switch result {
433
- case . failure:
434
- break
435
- case . success( _) :
436
- break
437
- }
438
- }
439
- self . notificationCenter. sendNotifications ( type: NotificationType . Activate. rawValue, args: [ experiment, userId, attributes, variation, [ " url " : event. url as Any , " body " : event. body as Any ] ] )
397
+
398
+ sendImpressionEvent ( experiment: experiment, variation: variation, userId: userId, attributes: attributes)
440
399
}
441
400
442
401
decisionInfo [ Constants . DecisionInfoKeys. featureEnabled] = featureEnabled
@@ -664,32 +623,86 @@ open class OptimizelyManager: NSObject {
664
623
throw OptimizelyError . eventKeyInvalid ( eventKey)
665
624
}
666
625
667
- // TODO: fix to throw errors
668
- guard let body = BatchEventBuilder . createConversionEvent ( config: config,
669
- eventKey: eventKey,
670
- userId: userId,
671
- attributes: attributes,
672
- eventTags: eventTags) else
673
- {
674
- throw OptimizelyError . eventBuildFailure ( eventKey)
626
+ sendConversionEvent ( eventKey: eventKey, userId: userId, attributes: attributes, eventTags: eventTags)
627
+ }
628
+
629
+ }
630
+
631
+ extension OptimizelyManager {
632
+
633
+ func sendImpressionEvent( experiment: Experiment ,
634
+ variation: Variation ,
635
+ userId: String ,
636
+ attributes: OptimizelyAttributes ? = nil ) {
637
+
638
+ eventImitterQueue. async {
639
+ guard let config = self . config else { return }
640
+
641
+ guard let body = BatchEventBuilder . createImpressionEvent ( config: config,
642
+ experiment: experiment,
643
+ varionation: variation,
644
+ userId: userId,
645
+ attributes: attributes) else
646
+ {
647
+ self . logger. e ( OptimizelyError . eventBuildFailure ( DispatchEvent . activateEventKey) )
648
+ return
649
+ }
650
+
651
+ let event = EventForDispatch ( body: body)
652
+ // because we are batching events, we cannot guarantee that the completion handler will be
653
+ // called. So, for now, we are queuing and calling onActivate. Maybe we should mention that
654
+ // onActivate only means the event has been queued and not necessarily sent.
655
+ self . eventDispatcher. dispatchEvent ( event: event) { result in
656
+ switch result {
657
+ case . failure:
658
+ break
659
+ case . success( _) :
660
+ break
661
+ }
662
+ }
663
+
664
+ self . notificationCenter. sendNotifications ( type: NotificationType . Activate. rawValue, args: [ experiment, userId, attributes, variation, [ " url " : event. url as Any , " body " : event. body as Any ] ] )
665
+
675
666
}
667
+
668
+ }
669
+
670
+ func sendConversionEvent( eventKey: String ,
671
+ userId: String ,
672
+ attributes: OptimizelyAttributes ? = nil ,
673
+ eventTags: OptimizelyEventTags ? = nil ) {
676
674
677
- let event = EventForDispatch ( body: body)
678
- // because we are batching events, we cannot guarantee that the completion handler will be
679
- // called. So, for now, we are queuing and calling onTrack. Maybe we should mention that
680
- // onTrack only means the event has been queued and not necessarily sent.
681
- eventDispatcher. dispatchEvent ( event: event) { result in
682
- switch result {
683
- case . failure:
684
- break
685
- case . success( _) :
686
- break
675
+
676
+ eventImitterQueue. async {
677
+ guard let config = self . config else { return }
678
+
679
+ guard let body = BatchEventBuilder . createConversionEvent ( config: config,
680
+ eventKey: eventKey,
681
+ userId: userId,
682
+ attributes: attributes,
683
+ eventTags: eventTags) else
684
+ {
685
+ self . logger. e ( OptimizelyError . eventBuildFailure ( eventKey) )
686
+ return
687
+ }
688
+
689
+ let event = EventForDispatch ( body: body)
690
+ // because we are batching events, we cannot guarantee that the completion handler will be
691
+ // called. So, for now, we are queuing and calling onTrack. Maybe we should mention that
692
+ // onTrack only means the event has been queued and not necessarily sent.
693
+ self . eventDispatcher. dispatchEvent ( event: event) { result in
694
+ switch result {
695
+ case . failure:
696
+ break
697
+ case . success( _) :
698
+ break
699
+ }
687
700
}
701
+ self . notificationCenter. sendNotifications ( type: NotificationType . Track. rawValue, args: [ eventKey, userId, attributes, eventTags, [ " url " : event. url as Any , " body " : event. body as Any ] ] )
702
+
688
703
}
689
- self . notificationCenter. sendNotifications ( type: NotificationType . Track. rawValue, args: [ eventKey, userId, attributes, eventTags, [ " url " : event. url as Any , " body " : event. body as Any ] ] )
690
-
704
+
691
705
}
692
-
693
706
}
694
707
695
708
extension OptimizelyManager {
0 commit comments