Querying the Reporting API

This guide explains how to call the Matomo (formerly Piwik) API to request your web analytics data.

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:

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> (14556 hits)<br>
<b>matomo dsgvo</b> (3 hits)<br>
<b>xxx_com</b> (2 hits)<br>
<b>1.1.1.logetin</b> (1 hits)<br>
<b>always_populate_raw_post_data&#039; to &#039;-1&#039; in php.ini</b> (1 hits)<br>
<b>cannot redeclare class in php</b> (1 hits)<br>
<b>does xampp support pdo</b> (1 hits)<br>
<b>drupal statistiques</b> (1 hits)<br>
<b>failed (104: connection reset by peer) while reading response header from up</b> (1 hits)<br>
<b>file config.ini not exists or invalid</b> (1 hits)<br>
<b>geoip file too big</b> (1 hits)<br>
<b>gerwan hompage bearbeiten</b> (1 hits)<br>
<b>how to create individual dashboards to users in website</b> (1 hits)<br>
<b>how to send fake debit alert</b> (1 hits)<br>
<b>http //1.1.1/login.php</b> (1 hits)<br>
<b>https //1.1.1/logout.html</b> (1 hits)<br>
<b>https//muchhetont.ru/hitsup.js/hits.php?sid=89815</b> (1 hits)<br>
<b>https://1.1.1</b> (1 hits)<br>
<b>https://forum.matomo.org/t/does-matomo-opt-out-disable-tracking-completely/28280</b> (1 hits)<br>
<b>https://forum.matomo.org/t/free-open-source-matomo-tag-manager-now-available-as-a-beta/28684</b> (1 hits)<br>

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.