Ligonsker's avatar

Designing a system that will gather usage information on my website

Hello, I want to start creating a system that will gather information regarding the usage of the website to get an idea of which parts of the website are in use the most, and which are less so that I will be able to perhaps deprecate them or merge them with others.

For that I will need to probably track how many times each page is visited, and if it's a page that has forms - how many times was this form submitted (either AJAX or HTML form).

But where do I start? What key things do I need to consider before starting to design the system. What else do I need to consider except the things I wrote above? And even for simple things like counting page views -what is a way to implement it on all the websites (Maybe a middleware that will wrap all the routes in web.php?), and how to make sure it doesn't stress the system? If it's possible?

I'd like to get some tips before I start implementing it alone

Thanks!

0 likes
1 reply
LaryAI's avatar
Level 58

To design a system that gathers usage information on a website, there are a few key things to consider:

  1. What data do you want to collect? As mentioned in the question, you may want to track page views and form submissions, but you may also want to track other user actions such as clicks on certain buttons or links.

  2. How will you collect the data? One common approach is to use a tracking code or script that is embedded on each page of the website. This code can send data to a server-side application that stores the data in a database.

  3. How will you store and analyze the data? You will need to decide on a database schema that can store the data you want to collect, and you will need to develop a system for analyzing the data to gain insights into user behavior.

  4. How will you ensure that the tracking system does not impact website performance? One approach is to use asynchronous tracking code that does not block the loading of the page. You may also want to consider using a content delivery network (CDN) to distribute the tracking code and reduce the load on your servers.

Here is an example of how you could implement a simple page view tracking system using Laravel:

  1. Create a new middleware that will increment a page view counter for each request:
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Redis;

class TrackPageViews
{
    public function handle($request, Closure $next)
    {
        $key = 'pageviews:' . $request->path();
        Redis::incr($key);
        return $next($request);
    }
}
  1. Register the middleware in your Kernel.php file:
protected $middlewareGroups = [
    'web' => [
        // ...
        \App\Http\Middleware\TrackPageViews::class,
    ],
];
  1. Use Redis or another database to store the page view counts. You can retrieve the counts in your application code and use them to display analytics or make decisions about which pages to deprecate or merge.

Note that this is just a simple example and there are many other factors to consider when designing a usage tracking system.

Please or to participate in this conversation.