最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript execute after google analytics - Stack Overflow

programmeradmin1浏览0评论

I am using Google Analytics and doing redirect after analytics request has finished.

I am using this code:

var _gaq = _gaq || [];

_gaq.push(['_setAccount', 'UA-12345678-1']);

_gaq.push(['_trackPageview']);

_gaq.push(function () {
     window.location.replace("myRedirectToUri");
});

This is not executed correctly.

Redirect is done correctly (as analytics callback) on Firefox, but not on other browsers (IE, Chrome, Safari) so I lose analytics data.

At the moment I have set timeout of 1 sec but that is not the real solution.

Any help how to implement this correctly?

I am using Google Analytics and doing redirect after analytics request has finished.

I am using this code:

var _gaq = _gaq || [];

_gaq.push(['_setAccount', 'UA-12345678-1']);

_gaq.push(['_trackPageview']);

_gaq.push(function () {
     window.location.replace("myRedirectToUri");
});

This is not executed correctly.

Redirect is done correctly (as analytics callback) on Firefox, but not on other browsers (IE, Chrome, Safari) so I lose analytics data.

At the moment I have set timeout of 1 sec but that is not the real solution.

Any help how to implement this correctly?

Share Improve this question edited Feb 16, 2012 at 10:20 pimvdb 155k80 gold badges311 silver badges356 bronze badges asked Feb 16, 2012 at 10:17 AmirAmir 2331 gold badge3 silver badges15 bronze badges 3
  • See stackoverflow./questions/8147065/… – jk. Commented Feb 16, 2012 at 14:33
  • Have you tried to use window.location.href = url instead of window.location.replace(url) ? – Eduardo Commented Feb 16, 2012 at 18:38
  • Same behaviour with window.location.href = url :( – Amir Commented Feb 17, 2012 at 8:56
Add a ment  | 

1 Answer 1

Reset to default 13

Right now there's no good solution to this problem. The best you can do is to add a timeout to delay the redirection. Currently there's no callback to the _trackPageview. When it returns it means that it started the tracking, but it's not garanted that it have successfully registered the pageview until the __utm.gif request is plete.

1 sec timeout may be too much. I usually keep the timeout around 200-400 ms.

var _gaq = _gaq || [];

_gaq.push(['_setAccount', 'UA-XXXXXXX-X']);

_gaq.push(['_trackPageview']);

_gaq.push(function () {
     setTimeout(function(){
         window.location.href = newUrl;
     }, 200);
});

EDIT:

It's been 2 years since I originally initially posted this answer and since then Google Analytics has e a long way.

Now there's a proper way to do this:

var _gaq = _gaq || [];

_gaq.push(['_setAccount', 'UA-XXXXXXX-X']);

_gaq.push(['_set','hitCallback',function(){
  window.location.href = newUrl;
}]);  

_gaq.push(['_trackPageview']);  

And if you have migrated to Universal Analytics using analytics.js the equivalent would be:

ga('create', 'UA-XXXXXXX-X')

ga('send', 'pageview', {
  'hitCallback': function() {
    window.location.href = newUrl;
  }
});

EDIT 2

Here's the more proper way to do this to make sure your code executes even if Google Analytics code is blocked or tampered by an extension or adBlocker.

var t = undefined;

var myCode = function(){
  window.clearTimeout(t);
  t = undefined;
  window.location.href = newUrl;

};

t = setTimeout(myCode, 3000);

ga('create', 'UA-XXXXXXX-X')

ga('send', 'pageview', {
  'hitCallback': myCode
});
发布评论

评论列表(0)

  1. 暂无评论