Hooking after an API event in Matomo 4.x

Hello all,
I’m updating an old plugin who used to rely on API event, after creating a new website.

In the past I used to hook by registering at SitesManager.addSite.end.
Now I found a documentation page on hooking events who says a different way to subscribe at the event. :man_shrugging:

The source code of the API method, is still triggering the ancient SitesManager.addSite.end callback:thinking:

I’m confused… :face_with_raised_eyebrow: Anyone?

I’m not entirely sure what you mean.

The only thing that changed here is that the old 2.x method getListHooksRegistered doesn’t work in Matomo 4 anymore and you have to use registerEvents instead. But that should just be a matter of renaming one function.

Indeed. I’ve registered to the classic hook SitesManager.addSite.end in the “new” method registerEvents() to get things working.

Documentation about Callback Execution Order in my opinion is not clear, does it refer to a multiple callbacks registration on the same event?
Does it place the registered callbacks (before|after) event execution?
Does it order all callback registerd for the same event, relatively (before|after) themself?

No matter if you set 'before' => true or 'after' => true, the event will always run exactly when the Piwik::postEvent is executed in the Matomo source.

The only difference is if multiple plugins listen to this event and you can decide if you want to go after or before the other plugins. But in case of SitesManager.addSite.end it will nevertheless always be after the site has been added.

Thanks for clarification!
Maybe the same behaviour can be archieved in a simpler way, if we declare dependencies on other plugins… Our hook should be run always after the other dependencies. :thinking: