Hello, there's a problem in Matomo with leaving old "userID-empty" visits when visitor gets own userID when that userID already has a visit.
I'm using newest version of Matomo(3.3.0).
How to reproduce:
What should happen instead? That second visit with 3 actions should be connected to that first visit.
Why there's a problem? When last visit for reused userID already ended because of timeout, there's no problem because Matomo just updates record with new visit and writes new userID for the visit. When there's ongoing visit for that userID, Matomo can't just update userID for the new visit because there would be two separate visits for the same userID that hasn't ended yet but current Matomo reaction to this situation also isn't good because it can create false data about users that end their visits on login website.
The best way to fix it from the functional side would be to update idvisit of all old events from piwik_log_link_visit_action with the correct one and delete that second visit from database. This would merge these visits and leave only correct one. I don't know however if this wouldn't create too much load(searching the data with specific idvisit).
Here is where the problem begins: Matomo starts counting all actions again for the first visit with userID but it leaves previous actions from second visit.
[...] When there's ongoing visit for that userID, Matomo can't just update userID for the new visit because there would be two separate visits for the same userID that hasn't ended yet
Currently it is by design, if the second "visit" happens within 30min of the first one, then the actions/pageviews/etc will be added to the existing visit as per the visit definition in https://matomo.org/faq/general/faq_36/
but current Matomo reaction to this situation also isn't good because it can create false data about users that end their visits on login website.
It would be interesting to know when a logout/login happens though, as it may indicate some issue or bug in the website/app... What I would recommend is that, when users login again, you could force Matomo to create a new visit manually yourself, see: https://matomo.org/faq/how-to/faq_187/
(I reckon we won't change this behavior in Matomo because it would be hard to detect + it would probably end up creating too many visits in some cases when for example on one page you forget to set a User ID and on the next page you set it again. Will mark as closed for now but we could reopen if needed)