Is there a way to define a callback for w开发者_JS百科hen _trackPageview completes?
I have the following code to track when a user logs into my site, and to refresh the page afterwards:
pageTracker._trackPageview('/login/complete');
location.reload(true);
The problem with the above code is that _trackPageview attaches an image to the DOM and there's no guarantee that the image loads before the page refreshes.
Overwriting the current Image()
function in the page is one solution:
var oldImage = window.Image;
window.Image = function()
{
var self = this;
var theImage = new oldImage();
$(theImage).load(function(){
// Analytics ajax finished
// Do something here (submit the form or whatever)
});
return theImage;
};
But I actually think that Malvolio has a better solution using _gaq.push()
that will work for what you need.
Most implementations I've seen get around this problem by putting the reload/redirect in a setTimeout() of 500ms-1s
精彩评论