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

client.addEventSync() #188

Closed
will123195 opened this issue Oct 31, 2014 · 9 comments
Closed

client.addEventSync() #188

will123195 opened this issue Oct 31, 2014 · 9 comments

Comments

@will123195
Copy link

Is it possible to call addEvent synchronously?

For example:

window.onbeforeunload = function() {
  client.addEventSync('my-collection');
}
@will123195
Copy link
Author

This is a workaround:

window.onbeforeunload = function() {
  var request = new XMLHttpRequest();
  var url = 'https://api.keen.io/3.0/projects/' + projectId + '/events/' + collection + '?api_key=' + writeKey + '&data=' + encodeURIComponent( Keen.Base64.encode( JSON.stringify(data) ) );
  request.open('GET', url, false);  // synchronous
  request.send(null);
}

@dustinlarimer
Copy link
Contributor

@will123195 help me understand that – would that let you fire a payload off before the window closes?

@will123195
Copy link
Author

Yes, for example I'm tracking how long the user was on the page.

@dustinlarimer
Copy link
Contributor

hey @will123195 sorry for the lag! I really like this idea, we're discussing the best way to expose this functionality.. can you think of any other scenarios where you would want a synchronous request?

@will123195
Copy link
Author

It would help if you need your events to be logged in a specific order. Surely there are more performant ways to achieve that without blocking i/o, but could be convenient for testing or debugging things easily.

@dustinlarimer
Copy link
Contributor

@will123195 haven't forgotten about this.. not sure how we'll expose in the library, but here's a cool workaround I found that has a high success rate on mobile (onpagehide):

function setUnloadListener(){
    var attachEvent, whichEvent;
    if (window.onpagehide || window.onpagehide === null) {
      window.addEventListener("pagehide", handleExit, false);
    }
    else {
      attachEvent = window.attachEvent || window.addEventListener
      whichEvent = window.attachEvent ? "onbeforeunload" : "beforeunload";
      attachEvent(whichEvent, handleExit);
    }
}

function handleExit(e){
  client.trigger("exit");
}

// from superagent.js
function getXHR() {
    if (window.XMLHttpRequest && ('file:' != window.location.protocol || !window.ActiveXObject)) {
      return new XMLHttpRequest;
    } else {
      try { return new ActiveXObject('Microsoft.XMLHTTP'); } catch(e) {}
      try { return new ActiveXObject('Msxml2.XMLHTTP.6.0'); } catch(e) {}
      try { return new ActiveXObject('Msxml2.XMLHTTP.3.0'); } catch(e) {}
      try { return new ActiveXObject('Msxml2.XMLHTTP'); } catch(e) {}
    }
    return false;
}

setUnloadListener();

client.on("exit", function(){
  var request = getXHR();
  var url = client.url("/projects/"+client.projectId()+"/events/" + "visit");
  if (request) {
    url += "?api_key=" + client.writeKey();
    url += "&data=" + encodeURIComponent( Keen.Base64.encode( JSON.stringify( DataModel ) ) );
    request.open('GET', url, false);  // synchronous
    request.send(null);
  }
});

@dustinlarimer
Copy link
Contributor

this is now possible in v3.2 by passing a fourth 'null' argument to addEvent(). just be sure to check the changelog for updates to the single callback pattern. thanks for suggesting this feature!

@dustinlarimer
Copy link
Contributor

that's #addEvent ^ mobile typo :)

@caschoener
Copy link

It would be nice if this worked for addEvents() too

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

No branches or pull requests

3 participants