Community Forums › Forums › Archived Forums › General Discussion › How to print category and author – PHP problem?
- This topic has 7 replies, 2 voices, and was last updated 9 years, 5 months ago by Tmas.
-
AuthorPosts
-
October 25, 2014 at 12:13 am #129056TmasMember
Hi!
I use Genesis Eleven40 theme.I am trying to add Content Group to Google Analytics, but I've some problem with my PHP code which should print author and category.
I added this code using wp_head Hook. Execute PHP on this hook is on.
Problem is that this code doesn't print anything in source code. There could be some very basic error, but I'm not so familiar with PHP.
<?php
if (is_single()){
echo "ga('set', 'contentGroup1', '".get_the_author()."');
$category = get_the_category();
if ($category && !empty($category[0]->cat_name)){
echo “ga(‘set’, ‘contentGroup2', ‘”.$category[0]->cat_name.”‘);
}
}
?>This should print something like this
ga('set', 'contentGroup1', 'Author's Name');
ga('set', 'contentGroup2', 'Category1');October 25, 2014 at 12:39 am #129062Brad DaltonParticipantOctober 25, 2014 at 1:03 am #129066TmasMemberSorry but I don't understand what you mean. I hoped that this PHP print author and category name but it doesn't do anything.
October 26, 2014 at 1:59 am #129248TmasMemberSo, here is the whole Google Analytics code which is in wp_head Hook.
<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-XXX', 'auto'); <?php if (is_single()){ echo "ga('set', 'contentGroup1', '".get_the_author()."'); $category = get_the_category(); if ($category && !empty($category[0]->cat_name)){ echo “ga(‘set’, ‘contentGroup2', ‘”.$category[0]->cat_name.”‘);”n”; } } ?> ga('send', 'pageview'); </script>
I hoped that this will print following code in source 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-XXX', 'auto'); ga('set', 'contentGroup1', 'Author'); ga('set', 'contentGroup2', 'CategoryName'); ga('send', 'pageview'); </script>
But now my php does this that the whole Analytics code doesn't appear in the source code.
October 26, 2014 at 9:28 am #129288Brad DaltonParticipantPlease link to the URL where you got the code as there maybe some documentation on how to install it.
October 26, 2014 at 9:57 am #129291TmasMemberHi!
I found this code here:
https://www.highposition.com/blog/how-to-send-author-content-groups-wordpress-google-analytics/October 26, 2014 at 10:54 am #129297Brad DaltonParticipantTry it in the Genesis > Theme Settings > Header and Footer scripts box
October 27, 2014 at 11:58 am #129452TmasMemberI tried to set this code in Header and Footer scripts box
<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-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>
But this is what I found in source code
(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', 'XXX', '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>
So it doesn't handle PHP?
And if I added this same code in wp_head Hook, it doesn't print anything in source code.
And when I remove this part of code from wp_head Hook, then I can find Google Analytics code in source code.
I think that this php should have some error.
<?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"; } } ?>
-
AuthorPosts
- The forum ‘General Discussion’ is closed to new topics and replies.