Found this useful? Love this post
10
jetpackstats

Disable WordPress Jetpack Stats when someone saves your webpage

I recently ran into a small problem when someone appeared to have saved a page on my site and seemed to be testing it locally on their machine (based on the analytics). I contacted the (awesome) guys at Automattic about the issue, they referred it up the chain but ultimately were not able to resolve it:

Dom,

I just received a reply from one of our developers, and I’m afraid we won’t be able to help; If someone saved a copy of your page on their machine in India, we do not have a way to exclude their hits since no referrer information gets sent from pages with file:// protocol. We also cannot clean stats for a specific post at the moment, unfortunately.

Sorry not to be able to help you more.

*Team Member*
Happiness Engineer | WordPress.com | Automattic


The following method will work for pages that are saved after this code has been added. Pages that have already been saved and are sending page views will still continue sending page views as there currently doesn’t appear to be a way to disable them*.

I ended up writing a very quick solution to the problem by simply checking the document.location.hostname to see if it matches my domain, and if not, then remove the WordPress JetPack stats script.

In jQuery:

if (document.location.hostname !== 'www.domsammut.com') {
    $("script[src*='stats.wordpress.com").remove();
}

In pure JavaScript (in case your jQuery script can’t be reached):

if (document.location.hostname !== 'www.domsammut.com') {
    var i = 0,
	    l,
	    scripts = document.getElementsByTagName('script');
    for(i, l = scripts.length; i < l; i += 1) {         
        if(scripts[i].src.indexOf('stats.wordpress.com') > -1){
            scripts[i].remove();
            break;
        }
    }
}

Let me know if you have a better solution or if you’ve found a gapping hole in this method :)

*Depending on how your site is setup. If your site makes AJAX calls, for example, to get content or something similar, you could attach this code as part of the call in some fashion.

Subscribe

You'll only get 1 email per month containing new posts (I hate spam as much as you!). You can opt out at anytime.

Categories

Leave a Reply

Your email address will not be published. Required fields are marked *

Preview Comment

css.php