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

How do I set UserInfo anonymousId? #233

Closed
nwagu opened this issue Aug 2, 2024 · 6 comments · May be fixed by #234
Closed

How do I set UserInfo anonymousId? #233

nwagu opened this issue Aug 2, 2024 · 6 comments · May be fixed by #234
Assignees

Comments

@nwagu
Copy link

nwagu commented Aug 2, 2024

Describe the bug
I would like to set the anonymousId for a user, preferably when calling identify, but there is no overload of that method that allows this. Is there any other way to do this I am missing?

I have considered setting this in the EnrichmentClosure, but this only changes the anonymousId for the identify event. Other events still contain the random anonymous id generated internally by this lib.

To Reproduce
n/a

Expected behavior
I should be able to set anonymousId for a user from the client.

Screenshots
n/a

Platform (please complete the following information):

  • Library Version in use: 1.16.3
  • Platform being tested: Android
  • Integrations in use: Amplitude

Additional context
n/a

@wenxi-zeng
Copy link
Contributor

hi @nwagu , you can do this via a plugin. please refer to this comment for example. if you'd like the anonymousId to stay once identify is called, you'd be doing something similar to the following:

// Pseudo code below 
class AnonymousIdPlugin : EventPlugin {
    anonymousId: String

    identify(event) {
        // update anonymousId with the value from event
        anonymousId = event.properties.anonymousId
    }

    execute(event) {
        // let super run first, so identify is called before overwriting anonymousId
       super.execute(event)

        // overwrite anonymousId with local variable
       event.anonymousId = anonymousId
    }
}

@nwagu
Copy link
Author

nwagu commented Aug 2, 2024

Thanks, I did not know I could do this.

But then this solution only modifies events, it does not change the actual userInfo, so calling something like analytics.anonymousId() still returns the internally generated anonymousId. Is there any reason why we should not be able to set the userInfo anonymousId?

@nwagu
Copy link
Author

nwagu commented Aug 6, 2024

@wenxi-zeng could you please review my PR and see if it can be merged? It would be more convenient for us to set the anonymousId directly instead of using a plugin

@wenxi-zeng
Copy link
Contributor

hi @nwagu sorry for delayed response. I have brought this to our team. the anonymousId is also used by the backend for other things. we need more discussion and evaluation to make this API changes across the board. in the meanwhile, please use the plugin to overwrite anonymousId and retrieve it via an extension function through your plugin instance. something like:

fun Analytics.myAnonymousId(): String {
    return anonymousIdPlugin. anonymousId;
}

@nwagu
Copy link
Author

nwagu commented Aug 7, 2024

Okay. Could I get more info (maybe link to a doc) about what the anonymous id is used for in the backend? This is just so that I am clear what overwriting it implies @wenxi-zeng

@wenxi-zeng
Copy link
Contributor

@nwagu one of the known use case is de-duping on backend. changing the anonymousId might not have any impact since we do have customers doing it via plugins and we hear no complains so far. we just have to be very cautious on making this change, since it's a public API and we need to evaluate the potential risk.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants