On December 16, 2011 In CMS with , , , tags

No analytics for logged users

Before going in-depth, we should have a basic knowledge about WordPress shortcodes

To accurate the stats of website, we are going to create Google analytics shortcode.Which will not be served to logged users i.e, admin, editor, contributor and anyone who’s viewing the dashboard.

the analytics code consists of both double and single quotes, which drives php crazy without html escaping. So, in my case i covert all the double quote to single quotes

// Google Analytics
function google_analytics(){
	return "
	<script type='text/javascript'>
	  var _gaq = _gaq || [];
	  _gaq.push(['_setAccount', 'XX-XXXXXXXX-X']);
	  _gaq.push(['_setDomainName', 'XXXXXXXXXX.XXX']);
	  _gaq.push(['_setAllowLinker', true]);
	  _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);
	  })();
	</script>
	";
}
if(!is_user_logged_in()){
add_shortcode('ga','google_analytics');
}

That’s it, we get accurate results for our websites.