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

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top