Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryStatistics and Tracking

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Single-page Logging

Knowing how many hits a specific Twitter or Facebook post garners can be valuable information when planning future campaigns. Number of hits from robots compared to number of hits from people is also nice to know.

Yes!
Works with WordPress
Insert PHP plugin.

I'll show you how to implement two logs for individual pages – one log for real browsers and one for all hits, including robots. Robots that follow URLs in JavaScript would also be recorded in the "real browsers" log. But those should be few.

Compare the two logs to see how many robots loaded the page and, if the information is provided, the type of robot and its purpose.

The logs are CSV formatted.

This system works on non-WordPress pages and also on WordPress pages with the Insert PHP plugin.

The logs contain date and time, IP address, referrer information (if available), web page URL, and browser/robot user-agent information.

The logging script is written in PHP. It is installed separately, simply uploaded to a location with a URL.

Here is the logging script. Copy the code and save it as SinglePageLogging.php or other .php file name of your preference. (Customization notes follow.)

<?php
/* 
   Single-page Logging
   Version 1.0
   November 19, 2012

   Will Bontrager Software, LLC
   https://www.willmaster.com/
   Copyright 2012 Will Bontrager Software, LLC

   This software is provided "AS IS," without 
   any warranty of any kind, without even any 
   implied warranty such as merchantability 
   or fitness for a particular purpose.
   Will Bontrager Software, LLC grants 
   you a royalty free license to use or 
   modify this software provided this 
   notice appears on all copies. 
*/
//
// Customization:
//
// Four places to customize.
//

// Place 1 --
// Location of log file directory. The directory must exist 
//   and have permissions sufficient to create/update files.

$LogFileDirectory = "/Log_Single_Page";


// Place 2 --
// "All hits" log file name.

$LogFileNameAll = "log_all.txt";


// Place 3 --
// "Browsers only" log file name.

$LogFileNameOnlyBrowsers = "log_browsers.txt";


// Place 4 --
// The URL of this file.

$URLofThisFile = "http://example.com/SinglePageLogging.php";


 // End of customization //
//////////////////////////
$LogFileDirectory = preg_replace('!/*$!','',$LogFileDirectory);
$LogFileDirectory = preg_replace('!^/$!','/',$LogFileDirectory);
$AllFile = $_SERVER['DOCUMENT_ROOT'] . "$LogFileDirectory/$LogFileNameAll";
$BrowsersFile = $_SERVER['DOCUMENT_ROOT'] . "$LogFileDirectory/$LogFileNameOnlyBrowsers";
$Referrer = $_SERVER['HTTP_REFERER'] ? $_SERVER['HTTP_REFERER'] : '';
$SelfURL = $_SERVER['PHP_SELF'];
if( isset($_GET['JS']) )
{
	$Referrer = $_GET['JS'];
	$SelfURL = $_GET['ME'];
}
elseif( isset($_SERVER['QUERY_STRING']) and strlen($_SERVER['QUERY_STRING']) ) { $SelfURL .= '?' . urldecode($_SERVER['QUERY_STRING']); }
$systime = time();
$LogLine = implode( ',', array($systime,
                               MakeCSVfield(date('l, F j, Y \a\t G:i:s',$systime)),
                               $_SERVER['REMOTE_ADDR'],
                               MakeCSVfield($Referrer),
                               MakeCSVfield($SelfURL),
                               MakeCSVfield($_SERVER['HTTP_USER_AGENT'])) );
if( isset($_GET['JS']) )
{
   if( $f = fopen($BrowsersFile,'a') )
   {
      fwrite($f,"$LogLine\n");
      fclose($f);
   }
}
else
{
   if( $f = fopen($AllFile,'a') )
   {
      fwrite($f,"$LogLine\n");
      fclose($f);
   }
   $cracked = preg_replace('!http!',"h'+'t'+'t'+'p",$URLofThisFile);
   $cracked = preg_replace('!//!',"/'+'/",$cracked);
   $crackedref = preg_replace('!http!',"h'+'t'+'t'+'p",$Referrer);
   $crackedref = preg_replace('!//!',"/'+'/",$crackedref);
   echo "<script type='text/javascript' src='$URLofThisFile?JS=$Referrer&ME=$SelfURL'></script>";
}

function MakeCSVfield($s)
{
   if( strpos($s,',') === false ) { return $s; }
   $s = str_replace('"','""',$s);
   return '"'.$s.'"';
}
?>

Customization:

  1. $LogFileDirectory — Specify the location of the log file directory, in the document root directory (where the domain's home or index page file is located) or a subdirectory. The directory must exist and have permissions sufficient to create/update files. On some servers, permissions 777 is required. On others, 755 is sufficient.

  2. $LogFileNameAll — Specify an "all hits" log file name. This is where all pages loads will be recorded, both browsers and robots.

  3. $LogFileNameOnlyBrowsers — Specify a "browsers only" log file name. This is where page loads by browsers will be recorded. Robots will be excluded unless they follow URLs in JavaScript.

  4. $URLofThisFile — Specify the URL of the logging script, the one you are customizing.

Installation:

  1. Ensure the directory specified in customization step 1, above, exists and has sufficient permissions. On some servers, permissions 777 is required. On others, 755 is sufficient.

  2. Upload the logging script to the location specified at the customization step 4, above.

The logging script has now been installed.

Test it by copying and pasting the URL specified in customization step 4 into your browser. When the URL is loaded in the browser, two files should be created in the directory specified in customization step 1, the file names specified in steps 2 and 3.

Setting Up Single-page Logging

After the logging script has been installed and tested, it is relatively easy to enable a page for logging.

Non-WordPress page.

Put this code anywhere in the web page to be logged.

<?php
include($_SERVER['DOCUMENT_ROOT'] . "/SinglePageLogging.php");
?>

Replace "/SinglePageLogging.php" with the location of the logging script on your server.

WordPress post or page.

Ensure the Insert PHP plugin has been installed and activated.

Put this code anywhere in the post or page to be logged.

[insert_php]
include($_SERVER['DOCUMENT_ROOT'] . "/SinglePageLogging.php");
[/insert_php]

Replace "/SinglePageLogging.php" with the location of the logging script on your server.

As you can see, once the logging script is installed, setting up a page for logging is easy. Set it up before posting on Facebook or Twitter and see how many robots and browsers visit your page.

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC