Skip to content

Null userId and traits update fixes #199

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 5 commits into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 10 additions & 2 deletions Sources/Segment/ObjC/ObjCAnalytics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,16 @@ extension ObjCAnalytics {
/// - traits: A dictionary of traits you know about the user. Things like: email, name, plan, etc.
/// In the case when user logs out, make sure to call ``reset()`` to clear user's identity info.
@objc(identify:traits:)
public func identify(userId: String, traits: [String: Any]?) {
analytics.identify(userId: userId, traits: traits)
public func identify(userId: String?, traits: [String: Any]?) {
if let userId = userId {
analytics.identify(userId: userId, traits: traits)
Copy link
Contributor

@didiergarcia didiergarcia Mar 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a recursive infinite loop?

On line 68: is userId not an Optional anymore? and is there identify() function that doesn't take an optional that this will go to instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is calling into the swift version of identify where userId is not optional.

} else if let traits = try? JSON(traits as Any) {
analytics.store.dispatch(action: UserInfo.SetTraitsAction(traits: traits))
let userInfo: UserInfo? = analytics.store.currentState()
let userId = userInfo?.userId
let event = IdentifyEvent(userId: userId, traits: traits)
analytics.process(incomingEvent: event)
}
}

/// Track a screen change with a title, category and other properties.
Expand Down
2 changes: 1 addition & 1 deletion Sources/Segment/State.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ struct UserInfo: Codable, State {
}

struct SetUserIdAndTraitsAction: Action {
let userId: String
let userId: String?
let traits: JSON?

func reduce(state: UserInfo) -> UserInfo {
Expand Down
21 changes: 21 additions & 0 deletions Tests/Segment-Tests/ObjC_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class ObjC_Tests: XCTestCase {
config.defaultSettings = ["integrations": ["Amplitude": true]]

let analytics = ObjCAnalytics(configuration: config)
analytics.reset()

analytics.identify(userId: "testPerson", traits: ["email" : "[email protected]"])

waitUntilStarted(analytics: analytics.analytics)
Expand All @@ -63,6 +65,25 @@ class ObjC_Tests: XCTestCase {
let traits = analytics.traits()
XCTAssertTrue(traits != nil)
XCTAssertTrue(traits?["email"] as? String == "[email protected]")

let userId = analytics.userId
XCTAssertTrue(userId == "testPerson")
}

func testTraitsAndUserIdOptionality() {
let config = ObjCConfiguration(writeKey: "WRITE_KEY")
let analytics = ObjCAnalytics(configuration: config)
analytics.reset()

analytics.identify(userId: nil, traits: ["email" : "[email protected]"])

waitUntilStarted(analytics: analytics.analytics)
let userId = analytics.userId
XCTAssertNil(userId)
let traits = analytics.traits()
XCTAssertTrue(traits != nil)
XCTAssertTrue(traits?["email"] as? String == "[email protected]")

}
}

Expand Down