Logging Link Clicks
Sometimes it is desirable to know how often a certain link is clicked or tapped. Reasons include:
-
Determining degree of interest by counting number of clicks.
-
Logging clicks on a link you're being paid for, or an ad.
-
To see which links are the most popular on a specific page.
Any regular HTML a
tag link can be enabled for logging. Enabling entails inserting this attribute:
onclick="return LinkClicked(this)"
When the link is clicked, a JavaScript LinkClicked() function sends the link details to a PHP script for logging.
The log is a CSV file. The following items are logged when the link is enabled for logging:
-
The date and time.
-
The site visitor's IP address.
-
The URL of the web page containing the link.
-
The URL of the referring web page, if applicable.
-
The link URL that was clicked.
This system can let you know how many times certain links are clicked or tapped. And when.
When imported into a spreadsheet, duplicate IP addresses in the log can be removed for a more accurate count.
How to Implement Click Logging
When a link is clicked or tapped, JavaScript tells a PHP script on your server to log the event. The log is a CSV file with one log line per click or tap. The log lines look something like this:
"2024-11-21 01:20:47","127.43.9.213","https://example.com/page.php","https://example.com/other.php","https://example.com/destination.php" "2024-11-21 01:32:26","12.74.117.8","https://example.com/page.php","","https://destination.com/page.php"
The 5 items of the log line are in the order mentioned in the list further above.
The actual logging is done with a PHP script on your server. The PHP script gets its instructions from JavaScript on the web page where an enabled link is clicked.
Here is the source code of the PHP script. Notes follow.
<?php
/*
Log Clicks
Version 1.0
September 12, 2021
Will Bontrager Software LLC
https://www.willmaster.com/
Copyright 2021 Will Bontrager Software, LLC
*/
/* Customization */
// Specify the location of the CSV file for recording the link clicks.
$FileLocation = '/php/logging/LogClicks.csv';
/* No other customizations required. */
if( empty($_GET['desturl']) ) { exit; }
if( strpos($FileLocation,'/')===0 ) { $FileLocation=$_SERVER['DOCUMENT_ROOT'].(preg_replace('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','',$FileLocation)); }
$LogPart = array();
$LogPart[] = '"'.str_replace('"',"\\".'"',date('Y-m-d H:i:s')).'"';
$LogPart[] = '"'.str_replace('"',"\\".'"',(isset($_GET['ip']) and preg_match('/^[\d\.]+$/',$_GET['ip']))?$_GET['ip']:$_SERVER['REMOTE_ADDR']).'"';
$LogPart[] = '"'.str_replace('"',"\\".'"',$_GET['pageurl']).'"';
$LogPart[] = '"'.str_replace('"',"\\".'"',$_GET['refurl']).'"';
$LogPart[] = '"'.str_replace('"',"\\".'"',$_GET['desturl']).'"';
file_put_contents($FileLocation,implode(',',$LogPart)."\n",FILE_APPEND);
?>
Notes —
In the PHP script, replace /php/logging/LogClicks.csv
with the location for the CSV file.
The script will create the CSV file if it doesn't already exist. However, the directory for the file needs to exist before the file can be created.
Save the PHP script as LogClick.php
or other *.php file name. Upload it to your server and note its URL.
LogClick.php
gets its information from JavaScript on the web page where an enabled link is clicked.
Here is the source code of the JavaScript. Notes follow.
<script type="text/javascript">
function LinkClicked(d)
{
var scripturl = "/php/LogClick.php";
var http = new XMLHttpRequest();
if(! http) { return true; }
var params = new Array();
params.push( "ip=" + encodeURIComponent("<?php echo($_SERVER['REMOTE_ADDR']) ?>") );
params.push( "pageurl=" + encodeURIComponent(document.URL) );
params.push( "refurl=" + encodeURIComponent(document.referrer) );
params.push( "desturl=" + encodeURIComponent(d.href) );
http.onreadystatechange = function(){if(http.readyState==4 && http.status==200){return true;}}
http.open("GET",scripturl+"?"+params.join("&"),true);
http.send();
return true;
}
</script>
Notes —
In the JavaScript, replace /php/LogClick.php
with the URI of LogClick.php
. The URI is the URL minus the leading protocol and domain name. (As an example, URL https://example.com/php/file.php
becomes /php/file.php
when converted to a URI.)
Paste the JavaScript into a web page where one or more links are or will be click-count enabled. Alternatively, the JavaScript may be pulled into the page from an external file.
To enable a link for click counting, put this attributer into the HTML a
tag:
onclick="return LinkClicked(this)"
Note —
Put that attribute into every HTML a
tag where you want the link click/tap counted.
You are now ready to test your implementation.
Testing
To test, click or tap on a link to be tested. Then, see if the click is logged in the log file.
If no log entry is made or the log file is not created, it may be necessary to give 777 permissions to the directory where the CSV log file will be maintained.
You now have a system that logs the clicks or taps on certain links.
This article first appeared with an issue of the Possibilities newsletter.
Will Bontrager