How can I retrieve Visitor ID from JavaScript?


#1

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/


(Joseph) #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.