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

feat: add alias api with previousId support #554

Merged
merged 11 commits into from
Dec 9, 2024
Merged
1 change: 1 addition & 0 deletions Sources/Classes/Headers/Public/RSClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ typedef void (^Callback)(NSObject *_Nullable);
- (void) group:(NSString *)groupId traits:(NSDictionary<NSString*, id>*)traits;
- (void) group:(NSString *)groupId;

- (void) alias:(NSString *)newId options:(RSOption * _Nullable) options previousId:(NSString * _Nullable)previousId;
- (void) alias:(NSString *)newId options:(RSOption * _Nullable) options;
- (void) alias:(NSString *)newId;

Expand Down
11 changes: 6 additions & 5 deletions Sources/Classes/RSClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -225,21 +225,22 @@ - (void)alias:(NSString *)newId {
[self reportDiscardedEvent];
return;
}
[self alias:newId options:nil];
[self alias:newId options:nil previousId:nil];
}

- (void) alias:(NSString *)newId options:(RSOption *) options {
[self alias:newId options:options previousId:nil];
}

- (void) alias:(NSString *)newId options:(RSOption *) options previousId:(NSString *)previousId {
if ([RSClient getOptStatus]) {
[self reportDiscardedEvent];
return;
}
RSContext *rc = [RSElementCache getContext];
NSMutableDictionary<NSString*,NSObject*>* traits = [rc.traits mutableCopy];

NSObject *prevId = [traits objectForKey:@"userId"];
if(prevId == nil) {
prevId =[traits objectForKey:@"id"];
}
NSObject *prevId = previousId ?: [traits objectForKey:@"userId"] ?: [traits objectForKey:@"id"] ?: self.anonymousId;

traits[@"id"] = newId;
traits[@"userId"] = newId;
Expand Down