{"id":1156,"date":"2015-11-06T10:44:18","date_gmt":"2015-11-06T04:44:18","guid":{"rendered":"http:\/\/promincproductions.com\/blog\/?p=1156"},"modified":"2015-11-06T10:44:18","modified_gmt":"2015-11-06T04:44:18","slug":"wordpress-add-google-analytics-content-grouping-with-google-analyticator-post-category","status":"publish","type":"post","link":"https:\/\/promincproductions.com\/blog\/wordpress-add-google-analytics-content-grouping-with-google-analyticator-post-category\/","title":{"rendered":"WordPress &#8211; Add Google Analytics Content Grouping with Google Analyticator &#8211; Post Category"},"content":{"rendered":"<p>Google Analytics content grouping is a feature that really helps to break down site usage in a new level that can be super powerful. \u00a0But the point of this post isn&#8217;t to promote Google Analytics Content Grouping &#8211; you can <a href=\"https:\/\/support.google.com\/analytics\/answer\/2853546?hl=en\" target=\"_blank\" rel=\"nofollow noopener\" data-lasso-id=\"529\">read more about Content Groups\u00a0from Google<\/a> if you need to know more. \u00a0The focus of this article is around how to simply add content grouping to a WordPress website.<\/p>\n<h2>A Plethora of Options<\/h2>\n<p>The first, and possibly most simple solution, would be to use\u00a0the rule builder within Google Analytics itself to group together content. \u00a0However, it&#8217;s common that most sites don&#8217;t have a standard pattern that is consistent enough to write a rule or regular expression that will create the group sufficiently enough. \u00a0That is the reason for going one step further with writing some code to add the appropriate tag to your Google Analytics requests.<\/p>\n<p>I&#8217;ve seen many solutions that utilize Google Tag Manager to implement this, and that is something that I have chosen to NOT use. \u00a0Google Tag Manager is powerful and helpful, but honestly I have never utilized it for several reasons. \u00a0The two main ones are that I am a developer and so thus I prefer to just put the code in place directly as opposed to utilize a 3rd party tool to do something I can do. \u00a0The other issue I see with GTM is that it is a 3rd party tool &#8211; the more code you try to load that doesn&#8217;t come from your server directly the more potential issues you introduce in terms of site load time, possible server outages, etc.<\/p>\n<p>Another way to approach this problem is to hard-code Content Grouping code into the site, but if you are like most WordPress admins that use a plugin to implement Google Analytics you&#8217;ll need to send the Content Grouping request to Google Analytics via another request &#8211; typically an event request. \u00a0This isn&#8217;t a bad solution, but is inflating the requests you are sending and it&#8217;s not really all that necessary when you could pass this into the standard page view request. \u00a0That is what the solution below will do.<\/p>\n<h2>Requirements for the Solution<\/h2>\n<p>\u00a0but I&#8217;ll focus on the method I&#8217;m using, which includes utilizing the\u00a0<a href=\"https:\/\/wordpress.org\/plugins\/google-analyticator\/\" target=\"_blank\" data-lasso-id=\"530\" rel=\"noopener\">Google Analyticator WordPress Plugin<\/a> to implement Google Analytics. \u00a0There may be other plugins that will work with this solution &#8211; it all depends on if you can inject additional tracking parameters through the plugin or not.<\/p>\n<p>You also need to be able to modify your functions.php file. \u00a0So if you don&#8217;t have access to your codebase or feel comfortable modifying your site&#8217;s code, then this is not the solution you should use.<\/p>\n<h2>Setup Content Grouping in Google Analytics<\/h2>\n<p>The first step is to enable and setup content grouping within Google Analytics.<\/p>\n<figure id=\"attachment_1157\" aria-describedby=\"caption-attachment-1157\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2015\/11\/Google-Analytics-Content-Grouping.png\" rel=\"attachment wp-att-57\" data-lasso-id=\"531\" data-rel=\"lightbox-gallery-xM0TFGJ3\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" class=\"size-medium wp-image-1157\" src=\"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2015\/11\/Google-Analytics-Content-Grouping-500x424.png\" alt=\"Configure Google Analytics Content Group\" width=\"500\" height=\"424\" srcset=\"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2015\/11\/Google-Analytics-Content-Grouping-500x424.png 500w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2015\/11\/Google-Analytics-Content-Grouping-150x127.png 150w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2015\/11\/Google-Analytics-Content-Grouping-600x509.png 600w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2015\/11\/Google-Analytics-Content-Grouping.png 714w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-1157\" class=\"wp-caption-text\">Configure the Google Analytics Content Grouping settings to create a content group. Google Analytics allows you to have 5 different content groups, but you can have sub-content\/groups under each content group.<\/figcaption><\/figure>\n<ul>\n<li>Login into your Google Analytics account<\/li>\n<li>Select the property you want to setup Content Grouping for<\/li>\n<li>Click on\u00a0<strong>Admin<\/strong> at the top of the page<\/li>\n<li>Under the\u00a0<strong>View<\/strong> column, click on\u00a0<strong>Content Grouping<\/strong><\/li>\n<li>Click the\u00a0<strong>New Content Grouping<\/strong> button<\/li>\n<li>Enter a name for this content group &#8211; I used\u00a0<strong>Primary Category<\/strong><\/li>\n<li>Choose\u00a0<strong>Enable Tracking Code.<\/strong><\/li>\n<li>Ensure the\u00a0<strong>Enable<\/strong> switch is set to\u00a0<strong>On.<\/strong><\/li>\n<li>Take note of what the\u00a0<strong>Index<\/strong> number is. \u00a0If this is your first rule, it will be set to\u00a0<strong>1<\/strong> by default, though you can change this to a different index if you so choose to.<\/li>\n<li>Click <strong>Done<\/strong>.<\/li>\n<li>Click\u00a0<strong>Save<\/strong>.<\/li>\n<\/ul>\n<h2>Update functions.php within WordPress<\/h2>\n<p>Before I go too deep into this step, make sure you understand how to properly modify your theme within WordPress. \u00a0If you are modifying any files in a theme folder, such as the functions.php file, you should ensure you are utilizing a child-theme to allow future theme updates to be applied without breaking new features, such as this one. \u00a0I won&#8217;t go into detail on child-theming, but you can read about how to set it up in the <a href=\"https:\/\/codex.wordpress.org\/Child_Themes\" target=\"_blank\" rel=\"nofollow noopener\" data-lasso-id=\"532\">WordPress Codex about Child Themeing<\/a>.<\/p>\n<p>I considered making this script into a plugin which would prevent the modifications to the functions.php file, but that seemed like overkill. \u00a0However if there becomes a large need for this I could put that together at some point. \u00a0Feel free to leave a comment if you have an opinion about this.<\/p>\n<p>The purpose for adding a function to the functions.php file is to create a javascript variable in the &lt;head&gt; tag of the site that lists the categories that a post is listed under. \u00a0This javascript variable can then be accessed by the javascript code added below to the Google Analyticator plugin.<\/p>\n<p>This new variable, wpCategories, is an array with two sub-arrays. \u00a0The first is the parent level categories a post is listed under, and the second sub-array is the child categories that a post is listed under. \u00a0It maybe wasn&#8217;t necessary to list all of these here and I could have instead only returned one category to be used in the tracking, but I choose to leave more data incase it could be useful someday in the future. \u00a0It&#8217;s still a tiny bit of code and shouldn&#8217;t show any sort of performance degradation.<\/p>\n<p>So onto the code. \u00a0It&#8217;s a simple block of PHP code that shouldn&#8217;t need any modifications. \u00a0Just place this into the functions.php file, save it and upload it to your server.<\/p>\n<pre><code>\/*\r\n * Load categories to a Javascript variable\r\n * This allows us to pass the categories into Google Analytics tracking code.\r\n *\/\r\nadd_action('wp_head','hook_head_get_categories');\r\nfunction hook_head_get_categories() {\r\n \/* If post *\/\r\n if( is_single() ) {\r\n   \/* Define variables *\/\r\n   $parent = $child = array();\r\n\r\n   \/* Loop through each category *\/\r\n   foreach( get_the_category() as $category ) {\r\n     if( $category-&gt;parent == 0 ) {\r\n       $parent[] = $category-&gt;name;\r\n     } else {\r\n       $child[] = $category-&gt;name;\r\n     }\r\n   }\r\n\r\n   \/* Write Javascript to page *\/\r\n   echo \"&lt;script type=\\\"text\/javascript\\\"&gt;var wpCategories = [ ['\" . implode(\"','\", $parent) . \"'], ['\" . implode(\"','\", $child) . \"'] ];&lt;\/script&gt;\\n\";\r\n }\r\n}<\/code><\/pre>\n<h2>Add Content Grouping Code to Google Analyticator<\/h2>\n<p>The last piece to the puzzle is to use this newly created javascript variable with the categories to populate the Content Grouping in the request to Google Analytics.<\/p>\n<p>The Google Analyticator plugin utilized in this example already offers a feature to inject additional tracking options into the request. \u00a0So utilizing that field in the WordPress Admin we add some conditional javascript code that ensures the above variable is set. \u00a0Keep in mind that this code is only applicable on a post, but the code is designed to not cause issues on non-post pages on the site.<\/p>\n<figure id=\"attachment_1159\" aria-describedby=\"caption-attachment-1159\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2015\/11\/Google-Analyticator-Settings.png\" rel=\"attachment wp-att-59\" data-lasso-id=\"533\" data-rel=\"lightbox-gallery-xM0TFGJ3\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" class=\"size-medium wp-image-1159\" src=\"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2015\/11\/Google-Analyticator-Settings-500x298.png\" alt=\"Update the Google Analyticator settings\" width=\"500\" height=\"298\" srcset=\"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2015\/11\/Google-Analyticator-Settings-500x298.png 500w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2015\/11\/Google-Analyticator-Settings-768x457.png 768w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2015\/11\/Google-Analyticator-Settings-150x89.png 150w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2015\/11\/Google-Analyticator-Settings-600x357.png 600w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2015\/11\/Google-Analyticator-Settings.png 957w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-1159\" class=\"wp-caption-text\">Insert conditional javascript to insert the Content Grouping tag into the Google Analytics request.<\/figcaption><\/figure>\n<p>This script first looks to see if there is a parent\u00a0category defined. \u00a0If so, it utilizes that category for the data sent to Google Analytics. \u00a0If there is not a parent category set, then it utilizes the first child category. \u00a0If there isn&#8217;t a child category defined, the Content Group is not added to the Google Analytics request.<\/p>\n<p>One thing to point out is that in a case where you have multiple parent or children categories, I am unsure of how WordPress sorts those. \u00a0I&#8217;m assuming it is either alphabetically or numerically based on the ID given to the category. \u00a0I&#8217;m assuming it&#8217;s the later, and if so the the order is really based on when a category was added to WordPress.<\/p>\n<pre><code>\/* Add Content Group - primary category *\/\r\nif( typeof wpCategories != 'undefined' ) {\r\n var primaryCat = \"\";\r\n if( wpCategories[0][0] ) { primaryCat = wpCategories[0][0]; }\r\n else if( wpCategories[1][0] ) { primaryCat = wpCategories[1][0]; }\r\n if( primaryCat ) {\r\n   ga('set', 'contentGroup1', primaryCat );\r\n }\r\n}<\/code><\/pre>\n<p>There is one item in this script that you may need to update. \u00a0Remember back when we were setting up the Content Group in Google Analytics how I said you should take note of the Index of the Content Group that we created? \u00a0Well this is where that becomes important. \u00a0If you are using Index 1 then this script is good to go as is. \u00a0If not, update the index number on the 3rd to the last line to reflect the index number you have set in Google Analytics.<\/p>\n<pre><code>ga('set', 'contentGroup1', primaryCat );\r\n<\/code><\/pre>\n<h2>Testing the Code<\/h2>\n<p>The last step after saving our two blocks of code is to test that it worked. \u00a0I like to use Chrome as my browser, and a great plugin called <a href=\"https:\/\/chrome.google.com\/webstore\/detail\/omnibug\/bknpehncffejahipecakbfkomebjmokl?hl=en\" target=\"_blank\" rel=\"nofollow noopener\" data-lasso-id=\"534\">Omnibug<\/a> is an AWESOME tool to get instant proof that this worked. \u00a0<a href=\"https:\/\/addons.mozilla.org\/en-us\/firefox\/addon\/omnibug\/\" target=\"_blank\" rel=\"nofollow noopener\" data-lasso-id=\"535\">Omnibug is available for Firefox<\/a> as well as long as you have <a href=\"https:\/\/addons.mozilla.org\/en-us\/firefox\/addon\/firebug\/?src=dp-dl-dependencies\" target=\"_blank\" rel=\"nofollow noopener\" data-lasso-id=\"536\">Firebug<\/a> installed.<\/p>\n<p>Keep in mind of any rules you have set for Google Analyticator &#8211; I have it set to not track when logged into the site. \u00a0So to do testing I either log out or use an incognito window.<\/p>\n<p>With the developer tools open, load your post page. \u00a0In the developer tools, choose the\u00a0<strong>Onmibug<\/strong> tab and expand the last request. \u00a0Look for the <b>Hit Type<\/b> &#8211; we are looking for this to be\u00a0<strong>pageview<\/strong>. \u00a0Oftentimes you&#8217;ll also see a hit listed as\u00a0<strong>dc<\/strong> &#8211; this stands for double click and is related to paid search. \u00a0We can ignore that hit for now. \u00a0Find the last hit listed as a\u00a0<strong>pageview<\/strong>.<\/p>\n<p>Look at the bottom of this request in the <b>Other<\/b> section for the property labeled\u00a0<strong>cg1<\/strong> (or other number depending on the index you are using in Google Analytics for this Content Group) &#8211; the value will be the category name that is being passed to Google Analytics as the Content Group label for this pageview.<\/p>\n<figure id=\"attachment_1161\" aria-describedby=\"caption-attachment-1161\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2015\/11\/Screen-Shot-2015-11-05-at-10.34.12-PM1.png\" rel=\"attachment wp-att-6\" data-lasso-id=\"537\" data-rel=\"lightbox-gallery-xM0TFGJ3\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" class=\"size-medium wp-image-1161\" src=\"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2015\/11\/Screen-Shot-2015-11-05-at-10.34.12-PM1-500x246.png\" alt=\"Use Omnibug to test Google Analytics requests\" width=\"500\" height=\"246\" srcset=\"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2015\/11\/Screen-Shot-2015-11-05-at-10.34.12-PM1-500x246.png 500w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2015\/11\/Screen-Shot-2015-11-05-at-10.34.12-PM1-150x74.png 150w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2015\/11\/Screen-Shot-2015-11-05-at-10.34.12-PM1-600x295.png 600w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2015\/11\/Screen-Shot-2015-11-05-at-10.34.12-PM1.png 680w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-1161\" class=\"wp-caption-text\">Test that the Content Grouping code is working correctly with developer tools and Omnibug.<\/figcaption><\/figure>\n<p>If you choose to not use Omnibug, you could also\u00a0wait for the data to show up in Google Analytics and trust that it is being sent from the site correctly. \u00a0I like to test early and ensure that I&#8217;m confident that changes I made to the site worked though.<\/p>\n<p>I also discuss other methods, some that do not require plugins, to test your Google Analytics traffic in this <a href=\"https:\/\/promincproductions.com\/blog\/undocumented-lessons-learned-implementing-google-analytics-universal-enhanced-ecommerce\/#Google-Analytics-Debugging-Tools\" target=\"_blank\" data-lasso-id=\"538\" rel=\"noopener\">article about implementing features of Google Analytics<\/a>.<\/p>\n<h2>Summary<\/h2>\n<p>Content Grouping in Google Analytics is a great addition to the reporting of any website. \u00a0Utilizing an already great WordPress plugin we were able to extend it&#8217;s base functionality to also include Content Grouping with only two small blocks of code.<\/p>\n<p>You can also consider this as the basis for other possibilities. \u00a0It wouldn&#8217;t take much effort to modify these two code examples to create content groups under different premisses.<\/p>\n<p>I hope this helps someone out there enhance their reporting capabilities!<\/p>","protected":false},"excerpt":{"rendered":"<p>Google Analytics content grouping is a feature that really helps to break down site usage in a new [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1159,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"wprm-recipe-roundup-name":"","wprm-recipe-roundup-description":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[50,12,13,5,9],"tags":[],"class_list":["post-1156","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-analytics","category-javascript-jquery","category-php-development","category-website-development","category-wordpress-development"],"jetpack_featured_media_url":"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2015\/11\/Google-Analyticator-Settings.png","jetpack_shortlink":"https:\/\/wp.me\/p4BbcR-iE","jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts\/1156","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/comments?post=1156"}],"version-history":[{"count":2,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts\/1156\/revisions"}],"predecessor-version":[{"id":1162,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts\/1156\/revisions\/1162"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/media\/1159"}],"wp:attachment":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/media?parent=1156"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/categories?post=1156"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/tags?post=1156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}