Tracking time users spend on the website

MySQLsql

We are going to track user engagement (i.e. time spent on website, most viewed part/page of the system, etc).

I don't see Google Analytics / MixPanel being able to do this, since we have to analyze based on factors only present in OUR backend (such as users who go to specific school, users who are a specific type, etc) – not the general stuff like Country, Gender, etc.

I can think of a REALLY simple solution, but I am not sure if it is, serverwise, bad. You have a table that looks something like this:

CREATE TABLE `log` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `uri` int(11) DEFAULT NULL,
  `date` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  `user` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

And then whenever a page is loaded, a row is added to the "log" table, with current timestamp, the user id and the uri.

Now I got the data I want, which is great. I can figure out when the user was last online and how many minutes people spend in average.

BUT. Is this bad for the server? It is going to add a row each page load, which is a lot of rows, if you have 500 or 1000 active users. But will it matter? What are your thoughts?

Best Answer

It is not necessarily bad for the server to log a line per page request. However, there are few things to consider here. First of all, I see you are using an AUTO_INCREMENT as a key. This will result in some performance loss as you are writing records to it.

You could replace the id with a GUID field and generate GUID's as you write lines.

Another optimization that you could to is to make this asynchronously. So the user requests a page, which you serve to the user, and while you do, you fire off a request, event, or whatever is suitable for your server side technology and does not block sending back the request, to actually write your logging.

Another thing to consider is, do you really need the data in a database immediately? It might be sufficient to just write to a log file first and import the data into a database at a later point in time.