Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dpaternina9/b97a82bf47baf66993dedfd9fa77e888 to your computer and use it in GitHub Desktop.
Save dpaternina9/b97a82bf47baf66993dedfd9fa77e888 to your computer and use it in GitHub Desktop.
<?php
add_filter( 'monsterinsights_frontend_tracking_options_gtag_before_pageview', function( $options, $type ) {
$user_id = 999; // Replace with code to get actual user ID here
$meta_key = 'my_meta_key'; // Replace with the user meta key here
$meta_value = get_user_meta( $user_id, $meta_key );
if ( $type === 'ua' ) {
// If the customer is tracking for a UA property
// they need to make sure the "index" they set up for that custom dimension
// on their GA property matches this $index here:
$index = 9;
$options[ $index ] = $meta_value;
} else {
// For v4 properties, they need to make sure they have registered this
// custom dimension's parameter name on their GA4 property
$parameter_name = 'my_parameter_name';
$options[ $parameter_name ] = $meta_value;
}
return $options;
}, 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment