Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dpaternina9/3f9b221db2d276a00d13e562fc8cd9cc to your computer and use it in GitHub Desktop.
Save dpaternina9/3f9b221db2d276a00d13e562fc8cd9cc to your computer and use it in GitHub Desktop.
exactmetrics_custom_add_sml_dimensions
<?php
function exactmetrics_custom_add_sml_dimensions( $options ) {
if ( ! is_membership_available() ) {
return $options;
}
$dimensions = exactmetrics_get_option( 'custom_dimensions', array() );
if ( ! empty( $dimensions ) && is_array( $dimensions ) ) {
foreach ( $dimensions as $dimension ) {
if ( empty( $dimension['type'] ) || empty( $dimension['id'] ) ) {
continue;
}
$type = $dimension['type'];
$value = '';
switch ( $type ) {
case 'logged_in':
$value = var_export( sml_is_logged_in(), true );
break;
case 'user_id':
$value = sml_user_prop( 'member_type' );
break;
}
// In GA4, user_id is reserved for GA's own User ID tracking
// If we want to pull that on our reports, this custom dimension needs
// to be different
if ( $type === 'user_id' ) {
$type = 'wp_user_id';
}
if ( ! empty( $value ) ) {
$options[ $type ] = esc_js( addslashes( $value ) );
}
}
}
return $options;
}
add_filter( 'exactmetrics_frontend_tracking_options_gtag_before_pageview', 'exactmetrics_custom_add_sml_dimensions', 20 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment