Functions you can call in your Piwik plugin classes

This page aims to list the different functions you can use when programming plugins for Piwik.
Be careful, the following APIs may change in the near future as Piwik is still in development.

General

Accessible from your plugin controller

$this->date = current selected Piwik_Date object (documentation)
$period = Piwik_Common::getRequestVar("period"); - Get the current selected period
$idSite = Piwik_Common::getRequestVar("idSite"); - Get the selected idSite
$site = new Piwik_Site($idSite); - Build the Piwik_Site object (documentation)
$this->str_date = current selected date in YYYY-MM-DD format

Misc

Piwik_AddMenu( $mainMenuName, $subMenuName, $url ); - Adds an entry to the menu in the Piwik interface (See the example in the UserCountry Plugin file)
Piwik_AddWidget( $pluginName, $controllerMethodToCall, $widgetTitle ); - Adds an entry to the menu in the Piwik interface (See the example in the UserCountry Plugin file)
Piwik::prefixTable("site") = piwik_site

User access

Piwik::getCurrentUserLogin() = anonymous
Piwik::isUserHasSomeAdminAccess() = false
Piwik::isUserHasAdminAccess( array $idSites = array(1,2) ) = false
Piwik::isUserHasViewAccess( array $idSites = array(1) ) = true
Piwik::isUserIsSuperUser() = false

Execute SQL queries

Piwik_FetchOne("SELECT token_auth FROM piwik_user WHERE login = ?", array("anonymous")) = 'anonymous'

$query = Piwik_Query("SELECT token_auth FROM piwik_user WHERE login = ?", array("anonymous"))
$fetched = $query->fetch();
At this point, we have: $fetched['token_auth'] == 'anonymous'

Example Sites information API

Piwik_SitesManager_API::getSitesWithViewAccess() =
array (
  0 => 
  array (
    'idsite' => '1',
    'name' => 'piwik.org',
    'main_url' => 'http://piwik1.org',
    'ts_created' => '2008-01-12 04:21:34',
    'feedburnerName' => 'Piwik',
  ),
  1 => 
  array (
    'idsite' => '3',
    'name' => 'virtual-drums.com',
    'main_url' => 'http://www.virtual-drums.com',
    'ts_created' => '2008-01-27 01:41:53',
    'feedburnerName' => 'Piwik',
  ),
)

Piwik_SitesManager_API::getSitesWithAdminAccess() =
array (
)

Example API Users information

View the list of API methods you can call on API reference
For example you can try Piwik_UsersManager_API::getUsersSitesFromAccess("view"); or Piwik_UsersManager_API::deleteUser("userToDelete");

Smarty plugins

There are some builtin plugins for Smarty especially developped for Piwik.
You can find them on the SVN at /trunk/modules/SmartyPlugins.
More documentation to come about smarty plugins.

Existing Piwik Plugins

  • Have a look at the already long Piwik plugins list bundled with the Piwik package. It will help you to build yours!