0

I am trying to build a solution to monitor my website visits without relying on cookies and third parties. Currently, by monitoring the access logs I can get enough and useful information but I am missing the length of the visits (i.e. to check whether people actually read what I write).

What would be a good strategy to monitor visit length with access logs? (I am using Nginx, but presumably the same ideas will be valid for Apache)

2 Answers 2

1

If not already part of your build then install the Nchan websockets module for Nginx.

Configure a websocket subscriber location directive on your Nginx server and specify nchan_subscribe_request and nchan_unsubscribe_request directives within it.

Insert a line of code into your page to establish a client connection to your websocket location upon page load.

That's it, done.

Now when I visit your page my browser will connect to your Nginx/Nchan websocket server. Nginx will make an internal request to whatever address you set as the nchan_subscribe_request URL, you can pass my IP in the headers of this request or whatever you need to identify me. Log this in your main log, a separate log, pass it to an upstream server, php, node, make a database entry, save my ip+timestamp in memcached, whatever.

Then when I leave the site my websocket connection will disconnect and Nginx will do the same thing but to the nchan_unsubscribe_request URL instead. Depending upon what you did when I connected you can now do whatever you need to do in order to work out how long I spent on your site.

As you now have a persistent connection to your clients you could take it a step further and include some code to monitor certain client behaviours or watch for certain events.

You are trying to determine whether or not people are reading what you write so you could use a few lines of javascript to monitor how far down the page visitors had scrolled. Each time they scrolled to a new maximum scroll position send that data over the websocket back to your server.

2
  • I will check Nchan. It seems a very elegant solution. And indeed, it opens the door to tracking anything the user does on the website very easily. Jul 7, 2018 at 9:34
  • Yes and much more than that too, having a persistent connection allows you to update content in the browser whenever you want without having to rely on client polling. Updating news feeds, instant messaging, track info for streaming audio etc etc.
    – miknik
    Jul 7, 2018 at 14:06
0

Due to the disconnected nature of HTTP, your access log would probably not give you what you need.

Not totally familiar with nginx or apache log, but I think most logs contain a timestamp, an HTTP request (the document requested and status, etc.) and an IP address.

Potential issues

  1. Without a session cookie, all IP addresses (same household, same company, etc.) would be seen as the same session.
  2. If someone goes to your site (1 HTTP request), consumes content on your site, doesn't proceed to another page, and leaves, your log will only contain that request (which is essentially a bounce, and you won't be able to calculate duration). If your application makes uses of a lot of javascript calls, then you might be able to log from the server side application,
  3. 2) If you use a tool like GA, etc., you can still use timer and javascript events (etc. though not perfect) to tell GA that the session is still active. Not sure if it works for typical server logs.
  4. It might not be as big as an issue if a typical visit contains more than 1 request, knowing that there is no easy way to get the duration after the last server request.
2
  • Thanks for the comments. Quick clarification, what is GA? Jul 7, 2018 at 9:32
  • GA refers to Google Analytics
    – jody tate
    Jan 17, 2019 at 22:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.