Skip to content

Fix ObjC dictionary conversion failure. #226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Sources/Segment/Events.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions Sources/Segment/ObjC/ObjCAnalytics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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()
Expand Down Expand Up @@ -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.
Expand All @@ -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:)
Expand Down
3 changes: 0 additions & 3 deletions Sources/Segment/Utilities/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,3 @@ extension Optional: Flattenable {
}
}

internal func codable(_ dict: [String: Any]?) -> [String: Codable]? {
return dict as? [String: Codable]
}
63 changes: 63 additions & 0 deletions Tests/Segment-Tests/ObjC_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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