开发者

PHP problems that seem to break my Google Analytics script

开发者 https://www.devze.com 2023-03-09 08:45 出处:网络
I have a global.php file that I call from the top of every script.That script is meant to host all my header information.

I have a global.php file that I call from the top of every script. That script is meant to host all my header information.

The problem that is happening is that once I place the google analytics code into the global.php, it no longer registers data to my google开发者_StackOverflow analytics.

Here is the global.php code:

<?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 ========================*/
?>

Here is an example page where this happens:

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

If I take the goog analitics code out of the global.php and place it in the header as normal html, it does work because I can see the visitor data. If I put it into the global.php file, it doesn't work.

And here is the function I try to call from the header that is in the global.php

Any idea what is going wrong here? If it was just a syntax issue it would be much more broken, right?


I looked at the code and i can't see anything wrong, the code properly create the script tag and add it in the head of your document.

However recently i had a similar problem when trying to validate my page on google to find their script tag. My first solution was similar to yours which is pasting the code directly in the html, however this was not what i wanted since i wanted it to be automatically included in all pages so here is what i did instead.

Rewriting the code in php seems to break it for some reason. so instead of rewriting in line per line you can write the whole thing as one string using HEREDOC syntax:

$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;

Then you can echo your $str.

echo $str;

also since you are using output buffer you can also use this method

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

calling the script will print the html in between.

I hope that help fix your problem

0

精彩评论

暂无评论...
验证码 换一张
取 消