Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve handling of certain attributes #137

Draft
wants to merge 2 commits into
base: 4.x-dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
158 changes: 110 additions & 48 deletions MatomoTracker.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Matomo - free/libre analytics platform
*
Expand All @@ -19,7 +20,6 @@
* @package MatomoTracker
* @api
*/
#[AllowDynamicProperties]
class MatomoTracker
{
/**
Expand All @@ -29,7 +29,7 @@ class MatomoTracker
*
* @var string
*/
static public $URL = '';
public static $URL = '';

/**
* API Version
Expand Down Expand Up @@ -79,33 +79,75 @@ class MatomoTracker

public $forcedDatetime = false;

/**
* @var bool
*/
public $forcedNewVisit = false;

public $networkTime = false;
/**
* @var int|null
*/
public $networkTime = null;

public $serverTime = false;
/**
* @var int|null
*/
public $serverTime = null;

public $transferTime = false;
/**
* @var int|null
*/
public $transferTime = null;

public $domProcessingTime = false;
/**
* @var int|null
*/
public $domProcessingTime = null;

public $domCompletionTime = false;
/**
* @var int|null
*/
public $domCompletionTime = null;

public $onLoadTime = false;
/**
* @var int|null
*/
public $onLoadTime = null;

/**
* @var array
*/
public $pageCustomVar = [];

/**
* @var array
*/
public $ecommerceView = [];

/**
* @var array
*/
public $customParameters = [];

/**
* @var array
*/
public $customDimensions = [];

public $customData = false;
/**
* @var string
*/
public $customData = '';

public $hasCookies = false;
/**
* @var bool|null
*/
public $hasCookies = null;

public $token_auth = false;
/**
* @var string|null
*/
protected $token_auth = null;

public $userAgent = false;

Expand Down Expand Up @@ -147,7 +189,10 @@ class MatomoTracker

public $acceptLanguage;

public $clientHints = [];
/**
* @var array
*/
protected $clientHints = [];

// Life of the visitor cookie (in sec)
public $configVisitorCookieTimeout = 33955200; // 13 months (365 + 28 days)
Expand All @@ -159,12 +204,15 @@ class MatomoTracker
public $configReferralCookieTimeout = 15768000; // 6 months

// Visitor Ids in order
public $userId = false;

/**
* @var string|null
*/
protected $userId = null;

public $forcedVisitorId = false;

public $cookieVisitorId = false;

public $randomVisitorId = false;

public $configCookiesDisabled = false;
Expand Down Expand Up @@ -233,7 +281,7 @@ public function __construct(int $idSite, string $apiUrl = '')

$this->currentTs = time();
$this->createTs = $this->currentTs;

$this->visitorCustomVar = $this->getCustomVariablesFromCookie();
}

Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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).
Expand All @@ -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()
Expand Down Expand Up @@ -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.");
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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, '/');
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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] !== '/') {
Expand All @@ -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';
Expand Down Expand Up @@ -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'];
Expand Down Expand Up @@ -2492,8 +2555,7 @@ public function setOutgoingTrackerCookie($name, $value)
{
if ($value === null) {
unset($this->outgoingTrackerCookies[$name]);
}
else {
} else {
$this->outgoingTrackerCookies[$name] = $value;
}
}
Expand Down Expand Up @@ -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;
}
Expand Down
Loading