1

一直以来,我们都在使用这个可靠的网站重定向 HTML/JavaScript 代码

<!DOCTYPE HTML>
<html lang="en-US">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="refresh" content="1;url=https://www.nosuchwebsite.com">
        <script type="text/javascript">
            window.location.href = "https://www.nosuchwebsite.com"
        </script>
        <title>Page Redirection</title>
    </head>
    <body>
        <!-- Note: don't tell people to `click` the link, just tell them that it is a link. -->
        If you are not redirected automatically, follow the <a href='https://www.nosuchwebsite.com'>nosuchwebsite</a>
    </body>
</html>

现在,我们希望在重定向之前执行 Google Analytics 跟踪代码。

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-xxxxxxxx-x']);
  _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>

我意识到,元“刷新”可能比 JavaScript 跟踪代码更早执行。我想知道,是否有任何方法可以让 Google Analytics 跟踪代码在元“刷新”之前运行

4

2 回答 2

0

或者不要使用元刷新并使用 Javascript 刷新并在一个<noscript>块中进行元刷新,因为当 Javascript 被禁用时,分​​析无论如何都不会工作。

于 2013-06-20T05:30:40.497 回答
0

只需增加元刷新命令的秒数,它就会在重定向到新页面之前等待很长时间。这将允许您的 JavaScript 同时执行。

<meta http-equiv="refresh" content="1;url=https://www.nosuchwebsite.com">
                                    ^

<meta http-equiv="refresh" content="3;url=https://www.nosuchwebsite.com">
于 2013-06-20T05:22:48.237 回答