Exclude pageviews from certain IP addresses

Koko Analytics comes with a setting to exclude certain WordPress user roles from being counted. This will ensure that no visits, pageviews or events are counted when a logged-in user with any of the selected roles views your site.

If you visit your website while not being logged-in, the plugin will still count your visit though. You can address this by adding a small PHP snippet to your site that prevents the Koko Analytics script from being loaded based on the visitor’s IP address.

Here’s what that looks like:

/**
 * Plugin Name: Koko Analytics: Do not count certain IP addresses
 */
add_filter( 'koko_analytics_load_tracking_script', function() {
    $ip_address = $_SERVER['REMOTE_ADDR'];
    $excluded_ip_addresses = array(
        '127.0.0.1',
        '254.123.182.120',
    );
    if (in_array($ip_address, $excluded_ip_addresses, true)) {
        return false;
    }

    return true;
});Code language: PHP (php)

Modify the snippet above to include your IP address and then save it as /wp-content/plugins/koko-analytics-exclude-ips.php. You can then activate it from your WP Admin > Plugins page.

If you would like to see this become a setting on the Koko Analytics settings page instead, please vote for this feature on our ideas board here.

More code snippets like this

For more code snippets like this that extend or modify the default behavior of the Koko Analytics plugin, please have a look at our repository of sample code snippets.

Was this article helpful? Yes / No