Track same user by JS and PHP

I’m trying to track the same user with JS and PHP. AFAIK I must set the piwik visitor id on the php side, so piwik knows it is the same user. I am trying to do it with this:


$_p = new PiwikTracker( $id, $piwik_url );
$_p->setTokenAuth( $piwik_token );
$_p->setIp( $_SERVER['REMOTE_ADDR'] );
$_p->setVisitorId( $visitor_id );

The only problem is to set the right $visitor_id. First, I thought I can get it by JS like this:


var visitor_id;
_paq.push(function() { visitor_id = this.getVisitorId(); });

And then send the visitor_id to the server. But the visitor_id I get by this is always a different one from the one getting logged in the sql table log_visits in column idvisitor and therefore in the visitor log I get two different users.

I looked at the cookies that are sent to the piwik instance and my php site, too. There is always a cookie like the following:


_pk_id.<SITE_NUMBER>.4014=<VISITOR_ID>.1367416459.1.1367417149.1367416459.;

the <VISITOR_ID> is the same one I get by this.getVisitorId() in javascript. So this again is not the one I need.

How can I track the same user by JS and PHP? Is this possible? Can I get the visitor_id used by piwik in some way or is there another solution?

In the PHP you should actually be able to write


$_p->setVisitorId( $_p->getVisitorId() );

the getVisitorId() will actually fetch the visitor ID from the cookie (this will work if the PHP runs on the same domain/server as the website being tracked)