@tschallacka opened this Issue on November 7th 2018

https://developer.matomo.org/guides/tracking-javascript-guide#manually-trigger-goal-conversions

Is it possible to have a callback function? if so how?
I found https://forum.matomo.org/t/callback-after-tracking-a-goal-with-javascript/3190, which leads to https://github.com/matomo-org/matomo/pull/10337 which leads to https://github.com/matomo-org/matomo/pull/10467 which leads to https://github.com/matomo-org/matomo/commit/04d0c74e08bb4d9575f50f79dbb38c403b327570

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?

@tsteur commented on November 7th 2018 Member

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?

@tschallacka commented on November 7th 2018

On our website we have a lot of javascript functionality. We want to track the usage of some features.

One of the simpler features is a whatsapp contact button, when the button is pressed and the javascript magic has happened we wish to send the people to their destination so the whatsapp window will open.

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.

@tsteur commented on November 8th 2018 Member

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.

@tschallacka commented on November 8th 2018

From MDN
https://developer.mozilla.org/en-US/docs/Web/API/Navigator/sendBeacon

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.

@tsteur commented on November 8th 2018 Member

True we could just assume the request worked when sendBeacon works. I just see I've implemented that actually already in https://github.com/matomo-org/matomo/pull/13451 :)

Powered by GitHub Issue Mirror