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
Piwik.js JS tracker: allow developer to pass a 'callback' argument #5850
Conversation
Build failed for reasons outside of scope of this PR I think.
|
Hi @Sija |
Yeah, sure. It's useful mostly in the case when you want to track outlinks, while being sure the connection to piwik server will succeed before redirecting user to foreign website. I understand that |
it means the JSLint failed, please go to: http://jslint.com/ copy paste the piwik.js code and you can see the error message there. JSLint is a bit tricky but once you're used to it may make sense. |
I'm not sure about this PR, maybe there could be a setter to set the callback rather than pass it around all methods? Or maybe you could reuse the piwik.js plugin mechanism introduced in https://github.com/piwik/piwik/pull/336/files ? |
@mattab Getter/Setters aren't really a thing in JS. Using the last parameter as callback is the norm and the correct thing to do (if we want to do it). |
-1 If you don't want the delay, set the delay to 0. A callback that does |
No, because callback is going to be invoked only after xhr request succeeded or failed and not before. On 28 Jul 2014, at 04:24, Anthon Pang notifications@github.com wrote:
|
So maybe it could be called something else, eg. |
@mattab It's not a callback that is called on error - it is always called after xhr. I like the current implementation (actually meant to implement that myself for quite some time). |
sorry, I misread while glancing thru the PR. the callback idea is fine. I would consider something like this https://github.com/robocoder/piwik/commit/e041d365bf500aff23846e4eb6d6128e5c9b83bf instead of
but that's just me. |
@Sija since @halfdan thinks the callback is a good idea, I think we can merge it. Maybe you would be kind to add some tests for this new functionnality? the piwik.js is tested in this file: https://github.com/piwik/piwik/tree/master/tests/javascript#javascript-tests |
@mattab Piwik's test files are tad confusing I must say—at the beginning I wasn't sure which file to edit, but it seems that |
@Sija yes sorry, the piwik.js file is index.php and yes it's quite confusing. We should split it across several files to make it more readable. Sorry about that and thanks for trying 👍 |
@mattab Callback test added. |
@@ -3002,9 +3005,9 @@ if (typeof Piwik !== 'object') { | |||
* @param string linkType | |||
* @param mixed customData |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a doc block is missing for the new parameter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed.
@Sija test looks good! Also would you mind check that the JSLint test passes? http://jslint.com/ |
@mattab I don't now how should I lint test file since it's a mix of everything, but the code is simple enough to see through its complexity without the need of external tools (I hope) :) |
@Sija I meant to check the piwik.js is JSLinted (not the test file). can you please rebase the PR and then I'll merge it. Thanks! |
Implemented in the API #trackLink method, as it’s the one in which using callback makes most sense
@mattab Done! |
Piwik.js JS tracker: allow developer to pass a 'callback' argument
@Sija Thanks for the pull request! |
@mattab Sure! Thanks for Piwik! :) |
Also want to thank you for this, I have been using this change for quite some time because in my experience it is the only reliable way to track outlinks and downloads as long as some browsers cancel pending requests on window.location changes. I never liked the 500ms workaround, 500ms is not enough time if you have visitors from halfway around the globe with bad internet connections. Increasing it just to be safe only adds a long delay for everyone and it is still just guess work. |
Adding a callback to other "track" functions could prove useful as well. Since it is a common asynchronous paradigm and we won't have to guess using I'd like to see it on |
Implemented in the API #trackLink method, as it’s the one in which using callback makes most sense but it can be added to all of the other methods as well.
Somewhere in the code where link
click
is being caught:_paq.push(['trackLink', url, type, {}, function() { window.location = url }])
In this way we don't block the UI in
#beforeUnloadHandler
function.