我是否需要将 _gaq.push(['_trackEvent' ..... 放在 PHP 代码中的
我想从我的 PHP 脚本将一些事件跟踪推送到我的 google 分析帐户。我包含一个外部 PHP 文件,该文件在我的标头脚本中包含 google 分析代码,该代码包含在我的所有站点 PHP 文件中 -
header.php :
<?php
include_once("analytics_script.php");
...
?>
index.php :
<?php
include("header.php");
//When i want to track an event from this script -
_gaq.push(['_trackEvent', 'Category', 'Action', 'Label']);
?>
analytics_script.php :
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-********-**']);
_gaq.push(['_trackPageview']);
(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>
所以在index.php中将只使用_gaq.push跟踪事件,或者我需要将行包装在javascript标签中,例如 -
<script>_gaq.push(... </script>
谢谢
I want to push some event tracking to my google analytics account from my PHP scripts. I include an external PHP file which has the google analytics code in my header script which is included in all my site PHP files -
header.php :
<?php
include_once("analytics_script.php");
...
?>
index.php :
<?php
include("header.php");
//When i want to track an event from this script -
_gaq.push(['_trackEvent', 'Category', 'Action', 'Label']);
?>
analytics_script.php :
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-********-**']);
_gaq.push(['_trackPageview']);
(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>
So in index.php will just using _gaq.push track events ok or do I need to wrap the line in javascript tags like -
<script>_gaq.push(... </script>
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除非包含的 header.php 文件在仍然存在开放脚本标记(我强烈建议不要这样做)的情况下结束,否则您需要将其包装在
标记中。否则它只会在用户屏幕上显示为文本。
编辑:Koraktor 是对的。我完全忘记了你没有关闭 PHP 标签。
Unless the included header.php files ends while there's still an open script tag (which I'd seriously recommend against) then yes you'll need to wrap that in
<script>
tags. Otherwise it would just show up as text on the user's screen.Edit: Koraktor is right. I completely missed that you don't close your PHP tag.