Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is PHP code in Google analytics code not properly executed?

I am using this code:

<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-xx', 'auto');
<?php 
   if (is_single()){
      echo "ga('set', contentGroup1, '".get_the_author()."');n";
      $category = get_the_category();
      if ($category && !empty($category[0]->cat_name)){
         echo "ga('set', contentGroup2, '".$category[0]->cat_name."');n";
      }
   }
?>
  ga('send', 'pageview');

</script>

The aim is here to send both the category name and author from my Wordpress website to Google Analytics (Content Grouping).

However, when my website is being viewed my source code (in the head section) still shows the php tags. How can I ensure that the PHP code is properly parsed and the author / category information is correctly inserted?

like image 232
Alex Avatar asked Jul 27 '15 07:07

Alex


1 Answers

The problem you are describing means that the server is not parsing the codes you provided.

In a basic environment that would be caused by using a file type that is not associated with PHP by default (such as .html) and is not being parsed.

In your case, you mention you are using wordpress, I would guess that you have inserted this code in some field that has protection and instead of parsing your code it is then outputting it literally instead.

What you can do is find the file and update it manually instead of using the wordpress UI. Additionally, make sure the file will be parsed by the preprocessor.

Also, if you are using the WordPress wysiwyg editor to add the php code then there are plugins available which can let you run/parse PHP code added there.

like image 92
php_nub_qq Avatar answered Oct 11 '22 13:10

php_nub_qq