Skip to content

Commit 06b2003

Browse files
authored
fix: allow setting anonymousId (#799)
Historically, the iOS library has allowed setting a custom anonymous identifier (#485). During adding middlewares, a bug was introduced which started ignoring the anonymousId option. This fixes the bug and adds a test to validate the fix. This was also tested in the example app end to end and I've verified that events show up with the custom anonymousId. Test snipet: ``` [[SEGAnalytics sharedAnalytics] identify:@"Prateek" traits:nil options: @{ @"anonymousId":@"test_anonymousId" }]; ``` Event in debugger: https://cloudup.com/cFyVH4NPR_f Subsequent events will automatically attach the new anonymous ID https://cloudup.com/cK-i_SCbwsN
1 parent 3efcd8c commit 06b2003

File tree

4 files changed

+35
-9
lines changed

4 files changed

+35
-9
lines changed

Analytics/Classes/Integrations/SEGIntegrationsManager.m

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,15 @@ - (void)context:(SEGContext *)context next:(void (^_Nonnull)(SEGContext *_Nullab
541541
switch (context.eventType) {
542542
case SEGEventTypeIdentify: {
543543
SEGIdentifyPayload *p = (SEGIdentifyPayload *)context.payload;
544-
[self identify:p.userId traits:p.traits options:p.options];
544+
NSDictionary *options;
545+
if (p.anonymousId) {
546+
NSMutableDictionary *mutableOptions = [[NSMutableDictionary alloc] initWithDictionary:p.options];
547+
mutableOptions[@"anonymousId"] = p.anonymousId;
548+
options = [mutableOptions copy];
549+
} else {
550+
options = p.options;
551+
}
552+
[self identify:p.userId traits:p.traits options:options];
545553
break;
546554
}
547555
case SEGEventTypeTrack: {

Analytics/Classes/SEGAnalytics.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ - (void)identify:(NSString *)userId traits:(NSDictionary *)traits options:(NSDic
201201
NSCAssert2(userId.length > 0 || traits.count > 0, @"either userId (%@) or traits (%@) must be provided.", userId, traits);
202202
[self run:SEGEventTypeIdentify payload:
203203
[[SEGIdentifyPayload alloc] initWithUserId:userId
204-
anonymousId:nil
204+
anonymousId:[options objectForKey:@"anonymousId"]
205205
traits:SEGCoerceDictionary(traits)
206206
context:SEGCoerceDictionary([options objectForKey:@"context"])
207207
integrations:[options objectForKey:@"integrations"]]];

AnalyticsTests/TrackingTests.swift

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class TrackingTests: QuickSpec {
1515
override func spec() {
1616
var passthrough: SEGPassthroughMiddleware!
1717
var analytics: SEGAnalytics!
18-
18+
1919
beforeEach {
2020
let config = SEGAnalyticsConfiguration(writeKey: "QUI5ydwIGeFFTa1IvCBUhxL9PyW5B0jE")
2121
passthrough = SEGPassthroughMiddleware()
@@ -24,21 +24,35 @@ class TrackingTests: QuickSpec {
2424
]
2525
analytics = SEGAnalytics(configuration: config)
2626
}
27-
27+
2828
afterEach {
2929
analytics.reset()
3030
}
31-
31+
3232
it("handles identify:") {
3333
analytics.identify("testUserId1", traits: [
3434
"firstName": "Peter"
3535
])
3636
expect(passthrough.lastContext?.eventType) == SEGEventType.identify
3737
let identify = passthrough.lastContext?.payload as? SEGIdentifyPayload
3838
expect(identify?.userId) == "testUserId1"
39+
expect(identify?.anonymousId).to(beNil())
40+
expect(identify?.traits?["firstName"] as? String) == "Peter"
41+
}
42+
43+
it("handles identify with custom anonymousId:") {
44+
analytics.identify("testUserId1", traits: [
45+
"firstName": "Peter"
46+
], options: [
47+
"anonymousId": "a_custom_anonymous_id"
48+
])
49+
expect(passthrough.lastContext?.eventType) == SEGEventType.identify
50+
let identify = passthrough.lastContext?.payload as? SEGIdentifyPayload
51+
expect(identify?.userId) == "testUserId1"
52+
expect(identify?.anonymousId) == "a_custom_anonymous_id"
3953
expect(identify?.traits?["firstName"] as? String) == "Peter"
4054
}
41-
55+
4256
it("handles track:") {
4357
analytics.track("User Signup", properties: [
4458
"method": "SSO"
@@ -48,14 +62,14 @@ class TrackingTests: QuickSpec {
4862
expect(payload?.event) == "User Signup"
4963
expect(payload?.properties?["method"] as? String) == "SSO"
5064
}
51-
65+
5266
it("handles alias:") {
5367
analytics.alias("persistentUserId")
5468
expect(passthrough.lastContext?.eventType) == SEGEventType.alias
5569
let payload = passthrough.lastContext?.payload as? SEGAliasPayload
5670
expect(payload?.theNewId) == "persistentUserId"
5771
}
58-
72+
5973
it("handles screen:") {
6074
analytics.screen("Home", properties: [
6175
"referrer": "Google"
@@ -65,7 +79,7 @@ class TrackingTests: QuickSpec {
6579
expect(screen?.name) == "Home"
6680
expect(screen?.properties?["referrer"] as? String) == "Google"
6781
}
68-
82+
6983
it("handles group:") {
7084
analytics.group("acme-company", traits: [
7185
"employees": 2333

Examples/CocoapodsExample/CocoapodsExample/AppDelegate.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
2929
configuration.trackAttributionData = YES;
3030
configuration.flushAt = 1;
3131
[SEGAnalytics setupWithConfiguration:configuration];
32+
[[SEGAnalytics sharedAnalytics] identify:@"Prateek" traits:nil options: @{
33+
@"anonymousId":@"test_anonymousId"
34+
}];
3235
[[SEGAnalytics sharedAnalytics] track:@"Cocoapods Example Launched"];
36+
3337
[[SEGAnalytics sharedAnalytics] flush];
3438
NSLog(@"application:didFinishLaunchingWithOptions: %@", launchOptions);
3539
return YES;

0 commit comments

Comments
 (0)