diff --git a/Sources/Segment/Events.swift b/Sources/Segment/Events.swift index f8e8664b..426be8a4 100644 --- a/Sources/Segment/Events.swift +++ b/Sources/Segment/Events.swift @@ -188,7 +188,9 @@ extension Analytics { /// - category: A category to the type of screen if it applies. /// - properties: Any extra metadata associated with the screen. e.g. method of access, size, etc. public func screen(title: String, category: String? = nil, properties: [String: Any]? = nil) { + // if properties is nil, this is the event that'll get used. var event = ScreenEvent(title: title, category: category, properties: nil) + // if we have properties, get a new one rolling. if let properties = properties { do { let jsonProperties = try JSON(properties) diff --git a/Sources/Segment/ObjC/ObjCAnalytics.swift b/Sources/Segment/ObjC/ObjCAnalytics.swift index 7285724b..9db0b065 100644 --- a/Sources/Segment/ObjC/ObjCAnalytics.swift +++ b/Sources/Segment/ObjC/ObjCAnalytics.swift @@ -41,7 +41,7 @@ extension ObjCAnalytics { @objc(track:properties:) public func track(name: String, properties: [String: Any]?) { - analytics.track(name: name, properties: properties as? [String: Codable]) + analytics.track(name: name, properties: properties) } /// Associate a user with their unique ID and record traits about them. @@ -67,7 +67,7 @@ extension ObjCAnalytics { if let userId = userId { // at first glance this looks like recursion. It's actually calling // into the swift version of this call where userId is NOT optional. - analytics.identify(userId: userId, traits: codable(traits)) + analytics.identify(userId: userId, traits: traits) } else if let traits = try? JSON(traits as Any) { analytics.store.dispatch(action: UserInfo.SetTraitsAction(traits: traits)) let userInfo: UserInfo? = analytics.store.currentState() @@ -100,7 +100,7 @@ extension ObjCAnalytics { /// - properties: Any extra metadata associated with the screen. e.g. method of access, size, etc. @objc(screen:category:properties:) public func screen(title: String, category: String?, properties: [String: Any]?) { - analytics.screen(title: title, category: category, properties: codable(properties)) + analytics.screen(title: title, category: category, properties: properties) } /// Associate a user with a group such as a company, organization, project, etc. @@ -117,7 +117,7 @@ extension ObjCAnalytics { /// - traits: Traits of the group you may be interested in such as email, phone or name. @objc(group:traits:) public func group(groupId: String, traits: [String: Any]?) { - analytics.group(groupId: groupId, traits: codable(traits)) + analytics.group(groupId: groupId, traits: traits) } @objc(alias:) diff --git a/Sources/Segment/Utilities/Utils.swift b/Sources/Segment/Utilities/Utils.swift index 7cd3bbab..3e8d0387 100644 --- a/Sources/Segment/Utilities/Utils.swift +++ b/Sources/Segment/Utilities/Utils.swift @@ -58,6 +58,3 @@ extension Optional: Flattenable { } } -internal func codable(_ dict: [String: Any]?) -> [String: Codable]? { - return dict as? [String: Codable] -} diff --git a/Tests/Segment-Tests/ObjC_Tests.swift b/Tests/Segment-Tests/ObjC_Tests.swift index a7689580..55d232c0 100644 --- a/Tests/Segment-Tests/ObjC_Tests.swift +++ b/Tests/Segment-Tests/ObjC_Tests.swift @@ -130,6 +130,69 @@ class ObjC_Tests: XCTestCase { XCTAssertTrue(lastEvent is IdentifyEvent) XCTAssertTrue((lastEvent as! IdentifyEvent).userId == "batman") } + + func testObjCDictionaryPassThru() { + Storage.hardSettingsReset(writeKey: "WRITE_KEY2") + + let config = ObjCConfiguration(writeKey: "WRITE_KEY2") + let analytics = ObjCAnalytics(configuration: config) + analytics.analytics.storage.hardReset(doYouKnowHowToUseThis: true) + + analytics.reset() + + let outputReader = OutputReaderPlugin() + analytics.analytics.add(plugin: outputReader) + + waitUntilStarted(analytics: analytics.analytics) + + let dict = [ + "ancientAliens": [ + "guy1": "hair guy", + "guy2": "old mi5 guy", + "guy3": "old bald guy", + "guy4": 4] as [String : Any], + "channel": "hIsToRy cHaNnEL"] as [String : Any] + + analytics.track(name: "test", properties: dict) + RunLoop.main.run(until: Date.distantPast) + let trackEvent = outputReader.lastEvent as? TrackEvent + let props = trackEvent?.properties?.dictionaryValue + XCTAssertNotNil(trackEvent) + XCTAssertTrue(props?.count == 2) + XCTAssertTrue((props?["ancientAliens"] as? [String: Any])?.count == 4) + + analytics.identify(userId: "test", traits: dict) + RunLoop.main.run(until: Date.distantPast) + let identifyEvent = outputReader.lastEvent as? IdentifyEvent + let traits = identifyEvent?.traits?.dictionaryValue + XCTAssertNotNil(identifyEvent) + XCTAssertTrue(traits?.count == 2) + XCTAssertTrue((traits?["ancientAliens"] as? [String: Any])?.count == 4) + + analytics.identify(userId: nil, traits: dict) + RunLoop.main.run(until: Date.distantPast) + let identifyEvent2 = outputReader.lastEvent as? IdentifyEvent + let traits2 = identifyEvent2?.traits?.dictionaryValue + XCTAssertNotNil(identifyEvent2) + XCTAssertTrue(traits2?.count == 2) + XCTAssertTrue((traits2?["ancientAliens"] as? [String: Any])?.count == 4) + + analytics.screen(title: "blah", category: nil, properties: dict) + RunLoop.main.run(until: Date.distantPast) + let screenEvent = outputReader.lastEvent as? ScreenEvent + let props2 = screenEvent?.properties?.dictionaryValue + XCTAssertNotNil(screenEvent) + XCTAssertTrue(props2?.count == 2) + XCTAssertTrue((props2?["ancientAliens"] as? [String: Any])?.count == 4) + + analytics.group(groupId: "123", traits: dict) + RunLoop.main.run(until: Date.distantPast) + let groupEvent = outputReader.lastEvent as? GroupEvent + let traits3 = groupEvent?.traits?.dictionaryValue + XCTAssertNotNil(groupEvent) + XCTAssertTrue(traits3?.count == 2) + XCTAssertTrue((traits3?["ancientAliens"] as? [String: Any])?.count == 4) + } } #endif