2023
A simple system to track website visitors
How do you track website visitors using a simple system? Get the basic visitor indicators like an IP address, country, city, coordinates...
Visit statistics are a necessary element of analysis for the performance of any web page. It can include various indicators, the most important of which are the number of visits for a certain period of time, the IP address and the location of the visitor. Here I will present a simple yet effective solution to track your website visitors.
Information about visit time, IP address, country, region, town and zip code is provided. Quite enough isn’t it!
The tracking system consists of three files that exchange data. For experiment purposes, you can place them in the root directory of your website.
Catch and track website visitors
Each visitor has a unique IP address (indicating the connection point on the network). If we intercept this address it will be easy to determine the other data as well. We will acquire this additional data from an external provider – IPinfoDB. In order to implement this system on your website, you need to register on IPInfoDB and get an API access key. Registration and key are free.
The first system file (tracking-visitors.html) contains JavaScript functions that perform the following tasks:
- Constructs a request to IPInfoDB, including the API address, the key and the format of the response (in this case JSON).
- The sent request automatically includes the IP address of the sender (in the example, the one who opens the page with the JavaScript code of the system).
- Several variables are created containing the date and time of the visit.
- This data is bundled together and sent using the XMLHttpRequest object to the next page count-visitors.php, which will process it and save it to a file.
Create the demo statistic page like tracking-visitors.html
<h1>Tracking visitors demonstration</h1> <h2>Refresh the page to see more results</h2> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js" integrity="sha512-DUC8yqWf7ez3JD1jszxCWSVB0DMP78eOyBpMa5aJki1bIRARykviOuImIczkxlj1KhVSyS16w2FSQetkD4UU2w==" crossorigin="anonymous"></script> <script> $(window).load(function(){ var api_url = "https://api.ipinfodb.com/v3/ip-city/?"; var api_key = "key=Your_Key_Here"; var api_format = "format=json"; var full_url = api_url + api_key + "&" + api_format; $.getJSON(full_url, function(data_visitor_json) { var data_visitor_inner = JSON.stringify(data_visitor_json, null, 2); sessionStorage.setItem("data_visitor", data_visitor_inner); }); var today = new Date(); var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate(); var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds() + "\n"; var dateTime = date+' '+time; var data = new FormData(); data.append("data" , "\n" + dateTime + sessionStorage.getItem("data_visitor") + "\n"); var xhr = (window.XMLHttpRequest) ? new XMLHttpRequest() : new activeXObject("Microsoft.XMLHTTP"); xhr.open('post', '/count-visitors.php', true); xhr.send(data); }); </script> <a href="/visitors.txt" target="_blank">Check visitors</a> <a href="/count-visitors.php" target="_blank">Check quota</a>
You can edit code to perform your view or add it in a template.
Create the records for website visitors
The second task after receiving the visitor data is to be able to process and record it in a suitable form. The responsibility takes count-visitors.php. For the example, I will use a simple text file called visitors.txt, which I will add to the data of each new visitor. Each visitor record consists of fifteen lines. To keep the records file from getting too big, I'll limit it to one hundred visitors. Once this limit is reached, the file is automatically purged.
The engine file count-visitors.php
<?php if(!empty($_POST['data'])) { $data = $_POST['data']; $fname = "visitors.txt"; $file = fopen($fname, 'a'); fwrite($file, $data); fclose($file); } $file = new \SplFileObject('visitors.txt', 'r'); $quota = 1500; $file->seek(PHP_INT_MAX); echo '<h1>Visitors quota</h1>'; echo '<h2>Counted '; // count the lines and divide them by fifteen per visitor. echo ceil(($file->key() + 1)/15); echo ' visitors from ' . ceil($quota / 15) . ' quota</h2>'; if (($file->key() + 1) > $quota) { $filePointer = fopen('visitors.txt', 'w+'); // Use unlink() function to delete a file if (!unlink($filePointer)) { echo ("$filePointer cannot be deleted due to an error"); } else { echo ("$filePointer has been deleted"); } } ?>
The listed code is located at count-visitors page.
Test the tracking system
We have already implemented the test system in our website. To test click Track Website Visitors Demo.
We developed another tool for tracking visitors. You can get a simple website visitor counter for your website.
Contact
Missing something?
Feel free to request missing tools or give some feedback using our contact form.
Contact Us