Problem with visitorId

Good afternoon,

I have a problem when collecting information about the user.
I know API /module=API&method=Live.getVisitorProfile&idSite=XXX&visitorId=YYY&format=json&token_auth=ZZZ but I can’t get the correct visitorId in the HTML where I added the matomo code.
I use the following function of JS:
_paq.push ([function() {this.getVisitorId();}]));

I have also tried:

window.Piwik.getAsyncTracker(). getVisitorId();

But in both cases the identifier that returns to me is different from the one that is registered in piwik, since afterwards I check the record of visits in the dashboard and I see that it is different.

Do you know why this happens?