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?
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.