Skip to content

Instantly share code, notes, and snippets.

@kimcoleman
Created September 14, 2020 18:28
Show Gist options
  • Save kimcoleman/68c69e7cc692e716490ccce2c48314fb to your computer and use it in GitHub Desktop.
Save kimcoleman/68c69e7cc692e716490ccce2c48314fb to your computer and use it in GitHub Desktop.
Custom Dimensions for Membership Level with the plugin GA Google Analytics by Jeff Starr
<?php
/**
* Custom Dimensions for Membership Level with the plugin GA Google Analytics by Jeff Starr
* https://wordpress.org/plugins/ga-google-analytics/
*
* You must create the custom dimension for Membership Level in your Google Analytics account
* and properly update that line in this recipe for the ID of your dimension.
*
*/
function custom_dimensions_membership_level( $custom_code ) {
$membership_level = '';
// Get the value to track for the current user.
if ( is_user_logged_in() && function_exists( 'pmpro_getMembershipLevelForUser' ) ) {
// Get the current users's membership level ID.
$current_user_membership_level = pmpro_getMembershipLevelForUser( get_current_user_id() );
$membership_level = $current_user_membership_level->ID;
} else {
// Set the tracked membership level ID to no_level.
$membership_level = 'no_level';
}
if ( ! empty( $membership_level ) ) {
$custom_code .= "ga('set', 'dimension4', '" . $membership_level . "' );";
}
return $custom_code;
}
add_filter( 'gap_custom_code', 'custom_dimensions_membership_level' );
@laurenhagan0306
Copy link

This recipe is included in the blog post on "Putting Google Analytics to work for your Membership Site" at Paid Memberships Pro here: https://www.paidmembershipspro.com/putting-google-analytics-to-work-for-your-membership-site/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment