Skip to content

Commit

Permalink
Merge pull request #104 from matomo-org/clienthints
Browse files Browse the repository at this point in the history
Adds support for client hints
  • Loading branch information
sgiehl committed Jul 4, 2022
2 parents b56acfd + 9c2ff78 commit 0001560
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions MatomoTracker.php
Expand Up @@ -121,6 +121,14 @@ public function __construct($idSite, $apiUrl = '')
$this->ip = !empty($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : false;
$this->acceptLanguage = !empty($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : false;
$this->userAgent = !empty($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : false;
$this->clientHints = [];
$this->setClientHints(
!empty($_SERVER['HTTP_SEC_CH_UA_MODEL']) ? $_SERVER['HTTP_SEC_CH_UA_MODEL'] : '',
!empty($_SERVER['HTTP_SEC_CH_UA_PLATFORM']) ? $_SERVER['HTTP_SEC_CH_UA_PLATFORM'] : '',
!empty($_SERVER['HTTP_SEC_CH_UA_PLATFORM_VERSION']) ? $_SERVER['HTTP_SEC_CH_UA_PLATFORM_VERSION'] : '',
!empty($_SERVER['HTTP_SEC_CH_UA_FULL_VERSION_LIST']) ? $_SERVER['HTTP_SEC_CH_UA_FULL_VERSION_LIST'] : '',
!empty($_SERVER['HTTP_SEC_CH_UA_FULL_VERSION']) ? $_SERVER['HTTP_SEC_CH_UA_FULL_VERSION'] : ''
);
if (!empty($apiUrl)) {
self::$URL = $apiUrl;
}
Expand Down Expand Up @@ -482,6 +490,49 @@ public function setUserAgent($userAgent)
return $this;
}

/**
* Sets the client hints, used to detect OS and browser.
* If this function is not called, the client hints sent with the current request will be used.
*
* Supported as of Matomo 4.12.0
*
* @param string $model Value of the header 'HTTP_SEC_CH_UA_MODEL'
* @param string $platform Value of the header 'HTTP_SEC_CH_UA_PLATFORM'
* @param string $platformVersion Value of the header 'HTTP_SEC_CH_UA_PLATFORM_VERSION'
* @param string|array $fullVersionList Value of header 'HTTP_SEC_CH_UA_FULL_VERSION_LIST' or an array containing
* all brands with the structure
* [['brand' => 'Chrome', 'version' => '10.0.2'], ['brand' => '...]
* @param string $uaFullVersion Value of the header 'HTTP_SEC_CH_UA_FULL_VERSION'
*
* @return $this
*/
public function setClientHints($model = '', $platform = '', $platformVersion = '', $fullVersionList = '', $uaFullVersion = '')
{
if (is_string($fullVersionList)) {
$reg = '/^"([^"]+)"; ?v="([^"]+)"(?:, )?/';
$list = [];

while (\preg_match($reg, $fullVersionList, $matches)) {
$list[] = ['brand' => $matches[1], 'version' => $matches[2]];
$fullVersionList = \substr($fullVersionList, \strlen($matches[0]));
}

$fullVersionList = $list;
} elseif (!is_array($fullVersionList)) {
$fullVersionList = [];
}

$this->clientHints = array_filter([
'model' => $model,
'platform' => $platform,
'platformVersion' => $platformVersion,
'uaFullVersion' => $uaFullVersion,
'fullVersionList' => $fullVersionList,
]);

return $this;
}

/**
* 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.
Expand Down Expand Up @@ -1709,6 +1760,7 @@ protected function sendRequest($url, $method = 'GET', $data = null, $force = fal
$this->clearCustomDimensions();
$this->clearCustomTrackingParameters();
$this->userAgent = false;
$this->clientHints = false;
$this->acceptLanguage = false;

return true;
Expand Down Expand Up @@ -1902,6 +1954,9 @@ protected function getRequest($idSite)
$customFields . $customDimensions .
(!$this->sendImageResponse ? '&send_image=0' : '') .

// client hints
(!empty($this->clientHints) ? ('&uadata=' . urlencode(json_encode($this->clientHints))) : '') .

// DEBUG
$this->DEBUG_APPEND_URL;

Expand Down

0 comments on commit 0001560

Please sign in to comment.