Is there any proven way to add a custom session parameter to the current session after the Piwik tracking code has been executed?
Currently, I am using this code:
window._paq.push(['trackEvent', 'vendor', 'VND001' ,'value']);
The issue is that this code does not work reliably. I believe this might be because the Piwik tracking code has already been executed and sent to the server but I am not sure.
From the JavaScript API:
For asynchronous tracking, configuration and tracking calls are pushed onto the global _paq array for execution, independent of the asynchronous loading of piwik.js. The format is:
_paq.push([ 'API_method_name', parameter_list ]);
So this seem to be alright on your end.
When you talk about unreliable behavior it's possible when you refresh the page (simply bash F5) you're bypassing the loading of the script file since it's already cached by a previous request. And then this call to _paq
could be undefined
. Try to use ctrl+F5 to see if the behavior changes.
Also note the initialization part: var _paq = _paq || [];
which defines the previously filled up array or an empty one. It doesn't hurt to put this in front of your code, but you have to pay attention to closure:
(function(){
var _paq = _paq || [];
_paq.push(['trackEvent', 'vendor', 'VND001' ,'value']);
}());
Now what you normally would do is get the current tracker first:
var piwikTracker = Piwik.getTracker(12, 'http://example.com/piwik/');
//piwikTracker.setSiteId(12);
//piwikTracker.setTrackerUrl('http://example.com/piwik/');
piwikTracker.trackEvent('vendor', 'VND001' ,'value');
But I'm not entirely sure if this is only related to Piwik 2.16.1 or earlier. Feel free to test them out and let me know ^^