使用php网站进行分析跟踪

时间:2012-07-10 10:14:59

标签: php google-analytics

我最近将我的网站格式更改为php(而不是html),即我已将所有HTML页面重命名为.php,并使用.htaccess文件修复了链接。

我需要使用Google分析来跟踪我的新php页面,因此我创建了一个包含Google的javascript片段的单独的php文件。我将它放在我的网站的根目录下,并使用<body>标记后面的代码将我的每个php标记链接到它:

<?php include_once("analyticstracking.php") ?>

我的问题是,这似乎只适用于我的index.php页面。我的所有其他页面都找不到analyticstracking.php(在Dreamweaver中它说''analyticstracking.php'不在本地磁盘上。获取“)

如果我将链接(通过添加“/”)更改为:

<?php include_once("/analyticstracking.php") ?>

然后我的所有网页都可以找到该文件,但谷歌分析似乎无法跟踪我的活动。

我正在使用“分析 - 实时”来测试它。

这是我的网址www.brp-architects.com。 (目前正在使用

<?php include_once("/analyticstracking.php") ?>

因为此代码带有“/”,允许我的所有页面找到我的跟踪代码php文件。)

我这样做的全部原因是我可以使用一段PHP代码通过代理服务器ip来检索我的网站访问者的IP地址:

<?
if (getenv(HTTP_X_FORWARDED_FOR)) {
    $ip_address = getenv(HTTP_X_FORWARDED_FOR);
} else {
    $ip_address = getenv(REMOTE_ADDR);
}

&GT;

谢谢你的回复!

以下是analyticstracking.php文件中的跟踪代码段:

<script type="text/javascript">

var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-5434990-2']);
  _gaq.push(['_trackPageview']);
  setTimeout('_gaq.push([\'_trackEvent\', \'NoBounce\', \'Over 30 seconds\'])',30000);

(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>

我的跟踪代码链接直接位于开头正文标记下:

<?php include_once("/analyticstracking.php") ?>

2 个答案:

答案 0 :(得分:4)

如果它适用于 index.php ,那么 analyticstracking.php 脚本就可以了。

导致这种情况的原因是您将其包含在其他脚本中的方式。

请确保该脚本的路径在其他脚本上正确无误 确定包括这样:

include($_SERVER['DOCUMENT_ROOT'].'PATH-TO-SCRIPT/analyticstracking.php');

答案 1 :(得分:0)

您可以通过多种方式解决此问题。 首先,您可以更改include_path变量 http://il.php.net/manual/en/ini.core.php#ini.include-path

其次,您可以使用getcwd()

在index.php中初始化自己的全局变量
相关问题