Is it possible to have a callback function? if so how?
but I have no clue after all this what i'm supposed to insert into the _paq.push(['trackGoal', 1]) to get a callback event working after the request has been sent to matomo and received back.
Is this a feature that is present but undocumented? If so, how can I use it in goal tracking?
it's not possible yet but added a pull request for this in https://github.com/matomo-org/matomo/pull/13680 . Will be available with Matomo 3.8 (not 3.7). Just out of curiosity, do you mind more explaining your use case what you do in the callback?
Unfortunately if you send someone directly to a third party site a lot of browsers will drop pending requests and those may not be adequately tracked.
By having a callback at the end you can queue up several requests that should happen before sending the user on it's way.
Google Analytics for example uses this wonderfully, if you look at the documentation https://developers.google.com/analytics/devguides/collection/analyticsjs/sending-hits#knowing_when_the_hit_has_been_sent you can chain several requests.
In our app for example we have analytics, adwords and want to chain motomo too for experimenting with the features of motomo.
How our chain currently works:
click whatsapp > handle graphcis; log_motomo(_paq.push()); log_goal_analytics(callback log_goal_adwords) > log_goal_adwords(callback move_to_whatsapp) > move_to_whatsapp > window.location=whatsappurl.
Right now, the motomo push get's dropped a lot because the google analytics callbacks are handled much faster than the piwik response on our server, which causes the browser to move to whatsapp before motomo is ready to accep/log.
By having a callback features, one can chain motomo in any other request and make sure everything is logged properly.
Cheers, that helped 👍 FYI: In Matomo 3.8 we will also have an option to use sendBeacon when it is available in the browser see https://github.com/matomo-org/matomo/pull/13451 and eventually it'll become the default: https://github.com/matomo-org/matomo/issues/13681
You may want to consider enabling this once it is available and then you wouldn't need to wait for the requests to finish and end up having bit faster UI there. It's supported in most browsers: https://caniuse.com/#search=sendbeacon
Unfortunately, sendBeacon does not let us know when the request is finished so you wouldn't be able to use the callback for other browsers unless you really checked whether sendBeacon is being used or not.
The sendBeacon() method returns true if the user agent is able to successfully queue the data for transfer, Otherwise it returns false.
You can use this in a callback feature. call the callback when it returns true, throw an error if it fails, even better, use a promise for easy handling all cases.