MainWP Pro Reports – Snippet

Tech Articles | June 8, 2025 | Coding, MainWP, Plugins, Wordpress

I use Independent Analytics for WordPress analytics, and I have always wanted to add my stats to my professional reports. That’s why I created the MainWP-IAWP addon.

In the last few months, Independent Analytics has created a plugin for MainWP that doesn’t require a child plugin, and this includes some pro-report tokens. In the latest version 1.3, they have finally added pro-report tokens.

With this, I decided to retire the above plugin from active development. Although the current version of the plugin doesn’t offer all the features of my previous version, it’s now sufficient to eliminate the need for a plugin to perform this task.

Specifically, I still wanted to be able to use my tokens in my HTML email report, so I decided to write a snippet that utilises the data available with the new plugin while maintaining backwards compatibility.

The below snippet does exactly that, it opens the [iawp.views] [iawp.visitors] [iawp.sessions] and my legacy tokens and the correct spelling [iwpa-views] [iwpa-visitors] [iwpa-sessions] & [ipwa-views] [ipwa-visitors] [ipwa-sessions] Using the same data as the IA’s new plugin, but allowing backwards compatibility.

<?php
add_filter( 'mainwp_pro_reports_custom_tokens', 'bridge_expose_all_iawp_tokens', 10, 4 );
function bridge_expose_all_iawp_tokens( $tokensValues, $report, $site, $templ_email ) {
    // Fetch the JSON analytics
    $raw = apply_filters( 'mainwp_getwebsiteoptions', false, intval( $site['id'] ), 'iawp_analytics' );

    // Default placeholder
    $defaults = [
        'views'    => 'n/a',
        'visitors' => 'n/a',
        'sessions' => 'n/a',
    ];

    // If no data, set all to default and bail
    if ( empty( $raw ) ) {
        foreach ( $defaults as $key => $value ) {
            // new format
            $tokensValues[ "[iawp.{$key}]" ]   = $value;
            // legacy
            $tokensValues[ "[ipwa-{$key}]" ]   = $value;
            // typo fallback
            $tokensValues[ "[iwpa-{$key}]" ]   = $value;
        }
        return $tokensValues;
    }

    // Decode JSON
    $analytics = json_decode( $raw, true );
    if ( ! is_array( $analytics ) ) {
        foreach ( $defaults as $key => $_ ) {
            $tokensValues[ "[iawp.{$key}]" ]   = 'error';
            $tokensValues[ "[ipwa-{$key}]" ]   = 'error';
            $tokensValues[ "[iwpa-{$key}]" ]   = 'error';
        }
        return $tokensValues;
    }

    // Map each metric into all three token styles
    foreach ( [ 'views', 'visitors', 'sessions' ] as $key ) {
        $val = isset( $analytics[ $key ] ) ? intval( $analytics[ $key ] ) : 0;

        $tokensValues[ "[iawp.{$key}]" ] = $val;
        $tokensValues[ "[ipwa-{$key}]" ] = $val;
        $tokensValues[ "[iwpa-{$key}]" ] = $val;
    }

    return $tokensValues;
}
PHP

I am posting it here so it’s available to others until such time as full support is added to the IAWP plugin to get these working. You just need this plugin on your MainWP dashboard site, and the IAWP plugin and your legacy tokens, using the above plugins, will work. Additionally, creating them outside of the builder will also be possible.

Support the Author

buy me a coffee
Really Useful Plugin Logo
Wpvideobank temp
Appoligies for any spelling and grammer issue. As a dyslexic i need to rely on tools for this they like me are not perfect but I do try my best