Registering piwik.js plugin before any event is tracked

Hello Piwik community,

It doesn’t seem possible to register a piwik.js plugin before any event is tracked.
To register piwik.js plugin one is supposed to call Piwik.addPlugin method.
I’m trying to call it from a function pushed to _paq, but items in _paq are evaluated before Piwik is constructed so before Piwik public methods like addPlugin are even declared.

Am I missing something?

I’m guessing that the order of statements in Piwik constructor isn’t correct to allow registering plugins before tracking any events.
Maybe a solution would be to move Piwik.addPlugin to the Tracker, or add equivalent addPlugin method to the Tracker public API, and make that addPlugin part of applyFirst (methods which execute first if pushed in _paq).

Kind regards,
Stevo Slavic

Hi Stevo,

What are you trying to achieve with your plugin?

Currently there is no functionality to load plugins in the piwik.js client. This is a feature request, that we would like to implement.

How would you envision this? can you tell me more about your use case?

Hello Matt,

As I can read from piwik.js code, I’m not doing anything special with a plugin, just regular Tracker plugin functionality - depending on interaction type (click, load, unload, log, …) append interaction type specific custom data to request query string, e.g. interaction type itself. Plugins are called for every Tracker.getRequest (line: “request += executePluginMethod(pluginMethod);”).

I’m registering plugin with regular _paq.push([function() …
Referencing Piwik singleton from such a function (e.g. to call addPlugin) will not work, because _paq array elements are evaluated in Piwik constructor, before Piwik is made accessible.

Calling addPlugin after piwik.js has been loaded is too late - I need plugin registered at construction time, before any interaction event has been tracked.

Moving/adding addPlugin from Piwik to Tracker public API, and making Tracker’s addPlugin one of applyFirst methods, did the trick for me. I’m working on a pull request (which leaves addPlugin on Piwik singleton, but adds one on Tracker too).

Kind regards,
Stevo Slavic.

I’ve created pull request with tests for this issue: [piwik-js] Added support for registering plugins in Tracker public API by sslavic · Pull Request #336 · matomo-org/matomo · GitHub

Thanks for the suggestion and pull request! it’s been merged :slight_smile: