@FalkHe opened this Issue on October 14th 2022

Expected Behavior

The callback in a trackPageView() call (and others) should always be executed, once the tracking Request is sent.

window._paq.push(['trackPageView', null, null, function () {
  console.log('successfully tracked pageView');
}]);

Current Behavior

In some circumstances, this is not happening. While testing, I noticed it's working in Firefox, but not in Chrome (v 106.0.5249.119 ).

I tried to debug this issue and found this line to be different in execution. In FF clientHintsResolved is true and in Chrome it isn't. I went down the rabbit hole a bit deeper and found that detectBrowserFeatures() is called, detectClientHints(callback) is also called, but the given callback of the latter is not called in Chrome but is mandatory for clientHintsResolved to be true. A bit deeper, I found that navigator.userAgentData.getHighEntropyValues() behaves different in Firefox and Chrome.

Nevertheless... What happens then, is that in sendRequest(request, delay, callback), the request is put into a queue but - and that's the issue, IMO - delay and callback are ignored and lost at this moment. The Request in the Queue is executed (sent) later on, but my originally provided callback is not. :(

Possible Solution

  1. Respect the async behavior of getHighEntropyValues() and either wait for it or continue normally, even if the desired data isn't available in time
  2. Put delay and callback along with request into the clientHintsRequestQueue and execute it from there.

Steps to Reproduce

  1. Setup a page with Matomo
  2. test in Chrome (v 106.0.5249.119 )
  3. call trackPageView(null, null, function() { console.log('success'); })
  4. check if 'success' is logged

Context

I've some pages that perform an immediate redirect (via window.location) but need to be tracked via Matomo. Therefore I need to know when Matomo has finished the tracking call so I securely can leave the page. ( Maybe there is a better way? )

Your Environment

Chrome (v 106.0.5249.119 )
Windows 11
Matomo version: 4.12.0

@FalkHe commented on October 14th 2022

For anyone having the same issue, this is my workaround for now:

window._paq.push([function () {
  const int = setInterval(() => {
    if (this.getNumTrackedPageViews()) {
      console.log('successfully tracked pageView');
      // do whatever you want when Matomo tracked at least one pageView
      clearInterval(int);
    }
  }, 250);
}]);
@sgiehl commented on November 2nd 2022 Member

Hi @FalkHe
Thanks for creating this issue and sorry for the delay in answering.

It seems you are right. The callback seems to be thrown away if a request is not directly sent, but pushed into a queue instead.
This might not only be the case for client hint detection, but also if consent is required, but was not yet given.

This is something we need to investigate further.

@peterhashair commented on November 10th 2022 Contributor

Updated in Matomo 5.x add callBackQueue, which should process the callback as normal.

This Issue was closed on November 10th 2022
Powered by GitHub Issue Mirror