Skip to content
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

fix: allow setting anonymousId #799

Merged
merged 1 commit into from
Nov 29, 2018
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
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