diff --git a/Sources/ODP/OdpManager.swift b/Sources/ODP/OdpManager.swift index 40ba6ad3..a69242b2 100644 --- a/Sources/ODP/OdpManager.swift +++ b/Sources/ODP/OdpManager.swift @@ -57,7 +57,8 @@ public class OdpManager { cacheTimeoutInSecs: cacheTimeoutInSecs, resourceTimeoutInSecs: timeoutForSegmentFetchInSecs) self.eventManager = eventManager ?? OdpEventManager(sdkKey: sdkKey, - resourceTimeoutInSecs: timeoutForEventDispatchInSecs) + resourceTimeoutInSecs: timeoutForEventDispatchInSecs) + self.odpConfig = OdpConfig() self.segmentManager.odpConfig = odpConfig self.eventManager.odpConfig = odpConfig @@ -80,6 +81,12 @@ public class OdpManager { completionHandler: completionHandler) } + func sendInitializedEvent(vuid: String) throws { + guard enabled else { throw OptimizelyError.odpNotEnabled } + guard odpConfig.eventQueueingAllowed else { throw OptimizelyError.odpNotIntegrated } + eventManager.sendInitializedEvent(vuid: vuid) + } + func identifyUser(userId: String) { guard enabled else { logger.d("ODP identify event is not dispatched (ODP disabled).") diff --git a/Sources/Optimizely+Decide/OptimizelyClient+Decide.swift b/Sources/Optimizely+Decide/OptimizelyClient+Decide.swift index c49d8e40..eda0d38a 100644 --- a/Sources/Optimizely+Decide/OptimizelyClient+Decide.swift +++ b/Sources/Optimizely+Decide/OptimizelyClient+Decide.swift @@ -36,7 +36,7 @@ extension OptimizelyClient { /// - Parameter attributes: A map of attribute names to current user attribute values. /// - Returns: An OptimizelyUserContext associated with this OptimizelyClient public func createUserContext(attributes: [String: Any]? = nil) -> OptimizelyUserContext? { - guard enableVuid, let vuid = self.vuid else { + guard let vuid = self.vuid else { logger.e("Vuid is not enabled or invalid VUID. User context not created.") return nil } diff --git a/Sources/Optimizely/OptimizelyClient.swift b/Sources/Optimizely/OptimizelyClient.swift index 49953611..b99c9393 100644 --- a/Sources/Optimizely/OptimizelyClient.swift +++ b/Sources/Optimizely/OptimizelyClient.swift @@ -60,7 +60,7 @@ open class OptimizelyClient: NSObject { var decisionService: OPTDecisionService! public var notificationCenter: OPTNotificationCenter? public var odpManager: OdpManager! - private var vuidManager: VuidManager! + public var vuid: String? let sdkSettings: OptimizelySdkSettings // MARK: - Public interfaces @@ -90,17 +90,19 @@ open class OptimizelyClient: NSObject { self.sdkKey = sdkKey self.sdkSettings = settings ?? OptimizelySdkSettings() self.defaultDecideOptions = defaultDecideOptions ?? [] - + super.init() - self.vuidManager = VuidManager.shared - self.vuidManager.configure(enable: self.sdkSettings.enableVuid) + VuidManager.shared.configure(enable: self.sdkSettings.enableVuid) + if self.sdkSettings.enableVuid { + self.vuid = VuidManager.shared.vuid + } + self.odpManager = odpManager ?? OdpManager(sdkKey: sdkKey, disable: sdkSettings.disableOdp, cacheSize: sdkSettings.segmentsCacheSize, cacheTimeoutInSecs: sdkSettings.segmentsCacheTimeoutInSecs, timeoutForSegmentFetchInSecs: sdkSettings.timeoutForSegmentFetchInSecs, timeoutForEventDispatchInSecs: sdkSettings.timeoutForOdpEventInSecs) - self.odpManager.vuid = vuidManager.vuid let userProfileService = userProfileService ?? DefaultUserProfileService() let logger = logger ?? DefaultLogger() type(of: logger).logLevel = defaultLogLevel ?? .info @@ -117,17 +119,10 @@ open class OptimizelyClient: NSObject { self.datafileHandler = HandlerRegistryService.shared.injectDatafileHandler(sdkKey: self.sdkKey) self.decisionService = HandlerRegistryService.shared.injectDecisionService(sdkKey: self.sdkKey) self.notificationCenter = HandlerRegistryService.shared.injectNotificationCenter(sdkKey: self.sdkKey) - - if let _vuid = self.vuidManager.vuid { - try? sendOdpEvent(type: Constants.ODP.eventType, - action: "client_initialized", - identifiers: [ - Constants.ODP.keyForVuid: _vuid - ], - data: [:]) + if let _vuid = vuid { + self.odpManager.vuid = _vuid + sendInitializedEvent(vuid: _vuid) } - - logger.d("SDK Version: \(version)") } @@ -983,13 +978,8 @@ extension OptimizelyClient { data: data) } - /// the device vuid (read only) - public var vuid: String? { - return self.vuidManager.vuid - } - - public var enableVuid: Bool { - return self.vuidManager.enable + func sendInitializedEvent(vuid: String) { + try? odpManager.sendInitializedEvent(vuid: vuid) } func identifyUserToOdp(userId: String) { diff --git a/Sources/Optimizely/VuidManager.swift b/Sources/Optimizely/VuidManager.swift index 6ffcb45a..47c22348 100644 --- a/Sources/Optimizely/VuidManager.swift +++ b/Sources/Optimizely/VuidManager.swift @@ -20,7 +20,6 @@ public class VuidManager { private var _vuid: String = "" private(set) var enable: Bool = false let logger = OPTLoggerFactory.getLogger() - // a single vuid should be shared for all SDK instances public static let shared = VuidManager() diff --git a/Tests/OptimizelyTests-APIs/OptimizelyClientTests_ODP.swift b/Tests/OptimizelyTests-APIs/OptimizelyClientTests_ODP.swift index 22600e79..882aa717 100644 --- a/Tests/OptimizelyTests-APIs/OptimizelyClientTests_ODP.swift +++ b/Tests/OptimizelyTests-APIs/OptimizelyClientTests_ODP.swift @@ -190,14 +190,28 @@ class OptimizelyClientTests_ODP: XCTestCase { func testVuidEnabled() { let settings = OptimizelySdkSettings(enableVuid: true) optimizely = OptimizelyClient(sdkKey: OTUtils.randomSdkKey, settings: settings) - XCTAssertTrue(optimizely.enableVuid) + XCTAssertNotNil(optimizely.vuid) XCTAssert(optimizely.vuid!.starts(with: "vuid_")) } - func testVuidDiabled() { - // Default client vuid diabled - XCTAssertFalse(optimizely.enableVuid) - XCTAssertNil(optimizely.vuid) + func test_register_vuid_autometically_when_enabled() { + let settings = OptimizelySdkSettings(enableVuid: true) + optimizely = OptimizelyClient(sdkKey: OTUtils.randomSdkKey, settings: settings) + let eventManager = optimizely.odpManager.eventManager! + let evt = eventManager.eventQueue.getFirstItem()! + + XCTAssertEqual("fullstack", evt.type) + XCTAssertEqual("client_initialized", evt.action) + XCTAssertEqual(["vuid": optimizely.vuid], evt.identifiers) + XCTAssertNotNil(optimizely.vuid) + } + + func test_vuid_does_not_register_autometically_when_enabled_but_odp_disabled() { + let settings = OptimizelySdkSettings(disableOdp: true, enableVuid: true) + optimizely = OptimizelyClient(sdkKey: OTUtils.randomSdkKey, settings: settings) + XCTAssertNotNil(optimizely.vuid) + let eventManager = optimizely.odpManager.eventManager + XCTAssertNil(eventManager) } // MARK: - OdpConfig Update