@@ -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 /**
0 commit comments