16

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?

12
  • What's the file extension of the file including your php code? If it's not php, then php won't process it Jul 27, 2015 at 7:45
  • Hello André, it's included on any regular page created by my Wordpress theme. Any other php code works just fine.
    – Alex
    Jul 27, 2015 at 7:48
  • You mean that you can see the php tags inside your script?
    – dingo_d
    Jul 27, 2015 at 8:31
  • 1
    Yes, exactly. The author and category information aren't properly inserted.
    – Alex
    Jul 27, 2015 at 11:32
  • You've not shown us the code which does the include. Have you reset your opcode cache since you deployed this?
    – symcbean
    Jul 27, 2015 at 12:17

1 Answer 1

18
+50

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.

3
  • OR .. there are plugins available for WordPress which lets you run PHP code added in wysiwyg editor .. Aug 4, 2015 at 13:29
  • @SyedQarib sorry, I'm not very familiar with wordpress. Feel free to add to this answer!
    – php_nub_qq
    Aug 4, 2015 at 13:32
  • 1
    no need to be sorry :) was just pointing it out, might be helpful for someone else .. Aug 4, 2015 at 19:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.