Calling the API
This page explains the two ways to call the Piwik API to request your web analytics data: internal call or external (internet http) call
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.
You can for example get the 100 best search engines keywords used on your website during the current week. Here is an example in PHP:
<?php // 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://piwik.org/demo/"; $url .= "?module=API&method=Referers.getKeywords"; $url .= "&idSite=1&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 = urldecode($row['label']); $hits = $row['nb_visits']; print("<b>$keyword</b> ($hits hits)<br>"); }
Here is the output of this code:
Keywords for the last month
piwik (5372 hits)web analytics (166 hits)
piwiki (146 hits)
open source analytics (144 hits)
piwik plugins (99 hits)
pwik (89 hits)
piwik.org (80 hits)
open source web analytics (79 hits)
google analytics alternative (76 hits)
piwik analytics (61 hits)
analytics (60 hits)
piwik api (50 hits)
free web analytics (43 hits)
open source google analytics (37 hits)
piwik download (36 hits)
piwik wordpress (32 hits)
website analytics (29 hits)
login screen design (27 hits)
google analytics open source (27 hits)
undefined login (25 hits)
Call the Piwik API in PHP
This is the most efficient technique as it doesn't go over http. You basically directly call the PHP runtime and get the PHP data structure back.
If you want to request data in a PHP script that is on the same server as Piwik, you can use this simple technique.
If you are developing a plugin, you have to use this technique.
<?php define('PIWIK_INCLUDE_PATH', '..'); define('PIWIK_ENABLE_DISPATCH', false); define('PIWIK_ENABLE_ERROR_HANDLER', false); require_once PIWIK_INCLUDE_PATH . "/index.php"; require_once PIWIK_INCLUDE_PATH . "/core/API/Request.php"; Piwik_FrontController::getInstance()->init(); // We call the API from a php code // it will check that you have the necessary rights // - either you are loggued in piwik and have a cookie in your browser // - or you replace the token_auth=xxx to the request string to authenticate $request = new Piwik_API_Request(' method=UserSettings.getResolution &idSite=1 &date=yesterday &period=week &format=XML &filter_limit=3 &token_auth=anonymous '); $result = $request->process(); echo $result;
Here is the output of this script:
<?xml version="1.0" encoding="utf-8" ?>
<result>
<row>
<label>1280x1024</label>
<nb_visits>3566</nb_visits>
<nb_actions>13023</nb_actions>
<max_actions>690</max_actions>
<sum_visit_length>971375</sum_visit_length>
<bounce_count>1817</bounce_count>
<nb_visits_converted>1029</nb_visits_converted>
<sum_daily_nb_uniq_visitors>2986</sum_daily_nb_uniq_visitors>
</row>
<row>
<label>1680x1050</label>
<nb_visits>3165</nb_visits>
<nb_actions>11719</nb_actions>
<max_actions>379</max_actions>
<sum_visit_length>878079</sum_visit_length>
<bounce_count>1550</bounce_count>
<nb_visits_converted>881</nb_visits_converted>
<sum_daily_nb_uniq_visitors>2587</sum_daily_nb_uniq_visitors>
</row>
<row>
<label>1280x800</label>
<nb_visits>3098</nb_visits>
<nb_actions>8437</nb_actions>
<max_actions>348</max_actions>
<sum_visit_length>615823</sum_visit_length>
<bounce_count>1749</bounce_count>
<nb_visits_converted>841</nb_visits_converted>
<sum_daily_nb_uniq_visitors>2609</sum_daily_nb_uniq_visitors>
</row>
</result>
Include Piwik from your PHP project
You can also directly include Piwik index.php in your project. Learn more about this advanced integration.
See also
Find other documentation on Piwik web analytics API
