diff --git a/MatomoTracker.php b/MatomoTracker.php index 62d21dd..1a06676 100644 --- a/MatomoTracker.php +++ b/MatomoTracker.php @@ -161,6 +161,7 @@ public function __construct($idSite, $apiUrl = '') // Allow debug while blocking the request $this->requestTimeout = 600; + $this->requestConnectTimeout = 300; $this->doBulkRequests = false; $this->storedTrackingActions = []; @@ -1697,6 +1698,32 @@ public function setRequestTimeout($timeout) return $this; } + /** + * Returns the maximum number of seconds the tracker will spend trying to connect to Matomo. + * Defaults to 300 seconds. + */ + public function getRequestConnectTimeout() + { + return $this->requestConnectTimeout; + } + + /** + * Sets the maximum number of seconds that the tracker will spend tryint to connect to Matomo. + * + * @param int $timeout + * @return $this + * @throws Exception + */ + public function setRequestConnectTimeout($timeout) + { + if (!is_int($timeout) || $timeout < 0) { + throw new Exception("Invalid value supplied for request connect timeout: $timeout"); + } + + $this->requestConnectTimeout = $timeout; + return $this; + } + /** * Sets the request method to POST, which is recommended when using setTokenAuth() * to prevent the token from being recorded in server logs. Avoid using redirects @@ -1752,6 +1779,7 @@ protected function prepareCurlOptions($url, $method, $data, $forcePostUrlEncoded CURLOPT_USERAGENT => $this->userAgent, CURLOPT_HEADER => true, CURLOPT_TIMEOUT => $this->requestTimeout, + CURLOPT_CONNECTTIMEOUT => $this->requestConnectTimeout, CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => array( 'Accept-Language: ' . $this->acceptLanguage,