Creating Content Group in Google Analytics via tracking code

This article is in conjunction with the article Introduction to Content Grouping in Google Analytics where I introduced the concept of ‘Content Grouping’ and ‘Content Groups’.

In this article I will show you, how to group web pages on your website to create content groups by using the ‘Group by Tracking Code’ method.

What is ‘Group by Tracking Code’

You can add a line of code to your Google Analytics Tracking Code to identify web pages and make them a part of the content group. This method is called ‘Group by Tracking Code’ in Google Analytics. You can see this method while creating/editing ‘Content grouping’:

Use the ‘Group by Tracking code’ method for creating content groups:

#1 If you are comfortable making code changes on your website and you understand the Google Analytics development environment and/or if you know/hire someone who does.

#2 You want to create very robust content groups which can withstand any change made to the URLs and/or title tags of the web pages which are part of the content groups.

#3 If you have got web pages whose titles or URLs are too hard or impossible to capture via regular expression (‘group using extraction’ method) or rule sets (‘group using rule definitions’ methods).

#4 If you want to make sure that the same web page is not included in multiple content groups.

The ‘Group by Tracking Code’ is my favourite method for creating content groups in Google Analytics because it is very robust.

Introduction to ‘Index number’

In the context of ‘Content Grouping’, an index number can be any number from 1 to 5.

Google Analytics internally identify a ‘content grouping’ through its index number and not by its name. Since in GA, you can create only five ‘content groupings’ per reporting view, the value of index number can only be one of the number from 1 to 5.

An index number of ‘1’ corresponds to the first ‘content grouping’ which is denoted by ‘contentGroup1’.

An index number of ‘2’ corresponds to the second ‘content grouping’ which is denoted by ‘contentGroup2’.

Similarly, an index number of ‘5’ corresponds to the fifth ‘content grouping’ which is denoted by ‘contentGroup5’.

Ideally a ‘content grouping’ like first ‘content grouping’ should be denoted by ‘contentGrouping1as ‘content grouping’ and ‘content group’ are not the one and the same thing. But this is not the case. Google is confusing you here. So beware.

In order to see the index number of a content grouping, navigate to the ‘Admin’ section of that GA view for which you created ‘content groupings’ and then click on ‘Content Grouping’ link:

You can now see the index number assigned to each ‘content grouping’:

Now if you want to create a new ‘content grouping’ then do not use the index number which is already in use. Otherwise your web pages may be assigned to the new content grouping.

These index numbers act as slots and you can put only one content grouping at a time to these slots.

Making a call to an index number

If you want to refer to a ‘content grouping’ within your code then you would need to make a call to its index number. For example, if the name of your content grouping is ‘Men Clothing’ then first identify its index number and then make a call to this index number within your code.

You can make a call to an index number by using the Google Analytics command called ‘set’ before sending a pageview hit. This command is also used to create new content groups.

Following is the syntax of the ‘set’ command for creating a new content group within the content grouping (specified by its index number):

ga(‘set’, ‘contentGroup<Index Number>’, ‘<Group Name>’);

This line of code is added to the Google Analytics tracking code of all those web pages which will be part of the new content group:

<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-Y’, ‘auto’);

ga(‘set’, ‘contentGroup<Index Number>’, ‘<Group Name>’);

ga(‘send’, ‘pageview’);

</script>

For example, if the index number of your content grouping named ‘Men Clothing’ is 1 and you want to group all the web pages on your website which sell men shirts into the content group named ‘Men Shirts’ then use the following GA ‘set’ command before sending a pageview hit:

ga(‘set’, ‘contentGroup1’, ‘Men Shirts’);

You would need to add this line of code to the Google Analytics tracking code of all those web pages which will be part of the new content group named ‘Men Shirts’:

<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-Y’, ‘auto’);

ga(‘set’, ‘contentGroup1’, ‘Men Shirts’);

ga(‘send’, ‘pageview’);

</script>

Similarly, if you want to create a new content group named ‘Men trousers’ within the content grouping named ‘Men Clothing’ then use the following GA ‘set’ command before sending a pageview hit:

ga(‘set’, ‘contentGroup1’, ‘Men Trousers’);

Now add this line of code to the Google Analytics tracking code of all those web pages which will be part of the new content group named ‘Men Trousers’:

<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-Y’, ‘auto’);

ga(‘set’, ‘contentGroup1’, ‘Men Trousers);

ga(‘send’, ‘pageview’);

</script>

Note: Do not call the same index number multiple times on the same web page like the one below:

<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-Y’, ‘auto’);

ga(‘set’, ‘contentGroup1’, ‘Men Shirts’);

ga(‘set’, ‘contentGroup1’, ‘Men Trousers);

ga(‘send’, ‘pageview’);

</script>

If you call the same index number multiple times on the same web page then only the last call for the index number will be sent to GA along with the pageview. In our case the last call would be:

ga(‘set’, ‘contentGroup1’, ‘Men Trousers);

So the web page where you put this code will belong to the ‘Men Trousers’ content group and not to ‘Men Shirts’ content group. Remember when you use the ‘Group by Tracking Code’ method, you can assign only one ‘content group’ at a time to a web page.

Now follow the steps below to create content grouping and corresponding content groups in GA via the ‘Group by Tracking Code’ method:

Step-1: Decide the names and number of content / product categories which you will use as ‘content grouping’ in your GA reporting view.

Step-2: Decide the names and number of content / product sub-categories which you will use as ‘content groups’ in GA for each ‘content grouping’.

Step-3: Identify all of the web pages which will be part of each ‘content group’.

Step-4: Navigate to the ‘Admin’ section of your GA view and then click on ‘Content Grouping’ link:

Step-5: Click on ‘New Content Grouping’ button:

Step-6: Name your content grouping and then click on ‘Enable Tracking Code’ link as shown below:

Step-7: Select the index number for your new content grouping from the ‘Select Index’ drop down menu:

Do not use the index number which is already in use.

Step-8: Modify your Google Analytics tracking code according to the snippet you see and the way I explained earlier:

Step-9: Click on the ‘Done’ button.

At this point you can choose to use other methods (‘Group using Extraction’ and ‘Group using rule definitions’) to create more content groups or you can simply click on the ‘save’ button:

Using different methods for creating content groups for a particular content grouping

You can use any or all of following three methods at the same time to create different content groups within a content grouping:

  1. ‘Group by Tracking Code’
  2. ‘Group using Extraction’
  3. ‘Group using rule definitions’

However when you use more than one method at a time to create content groups, GA execute various methods in following order:

#1 ‘Group by Tracking Code’ method is executed first for creating a content group.

#2 ‘Group using Extraction’ method is executed second.

#3 ‘Group using rule definitions’ method is executed last.

Within ‘Group using rule definitions’ method, GA execute various rules from top to bottom:

You can drag the rules up and down to change the order in which they should be executed by GA.

Related Article: Google Tag Manager Content Grouping Setup Guide

Announcement about my new books

Maths and Stats for Web Analytics and Conversion Optimization
This expert guide will teach you how to leverage the knowledge of maths and statistics in order to accurately interpret data and take actions, which can quickly improve the bottom-line of your online business.

Master the Essentials of Email Marketing Analytics
This book focuses solely on the ‘analytics’ that power your email marketing optimization program and will help you dramatically reduce your cost per acquisition and increase marketing ROI by tracking the performance of the various KPIs and metrics used for email marketing.

Attribution Modelling in Google Analytics and Beyond
Attribution modelling is the process of determining the most effective marketing channels for investment. This book has been written to help you implement attribution modelling. It will teach you how to leverage the knowledge of attribution modelling in order to allocate marketing budget and understand buying behaviour.

Himanshu Sharma

Certified web analyst and founder of OptimizeSmart.com

My name is Himanshu Sharma and I help businesses find and fix their Google Analytics and conversion issues. If you have any questions or comments please contact me.

  • Over eleven years' experience in SEO, PPC and web analytics
  • Google Analytics certified
  • Google AdWords certified
  • Nominated for Digital Analytics Association Award for Excellence
  • Bachelors degree in Internet Science
  • Founder of OptimizeSmart.com and EventEducation.com

I am also the author of three books:

error: Content is protected !!