Click here to Skip to main content
Click here to Skip to main content

How to track outgoing links using javascript and XML

By , 9 Apr 2013
 

The idea is simple, every time an outgoing link is clicked, we will load an XML file at the backend and pass the URL of the outgoing link or an associated unique ID (e.g. web trends ID in case we are using a web analytics software such as Web Trends)

This will leave a stamp of the outgoing link on the web server logs, which we can then use to get the required statistics e.g. we can use web trends to analyze existing logs or can build a custom software to analyze the logs, there are also a lot of free tools available to analyze your web server logs.

Here is the code:

<script language="JavaScript" type="text/javascript">
var wtrendsID = "web trends tag will go here";
function Tracker(xmlpath, url)
{
if (window.ActiveXObject)
{
btracker=new ActiveXObject("Microsoft.XMLDOM");
btracker.async=false;
btracker.load(xmlpath);
}
else if (document.implementation && document.implementation.createDocument)
{
btracker= document.implementation.createDocument("", "", null);
btracker.async=false;
btracker.load(xmlpath);
}
else
{
alert(‘Your browser does not allow this script.’);
}
}
//This function needs to be called to load a XML file for tracking impression on server and then load the offsite URL in new window


function customPopup2(url, features)
{
var xmlpath = "tracker.xml?" + wtrendsID;
Tracker(xmlpath, url);
window.open(url, ", features);
}
//Following can be used to trigger the offsite URL
<A href="javascript:customPopup2(‘http://www.offsitelink.com?’ + //wtrendsID,’toolbar=1, menubar=1,scrollbars=1,resizable=1, status=1, location=1, width=850, height=650, left=125, top=-25′);">www.offsitelink.com</A>

You can replace wtrendsID with the outgoing link and pass it as tracker.xml?outgoing=www.outgoinglink.com and then can parse / filter all the logs with outgoing keyword to get all stats on the outgoing links clicked.

How to track outgoing links using javascript and XML is a post from: CoolWebDeveloper.com

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Manny Khasria
Software Developer (Senior) NetCloud Media
Canada Canada
Member
I am a Senior Systems Developer with a leading financial firm in Toronto. I am a technology enthusiast and have worked on several large enterprise level projects.
 
You can visit my blog here: http://www.coolwebdeveloper.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130513.1 | Last Updated 9 Apr 2013
Article Copyright 2013 by Manny Khasria
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid