My Preview Problem
Whilst playing around with some theme switching (having one theme do some stuff, then trigger activating a new theme), I noticed that my Google page visits were going up on a site that is not public. That’s when I realised I had the analytics code for that site in my header and that previewing it was triggering a page hit.
Often it is easier to exclude logged in (or just admin) user page hits, but not always.
The Disabling Solution
The answer is easy, just wrap your code in a logical block using the is_preview() WordPress function.
if( !is_preview() ):
<script type="text/javascript">// <![CDATA[
javascript</span>">
// ]]></script>
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-123456-1']);
_gaq.push(['_setDomainName', 'tcbarrett.com']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
endif;
Sometimes it is the little things that help!