我建议使用以下方法。它使用hitCallback来进行重定向,这样事件将被发送到GA,然后重定向将发生。它还设置了一个超时,以便在任何情况下analytics.js不可用时(无论什么原因),查看器仍将最终发送到正确的位置。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>example</title>
<script>
setTimeout(redirect, 2000);
var redirected = false;
function redirect() {
if (!redirected) {
redirected = true;
location.href = 'http://your.website.here.com';
}
}
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-NNNNNNN-N', 'auto');
ga('send', 'event', 'Event Name', 'redirect', {
hitCallback: redirect
});
</script>
</head>
<body>
</body>
</html>
当然,跟踪代码和 URL 应该更新以匹配您自己的。
参考资料: