thecelebratedmrk's avatar

Adding simple visitor counter to site

I'm about to implement some super-super-simple statistics for my site, without any use of tracking or cookies.

  • Count the number of unique visitors to my site.
  • Count the number of clicks on a Call-To-Action-button. It doesn't have to have any functionality other than counting the clicks.

I've been recommended three different methods so far:

  1. Create middleware: https://blog.dzarsky.eu/anonymized-website-visitor-counter-in-laravel
  2. Use Redis: https://laracasts.com/series/learn-laravel-and-redis-through-examples/episodes/1
  3. Use Laravel Nova in some way.

I've never used Redis and would like this to be as effortless as possible (without installing and learning too much new), but I'm also not sure if adding thousands of rows to a database-table is the best way. At most my site has had 30k+ unique visitors per week.

All help and input appreciated.

0 likes
13 replies
Sinnbeck's avatar

Use livewire and just increment a count in a table column in your database?

1 like
thecelebratedmrk's avatar

@Sinnbeck Any specific reason to pull in Livewire at all? Can't I just increment a count in a table column on route-load? Or do you mean for the CTA-button-click?

Sinnbeck's avatar

@thecelebratedmrk sure you can just use ajax. That's all you need. Or have the button in a form which you submit when clicking on the button. It was just a suggestion on how to implement it in a couple of minutes

But save it in the database so it's persistent. Incrementing a field is super fast and is built into laravel

OussamaMater's avatar

Even if you are planning on using Redis, you will need to create a global middleware and apply it all the routes.

And if you want something accurate I am not sure if Redis is the best option, for example the viewers count on this thread is not accurate at all (I am assuming its built using Redis as mentioned in that episode), if you keep refreshing the page it will increment even though you are the same person, so that's not the kind of stats you are willing to go for I guess, if you are having +30k users and you don't make sure you are registering unique visits the Redis will expand so much, in no time.

So yes do take that in consideration, as for the database you have more control, you can save multiple information like the user-agent, the ip, time, and much more details to help decide if it's a unique view later on and to not save it again, which leads to less records in your database (you can do that in Redis too but it will take more effort as it is designed to be a key-value).

2 likes
Snapey's avatar

@OussamaMater your observation is nothing to do with redis, that's just a way to store the info quickly

The key point is if it is a page load counter, or a visitor counter. A visitor counter requires that you also store information about the visitor so that you don't count them twice, or give them a cookie and check for its existence

1 like
OussamaMater's avatar

@Snapey and what have I said? to be able to get the unique visitor count you need to store more information about the visitor, Redis is not really suitable for that, as it's designed to be a key-value store, if you intend to use it as a regular database then why not simply use the SQL database and have more control.

Your key point is exactly what I stated above :)

1 like
Snapey's avatar

if you keep refreshing the page it will increment even though you are the same person

Thats a function of it being a page counter. You seem to imply that its a failing of redis that it cannot track visitors.

All that is needed is a unique hash of the fingerprint of the user, and that can easily be stored and searched in redis.

OussamaMater's avatar

@Snapey No I was referring to that episode, where on every page refresh they incremented the counter directly without any logic or checking, and as he said a super-super simple counter I was thinking he will be doing the same method in the episode (the same implementation) so I pointed (in case he's not aware) that it will not be accurate at all! so if you have not watched it maybe do so you understand what I mean.

I am not implying that Redis is the reason of the inaccuracy, I have said that you can achieve it using both Redis or a SQL database at the end of my answer.

And I do agree that all you need is a fingerprint of the user by using whatever data you parse from the HTTP headers.

1 like
Snapey's avatar

Your main requirement is

Count the number of unique visitors to my site.

So you need a way to track the visitors. This requires that you either fingerprint their device and then store information about this fingerprint as a unique hash, or you give the visitor a cookie that indicates you recorded their access.

Tracking of visitors is quite a sensitive subject at the moment, and Fathom analytics are building a business on the back of privacy concerns

If you store the fingerprint of the device then it means you have to search the fingerprints to see if you have seen this user before and therefore if you should count them as unique. This means that you might need a store like redis to hold the fingerprint info in order to not slow requests.

The cookie approach is potentially quicker as you don't need to store anything on your side, you can see if they have the cookie (do nothing) or if not then send them a cookie and increment your counter, wherever that might be held.

Redis is just a fast value store, but unless you take steps to back it up, it only lives in memory and will be lost if the server is rebooted.

thecelebratedmrk's avatar

Tracking of visitors is quite a sensitive subject at the moment

True. I think I'll do the middleware-method linked in my OP: Store date and a hashed IP, and that's it. If I can avoid using Redis, I'll avoid it; I'm not the most tech-savvy, and would rather not install and learn more technology than absolutely neccessary.

Snapey's avatar

@thecelebratedmrk consider that ip address can be a very poor indication of uniqueness

Everyone on the same local network will have the same ip. Eg everyone at the same corporation or everyone in the same house. Conversely visitors on mobile might get a new a new ip every 30 minutes.

Avoid redis if you want but bear in mind that you will add one db query to every request.

thecelebratedmrk's avatar

bear in mind that you will add one db query to every request.

And that is a bad thing? Sorry if this is a super-basic question, but I'm mostly a frontend-guy and not very used to working with databases.

Please or to participate in this conversation.