使用Google Analytics进行Javascript重定向

Try*_*ain 36 javascript subdomain redirect google-analytics subdirectory

我需要帮助确定如何在包含Google Analytics代码的同时成功重定向.

该重定向文件的代码:

<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>
Run Code Online (Sandbox Code Playgroud)

仅当我不包含我的Google Analytics代码时,这才能用作重定向.我已经尝试过移动代码而没有任何变化.

问题 如何添加任何类型的重定向,并且仍然可以使用Google Analytics进行跟踪?

我已经尝试过PHP重定向但没有成功,我很确定htaccess重定向不会有帮助,尽管我愿意接受建议.

我使用JavaScript重定向的原因是我可以继续跟踪Google Analytics并显示一些消息或制作一个有延迟的自定义页面.

谢谢你的帮助.请不要是JS,如果您知道解决方案,欢迎任何输入.

mik*_*ike 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);
})();
Run Code Online (Sandbox Code Playgroud)

  • 有些人阻止GA所以使用`<meta>`标签重定向作为后备是一个好主意. (5认同)
  • 完美答案.您可以在正文中添加一个简单的文本链接到重定向网址,这样没有javascript的用户就可以访问重定向网址.此外,可以在此处找到有关推送功能部分的Google参考文档:https://developers.google.com/analytics/devguides/collection/gajs/#PushingFunctions (2认同)

Jar*_*rod 20

如果您使用新的GA代码,只需ga('send', 'pageview');使用以下代码替换此行:

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

完整示例:

  (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";
  }
});
Run Code Online (Sandbox Code Playgroud)


jfr*_*d00 14

我建议您将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>
Run Code Online (Sandbox Code Playgroud)

这应该保证它在您的重定向代码启动之前运行,它应该不受重定向脚本的影响,因此不会产生干扰.正如你现在所拥有的那样,你正在玩一个猜测游戏,考虑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>
Run Code Online (Sandbox Code Playgroud)


小智 9

提供的代码麦克工作确实.但是,我发现删除计时器也完全有效.然后中止__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);
})();
Run Code Online (Sandbox Code Playgroud)


Try*_*ain 7

使用元刷新就像一个魅力!遗产FTW!谢谢,@ 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>
Run Code Online (Sandbox Code Playgroud)

RECAP:我现在可以在使用Google Analytics跟踪这些重定向时重定向!

元刷新(取自维基百科)

例子

5秒后放入内部刷新页面:

<meta http-equiv="refresh" content="5">
Run Code Online (Sandbox Code Playgroud)

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

<meta http-equiv="refresh" content="5; url=http://example.com/">
Run Code Online (Sandbox Code Playgroud)

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

<meta http-equiv="refresh" content="0; url=http://example.com/">
Run Code Online (Sandbox Code Playgroud)