Extract visitorId from Cookie value of _pk_uid

Hallo,

I’d like to know how to extract the 16 chararacter visitor ID from the 3rd party cookie that is named _pk_uid.
I do not understand how it is actually encoded.

Thanks a lot
and best regards

Tom

You can use the Javascript function getVisitorInfo() which returns the following array:


// new visitor
                      
                        // uuid
                      
                        // creation timestamp - seconds since Unix epoch
                      
                        // visitCount - 0 = no previous visit
                      
                        // current visit timestamp
                      
                        // last visit timestamp - blank = no previous visit
                      
                        // last ecommerce order timestamp

Hey Matt,

thanks for the fast answer.

Strange thing is that for every page navigation/reload the method mentioned returns completely different content. Actually I am looking for an ID that I can use for the REST API in order to track backend actions. I now have that _pk_uid value in the backend, which is consistent.
Currently I also send that cookie to piwik in the header when doing backend API calls (as well as the URL parameter cookie=1). Unfortunately these calls are not always tracked as one visit by Piwik although the same cookie value is send that is known to the JS tracker in the browser.

In order to tell piwik more precisely which visitor belongs to the tracking command I’d like to send the visitor id in the fields _id and cid for the REST call. That is the reason for my initial question.

Could you please post your comment in: Accurate User Detection cross devices: User ID (set in JS and all other clients) · Issue #3490 · matomo-org/matomo · GitHub

I think this is the ticket you need, we hope to implement it soon!

Hey,

that’s funny; for every step and approach I am trying to get browser based and backend REST based user tracking to work, I always come back to that ticket :?

Happily that all works for me now. I was able to extract the visitor ID from the _pk_uid cookie value.
Using this visitor ID in the REST call, by setting parameters cid and _id lets piwik correctly determine that these tracking events belong to a visitor that was previously tracked by the Javascript tracker.

My solution is implemented in Java:


        // example input
        String input = "0%3DczoxNjoiODUzM2ExYTQyYjJmNzgxZSI7%3A_%3D054a6e6d4514a69e3a55fd95440da6b8631f0202";

        String urlDecoded = java.net.URLDecoder.decode(input, "UTF-8");
        String base64Encoded = urlDecoded.substring(urlDecoded.indexOf("0=")+2, urlDecoded.indexOf(":"));
        String base64Decoded = new String(org.apache.commons.net.util.Base64.decodeBase64(base64Encoded));
        String piwikVisitorId = base64Decoded.substring(base64Decoded.indexOf("\"")+1, base64Decoded.lastIndexOf("\""));

Best regards
Tom

1 Like