Forum Moderators: Robert Charlton & goodroi
-------------------
<script>
function clk(id,link){
window.location.href= 'http://www.mydomain.com/track.cgi?ID='+id;
return false;
}
</script>
Example of the link to an external site:
<a href="http://www.example.com" onclick="return clk(123,this)">
-------------------
I think that google spidering will bypass the javascript stuff, and just follow the link to the www.example.com external site.
Actual users who click on the link (and have javascript enabled) will allow the tracking to take place through the track.cgi script.
I checked, and if somehow google (or other spiders) follow the link in the tracking script with an ID number (e.g. [mydomain.com...] then it will go to the external site, and the server header response show 200 OK.
There is no 302 involved, so this wouldn't lead to any inadvertent hijacking problems would it?
Or does anyone else see any potential problems that this method could cause with google (or other major search engines)?
It's essential to put the code outside the link - if you wrote 'onClick="location.href='www.mysite.com/redir.php?url=www.othersite.com'"', Google would be able to follow this, not because of parsing JavaScript, but because it fetches the links from any text, if I'm not wrong.
The other problem is, that if Mozilla/Firefox user clicks with middle button (opening the page in new tab), there is no onClick event generated, so you won't track this click.
You can avoid this problem writing a javascript that changes a link, like
'onMouseOver="change_link(this)"
function change_link (anchor) {
old_link = this.href;
new_link = 'www.mysite.com/redir.php?url=' + old_link;
this.href = new_link;
return true;
}'
But this is a complicated method, and there is a risk Google may think you're cloaking (it may think the same in onClick method though). It _is_ showing different link for Googlebot than for user anyway. But currently this methods are not penalized, and if used in good purpose, like in your case, they shouldn't ever be penalised.
So, I've changed to using the actual google method where it loads an image that triggers the tracking script, but the user is always taken to the link that is shown in the <a href> tag.
The Javascript:
<script>
function clk(n) { if(document.images){ (new Image()).src="/track.cgi?ID="+n; } return true;}
</script>
Example Link:
<a href="http://www.example.com" onmousedown="return clk(123)">Example Site</a>
I hope that wouldn't cause any problem with google or the other major search engines.
If anyone thinks that it would, I would be interested to hear about it...
The onclick version:
<a href="http://www.example.com" onclick="return clk(123,this)">
Doesn't complete the track.cgi script for me (clickthrough not recorded) on my server.
The onmousedown version:
<a href="http://www.example.com" onmousedown="return clk(123,this)">
Does work (the track.cgi script records the clickthrough).
I wonder if there is a timing issue that once the click is recorded, and the user is immediately redirected, that there is no time to load the image, and run the script?
Anyway, I noticed that Google also uses onmousedown rather than onclick.