admin管理员组文章数量:1358056
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 ofwindow.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
1 Answer
Reset to default 13Right 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
});
本文标签: JavaScript execute after google analyticsStack Overflow
版权声明:本文标题:JavaScript execute after google analytics - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743913421a2560805.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论