Piwik Analytics API – Calling Techniques

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

  • using the standard REST API over HTTP
  • using the Piwik PHP library files directly

Call the Piwik API using the REST API over HTTP

If you want to request data in any language (PHP, Python, Ruby, ASP, C++, Java, etc.) you can use the REST API. It is a simple way to request data via standard 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 your 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=1
$url = "http://demo.piwik.org/";
$url .= "?module=API&method=Referers.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:

Keywords for the last month

Keyword not defined (9473 hits)
all website (103 hits)
downloads anzeigen (50 hits)
all websites (42 hits)
sqlstate[42s02] piwik (19 hits)
piwik forums (15 hits)
piwik forum (11 hits)
unique pageviews (11 hits)
piwik widgets cannot be created without an uniqueid (10 hits)
ssl proxy (8 hits)
all webside (7 hits)
all web sit (6 hits)
allwebsite (6 hits)
how to enable pdo in php (6 hits)
open web analytics vs piwik (6 hits)
redirect in javascript (6 hits)
unique page views (6 hits)
22658 segmentation fault (5 hits)
alle downloads (5 hits)
alle downloads anzeigen (5 hits)

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 have to use this technique.

<?php
// if you don't include 'index.php', you must also define PIWIK_DOCUMENT_ROOT
// and include "libs/upgradephp/upgrade.php" and "core/Loader.php"
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";

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

// This inits the API Request with the specified parameters
$request = new Piwik_API_Request('
			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>1450</nb_visits>
		<nb_actions>3320</nb_actions>
		<max_actions>68</max_actions>
		<sum_visit_length>316797</sum_visit_length>
		<bounce_count>965</bounce_count>
		<nb_visits_converted>33</nb_visits_converted>
		<sum_daily_nb_uniq_visitors>1250</sum_daily_nb_uniq_visitors>
	</row>
	<row>
		<label>1366x768</label>
		<nb_visits>1059</nb_visits>
		<nb_actions>1935</nb_actions>
		<max_actions>26</max_actions>
		<sum_visit_length>122844</sum_visit_length>
		<bounce_count>786</bounce_count>
		<nb_visits_converted>25</nb_visits_converted>
		<sum_daily_nb_uniq_visitors>960</sum_daily_nb_uniq_visitors>
	</row>
	<row>
		<label>1680x1050</label>
		<nb_visits>696</nb_visits>
		<nb_actions>1506</nb_actions>
		<max_actions>26</max_actions>
		<sum_visit_length>121043</sum_visit_length>
		<bounce_count>444</bounce_count>
		<nb_visits_converted>11</nb_visits_converted>
		<sum_daily_nb_uniq_visitors>574</sum_daily_nb_uniq_visitors>
	</row>
</result>

Include Piwik in your PHP project

You can also customize Piwik in more advanced ways, such as overriding constants, customizing the config file, and more. Learn more about Piwik advanced integration.

Feedback on this page

Have you found an error in this page, or do you think some information is missing or not clear? We appreciate you taking the time to send us your suggestions and feedback on this page.