在 php 中插入 Google 分析(我这样做的方式正确吗?)

Insert Google Analytics in php (Am I doing it the right way?)

我正在尝试使用 php 在每个文件上加载 Google 分析。 这不是网站,这些是一些带有 javascript redirectiib 的附属链接结构,所以我决定在它们上实施 GA 跟踪代码。他们基本上是这样工作的:

根文件夹是 affiliatestuff,它有:
googleanalytics123.php(包含GA javascript的文件),里面没有任何php,只有GA代码。
index.thml: 空白 HTML 文件。

在根文件夹中有一个名为 affiliate01 的子文件夹:在这个文件夹中,有一个带有以下代码的 index.php (请注意,出于安全原因,我在以下代码中更改了一些内容) :

开始index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<?php
include_once ('http:// my site/go/googleanalytics123.php');
?>

<script type="text/javascript"> function recordOutboundLink(link, category, action) { try { var myTracker=_gat._getTrackerByName(); _gaq.push(['myTracker._trackEvent', category , action ]); setTimeout('document.location = "' + link.href + '"', 100) }catch(err){} } </script>

<script type="text/javascript"><a href="../trafficapp">trafficapp</a>

recordOutboundLink(this, 'Go to Australia', 'http:// my affiliate link /191719/18040');
setTimeout(function(){window.location='http:// my affiliate link/191719/18040';}, 500); // working with a 500ms timeout to make sure the tracking is done correctly

</script></head>
<title>Live and work in Australia</title>
<body>
</body>

</html>

最后一个文件应该将用户重定向到附属产品的页面,它正在工作,我想知道我是否以正确的方式获得 googleanalytics123.php。

有googleanalytics123.php

的代码
<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-xxxxxxxx-x', 'auto');
  ga('send', 'pageview');

</script>

只是在脚本里面,根本就没有php。请注意,我故意更改了 UA。

那我的做法对吗?我设法跟踪了 google 分析文件,但没有跟踪附属文件夹。

创建一个名为 analytics.php 的文件,其中包含 google 分析代码,然后包含该文件

analytics.php

<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),include "analytics.php";
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-xxxxxxxx-x', 'auto');
  ga('send', 'pageview');

</script>

然后在你的脚本中:

  <?php include "analytics.php"; ?>
  </body>
</html>