Search code examples
javascriptphpxmlhttprequesttrackingissue-tracking

XMLHttpRequest tracking issue (only for Firefox and Safari)


I'm using a system to track user activity. It works like a charm on Google Chrome but it doesn't work on Firefox and Safari (I guess caused by window.close) because doesn't write 'end_start' in my db and the page results still opened :

jsHandler.js

function start(pageName) {
    var xmlhttp3;
    if (window.XMLHttpRequest) {
        xmlhttp3 = new XMLHttpRequest();
    } else {
        xmlhttp3 = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp3.onreadystatechange=function() {
    if (xmlhttp3.readyState==4 && xmlhttp3.status==200) {

    }
}

xmlhttp3.open("GET","includes/trackStart.php?pageName="+pageName,true);
    xmlhttp3.send();
}


function end() {
    var xmlhttp3;
    if (window.XMLHttpRequest) {
        xmlhttp3 = new XMLHttpRequest();
    } else {
        xmlhttp3 = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp3.onreadystatechange=function() {
    if (xmlhttp3.readyState==4 && xmlhttp3.status==200) {

    }
}

xmlhttp3.open("GET","includes/trackEnd.php",true);
    xmlhttp3.send();
}

HTML

<script src="../includes/jsHandler.js"></script>
<script>
    window.onload = function(){ return start('<?php echo basename($_SERVER['REQUEST_URI'], '?' . $_SERVER['PATH_INFO']); ?>');};
    window.onbeforeunload = function(){ return end(); };
    window.onclose = function(){ return end(); };
</script>

Thank you!


Solution

  • Solved! I'm using JQuery:

    $(window).on('beforeunload', function(){
            return end();
          });
          $(window).on('unload', function(){
            return end();
          });