Skip to Content
Menu

track_notable_bots()

Detect and track notable bot traffic in Google Analytics.

PHP April 27, 2017

Usage

This function runs automatically, so it is not called manually. Is this incorrect?

Was this page helpful? Yes No


    A feedback message is required to submit this form.


    Please check that you have entered a valid email address.

    Enter your email address if you would like a response.

    Thank you for your feedback!

    Source File

    Located in /libs/Security.php on line 171.

    1 Hook

    Find these filters and actions in the source code below to hook into them. Use do_action() and add_filter() in your functions file or plugin.

    Filters
    "pre_track_notable_bots"
    Need a new filter hook? Request one here.

    Actions
    This function has no action hooks available. Request one?

    Note: This function contains 6 to-do comments.

    PHP
            function track_notable_bots(){
                $override = apply_filters('pre_track_notable_bots', null);
                if ( isset($override) ){return;}
    
                //Ignore logged-in users
                if ( is_user_logged_in() ){
                    return false;
                }
    
                if ( isset($this->super->server['HTTP_USER_AGENT']) ){
                    $user_agent = str_replace(' ', '_', strtolower($this->super->server['HTTP_USER_AGENT'])); //Normalize the user agent for matching against
    
                    //Lighthouse (Ex: web.dev) (Formerly Google Page Speed Insights) - Ignore Nebula Dashboard tests (?noga)
                    if ( !isset($this->super->get['noga']) && strpos($user_agent, 'chrome-lighthouse') !== false ){ //@todo "Nebula" 0: Update strpos() to str_contains() in PHP8
                        if ( $this->url_components('extension') !== 'js' ){
                            $this->ga_send_data($this->ga_build_event('notable_bot', array('bot' => 'Chrome Lighthouse')));
                        }
                    }
    
                    //W3C Validators
                    if ( strpos($user_agent, 'w3c_validator') !== false || strpos($user_agent, 'w3c_css_validator') !== false ){ //@todo "Nebula" 0: Update strpos() to str_contains() in PHP8
                        $this->ga_send_data($this->ga_build_event('notable_bot', array('bot' => 'W3C Validator')));
                    }
    
                    //Redditbot
                    if ( strpos($user_agent, 'redditbot') !== false ){ //@todo "Nebula" 0: Update strpos() to str_contains() in PHP8
                        $this->ga_send_data($this->ga_build_event('notable_bot', array('bot' => 'Redditbot')));
                    }
    
                    //OpenAI GPT Bot
                    if ( strpos($user_agent, 'gptbot') !== false ){
                        $this->ga_send_data($this->ga_build_event('notable_bot', array('bot' => 'GPTBot')));
                    }
    
                    //Slackbot
                    if ( $this->is_slackbot() ){
                        $this->ga_send_data($this->ga_build_event('notable_bot', array('bot' => 'Slackbot')));
                    }
    
                    //Discordbot
                    if ( strpos($user_agent, 'discordbot') !== false ){ //@todo "Nebula" 0: Update strpos() to str_contains() in PHP8
                        $this->ga_send_data($this->ga_build_event('notable_bot', array('bot' => 'Discordbot')));
                    }
    
                    //Screaming Frog SEO Spider
                    if ( strpos($user_agent, 'screaming_frog') !== false ){ //@todo "Nebula" 0: Update strpos() to str_contains() in PHP8
                        $this->ga_send_data($this->ga_build_event('notable_bot', array('bot' => 'Screaming Frog SEO Spider')));
                    }
    
                    //Internet Archive Wayback Machine
                    if ( strpos($user_agent, 'archive.org_bot') !== false || strpos($user_agent, 'wayback_save_page') !== false ){ //@todo "Nebula" 0: Update strpos() to str_contains() in PHP8
                        $this->ga_send_data($this->ga_build_event('notable_bot', array('bot' => 'Internet Archive Wayback Machine')));
                    }
                }
    
                //Other Notable Bots (these have been manually normalized to match above):
                    //bingbot
                    //seositecheckup
                    //gtmetrix
                    //pingdompagespeed or pingbot
                    //twitterbot
                    //semrushbot
                    //ahrefsbot
                    //facebookexternalhit
                    //microsoft_office
                    //google-structured-data-testing-tool
            }
    

    Override

    To override this PHP function, use this hook in your child theme or plugin ("my_custom" can be changed):

    PHP
    add_filter('pre_track_notable_bots', 'my_custom_track_notable_bots'); 
    function my_custom_track_notable_bots(){ 
        //Write your own code here
    
        return true; //Return true to prevent the original function from running afterwords
    }

    You can completely disable this PHP function with a single line actions:

    PHP
     add_filter('pre_track_notable_bots', '__return_false');