Created
January 16, 2019 17:52
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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