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

What is the right way to handle user session? #40

Closed
wilburx9 opened this issue Oct 19, 2023 · 3 comments
Closed

What is the right way to handle user session? #40

wilburx9 opened this issue Oct 19, 2023 · 3 comments

Comments

@wilburx9
Copy link

We run an experiment before the user log in and after they log in. We initialize the client like so:

val config = ExperimentConfig.builder()
      .build()
val experiment = Experiment.initializeWithAmplitudeAnalytics(app, $API_KEY  config,)
val user = ExperimentUser.builder()
      .userId(userAuth0Id)
      .deviceId(deviceIdFromAnalytics)
      .build()
experiment?.start(user)?.get()

Before the user logs in, userAuth0Id is null and calling experiment?.variant(variantKey) returns the expected variant.

After logging in, we call the code above with a valid userAuth0Id. But the now logged-in user doesn't get the expected variant of the experiment that is assigned to their userId, the properties in the object returned in experiment?.variant(variantKey) are just null. I have tried calling experiment?.clear() after login but that doesn't fix it.

However, if you restart the app, they get the right values.

What is the right way to handle this?

I am using v1.11.0

@tyiuhc
Copy link
Collaborator

tyiuhc commented Oct 19, 2023

Hi @wilburx9, many thanks for reaching out. In your implementation, after the user logs in, do you re-initialize the client as well, or just call experiment?.variant(variantKey) again? If it's the latter, you should use setUser() on the new user object (with valid userAuth0Id) before calling variant(). Let us know if this helps with your issue.

@wilburx9
Copy link
Author

After the user logs in, I call:

val config = ExperimentConfig.builder()
      .build()
val experiment = Experiment.initializeWithAmplitudeAnalytics(app, $API_KEY  config,)
val user = ExperimentUser.builder()
      .userId(userAuth0Id)
      .deviceId(deviceIdFromAnalytics)
      .build()
experiment?.start(user)?.get()

And userAuth0Id is a valid non-null and non-empty string at this point.

@tyiuhc
Copy link
Collaborator

tyiuhc commented Oct 21, 2023

This issue has been addressed in v1.11.1. Additionally, if you have initialized the client before the user logged in, you can also just do:

val user = ExperimentUser.builder()
      .userId(userAuth0Id)
      .deviceId(deviceIdFromAnalytics)
      .build()
experiment.setUser(user)

to set the user to be a new user with valid userAuth0Id.

@tyiuhc tyiuhc closed this as completed Nov 1, 2023
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

No branches or pull requests

2 participants