Google Analytics offers a powerful way to track and understand your website’s traffic. By integrating Google Analytics with your WordPress site using the Paid Memberships Pro Google Analytics Add On, you can extend the tracking capabilities to include membership site-specific data.

The default Add On includes features like membership site ecommerce tracking and custom dimensions, including a user property for the membership level of the person viewing your content.

But what if you have more unique needs and want to track additional data?

In this code recipe, we show you an advanced technique to capture unique post data (the post tags) as a custom dimension. Use this data to analyze how specific content groups perform.

Infographic for: Capture Unique Post Data as Custom Dimensions with Google Analytics

How the Code Works

The code snippet below extends the Paid Memberships Pro Google Analytics Add On features using the pmproga4_default_custom_dimension hook.

This hook allows you to add additional custom dimensions to your website tracking code. For this recipe, we are extending the default dimensions to also include the post tags. You can adjust this code for your own unique custom fields, for example if you are using Advanced Custom Fields to add data to your posts, such as a download link, secondary authors, last update date, and more.

Step-by-Step Breakdown

For those wanting to fully understand how this code works, here’s a line by line breakdown of what exactly this custom function does:

  1. Check for a Single Post: The is_single() function checks if the current view is a single post of any post type. If it is, the code proceeds to fetch data from this post.
  2. Extract Tags: Using get_the_tags(), the code fetches all tags associated with the post. 
  3. Convert Tags: It then uses the wp_list_pluck() function to extract just the names of these tags into an array.
  4. Set Custom Dimension: The array of tag names is converted to a comma-separated list then added to the $dimensions array using the key tags.

By adding this custom code to your WordPress site, you’ll allow Google Analytics to receive and store additional data about your posts, in this case the post tags. Use this data to perform a more detailed analysis of how posts with certain tags perform.

The Code Recipe (PHP)

Adding the Recipe to Your Website

You can add this recipe to your site by creating a custom plugin or using the Code Snippets plugin available for free in the WordPress repository. Read this companion article for step-by-step directions on either method.

Customizing This Code Recipe

The code recipe will work as-is if all you want to do is capture the post tags. The final step needed is to create the custom dimensions within your Google Analytics.

The dimension key in this code is tags. You’ll need this key to set up your custom dimension in GA.

Refer to our Google Analytics Integration Add On docs to learn the steps to create the custom dimensions in GA4.

Free Course: Membership Site Development—The Basics

Develop a deeper understanding of membership site development in this beginner-level course. Learn how to make your site work better, save yourself time and money, and improve your site's performance.

Featured Image for Membership Site Development Course: The Basics
Was this article helpful?
YesNo