GA Pro is equipped with lots of nice action and filter hooks. So it is possible for developers to customize the functionality of GA Pro quite easily. For example, the plugin Paid Memberships Pro makes it possible to add the user’s membership level to the Google Analytics tracking code. This quick tutorial shows how it’s done for either free or pro versions of GA Google Analytics.

Add Membership Level to GA Pro

This technique is for the Pro version of GA Google Analytics. For the free version of the plugin, skip ahead to the next section.

Thanks to a handy code snippet by Kim Coleman, it’s easy to add the current user’s membership level when tracking with Universal Analytics. To do it, follow these steps:

Step 1

This technique is designed for Universal Analytics. So “Universal Analytics” should be selected for the plugin setting, “Tracking Method”. Also in case it’s not yet clear, this technique is for the WordPress plugin, Paid Memberships Pro. So that needs to be active as well as GA Pro.

Step 2

Add the following code snippet to your (child) theme’s functions.php, or add via simple custom plugin. The magic code:

// GA Pro - Add membership level for Paid Memberships Pro
function gapro_custom_dimensions_membership_level($custom_code) {
	
	$membership_level = '';
	
	if (is_user_logged_in() && function_exists('pmpro_getMembershipLevelForUser')) {
		
		$current_user_membership_level = pmpro_getMembershipLevelForUser(get_current_user_id());
		$membership_level = $current_user_membership_level->ID;
		
	} else {
		
		$membership_level = 'no_level';
		
	}
	
	if (!empty($membership_level)) {
		
		$custom_code .= "ga('set', 'dimension4', '". $membership_level ."');";
		
	}
	
	return $custom_code;
	
}
add_filter('gapro_custom_filter', 'gapro_custom_dimensions_membership_level');

No changes are required, simply add to WordPress and done.

To verify that the tracking code is correct, check out the live preview in the plugin settings, and/or examine the source code of your web pages. Try logging in and then viewing the tracking code. It should show your membership level included in the added tracking output. Then try logging out and reloading the page; viewing the tracking code should show no_level.

Add Membership Level to Free Version

Except for the code snippet, the steps are identical for the free version of GA Google Analytics. So to add membership level to the free version of the plugin, follow the steps above, but use this alternate code snippet instead (for step 2).

// GA Google Analytics (free) - Add membership level for Paid Memberships Pro
function ga_custom_dimensions_membership_level($custom_code) {
	
	$membership_level = '';
	
	if (is_user_logged_in() && function_exists('pmpro_getMembershipLevelForUser')) {
		
		$current_user_membership_level = pmpro_getMembershipLevelForUser(get_current_user_id());
		$membership_level = $current_user_membership_level->ID;
		
	} else {
		
		$membership_level = 'no_level';
		
	}
	
	if (!empty($membership_level)) {
		
		$custom_code .= "ga('set', 'dimension4', '". $membership_level ."');";
		
	}
	
	return $custom_code;
	
}
add_filter('gap_custom_code', 'ga_custom_dimensions_membership_level');

No changes are required, simply add to WordPress and done.

In case you are wondering, there are two differences between this code and the previous. Specifically, the name of the filter hook and function name are different between the two functions. Everything else (the function logic) is the same.

Note: The free-version snippet above is available as a GitHub Gist.