I have seen some similar script available but want these following things. Please help me with the necessary code...
• I want to have all the 'a href' links in a page, like 'http://www.google.com'. Which is very normal. But, I want to write a JavaScript, and put it in the HEAD section of the page, by which, each of the 'a href' links will be converted to something like 'clicks.php?url=http://www.google.com', when clicked. It would be simply needed for click tracking purposes.
• Also, I do NOT want to put that click tracking, to certain domains' links, so I will put all those 'exclude' domains in an Array.
• The needed JavaScript will check ALL the 'href' in a page, and exclude the links, which contain the domains of that array. All the rest links will then have 'clicks.php?url=' before the actual links.
So, point by point, here the needs are...
1. I will put the JavaScript in the HEAD section, or somewhere else, NOT for each 'a href' links.
2. The script will check every links and exclude the links which have the domains from an 'exclude' array.
3. Then, all the rest links will have 'clicks.php?url=' before that link, which will look like 'clicks.php?url=http://www.google.com', when clicked. It can be like an 'onClick' method.
First of all, a big thanks for the reply, with the full code!
However, I have tried it with a plain HTML file, with some links! NO link is being tracked with your code!
Here is what my file is looking like...
Code:
<base target="_blank">
<table width="593" border="0"><tr><td width="50" valign="top"><p style="clear: both"><div class="pr">PR: 8<div class="prg"><div class="prb" style="width: 32px"></div></div></div></p></td><td width="543"><p><a id="3" href="http://www.wikipedia.com"><strong>Pellentesque lectus felis</strong></a><br />Donec eu felis et leo consectetuer elementum. Vivamus nec metus quis sapien aliquam convallis. Sed quam. Ut sit amet orci. Aliquam mattis auctor sapien. Donec aliquam varius odio. Fusce malesuada, felis sit amet tristique adipiscing, risus risus tincidunt<br /><span class="url">http://www.wikipedia.com<br /></span><br /></p></td></tr></table>
<table width="593" border="0"><tr><td width="50" valign="top"><p style="clear: both"><div class="pr">PR: 7<div class="prg"><div class="prb" style="width: 28px"></div></div></div></p></td><td width="543"><p><a id="5" href="http://www.bbc.com"><strong>Pellentesque habitant tristique</strong></a><br />Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nunc hendrerit pretium enim. Fusce tempus ornare justo. Sed neque elit, tincidunt in, iaculis et, facilisis in, nunc. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Vivamus eu ante vitae<br /><span class="url">http://www.bbc.com<br /></span><br /></p></td></tr></table>
</div>
<script>
document.onload=function findAllLinks(){
var exclude=["domainA.com","domainB.com"] // This is the array you modify
var excludeRegExp=new RegExp(exclude.join("|"),"i") // This generates a regular expression based on the excluded domains.
var links=document.getElementsByTagName('a') // Get all the links
for(var link=0;link<links.length;link++){
if(!excludeRegExp.test(links[link])){ // If none of the domains in the RegExp exists in the URL, modify the link.
links[link].href="http://url.w2n.net/*"+links[link].href // Prepend the clicktracker.
}
}
}
</script>
If you click either of these 2 links, none of these are being tracked, by the tracking script! It's not a fault of the tracking script, because, it's 100% working otherwise! If you click the links, it is opening as the direct URL, not via the tracking URL.
Many thanks for the exact script, and it is fully working now!
However, just like this script, I want another one, with little twist.
Suppose, a page has several images, with 'img src' tag, and root URLs, like "/folder/image.jpg" etc.
I want to have a script, by which, all those "/" root tag will be converted to "http://www.domain.com/images/folder/image.jpg" tag, for example.
That means...
All the images URLs (from ""/folder/image.jpg") will be converted to... "http://www.domain.com/images/folder/image.jpg" URLs.
Only IF... the image URLs start with an array of particular starting folders, like "folderA" or "folderB". All other folders (apart from that array) URLs will remain the same, which means, the root URLs, or the exact URLs as in that 'img src' tag.
It's simply because of many reasons, such as, some folders are in different domains, some paths can include the images from other domains (owned by us) etc.
So, it would be great to have a small script, which could do the things!