Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kimcoleman/4763cacca4b419be404008ed731e1b81 to your computer and use it in GitHub Desktop.
Save kimcoleman/4763cacca4b419be404008ed731e1b81 to your computer and use it in GitHub Desktop.
Track membership level as Custom Dimension 1 (dimension1) in your Google Analytics tracking code.
<?php
/**
* Track membership level as Custom Dimension 1 (dimension1) in your Google Analytics tracking code.
*
* Requires Paid Memberships Pro and the Google Analytics Dashboard for WP by ExactMetrics (formerly GADWP) plugin.
*/
function my_pmpro_membership_level_gadwp_analytics( $gadwp ) {
$commands = $gadwp->get(); // Get commands array
// 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() );
$value = $current_user_membership_level->ID;
} else {
// Set the tracked membership level ID to no_level.
$value = 'no_level';
}
$fields = array();
$fields['option'] = 'dimension1';
$fields['value'] = $value;
if ( $fields['value'] ){
$command = array( $gadwp->prepare( 'set', $fields ) );
array_splice( $commands, -1, 0, $command ); //insert the command before send
}
$gadwp->set( $commands ); // Store the new commands array
}
add_action( 'gadwp_analytics_commands', 'my_pmpro_membership_level_gadwp_analytics', 10, 1 );
@kimcoleman
Copy link
Author

This recipe is no longer supported by the updated GADWP version that was purchased by Exact Metrics. Please use this recipe in its place: https://gist.github.com/kimcoleman/1114506c1c3e6177c495e3664c6013bb

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