Current location: Hot Scripts Forums » General Web Coding » JavaScript » Auto Insert URL Tracking Via JavaScript...


Auto Insert URL Tracking Via JavaScript...

Reply
  #1 (permalink)  
Old 04-10-06, 04:09 AM
w2n's Avatar
w2n w2n is offline
Newbie Coder
 
Join Date: Jan 2006
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Question Auto Insert URL Tracking Via JavaScript...

Hi,

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.


I hope, I have given here the enough details.


Please kindly help me in this.

Thanks in advance...

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 04-10-06, 06:12 PM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
You've basically written the code yourself already, here's how it could look translated to JS.
Code:
...
I use a Regular Expression to avoid nested loops.
More about those here: http://www.evolt.org/article/Regular...ript/17/36435/
__________________
[W3Schools - learn all about the standards.] [QuirksMode - Browser Quirks] [MS's Online Reference Docs] [DOM in Gecko.]
Please pay attention to stickys, announcements and forum rules, thank you.
Please also remember Code Wrappers and [SOLVED] Marking, this helps everyone.

Last edited by TwoD; 04-14-06 at 06:34 AM. Reason: Removed bad code to avoid confusion.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 04-11-06, 02:25 AM
w2n's Avatar
w2n w2n is offline
Newbie Coder
 
Join Date: Jan 2006
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Hi,

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.

By the way, the tracking URL (http://url.w2n.net/*) looks like...

For Google.com: http://url.w2n.net/*http://www.google.com

Please help, if possible!

Thanks again...

__________________
SWAGATO GANGOPADHYAY
Founder/Owner/CEO
The Rozaleenda Group, Inc.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 04-11-06, 08:35 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 Posts
The script works, the browser just doesn't load it onload. Try removing this:

Code:
document.onload=function findAllLinks(){
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 04-12-06, 01:08 AM
w2n's Avatar
w2n w2n is offline
Newbie Coder
 
Join Date: Jan 2006
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Wow! It really works now, after I removed the function line (at start, and the end '}').

Many thanks to 'TwoD' for the necessary script, and 'nico_swd' for the simple solution to remove the first and last line!

I am so happy now.

__________________
SWAGATO GANGOPADHYAY
Founder/Owner/CEO
The Rozaleenda Group, Inc.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 04-12-06, 04:52 AM
w2n's Avatar
w2n w2n is offline
Newbie Coder
 
Join Date: Jan 2006
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
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.



Thanks in advance!
__________________
SWAGATO GANGOPADHYAY
Founder/Owner/CEO
The Rozaleenda Group, Inc.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 04-12-06, 04:58 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 Posts
Can I may ask what this would be good for?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 04-12-06, 05:17 AM
w2n's Avatar
w2n w2n is offline
Newbie Coder
 
Join Date: Jan 2006
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
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!


Quote:
Originally Posted by nico_swd
Can I may ask what this would be good for?
__________________
SWAGATO GANGOPADHYAY
Founder/Owner/CEO
The Rozaleenda Group, Inc.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #9 (permalink)  
Old 04-12-06, 07:49 AM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
This code should do the trick. Not tested though.
Code:
...
Note that neither of the scripts will work if the user has disabled javascript. It might also not work in some old browsers because of getElementById.

I'd want to run them onload since it might not catch all links/images otherwise.
__________________
[W3Schools - learn all about the standards.] [QuirksMode - Browser Quirks] [MS's Online Reference Docs] [DOM in Gecko.]
Please pay attention to stickys, announcements and forum rules, thank you.
Please also remember Code Wrappers and [SOLVED] Marking, this helps everyone.

Last edited by TwoD; 04-14-06 at 06:35 AM. Reason: Removed bad code to avoid confusion.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #10 (permalink)  
Old 04-12-06, 10:13 AM
w2n's Avatar
w2n w2n is offline
Newbie Coder
 
Join Date: Jan 2006
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
This is not working! Can you give me an example HTML portion, where the script is working? I mean, the image one?
__________________
SWAGATO GANGOPADHYAY
Founder/Owner/CEO
The Rozaleenda Group, Inc.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with Auto Dealer Script nuzzle PHP 17 04-14-10 09:34 PM
Can you retrieve file list from url using Javascript? tommyb JavaScript 6 05-06-05 04:58 PM
URL Tracking Code Stripper DevilDog Script Requests 0 02-17-05 10:24 AM
Auto Insert into IE MrDarko Visual Basic 0 10-11-04 09:43 PM
javascript redirection based on referring URL andyinorbit2000 Script Requests 1 05-31-04 02:56 AM


All times are GMT -5. The time now is 01:37 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.