Accessing Piwik singleton when using async tracker

I’m testing out the Piwik JS tracker, and wanted to play about with plugins.

I’ve set a test page up using the async tracking code and _paq[] configuration array, and it tracks ok, but when I try to access the Piwik object for using e.g. Piwik.addPlugin(), it throws a “Piwik is not defined” javascript error./

Is it possible to access the Piwik singleton when using the async method?

EDIT:

I’m currently using the


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);

method to insert the piwik code. Should I be using document.write instead?

Loading it as a callback using onload seems to work:


//example callback
var loadCallback = function(){console.log(Piwik)};

  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";

        //add the onload callback, with support for IE8 as a fallback
        if (!g.addEventListener) {
            g.attachEvent("onload", loadCallback);
        }
        else {
            g.addEventListener("load", loadCallback, false);
        }
        
        s.parentNode.insertBefore(g,s);