PiwikTracker::getVisitorId PHP Method

getVisitorId() public method

If you call this function from a server, where the call is triggered by a cron or script not initiated by the actual visitor being tracked, then it will return the random Visitor ID that was assigned to this visit object. This can be used if you wish to record more visits, actions or goals for this visitor ID later on.
public getVisitorId ( ) : string
return string 16 hex chars visitor ID string
    public function getVisitorId()
    {
        if (!empty($this->userId)) {
            return $this->getUserIdHashed($this->userId);
        }
        if (!empty($this->forcedVisitorId)) {
            return $this->forcedVisitorId;
        }
        if ($this->loadVisitorIdCookie()) {
            return $this->cookieVisitorId;
        }
        return $this->randomVisitorId;
    }

Usage Example

 /**
  * Test setting/getting the first party cookie via the PHP Tracking Client
  * @param $t
  */
 private function testFirstPartyCookies(PiwikTracker $t)
 {
     $domainHash = $this->getFirstPartyCookieDomainHash();
     $idCookieName = '_pk_id_1_' . $domainHash;
     $refCookieName = '_pk_ref_1_' . $domainHash;
     $customVarCookieName = '_pk_cvar_1_' . $domainHash;
     $viewts = '1302307497';
     $uuid = 'ca0afe7b6b692ff5';
     $_COOKIE[$idCookieName] = $uuid . '.1302307497.1.' . $viewts . '.1302307497';
     $_COOKIE[$refCookieName] = '["YEAH","RIGHT!",1302307497,"http://referrer.example.org/page/sub?query=test&test2=test3"]';
     $_COOKIE[$customVarCookieName] = '{"1":["VAR 1 set, var 2 not set","yes"],"3":["var 3 set","yes!!!!"]}';
     // test loading 'id' cookie
     self::assertContains("_viewts=" . $viewts, $t->getUrlTrackPageView());
     self::assertEquals($uuid, $t->getVisitorId());
     self::assertEquals($t->getAttributionInfo(), $_COOKIE[$refCookieName]);
     self::assertEquals(array("VAR 1 set, var 2 not set", "yes"), $t->getCustomVariable(1));
     self::assertFalse($t->getCustomVariable(2));
     self::assertEquals(array("var 3 set", "yes!!!!"), $t->getCustomVariable(3));
     self::assertFalse($t->getCustomVariable(4));
     self::assertFalse($t->getCustomVariable(5));
     self::assertFalse($t->getCustomVariable(6));
     self::assertFalse($t->getCustomVariable(-1));
     unset($_COOKIE[$idCookieName]);
     unset($_COOKIE[$refCookieName]);
     unset($_COOKIE[$customVarCookieName]);
 }