Querying the Reporting API

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

Call the Piwik 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 Piwik interface
$token_auth = 'anonymous';

// we call the REST API and request the 100 first keywords for the last month for the idsite=7
$url = "http://demo.piwik.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>");
foreach ($content as $row) {
    $keyword = htmlspecialchars(html_entity_decode(urldecode($row['label']), ENT_QUOTES), ENT_QUOTES);
    $hits = $row['nb_visits'];

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

Here is the output of this code:

<h1>Keywords for the last month</h1><b>Keyword not defined</b> (23617 hits)<br><b>xxxxxx</b> (32 hits)<br><b>magento 1142 trigger command denied to user</b> (9 hits)<br><b>downloads anzeigen</b> (6 hits)<br><b>xxx.xxx</b> (6 hits)<br><b>www.xxx</b> (5 hits)<br><b>forum</b> (4 hits)<br><b>piwik</b> (4 hits)<br><b>www.xxxxx.com</b> (4 hits)<br><b>xxx_com</b> (4 hits)<br><b>failed to load html file: please check your server configuration. you may want to whitelist &quot;*.html&quot; files from the &quot;plugins&quot; directory. the http status code is 403 for url &quot;plugins/zenmode/angularjs/quick-access/quick-access.html&quot;</b> (3 hits)<br><b>http://forum.piwik.org/read.php?2,90686</b> (3 hits)<br><b>piwik adwords</b> (3 hits)<br><b>piwik forum</b> (3 hits)<br><b>piwik fran&ccedil;ais</b> (3 hits)<br><b>run piwik on iis</b> (3 hits)<br><b>sharepoint 2013 logon tracker</b> (3 hits)<br><b>alle downloads</b> (2 hits)<br><b>all website</b> (2 hits)<br><b>all website show</b> (2 hits)<br>

Call the Piwik API in PHP

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

If you are developing a plugin, you should be using this technique.

<?php
use Piwik\API\Request;
use Piwik\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);

// if you prefer not to include 'index.php', you must also define here PIWIK_DOCUMENT_ROOT
// and include "libs/upgradephp/upgrade.php" and "core/Loader.php"
require_once PIWIK_INCLUDE_PATH . "/index.php";
require_once PIWIK_INCLUDE_PATH . "/core/API/Request.php";

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

// This inits the API Request with the specified parameters
$request = new Request('
			module=API
			&method=UserSettings.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>1221</nb_visits>
		<nb_actions>2896</nb_actions>
		<max_actions>68</max_actions>
		<sum_visit_length>231742</sum_visit_length>
		<bounce_count>789</bounce_count>
		<nb_visits_converted>24</nb_visits_converted>
		<sum_daily_nb_uniq_visitors>1071</sum_daily_nb_uniq_visitors>
	</row>
	<row>
		<label>1366x768</label>
		<nb_visits>634</nb_visits>
		<nb_actions>1251</nb_actions>
		<max_actions>41</max_actions>
		<sum_visit_length>80345</sum_visit_length>
		<bounce_count>469</bounce_count>
		<nb_visits_converted>17</nb_visits_converted>
		<sum_daily_nb_uniq_visitors>565</sum_daily_nb_uniq_visitors>
	</row>
	<row>
		<label>1680x1050</label>
		<nb_visits>388</nb_visits>
		<nb_actions>981</nb_actions>
		<max_actions>79</max_actions>
		<sum_visit_length>58533</sum_visit_length>
		<bounce_count>244</bounce_count>
		<nb_visits_converted>13</nb_visits_converted>
		<sum_daily_nb_uniq_visitors>333</sum_daily_nb_uniq_visitors>
	</row>
</result>