Forum Moderators: open
I can track outbound links with Google Analytics with this code :
<a href="http://www.example.com" onclick="javascript:urchinTracker('/outgoing/example_com');">
onclick="javascript:urchinTracker('/outgoing/example_com');"
?
Also for the 'your code here' part of your example, is that where I'd stick this code:
onclick="javascript:urchinTracker('/outgoing/example_com');"
Thanks,
--Illah
[edited by: Illah at 7:12 pm (utc) on Sep. 29, 2006]
function AddHandler() {
var arrAnchors = document.getElementsByTagName('A');
for(i = 0; i < arrAnchors.length; i++) {
arrAnchors.onclick = function() {
// your code here to pass the href to the tracking function
}
}
}
The problem with that, though, is that it will replace any inline onclick events you already have defined. In other words, you might break other functionality.
Another option would be to use the Yahoo UI Library [developer.yahoo.com]'s Event Utility to add the onclick event listener. This would preserve any of the inline onclick handlers.
For example, you'd do something like this:
<script type="text/javascript" src="./yui/build/yahoo/yahoo.js"></script>
<script type="text/javascript" src="./yui/build/dom/dom.js"></script>
<script type="text/javascript" src="./yui/build/event/event.js"></script>
<script type="text/javascript">
function addHandler()
{
YAHOO.util.Event.addListener(
document.getElementsByTagName('A'),
'click',
function(){urchinTracker('/outgoing/example_com');} );
}
YAHOO.util.Event.addListener(window,'load',addHandler);
</script>
Note, I didn't test any of that, but essentially what it should do is call urchingTracker for every link click, regardless of what other event handlers are in place.
Hope that helps.
[edited by: Fotiman at 8:03 pm (utc) on Sep. 29, 2006]
I think this is about right
for (var i = 0; i < document.links.length; i++) adjustLink(document.links[i]);
...
if (link.onclick) link.onclick_ = link.onclick;
...
if (link.onclick_) link.onclick_();
...
Kaled.