I tried this:
var visitor_id;
_paq.push([ function () { visitor_id = this.getVisitorId(); }]);
This are working perfect until I clear the browser cookies, when I clear the browser cookies, Piwik getVisitorId() isn’t bringing the old visitor ID. Piwik is still tracking under the old visitor ID itself, I just want to retrieve it.
You can see the error message on one of the client sites here: https://www.artsocket.com/
jchandy
(Joseph)
July 31, 2018, 6:06pm
2
Hi ,
can you help me understand how the getVisitoId () works ? You have made more progress than me in getting the function to work.
This is the code I have used
var visitor_id;
_paq.push([ function () { visitor_id = this.getVisitorId(); }]);
console.log(visitor_id);
My console keep showing that the variable is undefined.
If I change the code to
var visitor_id;
_paq.push([ function () { visitor_id = this.getVisitorId(); console.log(visitor_id); }]);
cosole will show the correct ID.
I need to to access the visitor ID variable later on in the script.
I know this is not strictly a piwik/motomo question. But I’m anube at javascripts and would apreciate any help.
This is because .push(function) is an async call
so the visitor_id variable will be set, but not when you need it
the best way is to make an callback function for it
function getId(callback) {
_paq.push([ function () { callback(visitor_id); }]);
}