Wait for matomo request to finish (Callback/Event?)

Is there a callback I can use to see if trackPageView has been executed?
Reason: I am changing the url to remove GET-Parameters such as fbclid, but I don’t want to remove them before the page view has been tracked. If I remove them before, then Matomo will track the “clean” url.

Thanks!

The third parameter of trackPageView should be exactly this:

1 Like

Thanks! I found this as well, but how is it possible to use this with

_paq.push(['trackPageView']);

I tried the following, which doesn’t work though

_paq.push(['trackPageView', undefined, function() { console.log("hi"); }]);
_paq.push(['trackPageView', { callback: foobar }]); // also doesnt work

Lukas mentioned the callback as the 3rd argument, but in your 1st example you used it as the 2nd one…

2 Likes

Ahh, of course, ‘trackPageView’ is not the 1st parameter :open_mouth:
Thanks guys!

1 Like

Hi,
I want to reopen this discussion.

I’m not sure about the callback event and how it works:

I have a requirement to create a new visit and track an event immediately afterwards.
If use the trackPageView callback, the event is often added to the previous visit.

if(loggedOut){
_paq.push(['appendToTrackingUrl', 'new_visit=1']); // erzwinge einen neuen Besuch
_paq.push(['trackPageView',undefined,undefined,function(){
     _paq.push(['appendToTrackingUrl', '']); // do not force a new visit anymore (only do it once)
	 _paq.push(['trackEvent', 'Portal', 'User logged out']);}]);

}

If I delay the trackEvent call, it works, but I’m not entirely comfortable with the idea because Matomos trackPageView could of course not be finished with the delay if there is a load peak.

if(loggedOut)[
_paq.push(['appendToTrackingUrl', 'new_visit=1']); // erzwinge einen neuen Besuch
_paq.push(['trackPageView']);
setTimeout(function() {
					//delay after trackPageView
					_paq.push(['appendToTrackingUrl', '']); // do not force a new visit anymore (only do it once)
					_paq.push(['trackEvent', 'Portal', 'User logged out']);
				}, 1500);
   
}

Hi @utrautma
Not sure you need to remove the new_visit=1 after the page view. If I am not wrong (please tell me if I am), this is taken into account only for the page view.
Coming back to the callback itself, what is the exact behavior of your 1st code? (you can also put some console.log(...) in the callback :wink: )

Hi @heurteph-ei ,
the new_visit=1 approach is mentioned here https://matomo.org/faq/how-to/faq_187/ . The call is before the trackPageView-request, so I’m think its right.
The requirement for the callback is the following:
A logged in user was tracked with trackpageview. Now he logs out without a page-load and a new visit should be generated. An event should also be triggered that he has pressed a logout button.
I had assumed the callback function wouldn’t be executed until the trackPageview was processed. But apparently that is not guaranteed. This is because the code (trackEvent) within the callback function is sometimes executed before the trackPageView is completely processed.
As a result, the event is sometimes assigned to the visit before the logout.

Hi @utrautma
You’re right, I just tested, and the event with new_visit=1 indeed create also a new visit… :worried:
Browsing the Matomo GitHub, I found this:


:arrow_right: Will be fixed in Matomo 5.0


:arrow_right: you need then to disable the sendBeacon in this case


Could one of them explain your issue?

Hi @heurteph-ei , great that you picked out the two issues. Thank you very much.

Yes, both describe the problem I have.
I always get a callback so far, but sometimes too early. Here I will try to work without sendBeacon, although that could also have disadvantages. ongoing tracking requests cannot be processed securely when user is going to the next page.
The workaround of replying with a timer, as described in the first ticket, is also an approach. Waiting for Matomo 5 seems a bit too long at the moment.

1 Like