Piwik wont login inside of a function

I am trying to get Piwik to save the current SharePoint User. I am calling a service to get the user and that works just fine. If I try to run the piwik script inside of a JavaScript function, it doesn’t save anything to the database. If I remove the code from the function it works as expected. Any ideas what’s going on? Since I’m using SP, I need to utilize the ExecuteOrDelayUntilScriptLoaded() method which waits to get the current user until a certain script is enabled.

< script type=“text/javascript”>

function SavePiwikInfo(){
alert("SPUser: " + sessionStorage.SPUserName);

var _paq = _paq || [];
/* tracker methods like “setCustomDimension” should be called before “trackPageView” */
_paq.push([‘setUserId’, sessionStorage.SPUserName]);
(function() {
var u="//piwik.mycompany.com/";
_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.async=true; g.defer=true; g.src=u+‘piwik.js’; s.parentNode.insertBefore(g,s);

function GetCurrentUserViaClientOM()
var context = new SP.ClientContext.get_current();
this.website = context.get_web();
this.currentUser = website.get_currentUser();
context.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));

function onQuerySucceeded(sender, args)
var userName = currentUser.get_loginName();
sessionStorage.SPUserName = userName;
alert(“Username Set”);

function onQueryFailed(sender, args)
alert('request failed ’ + args.get_message() + ‘\n’+ args.get_stackTrace());

ExecuteOrDelayUntilScriptLoaded(GetCurrentUserViaClientOM, “sp.js”);
< /script>

I encounter the same problem, and i would like to know if you have found a solution.