I'm using a script to detect cross-domain links for google analytics cross-domain tracking. The original script (xdomain.js) was provided by the great folks at Luna Metrics. Here is the script with my modifications, hat-tip to educardocereto here on StackOverflow for the suggested changes to enable setAllowAnchor in the GATC (I've commented line 40 where the console error first points to):
var jQueryXD = jQuery.noConflict();
/* I added var because page loads 2 versions
of jquery - not the source of the problem.*/
function listenToClicks()
{
var domains=["domain1.com", "domain2.com"];
var fileTypes=[".pdf"];
jQueryXD('a').each(function(index) {
var link = jQueryXD(this);
var href = link.attr('href');
jQueryXD.each(fileTypes, function(i) {
if(jQueryXD(link).attr('href').indexOf(this)!=-1){ //this is line 40
valid = false;
jQueryXD(link).bind('click', function(c) {
c.preventDefault();
_gat._getTrackerByName()._trackEvent('Download', 'Click - ' + jQueryXD(link).attr('href'));
setTimeout('document.location = "' + jQueryXD(link).attr('href') + '"', 100);
});
}
});
var valid = false;
jQueryXD.each(domains, function(j) {
try
{
if((jQueryXD(link).attr('href').indexOf(this)!=-1)&&(window.location.href.indexOf(this)==-1)){
valid = true;
if (valid)
{
jQueryXD(link).bind('click', function(l) {
if(typeof(_gat)=="object"){
l.preventDefault();
if (jQueryXD(link).attr('target') != "_blank")
{ // _gaq.push(['_link',jQueryXD(link).attr('href')]);
_gaq.push(['_link',jQueryXD(link).attr('href'), true]); // mod
}
else
{
var tracker = _gat._getTrackerByName();
//var fullUrl = tracker._getLinkerUrl(jQueryXD(link).attr('href'));
var fullUrl = tracker._getLinkerUrl(jQueryXD(link).attr('href'), true); //mod
window.open(fullUrl);
}
}
});
}
}
}
catch(e)
{
//Bad A tag
}
});
var rootDomain = document.domain.split(".")[document.domain.split(".").length - 2] + "." + document.domain.split(".")[document.domain.split(".").length - 1];
if ( (href.match(/^http/)) && (href.indexOf(rootDomain) == -1) && !valid) {
jQueryXD(link).bind('click', function(d) {
d.preventDefault();
_gat._getTrackerByName()._trackEvent('Outbound Link', href);
setTimeout('document.location = "' + href + '"', 100);
});
}
});
}
jQueryXD(document).ready(function() {
listenToClicks();
});
The output from the Chrome javascript console:
Uncaught TypeError:
Cannot call method 'indexOf' of undefined xdomain-nfi-nfs-anchormod-noconflict.js:40
jQueryXD.each.valid xdomain-nfi-nfs-anchormod-noconflict.js:40
jQuery.extend.each jquery-1.2.6.min.js:21
(anonymous function) xdomain-nfi-nfs-anchormod-noconflict.js:39
jQuery.extend.each jquery-1.2.6.min.js:21
jQuery.fn.jQuery.each jquery-1.2.6.min.js:12
listenToClicks xdomain-nfi-nfs-anchormod-noconflict.js:35
(anonymous function) xdomain-nfi-nfs-anchormod-noconflict.js:100
jQuery.fn.extend.ready jquery-1.2.6.min.js:27
jQuery.extend.ready.jQuery.readyList jquery-1.2.6.min.js:27
jQuery.extend.each jquery-1.2.6.min.js:21
jQuery.extend.ready jquery-1.2.6.min.js:27
So, atleast it seems to be not mixing up the two jquery instances. I've also tried it with jquery 1.7.1. I'm using 1.2.6 because the script seems to have been moste tested on that version.