Matomo UserId is not showing Everytime

function matomo(userId) {
  let userId=localStorage.getItem('userEmail');
  console.log("matomo",userId);
  var _mtm = (window._mtm = window._mtm || []);
  // var _paq = window._paq = window._paq || [];
  _mtm.push(["trackPageView"]);
  _mtm.push(["enableLinkTracking"]);
  _mtm.push(["trackAllContentImpressions"]);
  _mtm.push({
    "mtm.startTime": new Date().getTime(),
    event: "mtm.Start",
    uid: userId,
  });
 
  (function () {
    var d = document,
      g = d.createElement("script"),
      s = d.getElementsByTagName("script")[0];
    g.async = true;
    g.src = "";
    s.parentNode.insertBefore(g, s);
  })();
}

We have used this code for tracking userId of logged in user in Angular Project.We are getting userId in console everytime.But in matomo all userId is not saved.What is the issue ?

Hi @trina
Did you create uid variable in the container, use this uid variable in the Matomo configuration variable, and use the last version of your container?

Yes we have used uid variable in the Matomo configuration variable.But still not getting userId.

EDIT by heurteph-ei:

Hi @trina
Looking at your code, I am not sure the uid is taken into account.
What if you put: _mtm.push({'uid': userId}); before the _mtm.push(["trackPageView"]);?
Can you also confirm the uid variable has been defined in MTM container variables?

Can you also check the value of the uid variable with the container preview?

NO we have commented // _mtm.push([“trackPageView”]); as it’s creating error.
Yes,it’s used in MTM container variables.
image

Hi @trina

Which error is thrown?

Hello @heurteph-ei When I am using _mtm.push([“trackPageView”]);

this error is showing in console:
VM64 container_phYIHtGk.js:19 method trackPageView is not valid

Hi @trina
Sorry, I read too quickly your initial code… You melted Matomo JavaScript and MTM bootstrap codes.
The methods you used are the ones of JavaScript tracking, not the ones of MTM. Link tracking and content impressions are indeed defined in the MTM configuration variables:


And the page view event, via Pageview Trigger and then Pageview Matomo Analytics Tag triggered by the previous trigger…

Then, just push the user ID alone before the mtm.startTime`:

function matomo() { // Here you defined a userId param... But it is squeezed by the next let
  let userId=localStorage.getItem('userEmail');
  console.log('matomo',userId);
  var _mtm = (window._mtm = window._mtm || []);
  _mtm.push({ 'uid': userId });
  _mtm.push({ 'mtm.startTime': (new Date().getTime()), 'event': 'mtm.Start' });
  var d = document, g = d.createElement('script'), s = d.getElementsByTagName('script')[0];
  g.async = true; g.src = ''; s.parentNode.insertBefore(g, s);
}

I have got it with the code and added this code instead of mine.What I need to configure in Matomo dashboard.I have enabled Enabled Link Tracking,Track All Content Impressions. can you please explain as it’s still not showing userId?

image

Hi @trina
Did you check the uid variable when debugging the container (with container preview)?
Did you publish the last version of the container?

how to check with container preview can you please explain?

Hi @trina
You’ll find some guidelines there:

Yes we have published last version of container.
Check uid variable is present in container.
But same problem is there we are not getting mailId ends with same domain which have the website and specific web domain name .

Hi @trina
Sorry, I don’t understand your last sentence:

About the container, I see you debug with the draft version ; it may not be the last container version (depending if you created and published the last container variables/triggers/tags).
To be sure the last version is the same as the draft version, click on Versions, and then button Create new version. If Changes since the last version is not empty, then the daft contains updates since the last version:


To be sure you preview a selected version, in the Versions table, use the bug icon of the desired version to preview:

Coming back to the uid problem, can you check the uid param is sent with the pageview event in the HTTP params? (Can you also check there is no JavaScript error in the page…) The HTTP query hsould looks like: https://matomo-server?action_name=page-title&idsite=1&rec=1&r=581010&h=10&m=52&s=16&url=https://visited-site/&urlref=https://previous-site/&uid=user-id&_id=926fb79c1db7a067&…

We have used uid variable in pageview via matomo configrutaion…
No error occur matomo related in our webapp…

The HTTP query hsould looks like: https://matomo-server?action_name=page-title&idsite=1&rec=1&r=581010&h=10&m=52&s=16&url=https://visited-site/&urlref=https://previous-site/ &uid=user-id &_id=926fb79c1db7a067&…

"Can you please explain this i am not able to understand this " and please provide a step by step guide for detecting uid of each user in Tag Manager of Matomo.

Hi @trina
When you use the MTM preview, and also when you normally run the tracking (without the preview), press F12 on your browser, then switch to the network tab of the developer tool. Then refresh the page. In the developer tool, you should see the tracking HTTP request.

Yes I am getting uid param is sent with pageView in Http params and it’s showing status code 204.

Hi @trina
After the fixes you did thanks to this thread, is the uid still not tracked?
Is it not tracked 100% of time?
Can you see any pattern (in user behavior for example) when uid is or is not tracked?
Do you use Matomo cloud or on premise?
Are you sure the uid is always set before the page view trigger / tag?