PiwikTracker::setVisitorId PHP Method

setVisitorId() public method

Note: it is recommended to use ->setUserId($userId); instead. Rather than letting Piwik attribute the user with a heuristic based on IP and other user fingeprinting attributes, force the action to be recorded for a particular visitor. If you use both setVisitorId and setUserId, setUserId will take precedence. If not set, the visitor ID will be fetched from the 1st party cookie, or will be set to a random UUID.
Deprecation: We recommend to use ->setUserId($userId).
public setVisitorId ( string $visitorId )
$visitorId string 16 hexadecimal characters visitor ID, eg. "33c31e01394bdc63"
    public function setVisitorId($visitorId)
    {
        $hexChars = '01234567890abcdefABCDEF';
        if (strlen($visitorId) != self::LENGTH_VISITOR_ID || strspn($visitorId, $hexChars) !== strlen($visitorId)) {
            throw new Exception("setVisitorId() expects a " . self::LENGTH_VISITOR_ID . " characters hexadecimal string (containing only the following: " . $hexChars . ")");
        }
        $this->forcedVisitorId = $visitorId;
        return $this;
    }

Usage Example

 private static function settingInvalidVisitorIdShouldThrow(PiwikTracker $t)
 {
     try {
         $t->setVisitorId('test');
         $this->fail('should throw');
     } catch (Exception $e) {
         //OK
     }
     try {
         $t->setVisitorId('61e8');
         $this->fail('should throw');
     } catch (Exception $e) {
         //OK
     }
     try {
         $t->setVisitorId('61e8cc2d51fea26dabcabcabc');
         $this->fail('should throw');
     } catch (Exception $e) {
         //OK
     }
 }