在PHP中插入Google Analytics(我的方式是否正确?) [英] Insert Google Analytics in php (Am I doing it the right way?)

查看:135
本文介绍了在PHP中插入Google Analytics(我的方式是否正确?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用php在每个文件上加载Google Analytics。
这不是一个网站,这些是一些与javascript redirectiib的会员链接结构,所以我决定在它们上实现GA跟踪代码。他们基本上是这样工作的:
$ b

根文件夹是 affiliatestuff ,它有:

googleanalytics123.php (包含GA javascript的文件),里面没有任何php,只是GA代码。

index.thml :空白HTML文件。

b $ b在根文件夹中有一个名为 affiliate01的子文件夹, code>:在这个文件夹里面,有一个 index.php 和下面的代码(请不要在下面的代码中改变了一些安全的东西原因):

开始 index.php

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

<?php
include_once('http://我的网站/ go / googleanalytics123.php');
?>

< script type =text / javascript>函数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(这里,'去澳大利亚','http://我的会员链接/ 191719/18040');
setTimeout(function(){window.location ='http:// my affiliate link / 191719/18040';},500); //使用500ms的超时时间确保跟踪正确完成

< / script>< / head>
< title>在澳大利亚居住和工作< / title>
< body>
< / body>

< / html>

最后一个文件应该将用户重定向到附属产品的页面,它正在工作,我'd想知道是否我的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。



那么我是否正确地做了这件事?我设法跟踪谷歌分析文件,但不是附属文件夹。

解决方案

创建一个名为 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)
})(窗口,文件, '脚本', '// www.google-analytics.com/analytics.js','ga');

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

< / script>

然后在脚本中:

 <?php includeanalytics.php; ?> 
< / body>
< / html>


I'm trying to load Google Analytics on every file using php. This is not a site, these are some affiliate links structure with javascript redirectiib, so I decided to implement GA tracking code on them. They basically work like this:

The root folder is affiliatestuff, and it has:
googleanalytics123.php (File which contains the GA javascript), it doesn't have any php inside, just the GA code.
index.thml: Blank HTML file.

Inside the root folder there is a subfolder named affiliate01: Inside this folder, there is a index.php with the following code (Please not that in the following code I've changed a few stuff for security reasons):

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

That last file is supposed to redirect the user to the affiliate product's page, it is working, what I'd like to know is if I'm getting the googleanalytics123.php the right way.

There is the code of the 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>

It's just that inside the script, no php at all. Please not that I've changed the UA on purpose.

So am I doing it the right way? I managed to track the google analytics file, but not the affiliates folder.

解决方案

create a file called analytics.php that contains the google analytics code in it then include that file

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>

Then in your script:

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

这篇关于在PHP中插入Google Analytics(我的方式是否正确?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆