Skip to content

Commit 9462dc6

Browse files
lutdevsgiehlmichalkleineralutskevychcheck24
authored
Fix: Not possible to create multiple Piwik tracker instances having different API urls (#145)
* Allow setting form factors client hint (#136) * Allow settting form factors client hint * update changelog * Fix handling of some attributes * Apply review feedback Co-authored-by: Michal Kleiner <[email protected]> --------- Co-authored-by: Michal Kleiner <[email protected]> * deprecate $URL * use old $URL if apiUrl is empty --------- Co-authored-by: Stefan Giehl <[email protected]> Co-authored-by: Michal Kleiner <[email protected]> Co-authored-by: Andrii Lutskevych <[email protected]>
1 parent b029aa2 commit 9462dc6

File tree

3 files changed

+57
-10
lines changed

3 files changed

+57
-10
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,19 @@ This is the Developer Changelog for Matomo PHP Tracker. All breaking changes or
44

55
## Matomo PHP Tracker 3.4.0
66
### Changed
7+
8+
- Fixed PHP 8.5 deprecation notice
9+
- static `$URL` is deprecated
710
- a lot of arguments of `MatomoTracker` methods have explicitly types
811
- a lot of `MatomoTracker` method return types have strict types
912

13+
### Added
14+
- new private property `apiUrl` for storing API URL
15+
16+
## Matomo PHP Tracker 3.3.2
17+
### Changed
18+
- Support for formFactors client hint parameter, supported as of Matomo 5.2.0
19+
1020
## Matomo PHP Tracker 3.3.1
1121
### Fixed
1222
- closed curl connection

MatomoTracker.php

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class MatomoTracker
2828
* MatomoTracker::$URL = 'http://yourwebsite.org/matomo/';
2929
*
3030
* @var string
31+
* @deprecated
3132
*/
3233
static public $URL = '';
3334

@@ -202,6 +203,8 @@ class MatomoTracker
202203

203204
private $requestMethod = null;
204205

206+
private $apiUrl = '';
207+
205208
/**
206209
* Builds a MatomoTracker object, used to track visits, pages and Goal conversions
207210
* for a specific website, by using the Matomo Tracking API.
@@ -223,10 +226,12 @@ public function __construct(int $idSite, string $apiUrl = '')
223226
!empty($_SERVER['HTTP_SEC_CH_UA_PLATFORM']) ? $_SERVER['HTTP_SEC_CH_UA_PLATFORM'] : '',
224227
!empty($_SERVER['HTTP_SEC_CH_UA_PLATFORM_VERSION']) ? $_SERVER['HTTP_SEC_CH_UA_PLATFORM_VERSION'] : '',
225228
!empty($_SERVER['HTTP_SEC_CH_UA_FULL_VERSION_LIST']) ? $_SERVER['HTTP_SEC_CH_UA_FULL_VERSION_LIST'] : '',
226-
!empty($_SERVER['HTTP_SEC_CH_UA_FULL_VERSION']) ? $_SERVER['HTTP_SEC_CH_UA_FULL_VERSION'] : ''
229+
!empty($_SERVER['HTTP_SEC_CH_UA_FULL_VERSION']) ? $_SERVER['HTTP_SEC_CH_UA_FULL_VERSION'] : '',
230+
!empty($_SERVER['HTTP_SEC_CH_UA_FORM_FACTORS']) ? $_SERVER['HTTP_SEC_CH_UA_FORM_FACTORS'] : ''
227231
);
228232
if (!empty($apiUrl)) {
229233
self::$URL = $apiUrl;
234+
$this->apiUrl = $apiUrl;
230235
}
231236

232237
$this->setNewVisitorId();
@@ -240,6 +245,7 @@ public function __construct(int $idSite, string $apiUrl = '')
240245
public function setApiUrl(string $url): void
241246
{
242247
self::$URL = $url;
248+
$this->apiUrl = $url;
243249
}
244250

245251
/**
@@ -593,6 +599,8 @@ public function setUserAgent(string $userAgent)
593599
* or an array containing all brands with the structure
594600
* [['brand' => 'Chrome', 'version' => '10.0.2'], ['brand' => '...]
595601
* @param string $uaFullVersion Value of the header 'HTTP_SEC_CH_UA_FULL_VERSION'
602+
* @param string|array<string> $formFactors Value of the header 'HTTP_SEC_CH_UA_FORM_FACTORS'
603+
* or an array containing all form factors with structure ["Desktop", "XR"]
596604
*
597605
* @return $this
598606
*/
@@ -601,7 +609,8 @@ public function setClientHints(
601609
string $platform = '',
602610
string $platformVersion = '',
603611
$fullVersionList = '',
604-
string $uaFullVersion = ''
612+
string $uaFullVersion = '',
613+
$formFactors = ''
605614
) {
606615
if (is_string($fullVersionList)) {
607616
$reg = '/^"([^"]+)"; ?v="([^"]+)"(?:, )?/';
@@ -617,12 +626,25 @@ public function setClientHints(
617626
$fullVersionList = [];
618627
}
619628

629+
if (is_string($formFactors)) {
630+
$formFactors = explode(',', $formFactors);
631+
$formFactors = array_filter(array_map(
632+
function ($item) {
633+
return trim($item, '" ');
634+
},
635+
$formFactors
636+
));
637+
} elseif (!is_array($formFactors)) {
638+
$formFactors = [];
639+
}
640+
620641
$this->clientHints = array_filter([
621642
'model' => $model,
622643
'platform' => $platform,
623644
'platformVersion' => $platformVersion,
624645
'uaFullVersion' => $uaFullVersion,
625646
'fullVersionList' => $fullVersionList,
647+
'formFactors' => $formFactors,
626648
]);
627649

628650
return $this;
@@ -810,7 +832,7 @@ public function doTrackPageView(string $documentTitle)
810832

811833
return $this->sendRequest($url);
812834
}
813-
835+
814836
/**
815837
* Override PageView id for every use of `doTrackPageView()`. Do not use this if you call `doTrackPageView()`
816838
* multiple times during tracking (if, for example, you are tracking a single page application).
@@ -2097,7 +2119,7 @@ protected function sendRequest(string $url, string $method = 'GET', $data = null
20972119
curl_setopt_array($ch, $options);
20982120
ob_start();
20992121
$response = @curl_exec($ch);
2100-
2122+
21012123
try {
21022124
$header = '';
21032125

@@ -2152,23 +2174,29 @@ protected function getTimestamp()
21522174

21532175
/**
21542176
* Returns the base URL for the Matomo server.
2177+
*
2178+
* @throws Exception
21552179
*/
21562180
protected function getBaseUrl(): string
21572181
{
2158-
if (empty(self::$URL)) {
2182+
$apiUrl = $this->apiUrl === ''
2183+
? self::$URL
2184+
: $this->apiUrl;
2185+
2186+
if ($apiUrl === '') {
21592187
throw new Exception(
21602188
'You must first set the Matomo Tracker URL by calling
21612189
MatomoTracker::$URL = \'http://your-website.org/matomo/\';'
21622190
);
21632191
}
2164-
if (strpos(self::$URL, '/matomo.php') === false
2165-
&& strpos(self::$URL, '/proxy-matomo.php') === false
2192+
if (strpos($apiUrl, '/matomo.php') === false
2193+
&& strpos($apiUrl, '/proxy-matomo.php') === false
21662194
) {
2167-
self::$URL = rtrim(self::$URL, '/');
2168-
self::$URL .= '/matomo.php';
2195+
$apiUrl = rtrim($apiUrl, '/');
2196+
$apiUrl .= '/matomo.php';
21692197
}
21702198

2171-
return self::$URL;
2199+
return $apiUrl;
21722200
}
21732201

21742202
/**

tests/Unit/MatomoTrackerTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,13 @@ public function test_setApiUrl()
8484

8585
$this->assertSame(substr($url, 0, strlen($newApiUrl)), $newApiUrl);
8686
}
87+
88+
public function testUsageApiUrl(): void
89+
{
90+
$newApiUrl = 'https://NEW-API-URL.com';
91+
$tracker = new \MatomoTracker(1, $newApiUrl);
92+
$url = $tracker->getUrlTrackPageView('test title');
93+
94+
$this->assertSame(substr($url, 0, strlen($newApiUrl)), $newApiUrl);
95+
}
8796
}

0 commit comments

Comments
 (0)