将Google Analytics代码放在单独的文件中会破坏分析

时间:2011-07-11 03:15:40

标签: php google-analytics

我试图将Google Analytics代码存储在所有脚本引用的单个文件中。但是我的文件似乎打破了分析代码,使用此文件的所有页面都不再显示在我的Google分析报告中。

以下是我在脚本的head部分所做的事情:

<?php
include ('global.php');
include ('connect.php');
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN"
    "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">
<html>
<head>

<?php
printGoogleAnalytics();
?>


<?php
printGoogleSiteVerification();
?>

</head>

以下是global.php中的printGoogleAnalytics():

function printGoogleAnalytics()
{
    if($production === true)
    {
$str = <<<EOT
        <script type="text/javascript">
        var _gaq = _gaq || [];
        _gaq.push(['_setAccount', 'UA-9425856-20']);
        _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>


EOT;
    echo $str;
    }
}

知道可能会破坏什么吗?我还需要其他代码吗?我为此使用PHP。

1 个答案:

答案 0 :(得分:2)

由于Google Analytics代码未显示在生成页面的来源中,因此意味着该块未获得评估。

最可能的嫌疑人是if($production===true)。使用3个等号表示它的STRICT比较。可能发生的事情是你没有将它精确地设置为真实,而是设置为真实的东西。

您可以通过修复$production的设置方式或使用==来解决这个问题。