Skip to content

Commit c5401b2

Browse files
authored
Null userId and traits update fixes (#199)
* Null userId and traits update fixes * Added clarifying comment. * attempt to fix destination example ci build * again ... * Removing destinationexample from CI and made note of why
1 parent ac88be5 commit c5401b2

File tree

5 files changed

+37
-26
lines changed

5 files changed

+37
-26
lines changed

.github/workflows/swift.yml

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -141,26 +141,4 @@ jobs:
141141
run: |
142142
cd Examples/apps/SegmentUIKitExample
143143
xcodebuild -workspace "SegmentUIKitExample.xcworkspace" -scheme "SegmentUIKitExample" -destination 'platform=macOS,variant=Mac Catalyst'
144-
145-
146-
build_and_test_dest_examples:
147-
needs: cancel_previous
148-
runs-on: macos-11
149-
steps:
150-
- uses: maxim-lobanov/setup-xcode@v1
151-
with:
152-
xcode-version: latest-stable
153-
- uses: actions/checkout@v2
154-
- uses: actions/cache@v2
155-
with:
156-
path: /Users/runner/Library/Developer/Xcode/DerivedData
157-
key: ${{ runner.os }}-spm-dest-example-${{ hashFiles('**/Package.resolved') }}
158-
restore-keys: |
159-
${{ runner.os }}-spm-dest-example
160-
- uses: webfactory/[email protected]
161-
with:
162-
ssh-private-key: ${{ secrets.SOVRAN_SSH_KEY }}
163-
- name: build for ios simulator
164-
run: |
165-
cd Examples/apps/DestinationsExample
166-
xcodebuild -workspace "DestinationsExample.xcworkspace" -scheme "DestinationsExample" -sdk iphonesimulator
144+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
This DestinationsExample app is not maintained. Please see the individual device mode
2+
destination repos for up-to-date examples on usage.

Sources/Segment/ObjC/ObjCAnalytics.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,18 @@ extension ObjCAnalytics {
6363
/// - traits: A dictionary of traits you know about the user. Things like: email, name, plan, etc.
6464
/// In the case when user logs out, make sure to call ``reset()`` to clear user's identity info.
6565
@objc(identify:traits:)
66-
public func identify(userId: String, traits: [String: Any]?) {
67-
analytics.identify(userId: userId, traits: traits)
66+
public func identify(userId: String?, traits: [String: Any]?) {
67+
if let userId = userId {
68+
// at first glance this looks like recursion. It's actually calling
69+
// into the swift version of this call where userId is NOT optional.
70+
analytics.identify(userId: userId, traits: traits)
71+
} else if let traits = try? JSON(traits as Any) {
72+
analytics.store.dispatch(action: UserInfo.SetTraitsAction(traits: traits))
73+
let userInfo: UserInfo? = analytics.store.currentState()
74+
let userId = userInfo?.userId
75+
let event = IdentifyEvent(userId: userId, traits: traits)
76+
analytics.process(incomingEvent: event)
77+
}
6878
}
6979

7080
/// Track a screen change with a title, category and other properties.

Sources/Segment/State.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ struct UserInfo: Codable, State {
110110
}
111111

112112
struct SetUserIdAndTraitsAction: Action {
113-
let userId: String
113+
let userId: String?
114114
let traits: JSON?
115115

116116
func reduce(state: UserInfo) -> UserInfo {

Tests/Segment-Tests/ObjC_Tests.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ class ObjC_Tests: XCTestCase {
5050
config.defaultSettings = ["integrations": ["Amplitude": true]]
5151

5252
let analytics = ObjCAnalytics(configuration: config)
53+
analytics.reset()
54+
5355
analytics.identify(userId: "testPerson", traits: ["email" : "[email protected]"])
5456

5557
waitUntilStarted(analytics: analytics.analytics)
@@ -63,6 +65,25 @@ class ObjC_Tests: XCTestCase {
6365
let traits = analytics.traits()
6466
XCTAssertTrue(traits != nil)
6567
XCTAssertTrue(traits?["email"] as? String == "[email protected]")
68+
69+
let userId = analytics.userId
70+
XCTAssertTrue(userId == "testPerson")
71+
}
72+
73+
func testTraitsAndUserIdOptionality() {
74+
let config = ObjCConfiguration(writeKey: "WRITE_KEY")
75+
let analytics = ObjCAnalytics(configuration: config)
76+
analytics.reset()
77+
78+
analytics.identify(userId: nil, traits: ["email" : "[email protected]"])
79+
80+
waitUntilStarted(analytics: analytics.analytics)
81+
let userId = analytics.userId
82+
XCTAssertNil(userId)
83+
let traits = analytics.traits()
84+
XCTAssertTrue(traits != nil)
85+
XCTAssertTrue(traits?["email"] as? String == "[email protected]")
86+
6687
}
6788
}
6889

0 commit comments

Comments
 (0)