Content grouping in Google Analytics is a great way to categorise and analyse site content. This is especially beneficial for WordPress users who use different templates for various content types. In this blog post, I can walk you through how to integrate content grouping for specific WordPress templates using Google Analytics 4 (GA4) with the gtag.js script.
1. The Basics of Content Grouping
With content grouping, Google Analytics users can aggregate content into desired categories. For instance, your site might have distinct sections for blog posts, listings, and products. Grouping these categories can provide a clearer understanding of each category’s performance.
2. Setting Up Basic Content Groupings
For commonly used WordPress templates such as single.php (individual posts), archive.php (archive pages), and search.php (search results), utilize the following script:
<script async src="https://www.googletagmanager.com/gtag/js?id=XXXXXXXX"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'XXXXXXXX'); // Remember to replace XXXXXXXX with your GA4 config ID
<?php
if(is_singular()) { echo "gtag('set', 'content_group', 'single');"; }
elseif(is_archive()) { echo "gtag('set', 'content_group', 'archive');"; }
elseif(is_search()) { echo "gtag('set', 'content_group', 'search');"; }
?>
</script>
Advanced: Grouping Custom Post Type Archives
Custom post types are prevalent in WordPress, offering greater flexibility. If you’ve designed a custom post type and have a template like archive-listing.php, you can set up content grouping:
if(is_post_type_archive('listing')) {
echo "gtag('set', 'content_group', 'listing_archive');";
}
This code checks if the current archive page belongs to the ‘listing’ custom post type and sets the content group as “listing_archive”.
Note: Ensure you replace ‘listing’ with your custom post type’s name.
Wrapping Up
Grouping content in Google Analytics 4 equips WordPress users with a deeper understanding of their content performance. Whether you’re managing a blog, e-commerce platform, or news portal, applying these techniques can pave the way for data-driven decisions.