How do I record data in my own table?


I’m trying to make a custom tracking plugin that would record data into my own table (ie one that doesn’t exist by default in piwik’s table structure), let’s call it test, after my own trigger. For now I’m using the newVisitorInformation trigger just to test the data recording part.

I have successfully added a table via install() with the following structure:
test(id int, stuff varchar(100), who varchar(100))

I use the following code:

function logMyStuff($notification)
$mydata = array( ‘who’ =>‘someone’, ‘id’ => 1);
$db = Zend_Registry::get(‘db’);
$table = Piwik::prefixTable(‘test’);
$db->insert($table, $mydata);

This code does nothing and doesn’t throw any errors. I get new visitors but nothing gets added to the test table. (I am checking directly in the db.)

This same code, when put into an API call, works just fine. Also if I add data to the log_visit table via the logMyStuff method, it works too (although it messes up the visitor count, so I definitely need my own table).

I have no idea how to make this code do what I want. Please help!


For performance reasons, the Tracker doesn’t use Zend_Registry. Try:

$db = Piwik_Tracker::getDatabase();

Thanks a lot! That worked, except I also had to use query() instead of insert() for some bizarre reason. I found a good example in the saveVisitorInformation method in core/Tracker.

Another thing I found bizarre, which I won’t investigate further but will mention anyway, is that making a call to the API (which worked) from the plugin didn’t work. I guess it’s for the better, if you want the code triggered by the tracker not to use Zend_Registry.

The insert() method is a Zend_Db method. Again, not used by the Tracker for performance reasons.

Not all plugins are loaded by the Tracker, and as you’ve seen, there are limitations. For a Tracker plugin to be loaded, it must return in its getInformation():

'TrackerPlugin' => true