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

Piwik.js JS tracker: allow developer to pass a 'callback' argument #5850

Merged
merged 4 commits into from Aug 25, 2014
Merged

Piwik.js JS tracker: allow developer to pass a 'callback' argument #5850

merged 4 commits into from Aug 25, 2014

Conversation

Sija
Copy link
Contributor

@Sija Sija commented Jul 16, 2014

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.

@Sija
Copy link
Contributor Author

Sija commented Jul 20, 2014

Build failed for reasons outside of scope of this PR I think.

Executing tests in test suite JavascriptTests...
Test failed in module externals: 'JSLint' 
Error: JSLint 
Source:     at http://localhost/tests/javascript/assets/qunit.js:534
    at http://localhost/tests/javascript/:173
    at http://localhost/tests/javascript/assets/qunit.js:203
    at http://localhost/tests/javascript/assets/qunit.js:361
    at process (http://localhost/tests/javascript/assets/qunit.js:1453)
    at http://localhost/tests/javascript/assets/qunit.js:479

@halfdan
Copy link
Member

halfdan commented Jul 22, 2014

Hi @Sija
Can you please provide a use case for this?

@Sija
Copy link
Contributor Author

Sija commented Jul 22, 2014

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 beforeUnloadHandler method is meant to handle that case, but based on the timeout idea. Proposed change guarantees recording event, regardless of default 500ms timeout.

@mattab
Copy link
Member

mattab commented Jul 24, 2014

Build failed for reasons outside of scope of this PR I think.

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.

@mattab
Copy link
Member

mattab commented Jul 24, 2014

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 ?

@halfdan
Copy link
Member

halfdan commented Jul 24, 2014

@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).

@Sija
Copy link
Contributor Author

Sija commented Jul 24, 2014

@mattab Done!
@halfdan that's right, we could use options object instead but right now there's no such approach used anywhere else in the code.

@robocoder
Copy link
Contributor

-1 If you don't want the delay, set the delay to 0. A callback that does window.location = url; is just as likely to cancel the outlink tracker.

@Sija
Copy link
Contributor Author

Sija commented Jul 28, 2014

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:

-1 If you don't want the delay, set the delay to 0. A callback that does window.location = url; is just as likely to cancel the outlink tracker.


Reply to this email directly or view it on GitHub.

@mattab
Copy link
Member

mattab commented Aug 1, 2014

callback is going to be invoked only after xhr request succeeded or failed and not before.

So maybe it could be called something else, eg. callbackOnError or better?

@halfdan
Copy link
Member

halfdan commented Aug 1, 2014

@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).

@robocoder
Copy link
Contributor

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

sendRequest(request, (callback ? 0 : configTrackerPause), callback);

but that's just me.

@mattab
Copy link
Member

mattab commented Aug 1, 2014

@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

@Sija
Copy link
Contributor Author

Sija commented Aug 3, 2014

@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 index.php is the one, right? I'll do my best ;)

@mattab
Copy link
Member

mattab commented Aug 3, 2014

@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 mattab added this to the Short term milestone Aug 3, 2014
@Sija
Copy link
Contributor Author

Sija commented Aug 16, 2014

@mattab Callback test added.

@mattab mattab changed the title Support for passing callback argument Piwik.js JS tracker: allow developer to pass a 'callback' argument Aug 21, 2014
@@ -3002,9 +3005,9 @@ if (typeof Piwik !== 'object') {
* @param string linkType
* @param mixed customData
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

@mattab
Copy link
Member

mattab commented Aug 21, 2014

@Sija test looks good!

Also would you mind check that the JSLint test passes? http://jslint.com/

@Sija
Copy link
Contributor Author

Sija commented Aug 22, 2014

@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) :) piwik.js is already lint-ed though.

@mattab
Copy link
Member

mattab commented Aug 23, 2014

@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
@Sija
Copy link
Contributor Author

Sija commented Aug 24, 2014

@mattab Done!

mattab pushed a commit that referenced this pull request Aug 25, 2014
Piwik.js JS tracker: allow developer to pass a 'callback' argument
@mattab mattab merged commit 4508163 into matomo-org:master Aug 25, 2014
@mattab
Copy link
Member

mattab commented Aug 25, 2014

@Sija Thanks for the pull request!

@Sija
Copy link
Contributor Author

Sija commented Aug 25, 2014

@mattab Sure! Thanks for Piwik! :)

@fredli74
Copy link

fredli74 commented Dec 2, 2014

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.

@mattab
Copy link
Member

mattab commented Dec 3, 2014

@fredli74 you need this: #6641

@pbojinov
Copy link

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 setTimeout. What are your thoughts?

I'd like to see it on trackEvent especially since there are a couple events that i'd like to track which which can possibly navigate the page.

@mattab
Copy link
Member

mattab commented May 21, 2015

@pbojinov FYI there is a PR at #7946 adding callback mechanism to trackEvent

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement For new feature suggestions that enhance Matomo's capabilities or add a new report, new API etc.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants