Skip to content

Instantly share code, notes, and snippets.

@chavesm
Last active January 28, 2021 12:43
Show Gist options
  • Save chavesm/fe4e9108acd644d3fb3dba95e0c4f55b to your computer and use it in GitHub Desktop.
Save chavesm/fe4e9108acd644d3fb3dba95e0c4f55b to your computer and use it in GitHub Desktop.
MonsterInsights filter that supports displaying the login name (or full name) rather than an internal ID in Top logged-in report.
<?php
/**
* Custom Filter for the User ID Dimension
*
* Add this to your theme's functions.php file.
*/
function display_username_instead_of_user_id( $user_id ) {
$user = get_user_by( 'id', $user_id );
return $user->user_login;
// or return $user->first_name . ' ' . $user->last_name;
// or return $user->user_login . ' ('. $user_id . ')';
}
add_filter( 'mi_get_user_by', 'display_username_instead_of_user_id', 10, 3 );
<?php
/**
* Apply a filter to display a more human-friendly string
* rather than the internal WordPress user ID.
*
* /google-analytics-premium/pro/includes/admin/reports/report-dimensions.php
*/
// If logged-in user dimension, then apply a filter
// to display user login, name, etc. ~mlc.
if ( intval( $key ) === 5 ) $title = apply_filters( 'mi_get_user_by', $title );
@chavesm
Copy link
Author

chavesm commented Oct 25, 2020

Where does the apply_filters call go?

Insert the if condition in the prepare_report_data() function around line 128 of the report-dimensions.php file.

@chavesm
Copy link
Author

chavesm commented Oct 25, 2020

The Top Logged-in Report (before)

mi-top-logged-in-users

Example Result (after)

mi-dimensions-report-user-id-filter-results

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