Link Tracking plugin + JSONP + Opera
Hi,
I'm tryng to develop a link tracker, something like google analytics. The goal is to get a command like this:
$('html').trackAllLinks(optional options);
$('a').trackAllLinks(optional options);
...
Now i developed a javascript that could do just what i need but the problem is it doesn't work in every browser as it should be. Because i want to track a link click i need to make a request to my server to say that the link has been clicked. The first problem i had was that i needed to make a cross domain ajax call. But this is fixed by using JSONP. Now my code looks like this:
-
jQuery(document).ready(function() {
var elem, opts
jQuery.fn.trackAllLinks = function(settings) {
settings = jQuery.extend({}, jQuery.fn.trackAllLinks.defaults, settings);
opts = settings
function track() {
href = jQuery(this).attr('href');
jQuery.ajax({
type: "GET",
url: opts.collector,
dataType: "jsonp",
cache: false,
data: {
"eurl" : href
},
success: function(msg){
document.location.href = href;
}
});
return false;
};
if(jQuery(this).is("a")) {
jQuery(this).click(track);
}
jQuery(this).find("a").click(track);
};
jQuery.fn.trackAllLinks.defaults = {
collector : 'default link of my stats collector'
};
});
The first idea was that i wouldn't do anything in the success of the jsonp call and just let the request go, but because this didn't work when i clicked on an external link, i changed it to wait on a response before opening the link.
The problem with this code is that it doesn't work in Opera, no request is being send to the server ... Now i know that JSONP mimics the ajax call by adding a script tag to the page which loads the script of the given url. Now i think when i do this in Opera the dynamically loaded javascript isn't executed. In Firefox, IE, Chrome, Safari it works but not in Opera.
Another idea i had was by dynamicly adding an image to the page. But this has the same problem in Opera and i was unable to track an external link in Safari and Chrome (Webkit).
Does someone of you know how to fix this? Or has a better solution for this?
greets,
Daan