Question

Suppose I need to track my clicks using google analytics scripts. I know that standard approach to ensure that tracking event was fired is to pass continuation into the tracking queue and return false from click event handler, like this:

 $('a').click(function () {
    var url = $(this).attr('href');
    _gaq.push(['_trackEvent', smth]);
    _gaq.push(function(){ document.location = url ;}
    return false;
});

My challenge is that I want to open this link in new tab.
My perfect solution is target="_blank", but you cant do it with javascript.
window.open(url,'_blank') will popup new window in all browsers except firefox and this window is usually blocked by default.
This means that I must return true from the click handler.

So this is my reason behind the fact that i need to wait synchronously in some way for ga script to request it's tracking pixel. Is some sort of What is the JavaScript version of sleep()? function can help me?

Thank you!

Was it helpful?

Solution

The only reason you want to ensure the tracking pixel is fired off before visitor is taken to new page, is for links that take you to a new page within the same window. In other words, you want to make sure the tracking code does its thing before the current window is unloaded.

If your link is opening a new window...regardless of how you are opening the new window...you don't need to worry about this sort of thing, because your current window is not being unloaded.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top