Matomo UserId is not showing Everytime

function matomo(userId) {
  let userId=localStorage.getItem('userEmail');
  var _mtm = (window._mtm = window._mtm || []);
  // var _paq = window._paq = window._paq || [];
    "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.

Hi @trina

Which error is thrown?