New Session on login when UserID is not empty

Hi all,

I am wanting to know the best way to achieve something.

We utilise the UserID functionality in Matomo, but we have the issue of users logging in using multiple UserIDs on the same device therefore using the same session.

I would like to be able to track usage before or after login/logout and I understand there is the functionality using new_visit ( to achieve this.

I am just struggling to understand whats the best trigger for forcing the new visit.

Essentially, I only want to force a new visit when I set the UserID and it is different to the current UserID and the current UserID is not empty.

I don’t particularly want to check UserID with Matomo every time and I am not even sure if it is possible to get the UserID and VisitID by calling the Matomo API in any case.

Has anyone had a similar use case and can enlighten me as to the best method here?

It would seem to me that if you set the UserID within Matomo and it changes then Matomo should force a new session but that is not my understanding of how it works, I believe the Actions will continue to track on the same Visit with the first set UserID.

Thanks in advance!