Kyle Decot
Kyle Decot

Reputation: 20835

Query Count of Specific Event Action in Piwik

I need to make a widget/plugin for my organization using Piwik but I'm a little lost. We send custom events to Piwik such as:

Category: Staff 
Action: Login 

How would i query piwik from within a widget/plugin to get the # of events that match that particular category/action?

Upvotes: 4

Views: 541

Answers (1)

Matthieu Napoli
Matthieu Napoli

Reputation: 49703

Here is the documentation regarding the Events API.

There are 2 methods that might interest you:

  • Events.getCategory
  • Events.getAction

They will return a list of actions/categories mapped to nb_uniq_visitors, nb_visits and nb_events.

The nb_events might be the one that you should use to get the number of events for each category or action. Here is an example of query for the event category list:

http://demo.piwik.org/?module=API&method=Events.getCategory&idSite=7&period=day&date=today&format=xml&token_auth=anonymous

Note that you don't especially need to write a Piwik plugin for that, you just need to query the Reporting API. However if you want to display those numbers in Piwik then indeed you'll need to write a plugin.


Edit: you want to know how to get that list from within a custom Piwik plugin.

Have a look at the Calling APIs of other plugins documentation. In your case you would have to call the Events API like this:

$table = \Piwik\API\Request::processRequest('Events.getCategory', array(
    'idSite' => $idSite,
    'period' => $period,
    'date'   => $date,
));

Once you have the table, you can find the row that interest you:

$row = $table->getRowFromLabel('Staff');

Then get the number of events in the period:

$numberOfEvents = $row->getColumn('nb_events');

Note that you can also optimize your call to the API to filter and get only the label you are searching, and the column you are using:

$table = \Piwik\API\Request::processRequest('Events.getCategory', array(
    'idSite'      => $idSite,
    'period'      => $period,
    'date'        => $date,
    'label'       => 'Staff',
    'showColumns' => 'nb_events',
));

Upvotes: 3

Related Questions