PiwikTracker::setRequestTimeout PHP Method

setRequestTimeout() public method

Sets the maximum number of seconds that the tracker will spend waiting for a response from Piwik.
public setRequestTimeout ( integer $timeout )
$timeout integer
    public function setRequestTimeout($timeout)
    {
        if (!is_int($timeout) || $timeout < 0) {
            throw new Exception("Invalid value supplied for request timeout: {$timeout}");
        }
        $this->requestTimeout = $timeout;
        return $this;
    }

Usage Example

示例#1
0
   private function _getInfosForBeforeBodyEndAsHTMLPHP () {
   	$retour = LF.'<!-- PIWIK tracking via PHP - BEGIN -->'.LF;

   	$tracking_array = array();
   	
   	// opt-out
   	if ( !empty($_COOKIE['CommSyAGBPiwik'])
   			and $_COOKIE['CommSyAGBPiwik'] == 1
   	   ) {
   		$retour .= '<!-- don\'t track - opt-out is activated -->'.LF;
   	}
   	// track
   	else {
   	   if ( $this->_environment->inServer() ) {
   		   $info_array = $this->_getInfosForTracking($this->_environment->getServerItem());
   		   if ( !empty($info_array) ) {
   			   $tracking_array[] = $info_array;
   		   }
   	   } else {
   	      $info_array = $this->_getInfosForTracking($this->_environment->getServerItem());
   		   if ( !empty($info_array) ) {
   			   $tracking_array[] = $info_array;
   	   	}
   	      $info_array = $this->_getInfosForTracking($this->_environment->getCurrentPortalItem());
   		   if ( !empty($info_array) ) {
   			   $tracking_array[] = $info_array;
   		   }
   	   }
   	}
   	
   	if ( !empty($tracking_array) ) {
   		
   		// site title
   		$title = '';
   		$current_context_item = $this->_environment->getCurrentContextItem();
   		if ( !empty($current_context_item) ) {
   			if ( !$current_context_item->isServer()
   				  and !$current_context_item->isPortal()
   				) {
   				$current_portal = $current_context_item->getContextItem();
   				if ( !empty($current_portal) ) {
   					$title .= $current_portal->getTitle().' > ';
   				}
   			}
   			$title .= $current_context_item->getTitle().' > ';
   			$title .= $this->_environment->getCurrentModule().' > ';
   			$title .= $this->_environment->getCurrentFunction();
   			if ( !empty($_GET['iid'])) {
   				$title .= ' > '.$_GET['iid'];
   			}
   		}
   		
   		// tracking
   		include_once('plugins/'.$this->_identifier.'/PiwikTracker.php');
   		foreach ($tracking_array as $site_array) {
   			if ( !empty($site_array['server_url'])
   				  and !empty($site_array['site_id'])
   				) {
   				if ( empty($site_array['server_https'])
   				     or $site_array['server_https'] == 'commsy'
   					) {
   					$c_commsy_domain = $this->_environment->getConfiguration('c_commsy_domain');
   					if ( stristr($c_commsy_domain,'https') ) {
   						$http = 'https';
   					} else {
   						$http = 'http';
   					}
   					unset($c_commsy_domain);
   				} else {
   					$http = $site_array['server_https'];
   				}
   				$t = new PiwikTracker($site_array['site_id'],$http.'://'.$site_array['server_url'].'/piwik.php');
   				$t->setRequestTimeout($this->_timeout_ms); // in milliseconds - to avoid long waiting time, when piwik server is gone or network is down
   				
   				// proxy
   				if ( $this->_environment->getConfiguration('c_proxy_ip') ) {
   					$proxy = $this->_environment->getConfiguration('c_proxy_ip');
   		   		if ( $this->_environment->getConfiguration('c_proxy_port') ) {
  		   				$proxy .= ':'.$this->_environment->getConfiguration('c_proxy_port');
      				}
      				$t->setProxy($proxy);
     				}
   				
   				$result = $t->doTrackPageView($title);
   	         $retour .= '<!-- tracking '.$site_array['site_id'].' -->'.LF;
   			   if ( empty($result) ) {
   			   	$retour .= '   <!-- don\'t receive result from tracking for site '.$site_array['site_id'].' -->'.LF;
   				}
   			}
   		}
   	}
   	
   	$retour .= '<!-- PIWIK tracking via PHP - END -->'.LF;
   	return $retour;
   }