Website Testing | Robots and other Crawlers | STF hosting
Php and Javascript Counters & Visitor Tracking Scripts.
This page is a compilation of resources on scripts related to tracking, pageviews, mouse and click counting etc. It is primarily devoted to php/mysql and javascript tracking but includes other tidbits related to visitor and site tracking.
|
|
DISCLAIMER Every effort has been taken to provide information which is accurate. The information is believed to be accurate at time of writing, however, please follow up with your own searches and appraisals and ensure that the scripts offered by a website meet your requirements. If you have any updates we should know about please contact SiteTrackingForum.com here.
|
Do it Yourself PHP Visitor Tracking
Php has predefined variables. These are the built-in variables that deliver tracking and browser information. Superglobal built-in variable $_SERVER takes certain parameters and will "dump" or delivers tracking information. Some of these variables rely on the server and user-Agent or browser settings being fully functional/not disabled. Host servers use CGI scripts and HTTP to serve webpages to the world. Typically servers have a base set of variables available and a set not available or optional for requests asked of gateway (CGI) programs. To build scripts you must rely or alter these specifications. All information not always available for code used to track visits to a site. In php, GETENV() can be used in place of $_SERVER but the later is more widely available on servers. Info on cgi environmental variables and their availability in gateway programs can be found here.
The php.ini file and configuration
The php.ini file controls the behaviour of php.
Usually, provided with php on host's server.
Possible to overwrite the default php.ini file by writing/commenting a custom one in a text editor such as notepad. You must include base settings(which differ for php4 and 5) then only environmental settings required.
If host server has register globals off (check this), create the custom php.ini file as described in the folder where the script runs and insert or uncomment this line: register_globals = ON. This sets the global variables required for tracking. A Custom php.ini file must go in the folder where you are using the settings/scripts that require it but if you use cpanel you can use the QuickConfig utility available for cpanel users where checks or changes to php.ini configuration settings are easily done. Is there anyone not using cpanel out there?
Relevant to tracking and php.ini is the timezone setting. If you want to change the timezone - say to your local time - you will need to uncomment/change "date.timezone." To start you off on this, check this page from Learning on demand. Also check out webcheatsheet's page on installing and configuring php.
session_start() and setcookie()
To pass info between pages for tracking you can use php sessions and set cookies. Using setcookie() will not always work if the visitor has disabled in browser.
session_start() will basically assign an id to the users visit for duration of stay and allows info (like request times,urls etc...) to be stored in the $_SESSION() variable. (Actually a temporary cookie stored on visitor's computer. If you check cookies folder should see "phpsessid" for sites using this feature.
The $_session variable can also be used to make a pageviews counter. To see how this worksthis w3schools page.
Few tricks to php sessions. You must start a session before any output - not even whitespace! Place session_start() at the very top of the page between php tags before any other code. Also, check if trans-sid is turned on in the php.ini file. Plus more! there should be a temporary directory allocated to store the session variables. By default, usually a folder in cpanel named TMP situated in the home/root directory. If not there you need to create it and specify the path (php.save_path) in php.ini.
Php Code for extracting tracking Info
Here is the php code that outputs tracking details. In this example, user's IP -YOUR IP!- from $_SERVER['REMOTE_ADDR']. All information in the table uses this format with a different parameter inside the [ ] square brackets. Variable displays using php echo (second column shows the output).
$userIp = ( isset($_SERVER['REMOTE_ADDR'])
&& ($_SERVER['REMOTE_ADDR'] != ""))
? $_SERVER['REMOTE_ADDR'] : "Unknown";
|
$_SERVER['REMOTE_ADDR'] dumps user IP
|
38.107.191.101 |
|
$_SERVER['REQUEST_TIME'] dumps the user request time. This is formatted in seconds since January 1, 1970. Possible to format this into a timestamp to store in a MySql. database, for example, create a new variable and use the date() function.See this tutorial.
|
In this case visitors request time in seconds (since Jan 1,1970) is:1280505373 which is equivalent to 2010-07-30 10:56:13 |
|
$_SERVER['HTTP_ACCEPT_LANGUAGE'] dumps the user's language
|
en-us,en;q=0.5 |
|
$_SERVER['HTTP_REFERER'] dumps the user's referer
|
Unknown |
|
$_SERVER['HTTP_USER_AGENT'] dumps the user's browser info.
|
CCBot/1.0 (+http://www.commoncrawl.org/bot.html) |
More resources (ordered by date found)
14 May, 2010, Google Analytics has become more and more sophisticated and it is pretty hard to go past the relatively new Asynchronous Code (_gaq) which allows the tracking of events (onclick etc...). In other words which links are clicked and these will not throw an exception in the case that a page has not finished downloading when the link is clicked. Events are queued into an array. Calls to AJAX requests can also be tracked which was available in a previous Google Analytics version (_gat) using a call when AJAX readystate equals 4 (See AJAX call tracking for (_gat). A general article can be found on the the Analytics AJAX tracking guide page See also:
this document on gaJS (Google Analytics Coide).
3 Apr, 2010, Javascript Tutorials by Stephen Chapman click here. Anyone interested can subscribe to the Javascript guide run by Stephen.
15 Feb, 2010, Came across this fascinating article on the history of the user-agent string (browser detection string). http://www.nczonline.net/blog/2010/01/12/history-of-the-user-agent-string/. Check it out it will explain why all user-agent strings start with Mozilla and why Mozilla originally was short for Mosaic Killer! 21 Aug,2009, Javascript tut/script for showing time spent on a site can be found at www.999tutorials.com.
31July,2009. Javascript to call a link counter helps to count how many times an outbound link is clicked on your site. See James Caws page here for a solution and pros and cons of doing this.
July31,2009. Arash Dejkam has a fully functional php/mysql tracking plugin for free at Tracewatch.com.
July19,2009. Most of us have crossed the bridge of knowing that you can't call a PHP/Mysql script using Javascript alert/confirm/prompt dialogue boxes. But you can indirectly call it by sending the user to another page which runs the script. It's not elegant but gets the job done and can even be used for clickcounting urls as shownhere
July,15,2009
Visitor polls are part of tracking visitors. A very elegant php/mysql piechart using Yahoo Flash API can be found on this hubpage:Php/Mysql and Yahoo Development Network
May 23,2009
Here's a useful page on reducing download times for a webpage - this is an essential consideration as user's don't like hanging around for pageloads. The page also includes javascript log script for determing download time and bandwidth of a user based on entry time minus body loaded time. See the article at die.net.
Mar 12,2009.
(Just scraped in before friday the 13). Useful Mysql database and tutorial:learning SQL
Mar 10,2009
AJAX and php. AJAX (Asynchronous JavaScript and XML). Uses the xmlhttprequest object in javascript to allow a browser displaying a webpage to communicate - or make http requests - to the server. Pretty awesome for making a page dynamic and reducing download time. In this W3 tutorial, the request is for the current time from an asp file on the server.
Using AJAX, it is possible to return information from a database and display it in a webpage on the fly. See this tutorial.
Mar 10,2009
Anyone heard of Fusion Charts? A gallery of charts on this page which can be integrated with many languages including php scripts and Mysql databases. I hope they have other colors too though!
Feb 17, 2009
Once you've got tracking info, you can write it to your database then retrieve it but the question still remains how will you display it...Especially if it turns out to be a number of pages long? Nettuts.com runs tutorials on a range of web building tools including one on data pagination using php. Article on creating secure php applications here: Nettuts.com 5 helpful tips to create more secure applications.
Feb 15, 2009.
Getting times for visitor sessions can be passed to cookies. A forum found at webmasterworld.com, although not overly specific a useful discussion. Click here to see. It discusses document.onload and document.onbeforeunload (Javascript)...or how to calculate differences in times between page loads and storing these in a session cookie.
Feb 12,2009.
php-development.ru provides an impressive number of PHP site statistics and javascript scripts to help monitor visitor activity.The page inlcudes a click tracker.
Jan 31,2009
Stevedawson.com has a visitor counter script in php on this page.
Jan 31, 2009
You may need to convert percentages, visitor numbers etc... into graphs and charts. A couple of pages relating to scripts to represent numerical values graphically. One at Jpowered.com and also see this page at devzone.zend.com.
Jan 27,2009
For those using Google Analytics code on their webpages, here is a free Javascript add-on from Panalysis.com which, in their own words:
"Automatically captures clicks to specified non HTML documents and links to other websites and feeds these into the Google Analytics tracking code version 1.02." These guys are Google authorized consultants and only ask you retain the copyright and promotional header in exchange for use of the script.
Jan 21,2009
Here's an interesting article on tracking mouse clicks (via co-ordinates) and putting them into a database for retrieval later for a clickmap. Check it out. The pic is humorous too!
Jan 21,2009
Today found this script by Jacques Noah for tracking visitors using php/Mysql at devshed.com
Jan18,2009
Visitor tracking or visitor hacking?
Here is an interesting dialogue relating to the JavaScript.history.back object and how you get access back a URL or two (with history.go(-2)). Does this have relevance to tracking? With referring pages it might - more research required here but if we can get info on earlier referring pages that led a user to our site that would be quite awesome visitor intelligence! We already have CSS properties like a:visited keeping track of accessed URLs/links doing a bit of the work behind the scenes. However, there could be some ethical issues with this which may or may not have been dealt with in browser security and development already. But when does visitor tracking become hacking into a visitor's records or history? We need to know where to draw the line. I started to enquire along this line of thinking on finding this hacker blog: steal browser history without javascript and this blog on history-sniffing by Niall Kennedy here. So what are we - just glorified hackers or business intelligence analysts and where do we draw the line between good tracking and evil tracking? Big questions! There is a blog post you can contribute to on this topic here. Sitetrackingforum.com tends to the view that it's better to check it all out - worts and all. To Know how it works but retain a SOCIAL CONSCIENCE.
Marcel Oelke has a script which will record mouse movements using Javascript and Ajax.It actually logs mouse movements on a webpage. Check this out - it is pure programming poetry!
After an outage leaving a gap in his visitor/hit data, Stephen Pierzchala - performance consultant - is inspired to write his own PHP tracking code which writes to MySql database.
Track Your Visitors Using PHP - by KINGS takes you through the steps to making your own logfile.
Willmaster.com has some interesting resources and the automatic form submission article here could be handy for tracking visitor info. The jury is still out on how useful automatic submission can be re tracking. Specifically, the code shows how to auto submit hidden form fields containing such data as "The current web page URL" or "time zone information from the visitor's computer". Although it is in Javascript, posts from these automatic hidden input fields should also be accessible to PHP scripts which in turn can communicate with a MySQL database. (Access is available via php $_GET[fieldname] and $_POST[fieldname] built in arrays.)
Click-tracking using PHP
Trimbitas Sorin has written this tutorial on How to do a simple tracking script for your links/ads in PHP (this is an archived blog page so if not found straight off scroll down and take next page in the archive. Great stuff here and at the end of the day it is link/ad info that gives the most insights for understanding visits. In a sense it is where you want to be heading with your tracking scripts as providing websites with click data inside pages - not just URL paths, but links clicked inside pages is what it is all about!
Go here for another tutorial on building your own visitor tracking script using PHP which writes to a MYSQL database.
Knowing "how" to access logs and visitor information is important but it must not be forgotten that "what" is tracked and "why" are also useful questions to ask. See this classic lecture by S. Raefeli.
As you may be aware (we hope so!) programming is not always hassle free and it can take time to streamline scripts and get them to execute in the way intended. See this great article for some useful advice on programming.
The usual disclaimer applies.
Visitor plotter
[quote]
The 'visitors plotter' allows you to plot the location of your visitors on a world map. Giving you (and your visitors) a bird eyes view on the location of your visitors.[unquote]Visitor plotter
php click counter
Knowing how many times links are clicked on a website. Script found at php junkyard.
Free script is a real beauty and rated highly by users at hotscripts.com which is one of the best script indexes I've found.
The php junkyard description says:
[quote]
Ccount is a PHP click counter which can also be used as a download counter. With its help you can count clicks on any link on your website. A great tool if you want to know how many times a link has been clicked, a file downloaded ("download counter") and similar. You can also hide your affiliate links with PHP click counter. As long as it's a link - Ccount can count clicks on it!
[unquote]Click here for php click counter script!
A great site - px.sklar - is full of PHP hit counter and tracking scripts and all of these are free.
No page on PHP would be complete without a reference to PHP.NET
Also check out Tizag.com Webmaster Tutorials - A collection of webmaster tutorials from HTML to PHP.
Return to top of this page
| CONTACT
|