Querying the Reporting API

This guide explains how to call the Matomo (formerly Piwik) API to request your web analytics data. There are two methods:

  • using the standard HTTP API
  • using Matomo's PHP code directly

Call the Matomo API using the HTTP API

If you want to request data in any language (PHP, Python, Ruby, ASP, C++, Java, etc.) you can use the HTTP API. It is a simple way to request data via an HTTP GET.

Security Notice

If the API call requires the token_auth and the HTTP request is sent over untrusted networks, we highly advise that you use an encrypted request. Otherwise, your token_auth is exposed to eavesdroppers. This can be done using https instead of http. In the following example, replace the string "http" by "https".

You can, for example, get the top 100 search engine keywords used to find your website during the current week. Here is an example in PHP:

<?php
exit; // REMOVE this line to run the script

// this token is used to authenticate your API request.
// You can get the token on the API page inside your Matomo interface
$token_auth = 'anonymous';

// we call the REST API and request the 100 first keywords for the last month for the idsite=7
$url = "https://demo.matomo.org/";
$url .= "?module=API&method=Referrers.getKeywords";
$url .= "&idSite=7&period=month&date=yesterday";
$url .= "&format=PHP&filter_limit=20";
$url .= "&token_auth=$token_auth";

$fetched = file_get_contents($url);
$content = unserialize($fetched);

// case error
if (!$content) {
    print("Error, content fetched = " . $fetched);
}

print("<h1>Keywords for the last month</h1>\n");
foreach ($content as $row) {
    $keyword = htmlspecialchars(html_entity_decode(urldecode($row['label']), ENT_QUOTES, 'UTF-8'), ENT_QUOTES, 'UTF-8');
    $hits = $row['nb_visits'];

    print("<b>$keyword</b> ($hits hits)<br>\n");
}


Here is the output of this code:

<h1>Keywords for the last month</h1>
<b>Keyword not defined</b> (10783 hits)<br>
<b>amazon</b> (2 hits)<br>
<b>https://forum.piwik.org/t/piwik-is-now-matomo/26830</b> (2 hits)<br>
<b>piwwik laravel</b> (2 hits)<br>
<b>1.1.1 login</b> (1 hits)<br>
<b>about:firefox1234</b> (1 hits)<br>
<b>access denied for&quot;@&quot;localhost</b> (1 hits)<br>
<b>always_populate_raw_post_data</b> (1 hits)<br>
<b>android facebook user agent</b> (1 hits)<br>
<b>beeg http://join.passion-hd.com/track/videos;37976:ppsbeta:passionhd,26/</b> (1 hits)<br>
<b>change language settings in google chrome</b> (1 hits)<br>
<b>check for dead links</b> (1 hits)<br>
<b>chroot environment</b> (1 hits)<br>
<b>connection timed out after 3000 milliseconds</b> (1 hits)<br>
<b>decode piwik</b> (1 hits)<br>
<b>downlov ...</b> (1 hits)<br>
<b>feature &#20013;&#25991;</b> (1 hits)<br>
<b>hosteurope</b> (1 hits)<br>
<b>how to check if piwik is working</b> (1 hits)<br>
<b>https://forum.matomo.org/t/goal-tracking-does-not-work-with-php-redirects/7042</b> (1 hits)<br>

Call the Matomo API in PHP

If you want to request data in a PHP script that is on the same server as Matomo, you can use this simple technique. This is a more efficient solution as it doesn't require network calls. You directly call the PHP Matomo runtime and get the PHP data structure back.

If you are developing a plugin, you should be using this technique. Please note that including index.php will set the default timezone of the calling code to UTC.

<?php
use Matomo\API\Request;
use Matomo\FrontController;

define('PIWIK_INCLUDE_PATH', realpath('../..'));
define('PIWIK_USER_PATH', realpath('../..'));
define('PIWIK_ENABLE_DISPATCH', false);
define('PIWIK_ENABLE_ERROR_HANDLER', false);
define('PIWIK_ENABLE_SESSION_START', false);

require_once PIWIK_INCLUDE_PATH . "/index.php";
require_once PIWIK_INCLUDE_PATH . "/core/API/Request.php";

$environment = new \Piwik\Application\Environment(null);
$environment->init();

FrontController::getInstance()->init();

// This inits the API Request with the specified parameters
$request = new Request('
			module=API
			&method=Resolution.getResolution
			&idSite=7
			&date=yesterday
			&period=week
			&format=XML
			&filter_limit=3
			&token_auth=anonymous
');
// Calls the API and fetch XML data back
$result = $request->process();
echo $result;


Here is the output of this script:

<?xml version="1.0" encoding="utf-8" ?>
<result>
	<row>
		<label>1920x1080</label>
		<nb_visits>1407</nb_visits>
		<nb_actions>2823</nb_actions>
		<max_actions>46</max_actions>
		<sum_visit_length>214019</sum_visit_length>
		<bounce_count>886</bounce_count>
		<nb_visits_converted>1</nb_visits_converted>
		<sum_daily_nb_uniq_visitors>1220</sum_daily_nb_uniq_visitors>
		<sum_daily_nb_users>0</sum_daily_nb_users>
		<segment>resolution==1920x1080</segment>
	</row>
	<row>
		<label>1366x768</label>
		<nb_visits>593</nb_visits>
		<nb_actions>1049</nb_actions>
		<max_actions>38</max_actions>
		<sum_visit_length>74178</sum_visit_length>
		<bounce_count>429</bounce_count>
		<nb_visits_converted>0</nb_visits_converted>
		<sum_daily_nb_uniq_visitors>521</sum_daily_nb_uniq_visitors>
		<sum_daily_nb_users>0</sum_daily_nb_users>
		<segment>resolution==1366x768</segment>
	</row>
	<row>
		<label>1920x1200</label>
		<nb_visits>337</nb_visits>
		<nb_actions>860</nb_actions>
		<max_actions>30</max_actions>
		<sum_visit_length>77853</sum_visit_length>
		<bounce_count>167</bounce_count>
		<nb_visits_converted>0</nb_visits_converted>
		<sum_daily_nb_uniq_visitors>282</sum_daily_nb_uniq_visitors>
		<sum_daily_nb_users>0</sum_daily_nb_users>
		<segment>resolution==1920x1200</segment>
	</row>
</result>

Learn more

To learn more about all the options available when calling the reporting API, read the Reporting API reference.

You can also have a look at the list of client libraries available to call the reporting API.