Google Analytics Googlebot Tracking

Here is a quick script that will allow you to track Googlebot visits to Google Analytics events. Just pop in your page, edit the UA code, and enjoy!

** Disclaimer: Use at your own risk. Also, may add extra visits to your Google Analytics account, so maybe a second one?

Insert in Page You Want To Track (Edit where marked)


    googleTrack = function(data){

            // Edit ###############################################
            var ua          = 'UA-XXXXXXXX-1'
            // End Edit ###########################################



            if (typeof ga === 'function') {

                console.log('GA Loaded');

                if ( navigator.userAgent.toLowerCase().indexOf('googlebot') > -1 ){

                    $.getJSON('//freegeoip.net/json/?callback=?', function(data) {

                        Object.defineProperty(document, "referrer", {get : function(){ return "notgoogle.com"; }});

                        var url         = window.location.href;
                        var city        = data.city;
                        var state       = data.region_code
                        var ip          = data.ip;

                        ga('create', ua , 'auto');

                        ga('send', {
                          hitType: 'event',
                          eventCategory: 'Googlebot',
                          eventAction: url,
                          eventLabel:  ip + " : " + city + ", " + state
                        });       

                    });      


                }


            } else {

                console.log('GA Not loaded');           
                setTimeout(function(){googleTrack();},500);
            }

        }

        googleTrack();