Integrating Google Analytics Content Grouping Into Google Tag Manager

In this post, we will intrigrate Content Grouping into Google Tag Manager.

Posted by Author At July 2nd, 2016

Content grouping is one of the great feature of Google Analytics, it let us to have a grouped report of content inside the google analytics metrics. This feature not only helps to retrieves the organized result, but also make the analysis easier. Actually for large websites like ecommerce with many products and categories, this feature can be a lifesaver.

# Let’s Create Content group inside Google Analytics

- go to Google Analytics>Admin>View>Content Grouping.

- Now let’s create a content group, in my case I’m filtering the contents of my blog categories.

- So I’m doing enable tracking code, and selecting the index 1.

# Now let’s go to the Google Tag Manager

  • Since we do need to have content subgroups, it’s better that we create a lookup table custom variable in Google Tag Manager defining each pair of input and output. So let’s do it, go to the variable>select all the built in variable options, >new>choose lookup table, following screen will appear. 
  • Now let’s filter our category using the page path macro consisting the page path matches. (In my cases, I’m assuming that I’ve the url hierarchy in the content, let’s say
  • Add desired numbers of columns and click on save. Now we have a lookup table ready.

# Let's Edit Our Tag

Now let’s go to our Google Analytics tag that we created a bit earlier and edit our configuration to add the content grouping feature. Click on configuration tag section:

Then more settings>content groups>add a content group . The index number should correspond to the index number of the google analytics content group and the content group to the lookup table variable that we created earlier.


Now preview and publish your changes, you should be able to tag being fired in accordance with your content groups in the google tag manager debugger.

Owhu! It’s that easy, you can see your result in the Google Analytics report inside Behaviour>Site Content>Content grouping.

Note:: We can achieve the same result just by using Google Analytics, using extraction filter in content grouping, in such case we do / blog/category/javascript/(.*?)/  to extract all the content that has been visited which belongs to javascript category.

# Content Grouping Using DataLayer

That was easy, because we were grouping the content using our url hierarchy, but what if we don’t use such url level hierarchy, or else we wanna do it using dataLayer. We do have the solution, let’s face that now.


Let’s send the categories of the article in the view, so that it could be accessed in the view. In my case I’m using laravel to develop my blog, so I would send the article categories from my controller, use your own convenient way to retrieve the value in the view. In this example, I'm passing a $article_cats array.


Next let’s push the $article_ cats array into the dataLayer object. Just before the GTM script add this line.

window.dataLayer = window.dataLayer || [];
'categories': <?php echo json_encode($article_cats); ?>

Now if you refresh the page, you will see the array of categories being pushed into dataLayer inside your Google Tag Manager debugger.


Let’s create a dataLayer variable in GTM to store the value of ‘categories’ inside the GTM.

I’m naming the variable name {{BlogCategories}} which will retrieve the value of ‘categories’, in my current page it’s a array [‘javascript’,’PHP’] .  



Since the value of dataLayer is now available inside GTM, now we can process the data, let’s create a custom javascript function to return the category name. In the varible page, click on create a new Variable, and choose custom Javascript.



Now let's add the following code, do note that, you can customize these codes as your need.


var cats = {{BlogCategories}};
var category = 'pages';
for (var i in cats)
    category = 'PHP';
    return category;
  else if(cats[i]=='SEO'){
   category = 'SEO'; 
   return category;
  else if(cats[i]=='Javascript'){
   category = 'Javascript';
   return category;
  else if(cats[i]=='Analytics'){
   category = 'Analytics'; 
   return category;
  else if(cats[i]=='Python'){
   category = 'Python';
   return category;
   return category;


Remember that we can only retrieve one content group in one page hit, if you want all the values inside the {{BlogCategories}} array, we might use different methods to retrieve them. For now we will retrieve the  last element of the array as a content group eg: ‘PHP’ in this example. Or else you can simply do


to retrive the first element of categories array to treat as a page category, but I'm leaving this part upto you, since you can customize that in accordance to your need. 

If you’re not using multi-category, then you can just push the category name as a plain string, and you don’t need to loop through the {{BlogCategories}} which we’re doing here in the custom javascript code, instead of that you remove the loop and return the category name with otherway around.

I’m pushing all the categories in my dataLayer because I might use the values and store them differently for further analysis.


Now let’s create a lookup table to assign each  return value a different category, so that that would be appended to the content group.



Now let’s edit our original pagview tag, and add the content group option to it. The content group option should be the lookup table that we created, and index is the GA content group index.

Now you can preview and debug. Yes! We did it. If you’ve clear url hierarchy then you can just filter the content group inside your Analytics, or else can use the GTM UI, if you have complex categorization then you shall use the Datalayer. We went through all the options in this post.

If you have any quires please let me know in the comment.



Posted Under