I want to create a visit counter online on my site, but I do not know where to start, it weighs a lot in the database? how to start?
I want to create a visit counter online on my site, but I do not know where to start, it weighs a lot in the database? how to start?
There are two types of analysis that you want to do the counter, try to count single users, or try to count the total number of hits, so you can use cookies test or session respectively.
To count on cookies:
$access_test = isset($_COOKIE["access_test"]) ? $_COOKIE['access_test'] : null;
if($access_test != true){
setcookie("access_test",true);
//Aqui você adiciona mais um ao campo do contador no banco de dados
}
Or to count in general:
session_start();
$access_test = isset($_SESSION['access_test']) ? $_SESSION['access_test'] : null ;
if($access_test != true){
$_SESSION['access_test'] = true;
//Aqui você adiciona mais um ao campo do contador no banco de dados
}
OBS:
The count is not 100% accurate, it is enough for the user to refresh the page by clearing the cache that will be counted plus one, but can not accurately know without having registered each user of the site.
There are good services that already do this, such as Google Analytics , which in addition to giving you the total of visits in real time will also provide you:
You will also be able to follow the steps of the users until they reach the desired goal and thus evaluate how to improve the navigation to make it easier for the user to get to where you want, such as completing a purchase, sending a form, etc.
Paste the following snippet immediately following the <head>
label on each page of the site. Replace GA_TRACKING_ID
with your own% of Google Analytics tracking%
<script async src="https://www.googletagmanager.com/gtag/js?id=GA_TRACKING_ID"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'GA_TRACKING_ID');
</script>