Skip to content

Commit dc1e895

Browse files
authored
Fix ObjC dictionary conversion failure. (#226)
1 parent ef59c30 commit dc1e895

File tree

4 files changed

+69
-7
lines changed

4 files changed

+69
-7
lines changed

Sources/Segment/Events.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,9 @@ extension Analytics {
188188
/// - category: A category to the type of screen if it applies.
189189
/// - properties: Any extra metadata associated with the screen. e.g. method of access, size, etc.
190190
public func screen(title: String, category: String? = nil, properties: [String: Any]? = nil) {
191+
// if properties is nil, this is the event that'll get used.
191192
var event = ScreenEvent(title: title, category: category, properties: nil)
193+
// if we have properties, get a new one rolling.
192194
if let properties = properties {
193195
do {
194196
let jsonProperties = try JSON(properties)

Sources/Segment/ObjC/ObjCAnalytics.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ extension ObjCAnalytics {
4141

4242
@objc(track:properties:)
4343
public func track(name: String, properties: [String: Any]?) {
44-
analytics.track(name: name, properties: properties as? [String: Codable])
44+
analytics.track(name: name, properties: properties)
4545
}
4646

4747
/// Associate a user with their unique ID and record traits about them.
@@ -67,7 +67,7 @@ extension ObjCAnalytics {
6767
if let userId = userId {
6868
// at first glance this looks like recursion. It's actually calling
6969
// into the swift version of this call where userId is NOT optional.
70-
analytics.identify(userId: userId, traits: codable(traits))
70+
analytics.identify(userId: userId, traits: traits)
7171
} else if let traits = try? JSON(traits as Any) {
7272
analytics.store.dispatch(action: UserInfo.SetTraitsAction(traits: traits))
7373
let userInfo: UserInfo? = analytics.store.currentState()
@@ -100,7 +100,7 @@ extension ObjCAnalytics {
100100
/// - properties: Any extra metadata associated with the screen. e.g. method of access, size, etc.
101101
@objc(screen:category:properties:)
102102
public func screen(title: String, category: String?, properties: [String: Any]?) {
103-
analytics.screen(title: title, category: category, properties: codable(properties))
103+
analytics.screen(title: title, category: category, properties: properties)
104104
}
105105

106106
/// Associate a user with a group such as a company, organization, project, etc.
@@ -117,7 +117,7 @@ extension ObjCAnalytics {
117117
/// - traits: Traits of the group you may be interested in such as email, phone or name.
118118
@objc(group:traits:)
119119
public func group(groupId: String, traits: [String: Any]?) {
120-
analytics.group(groupId: groupId, traits: codable(traits))
120+
analytics.group(groupId: groupId, traits: traits)
121121
}
122122

123123
@objc(alias:)

Sources/Segment/Utilities/Utils.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,3 @@ extension Optional: Flattenable {
5858
}
5959
}
6060

61-
internal func codable(_ dict: [String: Any]?) -> [String: Codable]? {
62-
return dict as? [String: Codable]
63-
}

Tests/Segment-Tests/ObjC_Tests.swift

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,69 @@ class ObjC_Tests: XCTestCase {
130130
XCTAssertTrue(lastEvent is IdentifyEvent)
131131
XCTAssertTrue((lastEvent as! IdentifyEvent).userId == "batman")
132132
}
133+
134+
func testObjCDictionaryPassThru() {
135+
Storage.hardSettingsReset(writeKey: "WRITE_KEY2")
136+
137+
let config = ObjCConfiguration(writeKey: "WRITE_KEY2")
138+
let analytics = ObjCAnalytics(configuration: config)
139+
analytics.analytics.storage.hardReset(doYouKnowHowToUseThis: true)
140+
141+
analytics.reset()
142+
143+
let outputReader = OutputReaderPlugin()
144+
analytics.analytics.add(plugin: outputReader)
145+
146+
waitUntilStarted(analytics: analytics.analytics)
147+
148+
let dict = [
149+
"ancientAliens": [
150+
"guy1": "hair guy",
151+
"guy2": "old mi5 guy",
152+
"guy3": "old bald guy",
153+
"guy4": 4] as [String : Any],
154+
"channel": "hIsToRy cHaNnEL"] as [String : Any]
155+
156+
analytics.track(name: "test", properties: dict)
157+
RunLoop.main.run(until: Date.distantPast)
158+
let trackEvent = outputReader.lastEvent as? TrackEvent
159+
let props = trackEvent?.properties?.dictionaryValue
160+
XCTAssertNotNil(trackEvent)
161+
XCTAssertTrue(props?.count == 2)
162+
XCTAssertTrue((props?["ancientAliens"] as? [String: Any])?.count == 4)
163+
164+
analytics.identify(userId: "test", traits: dict)
165+
RunLoop.main.run(until: Date.distantPast)
166+
let identifyEvent = outputReader.lastEvent as? IdentifyEvent
167+
let traits = identifyEvent?.traits?.dictionaryValue
168+
XCTAssertNotNil(identifyEvent)
169+
XCTAssertTrue(traits?.count == 2)
170+
XCTAssertTrue((traits?["ancientAliens"] as? [String: Any])?.count == 4)
171+
172+
analytics.identify(userId: nil, traits: dict)
173+
RunLoop.main.run(until: Date.distantPast)
174+
let identifyEvent2 = outputReader.lastEvent as? IdentifyEvent
175+
let traits2 = identifyEvent2?.traits?.dictionaryValue
176+
XCTAssertNotNil(identifyEvent2)
177+
XCTAssertTrue(traits2?.count == 2)
178+
XCTAssertTrue((traits2?["ancientAliens"] as? [String: Any])?.count == 4)
179+
180+
analytics.screen(title: "blah", category: nil, properties: dict)
181+
RunLoop.main.run(until: Date.distantPast)
182+
let screenEvent = outputReader.lastEvent as? ScreenEvent
183+
let props2 = screenEvent?.properties?.dictionaryValue
184+
XCTAssertNotNil(screenEvent)
185+
XCTAssertTrue(props2?.count == 2)
186+
XCTAssertTrue((props2?["ancientAliens"] as? [String: Any])?.count == 4)
187+
188+
analytics.group(groupId: "123", traits: dict)
189+
RunLoop.main.run(until: Date.distantPast)
190+
let groupEvent = outputReader.lastEvent as? GroupEvent
191+
let traits3 = groupEvent?.traits?.dictionaryValue
192+
XCTAssertNotNil(groupEvent)
193+
XCTAssertTrue(traits3?.count == 2)
194+
XCTAssertTrue((traits3?["ancientAliens"] as? [String: Any])?.count == 4)
195+
}
133196
}
134197

135198
#endif

0 commit comments

Comments
 (0)