PHP code example of pfrenssen / matomo-reporting-api

1. Go to this page and download the library: Download pfrenssen/matomo-reporting-api library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

pfrenssen / matomo-reporting-api example snippets




use Matomo\ReportingApi\QueryFactory;

 HTTPS!*
$matomo_url = 'https://my.matomo.server';

// The user authentication token. You can get it in the web interface of the
// Matomo server at Administration > Platform > API.
$token = 'e0357ffdf830ca8be8af4151b564b53d';

// Instantiate the query factory. This class helps to quickly generate
// different query objects with reusable default parameters.
$query_factory = QueryFactory::create($matomo_url);

// Set some default parameters such as the site ID and user authentication
// token. 
$query_factory
    ->set('idSite', 1)
    ->set('token_auth', $token);

// Example: retrieve the version of the Matomo server.
$query = $query_factory->getQuery('API.getMatomoVersion');
$response = $query->execute()->getResponse();
$matomo_version = $response->value;

echo "Server is running Matomo $matomo_version.\n\n";

// Example: retrieve browser usage statistics for the past week.
$response = $query_factory->getQuery('DevicesDetection.getBrowsers')
  ->setParameter('date', 'today')
  ->setParameter('period', 'week')
  ->execute()
  ->getResponse();

foreach ($response as $browser) {
  echo "Browser: {$browser->label}\n";
  echo "Total visits: {$browser->nb_visits}\n";
  echo "Bounce count: {$browser->bounce_count}\n";
  echo "Daily unique visitors: {$browser->sum_daily_nb_uniq_visitors}\n\n";
}