2011-05-28 133 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