Reading userId in Javascript

Hello

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

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

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