Skip to content

Commit

Permalink
fix: allow setting anonymousId (#799)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
f2prateek authored Nov 29, 2018
1 parent 3efcd8c commit 06b2003
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
10 changes: 9 additions & 1 deletion Analytics/Classes/Integrations/SEGIntegrationsManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,15 @@ - (void)context:(SEGContext *)context next:(void (^_Nonnull)(SEGContext *_Nullab
switch (context.eventType) {
case SEGEventTypeIdentify: {
SEGIdentifyPayload *p = (SEGIdentifyPayload *)context.payload;
[self identify:p.userId traits:p.traits options:p.options];
NSDictionary *options;
if (p.anonymousId) {
NSMutableDictionary *mutableOptions = [[NSMutableDictionary alloc] initWithDictionary:p.options];
mutableOptions[@"anonymousId"] = p.anonymousId;
options = [mutableOptions copy];
} else {
options = p.options;
}
[self identify:p.userId traits:p.traits options:options];
break;
}
case SEGEventTypeTrack: {
Expand Down
2 changes: 1 addition & 1 deletion Analytics/Classes/SEGAnalytics.m
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ - (void)identify:(NSString *)userId traits:(NSDictionary *)traits options:(NSDic
NSCAssert2(userId.length > 0 || traits.count > 0, @"either userId (%@) or traits (%@) must be provided.", userId, traits);
[self run:SEGEventTypeIdentify payload:
[[SEGIdentifyPayload alloc] initWithUserId:userId
anonymousId:nil
anonymousId:[options objectForKey:@"anonymousId"]
traits:SEGCoerceDictionary(traits)
context:SEGCoerceDictionary([options objectForKey:@"context"])
integrations:[options objectForKey:@"integrations"]]];
Expand Down
28 changes: 21 additions & 7 deletions AnalyticsTests/TrackingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class TrackingTests: QuickSpec {
override func spec() {
var passthrough: SEGPassthroughMiddleware!
var analytics: SEGAnalytics!

beforeEach {
let config = SEGAnalyticsConfiguration(writeKey: "QUI5ydwIGeFFTa1IvCBUhxL9PyW5B0jE")
passthrough = SEGPassthroughMiddleware()
Expand All @@ -24,21 +24,35 @@ class TrackingTests: QuickSpec {
]
analytics = SEGAnalytics(configuration: config)
}

afterEach {
analytics.reset()
}

it("handles identify:") {
analytics.identify("testUserId1", traits: [
"firstName": "Peter"
])
expect(passthrough.lastContext?.eventType) == SEGEventType.identify
let identify = passthrough.lastContext?.payload as? SEGIdentifyPayload
expect(identify?.userId) == "testUserId1"
expect(identify?.anonymousId).to(beNil())
expect(identify?.traits?["firstName"] as? String) == "Peter"
}

it("handles identify with custom anonymousId:") {
analytics.identify("testUserId1", traits: [
"firstName": "Peter"
], options: [
"anonymousId": "a_custom_anonymous_id"
])
expect(passthrough.lastContext?.eventType) == SEGEventType.identify
let identify = passthrough.lastContext?.payload as? SEGIdentifyPayload
expect(identify?.userId) == "testUserId1"
expect(identify?.anonymousId) == "a_custom_anonymous_id"
expect(identify?.traits?["firstName"] as? String) == "Peter"
}

it("handles track:") {
analytics.track("User Signup", properties: [
"method": "SSO"
Expand All @@ -48,14 +62,14 @@ class TrackingTests: QuickSpec {
expect(payload?.event) == "User Signup"
expect(payload?.properties?["method"] as? String) == "SSO"
}

it("handles alias:") {
analytics.alias("persistentUserId")
expect(passthrough.lastContext?.eventType) == SEGEventType.alias
let payload = passthrough.lastContext?.payload as? SEGAliasPayload
expect(payload?.theNewId) == "persistentUserId"
}

it("handles screen:") {
analytics.screen("Home", properties: [
"referrer": "Google"
Expand All @@ -65,7 +79,7 @@ class TrackingTests: QuickSpec {
expect(screen?.name) == "Home"
expect(screen?.properties?["referrer"] as? String) == "Google"
}

it("handles group:") {
analytics.group("acme-company", traits: [
"employees": 2333
Expand Down
4 changes: 4 additions & 0 deletions Examples/CocoapodsExample/CocoapodsExample/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
configuration.trackAttributionData = YES;
configuration.flushAt = 1;
[SEGAnalytics setupWithConfiguration:configuration];
[[SEGAnalytics sharedAnalytics] identify:@"Prateek" traits:nil options: @{
@"anonymousId":@"test_anonymousId"
}];
[[SEGAnalytics sharedAnalytics] track:@"Cocoapods Example Launched"];

[[SEGAnalytics sharedAnalytics] flush];
NSLog(@"application:didFinishLaunchingWithOptions: %@", launchOptions);
return YES;
Expand Down

0 comments on commit 06b2003

Please sign in to comment.