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

Feat/callbacks error and success #7946

Closed
wants to merge 9 commits into from

Conversation

dhoko
Copy link

@dhoko dhoko commented May 20, 2015

Hi,

We cannot add a callback to detect both error and success for GET and POST.
Now, with trackEvent we can set an object as the fifth argument to set callbacks.

tracker
  .trackEvent('test',null,null,null,{
    success: function(isXhr) {
      console.log('Success', isXhr)
    },
    error: function(isXhr) {
      console.log('Error', isXhr)
    }
  });

@mattab
Copy link
Member

mattab commented May 21, 2015

Hi @dhoko

Thanks for the PR. I don't know if we need this (it's quite complicated).
There is already a callback parameter to the sendRequest function (added in #5850).
Maybe we could do a simpler change like this:


--- js/piwik.js (revision 530ff25b5cb5b6de89ff9e45f7c04757fd3cf26e)
+++ js/piwik.js (revision )
@@ -3714,7 +3714,7 @@
             /*
              * Log the event
              */
-            function logEvent(category, action, name, value, customData)
+            function logEvent(category, action, name, value, customData, callback)
             {
                 // Category and Action are required parameters
                 if (String(category).length === 0 || String(action).length === 0) {
@@ -3726,7 +3726,7 @@
                         'event'
                     );

-                sendRequest(request, configTrackerPause);
+                sendRequest(request, configTrackerPause, callback);
             }

             /*
@@ -5147,9 +5147,9 @@
                  * @param string name (optional) The Event's object Name (a particular Movie name, or Song name, or File name...)
                  * @param float value (optional) The Event's value
                  */
-                trackEvent: function (category, action, name, value) {
+                trackEvent: function (category, action, name, value, callback) {
                     trackCallback(function () {
-                        logEvent(category, action, name, value);
+                        logEvent(category, action, name, value, null, callback);
                     });
                 },

(I didn't test it). would it work for you?

if so feel free to update your PR

@dhoko
Copy link
Author

dhoko commented May 21, 2015

The default solution does not work as expected, it's triggered too many times and we cannot detect if the request is a success or an error cf:

screenshot from 2015-05-21 08 58 45

screenshot from 2015-05-21 09 03 33

I can change the implementation by using too arguments instead of one object:

trackEvent(category, action, name, value, {success,error});

// to
trackEvent(category, action, name, value, onSuccess, onError);

What do you think ?

@@ -5148,9 +5219,9 @@ if (typeof Piwik !== 'object') {
* @param string name (optional) The Event's object Name (a particular Movie name, or Song name, or File name...)
* @param float value (optional) The Event's value
*/
trackEvent: function (category, action, name, value) {
trackEvent: function (category, action, name, value, callbacks) {
Copy link
Member

Choose a reason for hiding this comment

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

Is callbacks a common name for that? Reading that parameter I expected to pass an array of callbacks. Also it needs to be documented as a parameter. Shall we maybe rather still use callback, especially since a function callback would work as well? Or is it maybe worth to use a settings object to allow {success: function()..., error: function ()...} that would be okay for me as it is optional anyway. Just to avoid having to add many more optional parameters in the future.

Copy link
Member

Choose a reason for hiding this comment

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

There are methods like trackLink that currently expect callback. We'd need to make sure to use consistent naming and behaviour and to maintain backwards compatibility.

Copy link
Author

Choose a reason for hiding this comment

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

Yup we can use onSuccess, onError 1 more argument to the function. I doesn't break the compatibility cf L2513

 // Compatibility for eventLink
       if('function' === typeof cb) {

@mattab
Copy link
Member

mattab commented May 24, 2015

The default solution does not work as expected, it's triggered too many times and we cannot detect if the request is a success or an error.

I didn't look, but maybe there is a way to "fix" the default solution so it can do what you need as well as keeping BC for those who may be using the callback already?

@dhoko
Copy link
Author

dhoko commented May 29, 2015

The meaning of this PR wasn't to rewrite the current callback system. I try to improve it and fix it.

Today with Piwik:

  • You cannot detect if the request is a success or a failure.
  • You cannot detect if the call was from a XHR or a GET.
  • The callback is triggered too many times

Bonus: it doesn't break the compatibility.

Why ?

Because I am working with a mobile app, I cannot do a request for each event (it's expensive) so I need to put these items to a cache (localStorage) and send my events after a .
To prevent any errors on mobile i need to know if I am online or not and if the request is a success. Piwik cannot do that so, that's why there are these PR ( #7949 too). (and I have a third one coming)

@dhoko dhoko force-pushed the feat/callbacksErrorAndSuccess branch from b6f8a6f to 29b697e Compare August 25, 2015 09:11
@dhoko dhoko force-pushed the feat/callbacksErrorAndSuccess branch from 29b697e to 596fc7e Compare September 10, 2015 07:36
@mattab
Copy link
Member

mattab commented Sep 11, 2015

Hi @dhoko - Do you still think we should merge this PR? if so please leave a comment, and we will take another look... For sure, we would need to have automated tests as well (tests/javascript/index.php) before we could merge it

@dhoko
Copy link
Author

dhoko commented Sep 11, 2015

Yup, I will try to write some tests.
First. I must understand how do you write tests with piwik.

I don't know Qunit yet, i work with Jasmine.

@dhoko dhoko force-pushed the feat/callbacksErrorAndSuccess branch from 596fc7e to 0aa454c Compare September 14, 2015 08:03
@dhoko
Copy link
Author

dhoko commented Sep 14, 2015

Something is fucked up with my fork. Sorry for the mess, I will create a new PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants