Skip to content

Instantly share code, notes, and snippets.

@cvan
Created January 29, 2014 21:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cvan/8697622 to your computer and use it in GitHub Desktop.
Save cvan/8697622 to your computer and use it in GitHub Desktop.
record user's history
/*
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