具有重定向功能的Universal Analytics

时间:2014-08-01 10:10:45

标签: google-analytics

我已经设置了一个重定向到我们的应用商店下载页面的页面。我使用该页面提供自定义元标题,以便分享到Facebook / Twitter等。但是,我想在页面上实现谷歌分析。目前,GA似乎没有使用以下代码正确启动:

    <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-XXXXXX', 'auto');
        ga('require', 'displayfeatures');
        ga('send', 'pageview');
    </script>

    <script type="text/javascript">
        window.location.href = "https://itunes.apple.com/......"
    </script>

1 个答案:

答案 0 :(得分:1)

您可能会在分析调用完成之前重定向。我建议使用hitCallback function并将超时作为后备。

很简单的例子:

<script>
    var redirectCallback = function() {
        window.location.href = "https://itunes.apple.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-XXXXXX', 'auto');
    ga('require', 'displayfeatures');
    ga('send', 'pageview', {
        'hitCallback': redirectCallback
    });
</script>

<script type="text/javascript">
    // Redirect after 2 seconds in case the analytics call didn't finish
    window.setTimeout(redirectCallback, 2000);
</script>