使用Google Analytics的Javascript重定向

37

我需要帮助解决如何成功重定向同时包括分析代码的问题。

重定向文件的代码:

<head>
<script type="text/javascript">
function delayedRedirect(){
    window.location = "https://market.android.com/developer?pub=Fractal%20Systems"
}
</script>
<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-1234567-8']); <!--I have my real ID there-->
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
   var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>
</head>
<body onLoad="setTimeout('delayedRedirect()', 3000)">
<h2>ADW.BuuF.Theme is no more! You will be redirected to new and better apps in 3 seconds.</h2>
</body>
</html>

如果我包含Analytics代码,这仅作为重定向有效。我尝试过移动代码但没有变化。

问题 如何添加任何一种重定向,仍然能够使用Google Analytics进行跟踪?

我已经尝试了PHP重定向,但没有成功,虽然我愿意听取建议,但我相信htaccess重定向不会有所帮助。

我使用JavaScript重定向的原因是为了可以继续使用Google Analytics进行跟踪,并且在延迟后显示一个小消息或制作自定义页面。

感谢任何帮助。请注意,不必使用JS,如果您知道解决方案,则欢迎提供任何输入。


1
你尝试使用meta refresh了吗? - balexandre
9个回答

42
注意:_gaq.push 允许将函数推送到队列中。下面的代码应在 _trackPageview 后 250 毫秒重定向(以便给跟踪像素留出时间):
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-1234567-8']);
_gaq.push(['_trackPageview']);
_gaq.push(function() {
    setTimeout(function() {
        window.location = "https://market.android.com/developer?pub=Fractal%20Systems";
    }, 250);
});

(function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

2
完美的答案。您可以在正文中添加一个简单的文本链接到重定向URL,以便没有JavaScript的用户可以访问重定向URL。此外,有关推送函数的Google参考文档可以在此处找到:https://developers.google.com/analytics/devguides/collection/gajs/#PushingFunctions - kontur
我并不认为你需要一个超时 - 请求图像应该在重定向之前始终发出 - 如果客户端从未收到任何数据,这并不重要。 - Simon_Weaver
@Simon_Weaver 同意客户端不接收任何数据并不重要,但是看起来(至少在Chrome和Firefox中),如果请求开始后窗口变化太快,浏览器将不会发出跟踪像素请求。Google甚至有一个关于使用延迟的注释,但他们的代码混乱不堪,混合了同步和异步样式的分析。 - mike
1
@mike 你还记得在哪里找到使用延迟的笔记吗? - fredrik
5
一些人会屏蔽Google Analytics,使用一个<meta>标签进行重定向作为备选方案是个不错的主意。 - kizzx2
我希望看到这个答案更新,同时也包括新的通用跟踪代码的答案。 - Jeff

22
如果您正在使用新的GA代码,您只需将此行 ga('send', 'pageview'); 替换为以下代码:
ga('send', 'pageview', {
  'hitCallback': function() {
      window.location = "http://www.your-site.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','//www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-xxxxxxx-2', 'auto');

ga('send', 'pageview', {
  'hitCallback': function() {
      window.location = "http://www.your-site.com";
  }
});

自动应该用引号括起来。 - Tosh

16

我建议你将Google Analytics代码从异步改为同步,只需将代码更改为以下内容:

<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-1234567-8']);
  _gaq.push(['_trackPageview']);
</script>
<script type="text/javascript" src="http://www.google-analytics.com/ga.js"></script>

通过这样做,你可以确保在重定向代码启动之前运行GA代码,并且它不会干扰你的重定向脚本。如果按照你现在的方式进行,你就需要猜测GA脚本加载需要多长时间以及它是否能够在3秒内成功加载和完成工作。虽然通常情况下可能是这样,但没有必要像你现在这样异步加载它并猜测它的加载时间。同步加载它,它将在你的JavaScript重定向触发之前完成其职责。

甚至可以直接将重定向放在GA代码之后,以最小化显示占位页面的时间:

<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-1234567-8']);
  _gaq.push(['_trackPageview']);
</script>
<script type="text/javascript" src="http://www.google-analytics.com/ga.js"></script>
<script type="text/javascript">
  window.location = "https://market.android.com/developer?pub=Fractal%20Systems";
</script>

谢谢,我尝试了所有的重定向方法,都可以正常工作。很好知道这一点。 - TryTryAgain

9

Mike提供的代码确实有效。然而,我发现完全删除计时器也同样有效。__utm.gif请求将被中止,但所有信息已经发送。窗口只是重定向并且不等待回复(这只是一个简单的200状态)。我测试过了,它似乎工作得很好。

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-1234567-8']);
_gaq.push(['_trackPageview']);
_gaq.push(function() {
  window.location = "https://market.android.com/developer?pub=Fractal%20Systems";
});


(function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

这个解决方案非常有效...但是如果用户禁用了JavaScript,GA不仅不会工作,页面也不会重定向吗?最好在此脚本之后加上meta重定向,以防万一。 - user1997781

7

使用元数据刷新非常成功!传统的胜利!感谢@Balexandre

<html>
<head>
<meta http-equiv="refresh" content="5; url=https://market.android.com/developer?pub=Fractal%20Systems">
<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-1234567-8']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>
</head>
<body>
<h2>ADW.BuuF.Theme is no more! You will be redirected to new and better apps in 5 seconds.</h2>
</body>
</html>

总结:我现在能够使用Google Analytics跟踪重定向并进行重定向!

Meta Refresh(摘自wikipedia

示例

将以下内容放入 <head> 中,以便在5秒后刷新页面:

<meta http-equiv="refresh" content="5">

在5秒后重定向到http://example.com/

<meta http-equiv="refresh" content="5; url=http://example.com/">

立即重定向到http://example.com/

<meta http-equiv="refresh" content="0; url=http://example.com/">

有没有快速重定向的想法(而不是5秒钟)? - DS_web_developer
@DS_web_developer 这个解决方案对我有用,你可以尝试将 content=5 的值更改为其他值,但这并不是最健壮的...你应该使用 https://dev59.com/cGoy5IYBdhLWcg3wN7i8#8692588 - TryTryAgain

2

2
刷新通常因可用性问题而不被推荐使用(特别是在短时间内或没有延迟的情况下),但作为客户端没有JavaScript的备选方案,在这种情况下我认为它是完全有效的。

如果您将同步的ga.js调用与刷新相结合,您将获得最佳效果:如果启用了JS,则几乎立即跟踪重定向;如果没有启用JS,则延迟但仍然有效的重定向(并在万不之际在正文中提供硬链接)。

<html>
<head>
    <!--For JS disabled: decent delay, both for usability and to allow plenty of time for JS to work if enabled -->
    <meta http-equiv="refresh" content="5;https://market.android.com/developer?pub=Fractal%20Systems"/>
    <script>
        var _gaq = _gaq || [];
        _gaq.push(['_setAccount', 'UA-1234567-8']);
        _gaq.push(['_trackPageview']);
    </script>
    <!--Synchronous call to ensure tracking fires before JS redirect-->
    <script type="text/javascript" src="//www.google-analytics.com/ga.js"></script>
    <script>
        /* if JS is enabled this will normally fire well before the meta refresh delay ends: an almost instantaneous redirect */
        window.location="https://market.android.com/developer?pub=Fractal%20Systems";
    </script>
</head>
<body>
    <!-- Include a hard link in case both js and the meta refresh fail -->
    <p>Redirecting you to <a href="https://market.android.com/developer?pub=Fractal%20Systems">https://market.android.com/developer?pub=Fractal%20Systems</a></p>
</body></html>

0

这种方法不需要延迟。首先同步执行Google Analytics代码,然后重定向。

<html>
<head>
<script src="//www.google-analytics.com/analytics.js"></script>
  <script>
    var tracker = ga.create('UA-xxxxxxxx-x', 'auto');
    tracker.send('pageview');
    window.location='http://your-url.com';
  </script>
</head>
<body>
</body>
</html>

-1
"

我认为可以使用 jQuery 的 ready 方法,这样在页面完全加载之前不会启动计时器...

"
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {       
    setTimeout(function() {
        //alert("redirecting!");
        window.location = '<?= $url ?>';
    }, 3000);
});
</script>

1
在这种情况下使用jQuery是非常多余的,而且最重要的是,不必要的。 - Koen.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接