4

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!

Tim
  • 5,521
  • 8
  • 36
  • 69

1 Answers1

4

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.

Ingo Bürk
  • 19,263
  • 6
  • 66
  • 100
  • Thanks for your comment, but what about different pages and closing the pages. HTTP requests can't finish in a window close event. – Tim Nov 02 '13 at 21:13
  • You can also store the current location to correlate data with the page. As for the other problem, you could periodically send data to the server, but also store them in `localStorage`. That way the lust chunk would be stored on the client and can be served the next time he visits your website. You'll only lose the last chunk. If time isn't critical, you could also only store it there and then only send it once every time they start visiting your website (then you'd lose data from people only visiting once). – Ingo Bürk Nov 02 '13 at 21:37
  • I mostly got one time visitors purchasing a product. `localStorage` would fix the multiple page problem when the browser supports it. Thanks I think I figure it out now ;) – Tim Nov 02 '13 at 21:42
  • I'm not sure how `localStorage` solves a multi-page problem, but if you say so :) If you can live with losing the last chunk of data, that should be a good start. Also, I think that periodic queries won't hurt your users as much as it might hurt your server if you have a lot of users. Glad to have helped. – Ingo Bürk Nov 02 '13 at 21:45
  • `localStorage` keeps the data intact during multi-page sessions since the data will be stored local. Thank you for your input! – Tim Nov 03 '13 at 15:47