【问题标题】:Do I need to put _gaq.push(['_trackEvent' ..... inside <script> tags in my PHP code?我是否需要将 _gaq.push(['_trackEvent' ..... 放在我的 PHP 代码中的 <script> 标记内?
【发布时间】:2011-08-14 18:27:44
【问题描述】:

我想将一些事件跟踪从我的 PHP 脚本推送到我的谷歌分析帐户。我包含一个外部 PHP 文件,该文件在我的头脚本中包含谷歌分析代码,该文件包含在我的所有站点 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 跟踪事件 ok 还是我需要将行包装在 javascript 标记中,例如 -

<script>_gaq.push(... </script>

谢谢

【问题讨论】:

    标签: php javascript asynchronous google-analytics event-tracking


    【解决方案1】:

    除非包含的 header.php 文件在仍然有一个打开的脚本标签时结束(我强烈建议不要这样做),否则是的,您需要将其包装在 &lt;script&gt; 标签中。否则它只会在用户屏幕上显示为文本。

    编辑:Koraktor 是对的。我完全错过了你没有关闭你的 PHP 标记。

    【讨论】:

    • 它不会显示在浏览器中,它会导致 PHP 语法错误,因为 PHP 无法处理 JavaScript 代码。
    猜你喜欢
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 2016-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多