我是否需要将 _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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゞ记忆︶ㄣ 2024-11-11 01:50:24

除非包含的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文