Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Callback in sendRequest() / trackPageView() is not always executed #19862

Closed
FalkHe opened this issue Oct 14, 2022 · 4 comments
Closed

Callback in sendRequest() / trackPageView() is not always executed #19862

FalkHe opened this issue Oct 14, 2022 · 4 comments
Assignees
Labels
Bug For errors / faults / flaws / inconsistencies etc. c: Tracking For issues related to getting tracking data into Matomo. not-in-changelog For issues or pull requests that should not be included in our release changelog on matomo.org.
Milestone

Comments

@FalkHe
Copy link

FalkHe commented Oct 14, 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 FalkHe added the Potential Bug Something that might be a bug, but needs validation and confirmation it can be reproduced. label Oct 14, 2022
@FalkHe
Copy link
Author

FalkHe commented Oct 14, 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
Copy link
Member

sgiehl commented Nov 2, 2022

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.

@sgiehl sgiehl added Bug For errors / faults / flaws / inconsistencies etc. c: Tracking For issues related to getting tracking data into Matomo. and removed Potential Bug Something that might be a bug, but needs validation and confirmation it can be reproduced. labels Nov 2, 2022
@sgiehl sgiehl added this to the 5.0.0 milestone Nov 2, 2022
@peterhashair peterhashair self-assigned this Nov 10, 2022
@peterhashair
Copy link
Contributor

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

@elabuwa elabuwa added the not-in-changelog For issues or pull requests that should not be included in our release changelog on matomo.org. label Nov 22, 2022
@MatomoForumNotifications

This issue has been mentioned on Matomo forums. There might be relevant details there:

https://forum.matomo.org/t/wait-for-matomo-request-to-finish-callback-event/45875/9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug For errors / faults / flaws / inconsistencies etc. c: Tracking For issues related to getting tracking data into Matomo. not-in-changelog For issues or pull requests that should not be included in our release changelog on matomo.org.
Projects
None yet
Development

No branches or pull requests

5 participants