Javascript 用谷歌分析重定向页面

Javascript 用谷歌分析重定向页面,javascript,html,redirect,google-analytics,url-redirection,Javascript,Html,Redirect,Google Analytics,Url Redirection,我有一个页面,使用此脚本将用户重定向到三个不同的页面: <html> <body> </body> <script src="jquery.js"></script> <script> $(document).ready(function (){ var redirected = false; if(navigator.userAgent.toLowerCase().indexOf("andro

我有一个页面,使用此脚本将用户重定向到三个不同的页面:

<html>
<body>
</body>
<script src="jquery.js"></script>
<script>
$(document).ready(function (){
        var redirected = false;
       if(navigator.userAgent.toLowerCase().indexOf("android") > -1){
           redirected = true;
           console.log("hello andorid");

           window.location.href = 'http://android.web.site.com';
        }
        if(navigator.userAgent.toLowerCase().indexOf("iphone") > -1){
            redirected = true;
            console.log("hello iphone");
            window.location.href = "http://iphone.web.site.com";
        }
        if(navigator.userAgent.toLowerCase().indexOf("ipad") > -1){
            redirected = true;
            console.log("hello ipad");
            window.location.href = "http://ipad.web.site.com";
        }
        if(navigator.userAgent.toLowerCase().indexOf("ipod") > -1){
            redirected = true;
            console.log("hello iphone");
            window.location.href = "http://ipod.web.site";
        }
        else if(redirected==false){
            console.log("hello windows");
            window.location.href = "app.html";

        }
    });
</script>
</html>

$(文档).ready(函数(){
var重定向=假;
if(navigator.userAgent.toLowerCase().indexOf(“android”)>-1){
重定向=真;
控制台日志(“你好和ID”);
window.location.href=http://android.web.site.com';
}
if(navigator.userAgent.toLowerCase().indexOf(“iphone”)>-1){
重定向=真;
log(“hello iphone”);
window.location.href=”http://iphone.web.site.com";
}
if(navigator.userAgent.toLowerCase().indexOf(“ipad”)>-1){
重定向=真;
log(“hello ipad”);
window.location.href=”http://ipad.web.site.com";
}
if(navigator.userAgent.toLowerCase().indexOf(“ipod”)>-1){
重定向=真;
log(“hello iphone”);
window.location.href=”http://ipod.web.site";
}
else if(重定向==false){
log(“hello windows”);
window.location.href=“app.html”;
}
});
因此,它会检查用户是否正在使用手机。现在,客户希望跟踪有多少用户单击该页面,以及有多少用户登录到“app.html”页面

我正在考虑在
标记中设置Google analytics脚本。对吗?我是否需要在重定向脚本上设置超时,以便GA有时间正确启动

我对分析非常熟悉,我看过这个页面:但我不知道这是否适合我


感谢您的帮助。

这是一个很好的设置。您可以对每个if使用“if$(document).ready(function()”,以确保在重定向之前将查询发送到GA。这应该不需要添加令人讨厌的延迟。希望它能有所帮助!

$(document).ready
让用户等待HTML结构呈现后再重定向。这不是必需的

在执行任何其他操作之前,先在标题中执行重定向。在用户到达其目标页面后,而不是在您执行重定向之前,测量用户(如果您坚持在重定向之前执行重定向,则在各自的if子句中执行虚拟页面视图,并将目标页面作为页面指定传递)

根据文档,GA位于body标签前面的head部分,尽管GA将测量内容(精确程度不同),无论您在页面中如何放置

补充:以下是一个完整的示例:

<html>
<head>

<script>
  (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-XXXXXXXX-X', 'auto');


</script>

</head>
<body>

<script>

 // no need for $document.ready or jQuery

       if(navigator.userAgent.toLowerCase().indexOf("android") > -1){
           redirected = true;
           console.log("hello andorid");
           ga('send', 'pageview', {
            'page': '/my-android-page',
            'hitCallback': function() {
            window.location.href = 'http://android.web.site.com';
           }
           });           
        }
        if(navigator.userAgent.toLowerCase().indexOf("iphone") > -1){

            console.log("hello iphone");
            ga('send', 'pageview', {
            'page': '/my-iphone-page',
            'hitCallback': function() {
            window.location.href = "http://iphone.web.site.com";
           }
           });                       
        }
        if(navigator.userAgent.toLowerCase().indexOf("ipad") > -1){

            console.log("hello ipad");
            ga('send', 'pageview', {
            'page': '/my-ipad-page',
            'hitCallback': function() {
            window.location.href = "http://ipad.web.site.com";
           }
           });                      

        }
        if(navigator.userAgent.toLowerCase().indexOf("ipod") > -1){

            console.log("hello iphone");
            ga('send', 'pageview', {
            'page': '/my-ipod-page',
            'hitCallback': function() {
            window.location.href = "http://ipod.web.site";
           }            
        }
        else if(redirected==false){
            console.log("hello windows");
            ga('send', 'pageview', {
            'page': '/app.html',
            'hitCallback': function() {
            window.location.href = "app.html";
           }
        }
    });
</script>


</body>
</html>

(函数(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]| |函数(){
(i[r].q=i[r].q | |[]).push(参数)},i[r].l=1*新日期();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(窗口,文档,“脚本”,“www.google-analytics.com/analytics.js”,“ga”);
ga(“创建”、“UA-XXXXXXXX-X”、“自动”);
//不需要$document.ready或jQuery
if(navigator.userAgent.toLowerCase().indexOf(“android”)>-1){
重定向=真;
控制台日志(“你好和ID”);
ga('send'、'pageview'{
“页面”:“/my android页面”,
“hitCallback”:函数(){
window.location.href=http://android.web.site.com';
}
});           
}
if(navigator.userAgent.toLowerCase().indexOf(“iphone”)>-1){
log(“hello iphone”);
ga('send'、'pageview'{
“页面”:“/my iphone页面”,
“hitCallback”:函数(){
window.location.href=”http://iphone.web.site.com";
}
});                       
}
if(navigator.userAgent.toLowerCase().indexOf(“ipad”)>-1){
log(“hello ipad”);
ga('send'、'pageview'{
“页面”:“/my ipad页面”,
“hitCallback”:函数(){
window.location.href=”http://ipad.web.site.com";
}
});                      
}
if(navigator.userAgent.toLowerCase().indexOf(“ipod”)>-1){
log(“hello iphone”);
ga('send'、'pageview'{
“页面”:“/my ipod页面”,
“hitCallback”:函数(){
window.location.href=”http://ipod.web.site";
}            
}
else if(重定向==false){
log(“hello windows”);
ga('send'、'pageview'{
“页面”:“/app.html”,
“hitCallback”:函数(){
window.location.href=“app.html”;
}
}
});

您不需要jQuery,因为您不需要等待DOM呈现。您不需要“redirected”var,因为每次将其设置为true时,您都会重定向到另一个页面(因此“true”的情况永远不会在任何地方进行计算)。各种条件下的虚拟页面视图采用您可以自己选择的页面路径(您还可以覆盖其他字段,如位置(包括主机名的完整url)-查找字段参考以获取通用分析)。记录虚拟页面视图后,访问者将在点击回调中重定向。

我没有包括jquery,因为我认为它没有必要。但我包括它是出于教学目的。我无法控制其他网站,只有app.html。如果你费心阅读我的评论,你会看到我想跟踪有多少人会访问重定向页面,以及有多少人将访问“app.html”。事实上,更好的方法是:在if子句中对虚拟页面视图使用点击回调,点击回调仅在记录点击后执行,因此您可以确保GA代码在用户重定向之前已运行。添加了完整示例。非常感谢!您真是太好了。祝您度过愉快的一天。:)