How to count page views on a website

Asked

Viewed 7,312 times

0

I would like to implement a page views counter on my site, present a numerical reference on the site, and use this data to perform a ranking of most accessed articles/post and automatically insert a widget, in the side bar of the site.

Node.js solves the problem, it makes this bridge between front and back using javascript, where I am more familiar, I can access the database, be it Mongobd and/or Mysql and learn a language that will dominate in the near future. Now is to devote to learning, let’s move on!

  • Why do you need this information? Just for analysis? If so, use Google Analytics (http://analytics.google.com). Then simply implement the tracking code on your website and you’ll get your information, including verifying visitors in real time.

  • For analysis you can use the Google Analytics, Now to create a Rankin own system of most accessed content, you need to calculate all the visits register in a database and then display this information. Can be done basically with any language.

  • They’re not just a tag, no Ricardo, I want more than just to analyze.

  • You can create a table in your database and each time the page is requested use a for() to add +1 to the database record! if you want to make a single counter use cookies to save the user and not re-register in the database if he has visited the page! Now this varies whether you do it in procedural or object-oriented! That was the first logic that came to mind, but has many ways to do this!

  • If what you need cannot be supplied by the use of Analytics, then it would be best to use a database for this and insert a php (or server-side language you prefer) to add +1 in the number of visits from that page in the database... Then to pull this in a widget would just take the database data... If something is missing comments here that I complete the answer

3 answers

2

A table should be created (if it is the case of a relational database) with 2 basic fields, ID of the post and the number of views, would look like this table

| id | id_post | qnt |
----------------------
| 30 | 000050  | 010 |
----------------------
| 31 | 000014  | 017 |
----------------------

Then create a script/function that every time the blog view page is called, then with the id at hand (and you should have) of the post, you would pass to this function. Thus it would be incremented 1 to the post record.

  • And as for the language to be used, the ideal is that it is in the language that the system is being developed.

  • I’m developing the action part using Jquery. I’m still trying to figure out what the table would look like in the database.

  • Jquery will be loaded into the browser, then executed. It will not run on the server. So if you are going to use Jquery you must request via ajax a url that will write the data that was mentioned above.

  • Node.js solves the problem, it makes this bridge between front and back using javascript, where I am more familiar, I can access the database, be it Mongobd and/or Mysql and learn a language that will dominate in the near future. Now is to devote to learning, let’s move on!

1

First create a page called counter.php, it should be empty if no code in it.

Then create another index.php file

<?php
$a = 0;
include 'contador.php';
if (isset($_COOKIE['counte'])) {
  $counte = $_COOKIE['counte'] + 1;
}else{
$counte = 1;
$a++; 
}
setcookie('counte', "$counte", time()+3700);
$abre =@fopen("contador.php","w");
 $ss ='<?php $a='.$a.'; ?>';
 $escreve =fwrite($abre, $ss);
 ?>
 <html>
 <head>Contador de Visitas</head>
 <body>

<?php 
echo "<br>$a Pessoas visitaram esse site e você já visitou" .$counte. "vezes";
?>  

 <p>Conteúdo do seu site</p>

<?php $a=0; ?>
</body>
</html>
  • Cara is not a visitor... I want to see the articles on my site and then rank, as @Ricardo said, the most accessed.

  • I believe that this is not the best option, and also does not fulfill the role when performing the ranking of the most accessed.

  • This video shows some things that might be useful to you. video: https://www.youtube.com/watch?v=5px-GRRFC08

  • I will watch, vlw. I expect other solutions from the masters in the subject.

-1

Solution for

Jquery--->>

<script>
    $(document).ready(function () {
        var getVisits  = sessionStorage.getItem('visit_'+"{{ $page_tag }}");
        if(!getVisits)
        {
            var saveVisits = sessionStorage.setItem('visit_'+"{{ $page_tag }}", 1);
            $.ajax("{{ route("visits") }}/" + "{{ $page_tag }}/" + saveVisits);
        }
    });
</script>

--> Php Route

Route::get('visits/{page_tag?}/{visit_count?}', function ($page_tag){
    $page = \App\Models\Pages::where('tag', $page_tag)->get()->first();
    $page->visits = $page->visits + 1;
    $page->save();
})->name('visits');

Browser other questions tagged

You are not signed in. Login or sign up in order to post.