Skip to content

Commit

Permalink
feat: add alias api with previousId support
Browse files Browse the repository at this point in the history
  • Loading branch information
1abhishekpandey committed Dec 2, 2024
1 parent f5182a9 commit b662b5b
Showing 1 changed file with 15 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,11 @@ void alias(@NonNull RudderMessage message) {
* @param newId New userId for the user
*/
public void alias(String newId) {
alias(newId, null);
alias(newId, null, null);
}

public void alias(@NonNull String newId, @Nullable RudderOption option) {
alias(newId, option, null);
}

/**
Expand All @@ -478,7 +482,7 @@ public void alias(String newId) {
* @param newId New userId for the user
* @param option RudderOptions for this event
*/
public void alias(@NonNull String newId, @Nullable RudderOption option) {
public void alias(@NonNull String newId, @Nullable RudderOption option, @Nullable String previousId) {
RudderContext context = getRudderContext();
Map<String, Object> traits = null;
if (context != null) {
Expand All @@ -488,12 +492,16 @@ public void alias(@NonNull String newId, @Nullable RudderOption option) {
return;
String prevUserId = null;

if (traits.containsKey("userId")) {
prevUserId = (String) traits.get("userId");
} else if (traits.containsKey("id")) {
prevUserId = (String) traits.get("id");
if (previousId != null) {
prevUserId = previousId;
} else {
prevUserId = RudderContext.getAnonymousId();
if (traits.containsKey("userId")) {
prevUserId = (String) traits.get("userId");
} else if (traits.containsKey("id")) {
prevUserId = (String) traits.get("id");
} else {
prevUserId = RudderContext.getAnonymousId();
}
}

traits.put("userId", newId);
Expand Down

0 comments on commit b662b5b

Please sign in to comment.