How can we generate `_id` for logged in user in matomo(piwik) http API tracking

I asked this question in SO. I will ask it again here as I believe there are more matomo users in the forum…

Short version:

In matomo JS tracking, we only need to set userId for logged-in user, matomo will take care of the other parameters like _id (which is used to identify the user). However, if I use http API to track the user, I do not have _id , matomo is not able to identify the request is from the same user if I only set uid . So how can I get/query/calculate the _id value for any userId ?

Full story:

This issue is raised when I use both JS and http API to track the same site, the reason is that the shopping cart tracking is hard to be done in frontend, so we decided to track it at backend and make use of matomo’s http API. It works correctly when only JS client is used. For example in shopping cart tracking, matomo correctly records the revenue left in the cart even if I do a series of shopping cart add/edit/remove. But when I track the same thing using http API, the revenue left in the cart is wrong, all requests are treated as from different users. Let me give a simple example.

  • Add $10 in cart
  • Clear cart
  • Add $20 in cart

JS client gives revenue left in cart to be: $20 (correct)

Http tracking gives revenue left in cart to be :$30 (wrong, since matomo thinks the two add events are from different user)

I’ve tried to use postman to poke around and found that the actual cause is the _id field. If I set it to the same value and perform the above actions. Revenue calculation is correct. So how can I set the _id value so the same user has same _id in both JS tracking and http tracking?

How can we generate `_id` for logged in user in matomo(piwik) http API tracking