Created
January 29, 2014 21:30
-
-
Save cvan/8697622 to your computer and use it in GitHub Desktop.
record user's history
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
1. Install dotjs (https://addons.mozilla.org/en-US/firefox/addon/dotjs/ for Firefox or https://github.com/defunkt/dotjs for Chrome) | |
2. Put the contents of this file at ~/.js/default.js | |
*/ | |
const API_URL = 'http://localhost:7000/'; | |
function getData(e, comment) { | |
var data = new FormData(); | |
data.append('comment', comment); | |
data.append('state', e.state ? JSON.stringify(e.state) : ''); | |
data.append('time', new Date().getTime()); | |
data.append('title', document.title); | |
data.append('url', document.location.href); | |
var xhr = new XMLHttpRequest(); | |
xhr.open('post', API_URL, true); | |
xhr.send(data); | |
} | |
function newPage(e) { | |
getData(e, 'page load'); | |
} | |
function closePage(e) { | |
getData(e, 'page close'); | |
} | |
window.addEventListener('load', function(e) { | |
// Synchronous page load. | |
newPage(e); | |
}, false); | |
window.addEventListener('beforeunload', function(e) { | |
// Closed tab. | |
closePage(e); | |
}, false); | |
var currentUrl = document.location.href; | |
setInterval(function() { | |
// New page pushed to history stack. | |
if (currentUrl !== document.location.href) { | |
currentUrl = document.location.href; | |
newPage({state: 'pushState'}, 'page pushed'); | |
} | |
}, 200); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment