matomo-org / matomo

Empowering People Ethically with the leading open source alternative to Google Analytics that gives you full control over your data. Matomo lets you easily collect data from websites & apps and visualise this data and extract insights. Privacy is built-in. Liberating Web Analytics. Star us on Github? +1. And we love Pull Requests!
https://matomo.org/
GNU General Public License v3.0
19.88k stars 2.65k forks source link

setCustomRequestProcessing blocks enableHeartBeatTimer #15243

Open crnm21 opened 4 years ago

crnm21 commented 4 years ago

If you use a setCustomRequestProcessing, an enableHeartBeatTimer does not fire in the JS tracker.

How to reproduce:

Add a

_paq.push(['setCustomRequestProcessing', function(params){
        .... send the request individually ....
      }]);

and _paq.push(['enableHeartBeatTimer', 20]);

Result: HeartBeatTimer is not executed.

Desired result: HeartBeatTimer gets executed through the CustomRequestProcessing.

tsteur commented 4 years ago

Looking at the code I'm not seeing why this shouldn't work. Is it actually sending the heartBeatTimer request? You might need to leave the page and focus again a few times etc or navigate to another page for the request to fire.

crnm21 commented 4 years ago

It's not sending the heartBeatTimer if a setCustomRequestProcessing is set, that is the problem. Without the setCustomRequestProcessing, the heartBeatTimer is send.

In my example case, the setCustomRequestProcessing is the following function:

function sendAjax(data){ httpRequest = new XMLHttpRequest(); httpRequest.open('GET', '[MATOMOHOST+PATHtoPiwik.php]?ac='+data); httpRequest.send();

This function gets a btoa(params) as data. It does work for the pageload, but then not for the heartBeatTimer. Without the setCustomRequestProcessing, the heartBeatTimer works.

tsteur commented 4 years ago

Sorry I can't reproduce this. Make sure you are on the latest version of Matomo in case you are not. This works here without any issue for me and looking that the code I can't even think how this wouldn't work. To make sure the heart beat timer is sent, stay on the page for say 1 minute before you navigate to some other window like the developer tools.

mattab commented 4 years ago

Could you send us a short JavaScript code that will reproduce this issue? @crnm21

crnm21 commented 4 years ago

Sorry for getting back to this so late. I'm using this code on Matomo 3.11.0:

<!-- Matomo -->
<script type="text/javascript">
  var _paq = window._paq || [];
function sendAjax(data){
httpRequest = new XMLHttpRequest();
httpRequest.open('GET', '{$MATOMO_URL}?d='+data+'&ts='+new Date().getTime());
httpRequest.send();
}
  _paq.push(['setCustomRequestProcessing', function(params){
        sendAjax(btoa(params));
    }]);
  _paq.push(['disableCookies']);
  _paq.push(['trackPageView']);
  _paq.push(['enableLinkTracking']);
  _paq.push(['enableHeartBeatTimer', 10]);
  (function() {
    var u="//{$MATOMO_URL}/";
    _paq.push(['setTrackerUrl', u+'matomo.php']);
    _paq.push(['setSiteId', {$IDSITE}]);
    var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
    g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
  })();
</script>
<!-- End Matomo Code -->

Matomo does fire once, but the HeartBeatTimer does not fire. (On Matomos side, the d query param is decoded and Motomo registers the pageload). I stayed on the site (focused), scrolling up and down for a minute or more but the HeartBeatTimer does not fire.