Creating Content Group in Google Analytics via tracking code using gtag.js

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’ (Admin >’ View’ column > 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 ‘content_group1’.

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

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

Ideally a ‘content grouping’ like first ‘content grouping’ should be denoted by ‘content_grouping1as ‘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.

Get the E-Book (37 Pages)

Learn to read e-commerce reports book banner

Get the E-Book (104 Pages)

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, from 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):

gtag(‘set’, {‘content_group<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:

<!– Global site tag (gtag.js) – Google Analytics –>
<script async src=”https://www.googletagmanager.com/gtag/js?id=UA-1123456-78″></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag(‘js’, new Date());
gtag(‘config’, ‘UA-1123456-78’, {‘content_group<Index Number>’: ‘<Group Name>’});
</script>

For example,

Let us suppose, you want to group all the web pages on your website, which sell men shirts into the content group named ‘Men Shirts’

Let us also suppose, that the index number of your content group is 1.

Now in order to create this content group, you would need to make following similar changes, to the Google Analytics tracking code, of all those web pages, which will be part of the new content group named ‘Men Shirts’:

<!– Global site tag (gtag.js) – Google Analytics –>
<script async src=”https://www.googletagmanager.com/gtag/js?id=UA-1123456-78″></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag(‘js’, new Date());
gtag(‘config’, ‘UA-1123456-78’, {‘content_group1’: ‘Men Shirts‘});
</script>

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

<!– Global site tag (gtag.js) – Google Analytics –>
<script async src=”https://www.googletagmanager.com/gtag/js?id=UA-1123456-78″></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag(‘js’, new Date());
gtag(‘config’, ‘UA-1123456-78’, {‘content_group1’: ‘Men Shirts‘, ‘content_group1’: ‘Men Trousers‘});
</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:

gtag(‘set’, {‘content_group1’: ‘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.

Note:

<!– Global site tag (gtag.js) – Google Analytics –>
<script async src=”https://www.googletagmanager.com/gtag/js?id=UA-1123456-78″></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag(‘js’, new Date());

gtag(‘set’, {‘content_group1’: ‘Men Trousers’});
gtag(‘config’, ‘UA-1123456-78’);
</script>

is same as:

<!– Global site tag (gtag.js) – Google Analytics –>
<script async src=”https://www.googletagmanager.com/gtag/js?id=UA-1123456-78″></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag(‘js’, new Date());
gtag(‘config’, ‘UA-1123456-78’, {‘content_group1’: ‘Men Trousers‘});
</script>

You can use either ‘set’ command or ‘config’ command to set up content group in GA.

Steps to create content groups in GA via the ‘Group by Tracking Code’ method

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:

Note: 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 different 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

Other Articles on Specialised Tracking in Google Analytics

  1. Google Analytics Recurring Revenue and Subscriptions Tracking Guide
  2. How to track the impact of cookie consent on website traffic in Google Analytics
  3. Phone Call Tracking in Google Analytics and Beyond
  4. Scroll Tracking via Scroll Depth Trigger in Google Tag Manager
  5. Video Tracking via YouTube Video Trigger In Google Tag Manager
  6. Calculated Metrics in Google Analytics – Complete Guide
  7. The one thing that you don’t know about PayPal.com and the referral exclusion list
  8. Introduction to Postbacks in Google Analytics
  9. Creating Content Group in Google Analytics via tracking code using gtag.js
  10. Tracking Site Search without Query Parameter in Google Tag Manager
  11. Setting Up Site Search Tracking for POST based search engines in Google Analytics
  12. Tracking Site Search for GET-Based Search Engines in Google Analytics
  13. Understanding site search tracking in Google Analytics
  14. Tracking Virtual Pageviews in Google Tag Manager – Complete Guide
  15. Creating and using Site Search Funnel in Google Analytics
  16. Learn to Setup Facebook Pixel Tracking via Google Tag Manager
  17. How to use Keyword Hero to reveal Not Provided keywords in Google Analytics
  18. Guide to Event Tracking via Google Tag Manager
  19. Learn to track Qualified and Won leads in Google Analytics
  20. Creating your own Google Analytics Tag Auditing System
  21. Using multiple Google Analytics tracking codes on web pages
  22. Guide to Offline Conversion Tracking in Google Analytics
  23. Google Analytics and YouTube Integration Guide
  24. Complete Guide to Google Analytics for Facebook
  25. Tracking true referrals in Google Analytics when using PayPal and other payment gateways
  26. Virtual pageviews in Google Analytics – Complete Guide
  27. YouTube Video tracking via Google Tag Manager
  28. Implementing E-Commerce Tracking via Google Tag Manager
  29. Event Tracking in Google Analytics – Complete Guide
  30. E-Commerce Tracking in Google Analytics – Complete Guide
  31. Google Analytics Cross Domain Tracking Explained Like Never Before
  32. Google Analytics Social Tracking – Twitter, Facebook, Google Plus and LinkedIn
  33. Setting up & Tracking AMP Pages in Google Analytics
  34. Setting up Sales Funnel across websites in Google Analytics
  35. Using Data Visualization to analyze the SEO Performance
  36. Tracking offline conversions in Google Adwords
  37. How to Correctly Measure Conversion Date & Time in Google Analytics
  38. Tracking Twitter and Linkedin Social Interactions in Google Analytics
  39. Implementing Scroll Tracking via Google Tag Manager
  40. Phone Call Tracking and ROI calculations – Champion’s Guide
  41. Google Analytics Cross Domain Tracking (ga.js)
  42. Open Graph Protocol for Facebook Explained with Examples

Learn about the Google Analytics Usage Trends Tool

The Google Analytics usage trend is a new tool which is used to visualise trends in your Google Analytics data and to perform trend analysis.


Do you want to Learn Web Analytics in 8 Weeks?

  • Learn and Master Web Analytics, Conversion Optimization & Google Analytics from Industry Expert in 8 weeks.
  • Lifetime access to the course + Lifelong FREE course updates.
  • New study material added every few months (lifelong learning).
  • Up to date training material.
  • Most exhaustive course on Google Analytics on the internet.
  • Hundreds of Assessments to test your learning.
  • Your 24/7, 365 days a year reference source.
  • Learn at your own pace and from any place.

Take your Analytics knowledge to the next level. Checkout my Best Selling Books on Amazon

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: Alert: Content is protected !!