koa-health / mixpanel_analytics

A dart wrapper on the mixpanel REST API to be used in Flutter applications.
https://pub.dev/packages/mixpanel_analytics
MIT License
19 stars 26 forks source link

Adding new unique id to userId$ stream does not set new user profile #10

Closed NateWilliams2 closed 3 years ago

NateWilliams2 commented 3 years ago

Expected behavior

I may be misunderstanding the expected behavior, but my understanding is that with the setup

  final _user$ = StreamController<String>.broadcast();

    _mixpanel = MixpanelAnalytics(
...
      userId$: _user$.stream,
...
    );
...

any time I call

    _user$.add(newUser);

The stream should trigger an event to provide a new user profile that uses this new user id. The use case for this is an app where a user can have multiple accounts, and each account needs a distinct analytics profile.

Actual behavior

In practice, however, it does not- I set up the initial user profile (eg. _user$.add("aaaaaaa"); and mixpanel correctly tracks events with this profile. When I add a new user ID (eg. _user$.add("bbbbbbb"); Mixpanel still reports that the events belong to profile aaaaaaa. This happens both when using single and batch MixpanelAnalytics.

I think a workaround to this problem is to dispose and re-initialize my _mixpanel variable each time the user profile changes. Please let me know if this is how the plugin was designed, or if this is indeed a bug. Thanks!

jamesnicholls04 commented 3 years ago

Second this!

robertohuertasm commented 3 years ago

Hi @NateWilliams2, thanks for reporting this. The plugin should behave as you are expecting. We'll review this and come back to you asap.

robertohuertasm commented 3 years ago

Ok, I see why you're getting this behavior. I guess your adding the value to the stream just before sending the event. Take a look at the StreamController docs. As you can see there:

If sync is false, the event will always be fired at a later time, after the code adding the event has completed.

So, either you set the sync argument to true, or you listen to the stream and just add the event once the change it's done.

Let me know if that solves your question and feel free to close the issue if it does.

Take into account that we have just released vesion 1.4.0 which adds a new setter called userId. This is not a stream so you won't be having this sync issues.

//cc @Oriol-GG fyi

NateWilliams2 commented 3 years ago

Thanks for the explanation!

I'll try out one or both of these solutions and report back if I'm still having issues.