getVisitorId() with asynchronous javascript

Hi! I know this is probably a really basic question, but I am relatively new to Piwik and need to access the visitor ID so I can pass it in a form on my page. The rest of the tracking is working fine with this code. I am using the default code provided by my piwik control panel:

<!-- Piwik -->
	<script type="text/javascript">
	  var _paq = _paq || [];

	  (function() {
		var u=(("https:" == document.location.protocol) ? "https" : "http") + "://";
		_paq.push(["setTrackerUrl", u+"piwik.php"]);
		_paq.push(["setSiteId", "1"]);
		var d=document, g=d.createElement("script"), s=d.getElementsByTagName("script")[0]; g.type="text/javascript";
		g.defer=true; g.async=true; g.src=u+"piwik.js"; s.parentNode.insertBefore(g,s);
	<noscript><img src="" style="border:0" alt="" /></noscript>
<!-- End Piwik Code -->

How would I go about getting the visitor ID piwik has assigned to the user?

I have tried this:


But it doesn’t appear to be working. Any ideas? Thanks!

See on the user guide: JavaScript Tracking Client: API Reference - Matomo Analytics (formerly Piwik Analytics) - Developer Docs - v3

For example:

var visitor_id;
_paq.push([ function() { visitor_id = this.getVisitorId(); }]);

or for example, to fetch a custom variable (name,value) using the asynchronous code:

_paq.push([ function() { var customVariable = this.getCustomVariable(1); }]);

You can push to the _paq array even after the piwik.js file has been loaded and run.