From 1de2aa6f89d6927bf54558fdb7789f0c6c9804e3 Mon Sep 17 00:00:00 2001 From: sgiehl Date: Tue, 8 Oct 2024 15:38:20 +0200 Subject: [PATCH 1/2] improve handling of certain attributes --- MatomoTracker.php | 158 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 110 insertions(+), 48 deletions(-) diff --git a/MatomoTracker.php b/MatomoTracker.php index 0e4f41b..1f14014 100644 --- a/MatomoTracker.php +++ b/MatomoTracker.php @@ -1,4 +1,5 @@ currentTs = time(); $this->createTs = $this->currentTs; - + $this->visitorCustomVar = $this->getCustomVariablesFromCookie(); } @@ -332,12 +380,12 @@ public function setPerformanceTimings( */ public function clearPerformanceTimings(): void { - $this->networkTime = false; - $this->serverTime = false; - $this->transferTime = false; - $this->domProcessingTime = false; - $this->domCompletionTime = false; - $this->onLoadTime = false; + $this->networkTime = null; + $this->serverTime = null; + $this->transferTime = null; + $this->domProcessingTime = null; + $this->domCompletionTime = null; + $this->onLoadTime = null; } /** @@ -438,7 +486,8 @@ public function getCustomVariable(int $id, string $scope = 'visit') } $cookieDecoded = $this->getCustomVariablesFromCookie(); - if (!is_array($cookieDecoded) + if ( + !is_array($cookieDecoded) || !isset($cookieDecoded[$id]) || !is_array($cookieDecoded[$id]) || count($cookieDecoded[$id]) !== 2 @@ -471,7 +520,7 @@ public function clearCustomVariables(): void */ public function setCustomDimension(int $id, string $value) { - $this->customDimensions['dimension'.$id] = $value; + $this->customDimensions['dimension' . $id] = $value; return $this; } @@ -492,7 +541,7 @@ public function clearCustomDimensions(): void */ public function getCustomDimension(int $id): ?string { - return $this->customDimensions['dimension'.$id] ?? null; + return $this->customDimensions['dimension' . $id] ?? null; } /** @@ -628,6 +677,16 @@ public function setClientHints( return $this; } + /** + * Returns currently set client hints + * + * @return array + */ + public function getClientHints(): array + { + return $this->clientHints; + } + /** * Sets the country of the visitor. If not used, Matomo will try to find the country * using either the visitor's IP address or language. @@ -713,7 +772,7 @@ public function enableBulkTracking(): void } /** - * Disables the bulk request feature. Make sure to call `doBulkTrack()` before disabling it if you have stored + * Disables the bulk request feature. Make sure to call `doBulkTrack()` before disabling it if you have stored * tracking actions previously as this method won't be sending any previously stored actions before disabling it. */ public function disableBulkTracking(): void @@ -810,7 +869,7 @@ public function doTrackPageView(string $documentTitle) return $this->sendRequest($url); } - + /** * Override PageView id for every use of `doTrackPageView()`. Do not use this if you call `doTrackPageView()` * multiple times during tracking (if, for example, you are tracking a single page application). @@ -825,7 +884,7 @@ public function setPageviewId(string $idPageview): void * Returns the PageView id. If the id was manually set using `setPageViewId()`, that id will be returned. * If the id was not set manually, the id that was automatically generated in last `doTrackPageView()` will * be returned. If there was no last page view, this will be false. - * + * * @return string|false The PageView id as string or false if there is none yet. */ public function getPageviewId() @@ -1554,11 +1613,11 @@ public function setIp(string $ip) * * A User ID can be a username, UUID or an email address, or any number or string that uniquely identifies a user or client. * - * @param string $userId Any user ID string (eg. email address, ID, username). Must be non empty. Set to false to de-assign a user id previously set. + * @param string|null $userId Any user ID string (eg. email address, ID, username). Must be non empty. Set to null to de-assign a user id previously set. * @return $this * @throws Exception */ - public function setUserId(string $userId) + public function setUserId(?string $userId) { if ($userId === '') { throw new Exception("User ID cannot be empty."); @@ -1595,7 +1654,8 @@ public static function getUserIdHashed($id): string public function setVisitorId(string $visitorId) { $hexChars = '01234567890abcdefABCDEF'; - if (strlen($visitorId) !== self::LENGTH_VISITOR_ID + if ( + strlen($visitorId) !== self::LENGTH_VISITOR_ID || strspn($visitorId, $hexChars) !== strlen($visitorId) ) { throw new Exception( @@ -1726,10 +1786,10 @@ public function getAttributionInfo() * - force the visitor IP * - force the date & time of the tracking requests rather than track for the current datetime * - * @param string $token_auth token_auth 32 chars token_auth string + * @param string|null $token_auth 32 chars token_auth string or null to unset * @return $this */ - public function setTokenAuth(string $token_auth) + public function setTokenAuth(?string $token_auth) { $this->token_auth = $token_auth; @@ -2051,7 +2111,7 @@ protected function sendRequest(string $url, string $method = 'GET', $data = null $this->clearCustomDimensions(); $this->clearCustomTrackingParameters(); $this->userAgent = false; - $this->clientHints = false; + $this->clientHints = []; $this->acceptLanguage = false; return true; @@ -2157,7 +2217,8 @@ protected function getBaseUrl(): string MatomoTracker::$URL = \'http://your-website.org/matomo/\';' ); } - if (strpos(self::$URL, '/matomo.php') === false + if ( + strpos(self::$URL, '/matomo.php') === false && strpos(self::$URL, '/proxy-matomo.php') === false ) { self::$URL = rtrim(self::$URL, '/'); @@ -2260,12 +2321,12 @@ protected function getRequest(int $idSite): string if (!empty($this->idPageview)) { $url .= - ($this->networkTime !== false ? '&pf_net=' . ((int)$this->networkTime) : '') . - ($this->serverTime !== false ? '&pf_srv=' . ((int)$this->serverTime) : '') . - ($this->transferTime !== false ? '&pf_tfr=' . ((int)$this->transferTime) : '') . - ($this->domProcessingTime !== false ? '&pf_dm1=' . ((int)$this->domProcessingTime) : '') . - ($this->domCompletionTime !== false ? '&pf_dm2=' . ((int)$this->domCompletionTime) : '') . - ($this->onLoadTime !== false ? '&pf_onl=' . ((int)$this->onLoadTime) : ''); + ($this->networkTime !== null ? '&pf_net=' . ((int)$this->networkTime) : '') . + ($this->serverTime !== null ? '&pf_srv=' . ((int)$this->serverTime) : '') . + ($this->transferTime !== null ? '&pf_tfr=' . ((int)$this->transferTime) : '') . + ($this->domProcessingTime !== null ? '&pf_dm1=' . ((int)$this->domProcessingTime) : '') . + ($this->domCompletionTime !== null ? '&pf_dm2=' . ((int)$this->domCompletionTime) : '') . + ($this->onLoadTime !== null ? '&pf_onl=' . ((int)$this->onLoadTime) : ''); $this->clearPerformanceTimings(); } @@ -2338,7 +2399,7 @@ protected static function getCurrentScriptName(): string if (empty($url) && isset($_SERVER['SCRIPT_NAME'])) { $url = $_SERVER['SCRIPT_NAME']; } elseif (empty($url)) { - $url = '/'; + $url = '/'; } if (!empty($url) && $url[0] !== '/') { @@ -2357,7 +2418,8 @@ protected static function getCurrentScriptName(): string */ protected static function getCurrentScheme(): string { - if (isset($_SERVER['HTTPS']) + if ( + isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] === 'on' || $_SERVER['HTTPS'] === true) ) { return 'https'; @@ -2390,7 +2452,8 @@ protected static function getCurrentHost(): string protected static function getCurrentQueryString(): string { $url = ''; - if (isset($_SERVER['QUERY_STRING']) + if ( + isset($_SERVER['QUERY_STRING']) && !empty($_SERVER['QUERY_STRING']) ) { $url .= '?' . $_SERVER['QUERY_STRING']; @@ -2492,8 +2555,7 @@ public function setOutgoingTrackerCookie($name, $value) { if ($value === null) { unset($this->outgoingTrackerCookies[$name]); - } - else { + } else { $this->outgoingTrackerCookies[$name] = $value; } } @@ -2527,7 +2589,7 @@ protected function parseIncomingCookies(array $headers): void $headerName = 'set-cookie:'; $headerNameLength = strlen($headerName); - foreach($headers as $header) { + foreach ($headers as $header) { if (strpos(strtolower($header), $headerName) !== 0) { continue; } From 15609ca07bad76dbc83bd88f3ade5b2f2e2bc548 Mon Sep 17 00:00:00 2001 From: sgiehl Date: Tue, 8 Oct 2024 15:51:07 +0200 Subject: [PATCH 2/2] adjust changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0c9fa36..0463fe3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ This is the Developer Changelog for Matomo PHP Tracker. All breaking changes or new features are listed below. -## Matomo PHP Tracker 3.4.0 +## Matomo PHP Tracker 4.0.0 ### Changed - a lot of arguments of `MatomoTracker` methods have explicitly types - a lot of `MatomoTracker` method return types have strict types