Reading userId in Javascript


After looking at the javascript documentation and multiple searches on I found the following 2 ways:

var currentUpdatedUserId ;
    _paq.push([ function() { currentUpdatedUserId = this.getUserId(); }]);
  currentUpdatedUserId = window.Matomo.getAsyncTrackers()[0].getUserId();

The 1st one just results in the variable being undefined (so it is not set)
The 2nd one gives an error: Uncaught TypeError: Cannot read properties of undefined (reading ‘getAsyncTrackers’)

Thanks in advance