Visitor Counter

0

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?

    
asked by anonymous 05.03.2018 / 02:50

2 answers

3

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.

    
05.03.2018 / 14:37
3

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:

  • Visits from previous days
  • Visits by technology (whether mobile or desktop)
  • Visits by region and country
  • Origin of visits (from where the user found your site)

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.

Configuring gtag.js:

  • Sign in to your Analytics account.
  • Click Admin .
  • Select an account from the menu in the ACCOUNT column.
  • Select a property from the menu in the PROPERTY column.
  • Under PROPERTY, click Tracking Info > Tracking code. The tracking ID is displayed at the top of the page.
  • Tracking code snippet
  • 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>
    
  • 17.05.2018 / 14:50