Access XHR object in callback of trackPageView()


(Aditya Sharma) #1

First of all, Kudos to Matomo community for such awesome analytics tools.
I am using Matomo for tracking page view. I have a requirement where on completion of XHR request of track page view I need to perform some operation which involves accessing xhr ( XmlHttpRequest ) object. Basically, I need some of the data extracted by Piwik to be sent to another server too. I figured out that with trackPageView() we can pass customTitle, customData, callback . I was able to pass a callback function as a parameter but I am not able to access xhr in the callback.
Is there some way I can access xhr in the callback function or extract data using some other method?

Thanks in advance!


#2

If the objective is to send data to another server, this can be done with a secondary tracker.

https://developer.matomo.org/guides/tracking-javascript-guide#multiple-piwik-trackers

Basically each request is sent to two servers. Might be worth a look


(Aditya Sharma) #3

Thanks @fatica for the reply.

I need to send some of the information from track hit to a non Matomo server. Is there any possible way of doing that before or after XHR hit?

I did some exploration towards the flow of Piwik.js.
In sendXmlHttpRequest() function,

if(this.readyState===4&&(typeof callback ==='function')){callback()}

can be changed to

if(this.readyState===4&&(typeof callback ==='function')){callback(xhr)}

When the library is providing callback for xhr, it should also provision access to the object.


#4

I’m really not sure. I would avoid wherever possible modifying any of the core Matomo js.

The reason I suggested to use multiple trackers would be to receive whatever data you need based on the Request object from the second tracker by creating your own piwik.php and print_r($_REQUEST). I’m not sure if what you’re after will be in there, but that would be my approach.