Adding Views by Page Title : Google Analytics Client Report

The clients overview on MainWP already displays the Views by Page Title data (two columns). Shouldn’t this be able to be presented in the pro reports for the client?
I get this requested all the time from clients. I already use all the available tokens for Google Analytics, which hasn’t changed for years (and doesn’t include this data).
So has anyone found away to capture data that is presented on the MAINWP clients dashboard, into the reports? It’s already in the database somewhere, so its just a matter of feeding it to the pro report etc…
Thanks for your help.

1 Like

Hey @nwdmanagewp

As you pointed out, those values are not currently supported by the Pro Reports tokens.

I’ll check with the development team if there’s a different way of inserting that data in Pro Reports and update you as soon as I can.

Thanks for the quick reply, and appreciate checking.

Hi @nwdmanagewp

The development team has provided this code snippet which should enable you to use those datapoints in Pro Reports.

Let us know if that helps.

add_filter( 'mainwp_pro_reports_custom_tokens', 'mycustom_2024_11_15_mainwp_pro_reports_custom_tokens', 10, 3 );
function mycustom_2024_11_15_mainwp_pro_reports_custom_tokens( $tokensValues, $report, $site ) {
    if(is_array( $site ) && isset( $site['id'] )){
        $site_id = $site['id'];
    } else if (is_numeric( $site ) ){
        $site_id = $site;    
    } else {
        return $tokensValues;
    }
        
    if ( is_array( $tokensValues ) && ! isset( $tokensValues['[viewbypagetitle.custom.token]'] ) ) {
        $tokensValues['[viewbypagetitle.custom.token]'] = get_viewbypagetitle( $site_id );
    }
    return $tokensValues;
}

function get_viewbypagetitle( $site_id ) {
    $cacheCheck = MainWP\Extensions\GoogleAnalytics\MainWP_GA_UI::get_instance()->load_ga_cached_site_data( $websiteid );
    
    $otherValues = array();
    
    if ( $cacheCheck ) {
        $otherValues = ( '' != $cacheCheck->otherValues ) ? json_decode( $cacheCheck->otherValues, true ) : array();
    }

    if ( ! is_array( $otherValues ) ) {
        $otherValues = array();
    }
    
    $valuesStatsPage = isset( $otherValues['stats_pages_screens'] ) ? $otherValues['stats_pages_screens'] : array();
    $views = MainWP\Extensions\GoogleAnalytics\MainWP_GA_UI::get_ga_stats_calculate( $valuesStatsPage,'pageTitle', 'screenPageViews' );
        
    $html = '';
    
    // Create view by page title from $ view data 
    // example.
    // if ( is_array( $views ) && isset( $views[ 'pageTitle' ] ) && is_array( $views[ 'pageTitle' ] ) ) {
    //     foreach ( $views[ 'pageTitle' ] as $key => $value ) {
    //         $html .= '<tr>';
    //         $html .= '<td>' . htmlentities( $key ) . '</td>';
    //         $html .= '<td class="right aligned">' . htmlentities( $value ) . '</td>';
    //         $html .= '</tr>';
    //     }
    // }
    
    return $html;
}

Thank you for that snippet.
I added that code to the mainwp child themes function.php and the token [viewbypagetitle.custom.token] to a mainwp>report-templates report for testing, but no data is displayed in the report generated (rest of GA related report / tokens generate correctly).
Assume I am missing something? Thanks again for your help!

Hey @nwdmanagewp

I will check with the development team for more info.
But I wanted to verify - did you add the snippet to the functions.php file of the child theme on the Dashboard site, or on the Child site?

An easy way to insert these snippets is by using our Custom Dashboard extension.

Child theme on the dashboard site. I will check out the Custom Dashboard as well.

Hi @nwdmanagewp

You are using the snippet correctly.

The team has made some modifications.
Please let us know if this works better:

add_filter( 'mainwp_pro_reports_custom_tokens', 'mycustom_2024_11_15_mainwp_pro_reports_custom_tokens', 10, 3 );
function mycustom_2024_11_15_mainwp_pro_reports_custom_tokens( $tokensValues, $report, $site ) {
    if(is_array( $site ) && isset( $site['id'] )){
        $site_id = $site['id'];
    } else if (is_numeric( $site ) ){
        $site_id = $site;    
    } else {
        return $tokensValues;
    }
        
    if ( is_array( $tokensValues ) && ! isset( $tokensValues['[viewbypagetitle.custom.token]'] ) ) {
        $tokensValues['[viewbypagetitle.custom.token]'] = get_viewbypagetitle( $site_id );
    }
    return $tokensValues;
}

function get_viewbypagetitle( $site_id ) {
    $cacheCheck = MainWP\Extensions\GoogleAnalytics\MainWP_GA_UI::get_instance()->load_ga_cached_site_data( $site_id );
    
    $otherValues = array();
    
    if ( $cacheCheck ) {
        $otherValues = ( '' != $cacheCheck->otherValues ) ? json_decode( $cacheCheck->otherValues, true ) : array();
    }

    if ( ! is_array( $otherValues ) ) {
        $otherValues = array();
    }
    
    $valuesStatsPage = isset( $otherValues['stats_pages_screens'] ) ? $otherValues['stats_pages_screens'] : array();
if( ! is_array( $valuesStatsPage ) ){
 $valuesStatsPage = array();
}
    $views = MainWP\Extensions\GoogleAnalytics\MainWP_GA_UI::get_ga_stats_calculate( $valuesStatsPage,'pageTitle', 'screenPageViews' );
        
    $html = '';

    // Create view by page title from $ view data 
    // for example.
    if ( is_array( $views ) && isset( $views[ 'pageTitle' ] ) && is_array( $views[ 'pageTitle' ] ) ) {
         foreach ( $views[ 'pageTitle' ] as $key => $value ) {
             $html .= '<tr>';
             $html .= '<td>' . htmlentities( $key ) . '</td>';
             $html .= '<td class="right aligned">' . htmlentities( $value ) . '</td>';
             $html .= '</tr>';
         }
    }
    
    return $html;
}

Kudos on the great update! With the new code, I can now display page titles and views on sites, enhancing our monthly reports significantly.

I consider this request closed, many thanks! However, I have a few minor suggestions to further improve the solution:

  • It lists all pages with views (some have 100+). Modifying the if statement to limit the display to the top 10 would be useful.
  • Currently, it pulls data from the last 7 days. It would be helpful to adjust the date range as needed.
  • I’ve noticed a few child sites not showing data, although they display correctly in the MAINWP GA dashboard. I’ll continue testing for a common cause.
  • The accuracy in displaying the highest page counts is pretty accurate, though there’s slight misordering at times.

Thank you once again for the swift turnaround and for enhancing our client reports’ value.

1 Like

I’m glad that the code snippet is now working.

Unfortunately, those suggestions are non-trivial and implementing them would take some time.

You are most welcome.!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.