2011-05-28 102 views
0

我有一个global.php文件,我从每个脚本的顶部调用。该脚本旨在承载我所有的标题信息。PHP问题,似乎打破了我的谷歌分析脚本

发生的问题是,一旦我将google分析代码放入global.php中,它不再向我的谷歌分析注册数据。

这里是global.php代码:

<?php 
/* this file needs to be included in every user-facing script that serves ads */ 

/* this will prevent "PHP Warning: Cannot modify header information - headers already sent.." */ 
ob_start(); 

/* this will make session cookie worl for both http://www.comehike.com and http://comehike.com 
* session cookie lifetime is set to 2 hours 
*/ 
session_set_cookie_params("7200", "/", ".comehike.com"); 

/* it is better to have session_start here rather then in every page 
* session_start needs to be called before any headers sent to not trigger php warning */ 
session_start(); 

global $production; 
$production = true; 

/*============================== ad pacement utils ===============================*/ 
/* 
* this helper module will simplify ads placement in production and simulate ads in development. 
* It will allow to turn of the ads for the user with premium account. 
* 
* To use this module: 
* in the head, where google site verification needs to be included, add the following 

    <?php 
    printGoogleSiteVerification(); 
    ?> 

* in the page, where ads needs to be inserted, add the following lines (example): 

    <?php 
     printGoogleAdPlacement(7425893768, 250, 250, '10/11/10'); 
    ?> 

*/ 

function printGoogleSiteVerification(){ 
    global $production; 
    $str = ''; 
    if($production===true){ 
     $str.= '<meta name="google-site-verification" content="RJcu8ObHW6huUbyea336xJ1WgLiC4478fz3_3r_zthc" />'."\n"; 
     $str.= '<meta name="alexaVerifyID" content="-2hEyu8phk6CFHF-PTqdQvP2B8k" />'."\n"; 
    } 
    echo $str; 
} 

function hasPremium(){ 
    return false; 
    /* 
    //this is a sample function to check is user has a premium account. 
    //in this example alex won't see any ads 
    if($_SESSION["user_id"]==='alex'){ 
     return true; 
    }else{ 
     return false; 
    }*/ 
} 

//ex: printGoogleAdPlacement(7425893768, 728, 90, 10/11/10) 
//ex: printGoogleAdPlacement(7425893768, 250, 250, 10/11/10) 
//ex: printGoogleAdPlacement(7425893768, 200, 200, 10/11/10) 
function printGoogleAdPlacement($google_ad_slot, $width, $height, $created){ 
    global $production; 

    //user with premium won't see any ads. 
    //the question is what goes into the slots instead? 
    if(hasPremium() === true) {return;} 

    $str = ''; 
    if($production === true){ 
     $str.= '<script type="text/javascript"><!--'."\n"; 
     $str.= 'google_ad_client = "pub-1884332214318974";'."\n"; 
     $str.= '/* '.$width.'x'.$height.', created '.$created.' */'."\n"; 
     $str.= 'google_ad_slot = "'.$google_ad_slot.'";'."\n"; 
     $str.= 'google_ad_width = '.$width.';'."\n"; 
     $str.= 'google_ad_height = '.$height.';'."\n"; 
     $str.= '//-->'."\n"; 
     $str.= '</script>'."\n"; 
     $str.= '<script type="text/javascript"'."\n"; 
     $str.= 'src="http://pagead2.googlesyndication.com/pagead/show_ads.js">'."\n"; 
     $str.= '</script>'."\n"; 
    }else{ 
     /* 
     $str .= '<div style="padding:0; height:'.($height+20).'px; width:'.$width.'px;border: 1px solid #888;background:#fff;">'. 
      '<div>Advertisement</div>'. 
      '<img src="images/fake_ads/ad_'.$width.'_'.$height.'.png"></img>'. 
      '</div>'; 
      */ 
     $str = '<img src="images/fake_ads/ad_'.$width.'_'.$height.'.png"></img>'; 
    } 
    $str = '<div style="padding:0; height:'.($height).'px; width:'.$width.'px;background:#fff;">'. 
      $str. 
      '</div>'; 

    echo $str; 
} 

/* =============== end of ad placement utils =================== */ 

/* ===================== google anaytics ========================*/ 
function printGoogleAnalytics() 
{ 
    $str = ''; 
    if($production === true) 
    { 
     $str.= '<script type="text/javascript">'."\n"; 

     $str.= '(function() {'."\n"; 
     $str.= ' var ga = document.createElement("script"); ga.type = "text/javascript"; ga.async = true;'."\n"; 
     $str.= ' ga.src = ("https:" == document.location.protocol ? "https://ssl" : "http://www") + ".google-analytics.com/ga.js";'."\n"; 
     $str.= ' var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(ga, s);'."\n"; 
     $str.= '})();'."\n"; 

     $str.= '</script>'."\n"; 
    } 
    echo $str; 
} 
/* ===================== end of google anaytics ========================*/ 
?> 

下面是一个例子页面,出现这种情况:

http://www.comehike.com/outdoors/hiking_headlamp.php

如果我参加了goog analitics码出global.php的并将它作为普通html放在标题中,它可以工作,因为我可以看到访问者数据。如果我把它放到global.php文件中,它不起作用。

这里是我尝试从在global.php

任何想法是怎么回事错在这里头调用该函数?如果它只是一个语法问题,它会更坏,对吧?

+1

乌尔页面呈现的时候,看看源代码,调试 – Ibu 2011-05-28 04:09:12

+0

@Ibu你指的是html源代码? – Genadinik 2011-05-28 04:09:49

+0

是的html源码,看看谷歌代码是否正确加载 – Ibu 2011-05-28 04:11:31

回答

1

我看了看代码,看不到任何错误,代码正确地创建脚本标记并将其添加到文档的头部。

但是最近我在尝试在Google上验证我的页面以查找其脚本标记时遇到了类似的问题。我的第一个解决方案类似于你直接在html中粘贴代码,但这不是我想要的,因为我希望它可以自动包含在所有页面中,所以这就是我所做的。

在php中重写代码似乎由于某种原因而破坏它。所以不是每行线重写你可以写了整个事情的使用HEREDOC语法一个字符串:

$str =<<<GOOGLECODE 
<script type="text/javascript"> 
var _gaq = _gaq || []; 
_gaq.push(['_setAccount', 'XX-XXXXXXX-XX']); 
_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> 
GOOGLECODE; 

然后你可以重复你的$海峡。

echo $str; 

也因为你使用的输出缓冲,您还可以使用此方法

function printGoogleAnalytics() 
{ 
?> 
<script type="text/javascript"> 
    .... 
</script> 
<?php 
} 
... 

调用脚本将打印HTML之间。

我希望帮助解决您的问题

+0

谢谢!你帮了我不少。 – Genadinik 2011-05-28 23:17:29