How to build a very simple Piwik plugin

For a simple Plugin example, you can have a look at the [source:/trunk/plugins/ExamplePlugin/ExamplePlugin.php ExamplePlugin]. The file contains two classes:

  • Piwik_ExamplePlugin which is the main Plugin file

It contains the global information about the plugin, and some generic methods such as getInformation(), install(), uninstall(), etc. View the Piwik_Plugin abstract class for more information about the methods you can implement in your main plugin file.

View the Piwik_ExamplePlugin class for a simple Plugin example.

If your plugin needs JS and CSS files, this class must contain two methods listing the files to be included. See the CoreAdminHome.php getCssFiles method for a detailed example. Don't forget to hook those methods to events AssetManager.getCssFiles and AssetManager.getJsFiles as in CoreAdminHome.php getListHooksRegistered method.

If your plugin needs tasks to be scheduled periodically and automatically, this class must contain a method defining your plugin scheduled tasks. See the CoreAdminHome.php getScheduledTasks method for a detailed example. Don't forget to hook the method to event TaskScheduler.getScheduledTasks as in CoreAdminHome.php getListHooksRegistered method. Tasks can be scheduled Hourly, Daily, Weekly and Monthly. See Piwik_ScheduledTime_Hourly class, Piwik_ScheduledTime_Daily class, Piwik_ScheduledTime_Weekly class and Piwik_ScheduledTime_Monthly class. Depending on the type of scheduling (hourly, daily, weekly or monthly), methods setHour and setDay can be called to control more precisely when tasks will be executed.

  • Piwik_ExamplePlugin_Controller which is the Plugin Controller

The controller is the entry point when you go to ?module=YourPluginName&action=TheControllerMethodName. For example going to ?module=YourPlugin&action=printTest will execute the method printTest() of your class Piwik_YourPlugin_Controller.

View the Piwik_ExamplePlugin_Controller class for a Plugin Controller Example.

  • An optional Piwik_ExamplePlugin_API that your plugin can publish in a file API.php

This API can be used to request data from within other Piwik plugins or from outside, in other softwares, using the Piwik Web Services Architecture.

View the Piwik_ExamplePlugin_API class for an example of a Plugin publishing an API.

You call this API using the link in the page API reference: look for the Module ExamplePlugin section and click the links on the right.

Last modified 3 years ago Last modified on Jan 3, 2011 9:28:20 PM