Preprocessing request on client side

Hello piwik community,

What is recommended way to add some preprocessing logic on client side before request data is sent to tracker?

Kind regards,
Stevo Slavic.

Hi Stevo, what are you trying to achieve in your case? if we know more about what you want to achieve we could help you see how it can be done with Piwik

I’d like piwik.js to track events first to a non-piwik tracker, a REST service, which can optionally forward requests to piwik tracker. Both custom URL and POST method are configurable in piwik.js Tracker, but not manipulating request content - I’d like to convert request parameters string to JSON string.

I tried proxying Tracker’s getRequest public method (getRequest with one, request parameter), but that didn’t help - all the log methods like logPageView method use private getRequest method with 4 parameters. I wonder what’s the purpose of public getRequest method, but most importantly, am I missing something or request content cannot be manipulated (without changing piwik.js itself)?

Kind regards,
Stevo Slavic.

Hi Stevo,

I think this is a new feature request. it makes sense that Piwik.js should allow you to change the request. Do you have an idea of how this could be done in Piwik.js ? (I’m asking about how you think the piwik.js API / new function should look like ?)

Hello Matt,

One approach would be to make 4 param getRequest public (remove existing single param public getRequest), so one can implement a getRequest proxy/decorator which will delegate to original getRequest and then call custom logic before returning result.
I’m most in favor of this approach.

A different approach would be to introduce a new public method (called e.g. processRequestContent), a callback/hook which would get called:

  • by getRequest, before returning result, or
  • by sendRequest, as first thing, or
  • calls to new method would be introduced in between all (getRequest, sendRequest) pairs
    Either of these 3 sub options has pros and cons. If I had to choose, I guess I’d go with first suboption here.
    That hook/method would receive request (prepared so far, in query parameters shape with everything including custom parameters). By default hook could be implemented/defined in piwik to return unmodified request (one could then override the hook with custom logic), or hook/callback implementation might not be defined by piwik so callees would have to check if hook/callback has been defined and only call it when it is defined.

Matt, and rest of community, which approach/option do you prefer?

I’ll play around locally with some of these approaches/options. Maybe something becomes good enough for pull request.

Kind regards,
Stevo Slavic.

I’ve just submitted a pull request (see #325 ) which adds a new configuration option for configuring custom request content processing logic.

Kuddos for the pull request sslavic! We hope you will keep contributing good ideas to Piwik :slight_smile: