User id is not being captured for few users

Hi, I have installed matomo for a high traffic website and also am using only the matomo tag manager code in my website. I followed https://peertube.floss-marketing-school.com/videos/watch/2e9c81d5-3f4b-4cdf-9959-34d52863b1d6 in setting up the user id. I have created a data layer as uid and associated with the custom variable. In my site, i’ll be getting a id for each unique user in the body tag.I dont have login/logout functionality.So i am tracking users based on this id.

<script>
		var _mtm = _mtm || [];
		window._mtm.push({'uid':'1000' });
    </script>

Here 1000 is the id for a unique user.If another user comes, another new id will be generated.If the user returns, already generated id for that user will be used. So, I have added this 4 lines of code in the body part of all pages. But the user id is getting mapped only for 50% of the users.

Is there any other way to do this? Thanks in advance

You don’t by chance have also still the non-tagmanger-tracking code in addition to the Matomo Tag manager? In that case you might be tracking users twice (with only the latter being set up correctly.

hmmm, is your data layer located above Matomo Tag Manager code?

Thanks for your response. I have included the matomo tag manager code in the beginning of the head section and the data layer in the body section.

Thanks for your response. But, No. I am not using the regular tag magaer code.

ok, so that should be your issue, just give a try with the datalayer above the tag manager one, and it should be solved.

But I’m getting the value for the userId only in the body part(tag). So i have used it in the body tag. Will it be right to shift the tag manager code from the beginning of the head tag to body tag after the data layer? Will it resolve this issue and will the tracking be done properly if I use it in body tag?? Thanks in advance :slight_smile:

that may be the issue, as the user id needs to be sent before the trackpageview.
https://developer.matomo.org/guides/tracking-javascript-guide#when-user-is-logged-in-set-the-user-id
Another way (I never tested it and it may be messy) you could send it as a request by using the http tracking api https://developer.matomo.org/api-reference/tracking-api for example sending it as a pixel within the tag manager.

Ok.Will try tris.Thank you :grinning:

Just a quick typo for future forum visitors who want to use the code from @suba

use ‘window’ instead of ‘windows’ as follows:

var _mtm = _mtm || [];
window._mtm.push({‘uid’:‘1000’ });

Hi @tvbuzz,

I edited in the original post for future users.