External Link Click Coutner

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
smurf00
Forum Newbie
Posts: 1
Joined: Mon Aug 28, 2006 10:21 am

External Link Click Coutner

Post 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
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post 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. :)
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post 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.
sparkle
Forum Newbie
Posts: 7
Joined: Thu Aug 24, 2006 9:14 am

Post by sparkle »

if you could expand on that...:D
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

sparkle wrote:if you could expand on that...:D
Which part?
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post by Dale »

bokehman wrote:
sparkle wrote:if you could expand on that...:D
Which part?
The Javascript bit I would believe...
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post 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

Code: Select all

to [sytnax="javascript"].[/color]
jabbaonthedais
Forum Contributor
Posts: 127
Joined: Wed Aug 18, 2004 12:08 pm

Post 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.
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post 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.
d_d
Forum Commoner
Posts: 33
Joined: Wed Jul 07, 2004 4:56 pm
Location: UK

Post by d_d »

jabbaonthedais wrote:
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
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.
jabbaonthedais
Forum Contributor
Posts: 127
Joined: Wed Aug 18, 2004 12:08 pm

Post by jabbaonthedais »

Ah I see! I didn't think about them ignoring javascript. Very clever. ;)
Post Reply