Popping from Asynchronous Array

I am trying to set up asynchronous tracking on my javascript-based website. I set up the _paq object according to the sample code at http://piwik.org/docs/javascript-tracking/#toc-asynchronous-tracking
I can push functions to be executed with no problems, but in some situations functions get pushed right before the page is closed. I want to set up a page close handler that makes sure all functions I pushed to _paq have finished executing, either by doing something like _paq.pop() or waiting for _paq.length() to go to 0. I understand that these will not work because _paq is a tracker proxy object rather than an array. How can I accomplish this?

You’ll want to take a look at the beforeUnloadHandler in piwik.js.

Would you mind clarifying? I’m not finding any documentation on a beforeUnloadHandler. I have a handler that captures the page close event. I need code to call inside that handler that allows all of my asynchronous functions to finish. Calling beforeUnloadHander() inside that handler is not working.

If you look in js/piwik.js, you’ll see piwik already has an unload handler.

It doesn’t keep track of the number of outstanding requests though.