Question

What would be a good way to track user interaction on a website. And then I mean capturing everything like:

  • Scrolling
  • Clicking
  • Moving the cursor
  • Emitting input

Of course with a lot of HTTP post/get request it could be displeasing for the user. I'm looking for a way to do this without annoying the user and without much loss of data.

I hope someone has some experience with this!

Was it helpful?

Solution

Disclaimer: Spying on your users like that is frowned upon by many users and I would recommend against doing this without having your users know exactly what you are doing.

That being said, you can add event listeners to events you want to monitor. Then just store them locally first and only store them on your server side at given intervals or when the users leaves the page. Here is a quick example (Fiddle: http://jsfiddle.net/8ucSV/):

var Spy = (function (register, undefined) {
    var eventStore = [],
        // you can also add other things like "mousemove" here…
        events = ['click'];

    var store = function (type, data) {
        eventStore.push({
            type: type,
            data: data
        });
    };

    events.forEach(function (event) {
        register(event, function (data) {
            store(event, data);
        }, false);
    });

    return {
        dump: function () {
            console.log(eventStore);
        },

        store: function () {
            // do whatever you wanna do here…
        }
    };
})(document.addEventListener);

Obviously you can add more abstraction to monitor things other than events that you can register via addEventListener. I also didn't really care too much about browser compatibility here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top