Google Analytics in PHP Header?


Google Analytics in PHP Header?

我是PHP新手,但学习缓慢。我遇到的问题是在我的标题中包含谷歌分析。

我已经为代码创建了一个单独的php文件,但我的问题是将其包含在下面的php HEADER文件中:

我认为问题出在@$content[1].='标签-但我需要包括这一点,因为这就是我的网站是如何建立的。

如何在不混淆文件中php代码的情况下,将Google Analytics代码放在标记之前。

提前谢谢。(我会问我的开发人员,但我想自学)。

头文件PHP代码如下:

<?php
@$content[1] .='
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html lang="en" dir="ltr">
<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>Title</title> 
<link id="www-core-css" rel="stylesheet" href="template/css/core.css"> 
<link id="www-core-css" rel="stylesheet" href="template/css/style.css">
</script></head> 
<body>
<div id="page" class=""> </div>
<div id="masthead-container"> 
    <div id="masthead" class="">
        <a href="index.php"> 
            <button id="logo" class="master-sprite" title="Online ePub Builder"></button> 
        </a> 
        <div align="right"> 
            <script type="text/javascript"><!--
google_ad_client = "ca-pub-6078741672481332";
/* UK 1 */
google_ad_slot = "5428463210";
google_ad_width = 728;
google_ad_height = 90;
//-->
 </script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
        </div>';
    if(is_login()) {
$content[1] .='<div id="masthead-utility"> 
            Hello, <a href="user.php">'.$_SESSION['uidrealname'].'</a> 
            &nbsp; &nbsp; &nbsp; &nbsp;
            <a href="logout.php">Sign Out</a> 
        </div>';
    } else {
$content[1] .='<div id="masthead-utility"> 
            <a href="login.php">Create Account</a>
            &nbsp; &nbsp; or &nbsp; &nbsp; 
            <a href="login.php">Sign In</a> 
        </div>';
    }

$content[1] .=' <div id="masthead-nav-user"> 
            <a href="help.php">Help</a>
                <a href="guide.php">FREE Guide</a> 
        </div> 
        <div id="masthead-nav-main"> 
            <a href="new.php">New Book</a> 
            '.(is_login() ? '<a href="books.php?    u='.$_SESSION['uid'].'">My Books</a>' : '').' 
        </div> 
        <div id="masthead-end"></div> 
    </div> 
</div> 
<div id="baseDiv"> 
<div id="homepage-main-content" class="">';
?>

获取文件内容:

$trackingCode = file_get_contents('yourFile');

然后把它添加到你的字符串中,就像这样:

<link id="www-core-css" rel="stylesheet" href="template/css/style.css">
</script>
'.$trackingCode.'
</head> 

我认为您的意思是希望在html <head>标记中使用Google analytics JavaScript——PHP标头通常被理解为发送到客户端浏览器的HTTP标头。

如果是这种情况,只需将谷歌分析脚本放在结束头标记</head>之前的某个位置。

附言:你目前有一个随机的</script>标签,在你关闭</head>之前没有打开标签-删除它!

您只需要将Google分析脚本放在<head> </head>标签之间,如

<head>

<script type="text/javascript">

//在这里通过你的谷歌分析脚本

</script>

</head>

提示我会把代码放在页脚而不是页眉中,这样你就可以得到真正的页面加载时间