Page 1 of 1
External Link Click Coutner
Posted: Mon Aug 28, 2006 10:32 am
by smurf00
Hi all.
Just set up a website and need to create an external click counter to find out who is clicking on my advertisers and more importantly how often.
I was told I will need a PHP script for this. I have a VERY BASIC knowledge of HTML and know nothing about PHP.
My website is
http://www.iwantafreehouse.com
Can someone give me an example of a basic webpage with a link and this type of counter and tell me how I actually get the results??
Thanks all!!
Shane
Posted: Mon Aug 28, 2006 11:42 am
by Dale
Hey Shane,
There are some PHP based click trackers here:
http://www.hotscripts.com/PHP/Scripts_and_Programs/Click_Tracking/index.html
If you have a look through this list there are some that you could use.
I hope that helps.

Posted: Mon Aug 28, 2006 3:19 pm
by bokehman
Using php as a click counter is a bad thing because it means no real link exists in the document (bad for SEO). A better way would be to use Javascript to rewrite the links.
Posted: Mon Aug 28, 2006 3:40 pm
by sparkle
if you could expand on that...

Posted: Mon Aug 28, 2006 3:52 pm
by bokehman
sparkle wrote:if you could expand on that...

Which part?
Posted: Mon Aug 28, 2006 4:28 pm
by Dale
bokehman wrote:sparkle wrote:if you could expand on that...

Which part?
The Javascript bit I would believe...
Posted: Mon Aug 28, 2006 4:48 pm
by bokehman
Ok! A php click-thru is still needed to take records but for SEO reasons the remote URL should be used for the links in the document. Two ways to rewrite the links... one is to rewrite all external links:
Code: Select all
function parse_links()
{
new_url = 'http://www.mydomain.com/click-thru.php?url='
for(i = 0; i < document.links.length; i++)
{
if(document.links[i].hostname != document.location.hostname)
{
document.links[i].href = new_url + document.links[i].href
}
}
}
onload = parse_links
Or you could just rewrite the URLs that are ina particular element:
Code: Select all
function parse_links(element)
{
new_url = '/clickThru.php?url='
LINKS = document.getElementById(element).getElementsByTagName('a')
for(i = 0; i < LINKS.length; i++)
{
if(LINKS[i].hostname != document.location.hostname)
{
LINKS[i].href = new_url + LINKS[i].href
}
}
}
onload = function(){ parse_links('rightcolumn') }
Just put that in an external .js file. The php script might look something like this:
Code: Select all
if((!empty($_GET['url'])) and (preg_match('/^(?:f|ht)tps?:\/\/[^\/]+\.[^\/]+(\/[^\'"]*|)$/i', $_GET['url'])))
{
connect();
$query = "INSERT INTO `clickthrus` (`url`, `clicks`) VALUES ('".
clean($url = preg_replace('/^http:\/\/(www\.)?([^\/]+).*$/i', '\2', $_GET['url'])).
"', 1)";
if(!mysql_query($query))
{
if(1062 === mysql_errno()) // this script was used on MySQL 3.23 so this replaces ON DUPLICATE KEY UPDATE
{
$query = "UPDATE `clickthrus` SET `clicks` = (`clicks` + 1) WHERE `url` = '".clean($url)."'";
mysql_query($query);
}
}
header('Location: '.$_GET['url']);
}
feyd | to avoid some confusion, I've adjusted a few of the blocks from
Posted: Mon Aug 28, 2006 6:32 pm
by jabbaonthedais
bokehman wrote:Using php as a click counter is a bad thing because it means no real link exists in the document (bad for SEO). A better way would be to use Javascript to rewrite the links.
So you're saying linking to:
http://www.mydomain.com/click-thru.php? ... oogle.com/
is better than just:
http://www.mydomain.com/click-thru.php?id=12
I was under the impression search engines didn't like either. I'm very interested though, because I would like to use something to track outgoing hits myself. Couldn't you set up something server-side that tracked outgoing hits but displayed actual urls (
http://www.google.com)? I have seen it done, but not sure how complicated that would be to set up.
Posted: Mon Aug 28, 2006 6:43 pm
by bokehman
The real url is displayed in the document. Javascript modifies it but search engines don't know that because they don't read the javascript document.
Posted: Mon Aug 28, 2006 6:49 pm
by d_d
No he is saying to link to the site as normal. For example
<a href="http://example.com">sponsor</a>
Then include some javascript that modifies all the off site urls on the page like so the above link would become.
<a href="
http://www.mydomain.com/click-thru.php? ... sponsor</a>
Search engine spiders don't evaluate javascript so the links appear normal to them. Most web browsers do evaluate javascript so the links will be tracked when clicked.
Posted: Mon Aug 28, 2006 6:49 pm
by jabbaonthedais
Ah I see! I didn't think about them ignoring javascript. Very clever.
