我正在尝试使用php在每个文件上加载Google Analytics(分析)。
这不是一个网站,这些是一些带有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(分析)文件,但没有跟踪会员文件夹。

最佳答案

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

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>

关于javascript - 在php中插入Google Analytics(分析)(我做对了吗?),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30268353/

10-16 07:59
查看更多