// Matomo Reporting API Wrapper
// API Reference: https://developer.matomo.org/api-reference/reporting-api
-class Reporting {
+class Reporting
+{
const PERIOD_DAY = 'day';
const PERIOD_WEEK = 'week';
const PERIOD_MONTH = 'month';
$date = self::DATE_YESTERDAY,
$period = self::PERIOD_DAY,
$format = self::FORMAT_JSON
- ) {
+ )
+ {
$this->serverUrl = $baseUrl;
$this->token = $token;
$this->siteId = $siteId;
/**
* @return int
*/
- public function getCacheDuration(): int {
+ public function getCacheDuration(): int
+ {
return $this->cacheDuration;
}
/**
* @param int $cacheDuration
*/
- public function setCacheDuration(int $cacheDuration): void {
+ public function setCacheDuration(int $cacheDuration): void
+ {
$this->cacheDuration = max(1, $cacheDuration);
}
*
* @return string
*/
- public function getToken(): string {
+ public function getToken(): string
+ {
return $this->token;
}
* @param string $token
* @return $this
*/
- public function setToken(string $token): Reporting {
+ public function setToken(string $token): Reporting
+ {
$this->token = $token;
return $this;
*
* @return mixed
*/
- public function getSiteId() {
+ public function getSiteId()
+ {
return $this->siteId;
}
* @param mixed $id
* @return $this
*/
- public function setSiteId($id = null): Reporting {
+ public function setSiteId($id = null): Reporting
+ {
$this->siteId = $id;
return $this;
}
*
* @return string
*/
- public function getFormat(): string {
+ public function getFormat(): string
+ {
return $this->format;
}
* FORMAT_ORIGINAL
* @return $this
*/
- public function setFormat(string $format): Reporting {
+ public function setFormat(string $format): Reporting
+ {
$this->format = $format;
return $this;
}
*
* @return string
*/
- public function getLanguage(): string {
+ public function getLanguage(): string
+ {
return $this->language;
}
* @param string $language
* @return $this
*/
- public function setLanguage(string $language): Reporting {
+ public function setLanguage(string $language): Reporting
+ {
$this->language = $language;
return $this;
}
*
* @return string|null
*/
- public function getDate(): string|null {
+ public function getDate(): string|null
+ {
return $this->date;
}
* DATE_YESTERDAY
* @return $this
*/
- public function setDate(string $date = null): Reporting {
+ public function setDate(string $date = null): Reporting
+ {
$this->date = $date;
return $this;
}
*
* @return string
*/
- public function getPeriod(): string {
+ public function getPeriod(): string
+ {
return $this->period;
}
* PERIOD_RANGE
* @return $this
*/
- public function setPeriod(string $period): Reporting {
+ public function setPeriod(string $period): Reporting
+ {
$this->period = $period;
return $this;
}
/**
* @return string
*/
- public function getServerUrl(): string {
+ public function getServerUrl(): string
+ {
return $this->serverUrl;
}
/**
* @return string
*/
- public function getLastApiUrl(): string {
+ public function getLastApiUrl(): string
+ {
return $this->lastApiUrl;
}
/**
* @return array
*/
- public function getAllSites() {
+ public function getAllSites()
+ {
if (null === $this->allSites) {
$a = $this->apiCall('SitesManager.getAllSites');
$this->allSites = [];
* @param bool $ecommerce
* @return int|null
*/
- public function createSite($name, $url, $search = true, $ecommerce = false) {
+ public function createSite($name, $url, $search = true, $ecommerce = false)
+ {
$res = $this->apiCall('SitesManager.addSite', ['siteName' => $name, 'urls' => $url, 'ecommerce' => $ecommerce, 'siteSearch' => $search]);
return $res['value'] ?? null;
}
* @param false $ecommerce
* @return int|null
*/
- public function createSiteIfNotExistsOrReturnId($name, $url, $search = true, $ecommerce = false) {
+ public function createSiteIfNotExistsOrReturnId($name, $url, $search = true, $ecommerce = false)
+ {
$id = $this->getSiteIdByURL($url);
if (null === $id) {
return $this->createSite($name, $url, $search, $ecommerce);
return $id;
}
- public function deleteSite($siteId) {
+ public function deleteSite($siteId)
+ {
if (isset($this->allSites[$siteId])) {
unset($this->allSites[$siteId]);
}
* @param $url
* @return int|null
*/
- public function getSiteIdByURL($url) {
+ public function getSiteIdByURL($url)
+ {
$res = $this->apiCall('SitesManager.getSitesIdFromSiteUrl', ['url' => $url]);
return $res[0]['idsite'] ?? null;
}
- public function sum($table) {
+ public function sum($table)
+ {
}
- public function getVisits($params = []) {
+ public function getVisits($params = [])
+ {
return $this->apiCall('VisitsSummary.get', $params);
}
- public function getPageViews($params = []) {
+ public function getPageViews($params = [])
+ {
return $this->apiCall('Actions.get', $params);
}
- public function getPageUrls($params = []) {
+ public function getPageUrls($params = [])
+ {
return $this->apiCall('Actions.getPageUrls', $params);
}
- public function getSearchKeywords($params = []) {
+ public function getSearchKeywords($params = [])
+ {
return $this->apiCall('Actions.getSiteSearchKeywords', $params);
}
- public function getEventsByCategory($params = []) {
+ public function getEventsByCategory($params = [])
+ {
// Note: Passing the parameter 'expanded' => 1 will include the sub-table data
return $this->apiCall('Events.getCategory', $params + ['secondaryDimension' => 'eventName', 'expanded' => 0]);
}
- public function getOutlinks($params = []) {
+ public function getOutlinks($params = [])
+ {
return $this->apiCall('Actions.getOutlinks', $params);
}
- public function getCountries($params = []) {
+ public function getCountries($params = [])
+ {
return $this->apiCall('UserCountry.getCountry', $params);
}
- public function getVisitsSummary($params = []) {
+ public function getVisitsSummary($params = [])
+ {
return $this->apiCall('VisitsSummary.get', $params);
}
/**
* @throws \Exception|RequestException
*/
- public function apiCall($method, $params = [], $options = []) {
+ public function apiCall($method, $params = [], $options = [])
+ {
$default = [
'token_auth' => $this->getToken(),
'idSite' => $this->getSiteId(),
$options = array_merge($defaultOptions, $options);
$cacheKey = 'matomo_api_call' . hash('sha256', print_r($options, true));
return Cache::remember($cacheKey, $this->getCacheDuration(), function () use ($options, $method) {
- try {
- $response = $this->client->get('/', $options);
- } catch (RequestException $e) {
- // Just return the error instead of the full message because it contains the URL, which reveals the auth token.
- throw new \Exception("Error: API call for method '{$method}' failed. {$e->getHandlerContext()['error']}");
- }
+ $response = $this->client->get('/', $options);
$API_response = json_decode($response->getBody()->getContents(), true);