Quickshiftin - Clever Crazy Code

Hiding WordPress Preview Pages from Google Analytics

October 9th, 2011

Getting tired of seeing preview pages from your WordPress blog in Google Analytics for your WordPress site?

Wordpress Preview Pages in Google Analytics

Wordpress Preview Pages in Google Analytics

Just add a conditional block around the call to Google Analytics in the appropriate template:

if(window.location.search.search('&preview=true') == -1) {
  // google analytics tracking code
}

The code simply says if the preview GET parameter isn’t in the URL and set to true, execute the Google Analytics code. If you’re using a plugin to implement Google Analytics on your WordPress site, you can hunt down the location to update in the plugin through the WordPress plugin editor.

Local Development

At Moxune we like to run a local WordPress installation to work on customizations, upgrades and even blog posts before releasing them on the live site. In this case you might add another check so Google Analytics is only invoked on the production site. Here we’re actually checking for either moxune.com or www.moxune.com, since www is a supported alias.

if(['moxune.com','www.moxune.com']
   .indexOf(window.location.hostname) > -1
   && window.location.search.search('&preview=true') == -1
) {
  // google analytics tracking code
}

Share

If you enjoyed this post please consider sharing it!

  • Twitter
  • LinkedIn
  • Facebook
  • Reddit
  • Digg

Leave a Response

jscript language enabled